Maxima – 203 – Definire funzioni – 4


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

Funzioni e variabili

apply (F, [x_1, ..., x_n])
Constructs and evaluates an expression F(arg_1, ..., arg_n).

apply does not attempt to distinguish array functions from ordinary functions; when F is the name of an array function, apply evaluates F(...) (that is, a function call with parentheses instead of square brackets). arrayapply evaluates a function call with square brackets in this case.

See also funmake and args.

apply evaluates its arguments. In this example, min is applied to the value of L.

(%i1) L : [1, 5, -10.2, 4, 3];
(%o1)                        [1, 5, - 10.2, 4, 3]
(%i2) apply (min, L);
(%o2)                               - 10.2

apply evaluates arguments, even if the function F quotes them.

(%i3) F (x) := x / 1729;
(%o3)                            F(x) := ----
(%i4) fname : F;
(%o4)                                  F
(%i5) dispfun (F);
(%t5)                            F(x) := ----

(%o5)                                [%t5]
(%i6) dispfun (fname);

fundef: no such function: fname
 -- an error. To debug this try: debugmode(true);
(%i7) apply (dispfun, [fname]);
(%t7)                            F(x) := ----

(%o7)                                [%t7]

apply evaluates the function name F. Single quote ' defeats evaluation. demoivre is the name of a global variable and also a function.

(%i8) demoivre;
(%o8)                                false
(%i9) demoivre (exp (%i * x));
(%o9)                         %i sin(x) + cos(x)
(%i10) apply (demoivre, [exp (%i * x)]);

apply: found false where a function was expected.
 -- an error. To debug this try: debugmode(true);
(%i11) apply ('demoivre, [exp (%i * x)]);
(%o11)                        %i sin(x) + cos(x)

How to convert a nested list into a matrix:

(%i12) a:[[1,2],[3,4]];
(%o12)                         [[1, 2], [3, 4]]
(%i13) apply(matrix, a);
                                   [ 1  2 ]
(%o13)                             [      ]
                                   [ 3  4 ]

block ([v_1, ..., v_m], expr_1, ..., expr_n)
block (expr_1, ..., expr_n)
The function block allows to make the variables v_1, ..., v_m to be local for a sequence of commands. If these variables are already bound block saves the current values of the variables v_1, ..., v_m (if any) upon entry to the block, then unbinds the variables so that they evaluate to themselves; The local variables may be bound to arbitrary values within the block but when the block is exited the saved values are restored, and the values assigned within the block are lost.

If there is no need to define local variables then the list at the beginning of the block command may be omitted. In this case if neither return nor go are used block behaves similar to the following construct:

( expr_1, expr_2,... , expr_n );

expr_1, ..., expr_n will be evaluated in sequence and the value of the last expression will be returned. The sequence can be modified by the go, throw, and return functions. The last expression is expr_n unless return or an expression containing throw is evaluated.

The declaration local(v_1, ..., v_m) within block saves the properties associated with the symbols v_1, ..., v_m, removes any properties before evaluating other expressions, and restores any saved properties on exit from the block. Some declarations are implemented as properties of a symbol, including :=, array, dependencies, atvalue, matchdeclare, atomgrad, constant, nonscalar, assume, and some others. The effect of local is to make such declarations effective only within the block; otherwise declarations within a block are actually global declarations.

block may appear within another block. Local variables are established each time a new block is evaluated. Local variables appear to be global to any enclosed blocks. If a variable is non-local in a block, its value is the value most recently assigned by an enclosing block, if any, otherwise, it is the value of the variable in the global environment. This policy may coincide with the usual understanding of “dynamic scope”.

The value of the block is the value of the last statement or the value of the argument to the function return which may be used to exit explicitly from the block. The function go may be used to transfer control to the statement of the block that is tagged with the argument to go. To tag a statement, precede it by an atomic argument as another statement in the block. For example: block ([x], x:1, loop, x: x+1, ..., go(loop), ...). The argument to go must be the name of a tag appearing within the block. One cannot use go to transfer to a tag in a block other than the one containing the go.

Blocks typically appear on the right side of a function definition but can be used in other places as well.

See also return and go.

break (expr_1, ..., expr_n)
Evaluates and prints expr_1, ..., expr_n and then causes a Maxima break at which point the user can examine and change his environment. Upon typing exit; the computation resumes.

catch (expr_1, ..., expr_n)
Evaluates expr_1, ..., expr_n one by one; if any leads to the evaluation of an expression of the form throw (arg), then the value of the catch is the value of throw (arg), and no further expressions are evaluated. This “non-local return” thus goes through any depth of nesting to the nearest enclosing catch. If there is no catch enclosing a throw, an error message is printed.

If the evaluation of the arguments does not lead to the evaluation of any throw then the value of catch is the value of expr_n.

(%i14) lambda ([x], if x < 0 then throw(x) else f(x))$

(%i15) g(l) := catch (map (''%, l))$

(%i16) g ([1, 2, 3, 7]);
(%o16)                     [f(1), f(2), f(3), f(7)]
(%i17) g ([1, 2, -3, 7]);
(%o17)                                - 3

The function g returns a list of f of each element of l if l consists only of non-negative numbers; otherwise, g “catches” the first negative element of l and “throws” it up.

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: