Jump to content

Maxima/Functions

From Wikibooks, open books for an open world

Definition

[edit | edit source]

For mathematical functions use always

define() 

instead of

:= 

There is only one small difference between them:

"The function definition operator. f(x_1, ..., x_n) := expr defines a function named f with arguments x_1, …, x_n and function body expr. := never evaluates the function body (unless explicitly evaluated by quote-quote '')."

"Defines a function named f with arguments x_1, …, x_n and function body expr. define always evaluates its second argument (unless explicitly quoted). "[1]

return

[edit | edit source]

"return doesn't mean the same thing in Maxima as it does in other programming languages. It means only to return a value from a block; that's not enough for what you need here." Robert Dodier[2]

If you wish to make a return from some expression inside the function then you must use block and return.

   block ([], expr1, ..., if (a > 10) then return(a), ..., exprn)


is itself an expression, and so could take the place of the right hand side of a function definition. Here it may happen that the return happens earlier than the last expression.

Local variables

[edit | edit source]
  block ([x], expr1, ..., exprn)

The first [] in the block, may contain a list of variables and variable assignments, such as [a: 3, b, c: []], which would cause the three variables a,b,and c to not refer to their global values, but rather have these special values for as long as the code executes inside the block, or inside functions called from inside the block. This is called dynamic binding, since the variables last from the start of the block to the time it exits. Once you return from the block, or throw out of it, the old values (if any) of the variables will be restored. It is certainly a good idea to protect your variables in this way. Note that the assignments in the block variables, are done in parallel. This means, that if you had used c: a in the above, the value of c would have been the value of a at the time you just entered the block, but before a was bound. Thus doing something like:

   block ([a: a], expr1, ... a: a+3, ..., exprn)

will protect the external value of a from being altered, but would let you access what that value was. Thus the right hand side of the assignments, is evaluated in the entering context, before any binding occurs. Using just block ([x], ...) would cause the x to have itself as value, just as if it would have if you entered a fresh Maxima session.

See also description by Darren Irvine[3]

compile

[edit | edit source]
  • "The modedeclare statement is important for compiled code
  • Compiling can greatly speed numerical code; for mostly symbolic code, the speed up is not great.
  • There are some differences between compiled and uncompiled code -- be careful." Barton Willis[4]


mode_declare

[edit | edit source]

"mode_declare tells the compiler that the compiled code needs not to be generic enough to be able to deal with all kind of objects. If your function only takes floats, but no symbolic arguments the compiled code might run much faster." Gunter Königsmann

explicit

[edit | edit source]

implicit

[edit | edit source]
  1. Discussion at Maxima interest list
  2. stackoverflow question : difference-between-throw-return-and-break-in-maxima
  3. Darren Irvine : local-variables-function-parameters
  4. maxima-discuss : compiling-files-written-in-maxima