Maxima – 200 – Definire funzioni – 1


Continuo da qui, copio dal Reference Manual, PDF scaricabile da qui, sono a p.605.

Funzioni ordinarie

To define a function in Maxima you use the := operator. E.g.

f(x) := sin(x)

defines a function f. Anonymous functions may also be created using lambda. For example lambda ([i, j], ...) can be used instead of f where

f(i,j) := block ([], ...);
map (lambda ([i], i+1), l)

would return a list with 1 added to each term.

You may also define a function with a variable number of arguments, by having a final argument which is assigned to a list of the extra arguments:

(%i1) f ([u]) := u;
(%o1)                             f([u]) := u
(%i2) f (1, 2, 3, 4);
(%o2)                            [1, 2, 3, 4]
(%i3) f (a, b, [u]) := [a, b, u];
(%o3)                      f(a, b, [u]) := [a, b, u]
(%i4) f (1, 2, 3, 4, 5, 6);
(%o4)                        [1, 2, [3, 4, 5, 6]]

The right hand side of a function is an expression. Thus if you want a sequence of expressions, you do

f(x) := (expr1, expr2, ..., exprn);

and the value of exprn is what is returned by the function.

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.

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.

The actual arguments to a function are treated in exactly same way as the variables in a block. Thus in

f(x) := (expr1, ..., exprn);



we would have a similar context for evaluation of the expressions as if we had done block ([x: 1], expr1, ..., exprn).

Inside functions, when the right hand side of a definition, may be computed at runtime, it is useful to use define and possibly buildq.

Funzioni array

An array function stores the function value the first time it is called with a given argument, and returns the stored value, without recomputing it, when that same argument is given.

Such a function is often called a memoizing function.

Array function names are appended to the global list arrays (not the global list functions). arrayinfo returns the list of arguments for which there are stored values, and listarray returns the stored values. dispfun and fundef return the array function definition.

arraymake constructs an array function call, analogous to funmake for ordinary functions. arrayapply applies an array function to its arguments, analogous to apply for ordinary functions. There is nothing exactly analogous to map for array functions, although map(lambda([x], a[x]), L) or makelist(a[x], x, L), where L is a list, are not too far off the mark.

remarray removes an array function definition (including any stored function values), analogous to remfunction for ordinary functions.

kill(a[x]) removes the value of the array function a stored for the argument x; the next time a is called with argument x, the function value is recomputed. However, there is no way to remove all of the stored values at once, except for kill(a) or remarray(a), which also remove the function definition.

Posta un commento o usa questo indirizzo per il trackback.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di

Stai commentando usando il tuo account Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: