1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 23.04.0-DevelopmentSnapshot ] */
/* [wxMaxima: title start ]
Difference between a Computer Algebra System and a typical programming language
[wxMaxima: title end ] */
/* [wxMaxima: section start ]
An equation is just an equation
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
We can name and re-use an equation. But otherwise an equation is is just an equation as an equation in your notebook would be, not something that immediately has an effect:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
eq0:a=b;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
pyth:a^2+b^2=c^2;
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
If a variable isn't declared nor defined that is OK.
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
The following equation contains an unknown "x" and an unknown "y".
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
eq1:x^2+(y+2)*x+3=0;
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
If we solve that equation without defining y first maxima won't issue an error. Instead the indicator that maxima doesn't know the value of "y" is that the solution contains an "y".
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
solve(eq1,x);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
Lists
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
Most maxima commands accept lists. These can be created ad-hoc by enclosing a bunch of comma-separated values in square brackets:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
list1:[1,2,a,b,c];
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
One can create lists according to a rule:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
list2:makelist(i*2,i,list1);
list3:makelist(i^2,i,1,10);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Lists can easily be manipulated:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
list4:append(list1,list2,list3);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
But just assigning the nth element of a list a value doesn't create that list, but a hash:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
a[4]:10;
a[qdwqd]:30;
a["qdwqd"]:45;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
a;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
arrayinfo(a);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
A CAS has many types of Numbers
[wxMaxima: section end ] */
/* [wxMaxima: subsect start ]
Exact rational numbers
[wxMaxima: subsect end ] */
/* [wxMaxima: comment start ]
Maxima supports many types of numbers:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
1/10*π*sqrt(2);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
is exactly 1/10*π*sqrt(2).
[wxMaxima: comment end ] */
/* [wxMaxima: subsect start ]
Floating-point numbers
[wxMaxima: subsect end ] */
/* [wxMaxima: input start ] */
0.1*π;
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
is a floating-point number of the type the machine's floating-point processor uses. These numbers are accurate enough for most uses. But since maxima was developed for symbolic computations not all of the maxima's algorithms deal with the rounding errors floating-point numbers introduce.
[wxMaxima: comment end ] */
/* [wxMaxima: subsect start ]
Arbitrary-precision floats
[wxMaxima: subsect end ] */
/* [wxMaxima: input start ] */
0.1b-1*sqrt(2);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
is a floating-point number whose precision can be changed by customizing the variable fpprec. It is way slower than machine floats. But it allows to look what happens if the precision is increased.
[wxMaxima: comment end ] */
/* [wxMaxima: section start ]
Handling of knowledge
[wxMaxima: section end ] */
/* [wxMaxima: subsect start ]
Storing actual values in a list
[wxMaxima: subsect end ] */
/* [wxMaxima: comment start ]
Maxima is very good at symbolical maths. That means that if you don't immediately assign variables whose value you know a value the result often is an equation that provides you with additional knowledge about the problem. The actual values can be kept in a list, instead, and be introduced into the equation whenever needed:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
vals:[b=3,c=2];
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
subst(vals,pyth);
/* [wxMaxima: input end ] */
/* [wxMaxima: subsect start ]
Hard assignments
[wxMaxima: subsect end ] */
/* [wxMaxima: comment start ]
If we assign a value to an variable that means that knowledge is used for new equations:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
b:3;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
pyth2:a^2+b^c=c^2;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
vals2:[b=3];
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
That knowledgie isn't automatically applied to existing equations, though:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
pyth;
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Two single quote marks make maxima interpret an known equation as one that just has been typed in, thouhg:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
''pyth;
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
If we mean the variable name, not its contents we can tell this maxima by preceding the variable name by a single quote
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
'b=b;
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
Is(a=b) can be evaluated before the real result is known
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
Even if we might know that we will assign a and c values lateron maxima doesn't know so currently a≠c:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
eq2:a=c;
is(eq2);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
If we assign c the value "a" that knowledge isn't immediately used for already-defined equations:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
c:a;
is(eq2);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
If we tell Maxima to act as if eq2 were typed in right now maxima knows that a=c
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
is(''eq2);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
Defining functions
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
If you just want a storage for an equation it often is better to actually keep it in an equation. For example an equation can be solved for x, a function not.
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
If you actually want a mathematical function similar to sin(x) or store a complete program in a function you can do that, though.
[wxMaxima: comment end ] */
/* [wxMaxima: subsect start ]
Storing a program in a function
[wxMaxima: subsect end ] */
/* [wxMaxima: comment start ]
If you don't need any local variables nor the possibility to issue a return() in the middle of a function just put parenthesis around comma-separated commands - and that program will return what the last command will return.
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
A naive "sum of all numbers up to x" program for increasing x would therefore be:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
last_num:0;
last_sum:0;
my_sum(x):=(
for i:last_num + 1 thru x do last_sum:last_sum + i,
last_num:x,
last_sum
);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
my_sum(10);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
my_sum(11);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
If you local variables or a premature return() is needed one needs a block() command:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
my_sum(x):=block([result:0],
for i:1 thru x do
result:result+i,
result
);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
my_sum(11);
/* [wxMaxima: input end ] */
/* [wxMaxima: subsect start ]
Functions and variables can have the same name
[wxMaxima: subsect end ] */
/* [wxMaxima: comment start ]
Normally that is an easy-to-use feature. But when defining a function it makes a difference: Local variables are declared using block(), local functions by the local() command
[wxMaxima: comment end ] */
/* [wxMaxima: subsect start ]
Optional arguments
[wxMaxima: subsect end ] */
/* [wxMaxima: input start ] */
f(x,[y]):=(disp(sconcat(length(y)," optional arguments")),x);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
f(a);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
f(a,1,2,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: subsect start ]
return() returns from the current block, not from the whole function
[wxMaxima: subsect end ] */
/* [wxMaxima: input start ] */
f(x):=block(block(return(test)),return(test2));
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
f(2);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
Maxima doesn't try hard to hide numerical errors
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
1/10 is an exact 1/10. But 0.1 is a floating-point variable of the type that your machine uses and is fast in - but that cannot represent an exact 1/10. This is true for all programming languages.
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
rationalize(0.1);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Maxima's output routine rounds that floating-point number to 0.1, by default. But it doesn't try hard to hide rounding errors if they accumulate:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
.1;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
%*.1;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
%*.1;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
%*.1;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
%*.1;
/* [wxMaxima: input end ] */
/* Old versions of Maxima abort on loading files that end in a comment. */
"Created with wxMaxima 23.04.0-DevelopmentSnapshot"$
|