Archivi Categorie: maxima

Maxima – 204 – Definire funzioni – 5

oTWI

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

compfile (filename, f_1, ..., f_n)
compfile (filename, functions)
compfile (filename, all)
Translates Maxima functions into Lisp and writes the translated code into the file filename.

compfile(filename, f_1, ..., f_n) translates the specified functions. compfile (filename, functions) and compfile (filename, all) translate all user-defined functions.

The Lisp translations are not evaluated, nor is the output file processed by the Lisp compiler. translate creates and evaluates Lisp translations. compile_file translates Maxima into Lisp, and then executes the Lisp compiler.

See also translate, translate_file, and compile_file.

compile (f_1, ..., f_n)
compile (functions)
compile (all)
Translates Maxima functions f_1, ..., f_n into Lisp, evaluates the Lisp translations, and calls the Lisp function COMPILE on each translated function. compile returns a list of the names of the compiled functions.

compile (all) or compile (functions) compiles all user-defined functions.

compile quotes its arguments; the quote-quote operator '' defeats quotation.

Compiling a function to native code can mean a big increase in speed and might cause the memory footprint to reduce drastically. Code tends to be especially effective when the flexibility it needs to provide is limited. If compilation doesn’t provide the speed that is needed a few ways to limit the code’s functionality are the following:

  • If the function accesses global variables the complexity of the function can be drastically be reduced by limiting these variables to one data type, for example using mode_declare or a statement like the following one: put(x_1, bigfloat, numerical_type)
  • The compiler might warn about undeclared variables if text could either be a named option to a command or (if they are assigned a value to) the name of a variable. Prepending the option with a single quote ' tells the compiler that the text is meant as an option.

define (f(x_1, ..., x_n), expr)
define (f[x_1, .--, x_n], expr)
define (f[x_1, ..., x_n] (y_1, ..., y_m ), expr)
define (funmake (f, [x_1, ..., x_n], expr)
define (arraymake (f, [x_1, ..., x_n], expr)
define (ev (expr_1), expr_2)
Defines a function named f with arguments x_1, ..., x_n and function body expr.

define always evaluates its second argument (unless explicitly quoted). The function so defined may be an ordinary Maxima function (with arguments enclosed in parentheses) or an array function (with arguments enclosed in square brackets).

When the last or only function argument x_n is a list of one element, the function defined by define accepts a variable number of arguments. Actual arguments are assigned one-to-one to formal arguments x_1, ..., x_(n - 1), and any further actual arguments, if present, are assigned to x_n as a list.

When the first argument of define is an expression of the form f(x_1, ..., x_n) or f[x_1, ..., x_n], the function arguments are evaluated but f is not evaluated, even if there is already a function or variable by that name.

When the first argument is an expression with operator funmake, arraymake, or ev, the first argument is evaluated; this allows for the function name to be computed, as well as the body.

All function definitions appear in the same namespace; defining a function f within another function g does not automatically limit the scope of f to g. However, local(f) makes the definition of function f effective only within the block or other compound expression in which local appears.

If some formal argument x_k is a quoted symbol (after evaluation), the function defined by define does not evaluate the corresponding actual argument. Otherwise all actual arguments are evaluated.

See also := and ::=.

define always evaluates its second argument (unless explicitly quoted).

(%i1) expr : cos(y) - sin(x);
(%o1)                           cos(y) - sin(x)
(%i2) define (F1 (x, y), expr);
(%o2)                     F1(x, y) := cos(y) - sin(x)
(%i3) F1 (a, b);
(%o3)                           cos(b) - sin(a)
(%i4) F2 (x, y) := expr;
(%o4)                          F2(x, y) := expr
(%i5) F2 (a, b);
(%o5)                           cos(y) - sin(x)

The function defined by define may be an ordinary Maxima function or an array function.

(%i6) define (G1 (x, y), x.y - y.x);
(%o6)                      G1(x, y) := x . y - y . x
(%i7) define (G2 [x, y], x.y - y.x);
(%o7)                       G2     := x . y - y . x
                              x, y

When the last or only function argument x_n is a list of one element, the function defined by define accepts a variable number of arguments.

(%i8) define (H ([L]), '(apply ("+", L)));
(%o8)                       H([L]) := apply("+", L)
(%i9) H (a, b, c);
(%o9)                              c + b + a

When the first argument is an expression with operator funmake, arraymake, or ev, the first argument is evaluated.

(%i10) [F : I, u : x];
(%o10)                              [I, x]
(%i11) funmake (F, [u]);
(%o11)                               I(x)
(%i12) define (funmake (F, [u]), cos(u) + 1);
(%o12)                        I(x) := cos(x) + 1
(%i13) define (arraymake (F, [u]), cos(u) + 1);
(%o13)                         I  := cos(x) + 1
                                x
(%i14) define (foo (x, y), bar (y, x));
(%o14)                      foo(x, y) := bar(y, x)
(%i15) define (ev (foo (x, y)), sin(x) - cos(y));
(%o15)                   bar(y, x) := sin(x) - cos(y)

Maxima – 203 – Definire funzioni – 4

Amsterdam

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;
                                          x
(%o3)                            F(x) := ----
                                         1729
(%i4) fname : F;
(%o4)                                  F
(%i5) dispfun (F);
                                          x
(%t5)                            F(x) := ----
                                         1729

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

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

(%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.

Maxima – 202 – Definire funzioni – 3

DwkZ

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

macroexpand1 (expr)
Returns the macro expansion of expr without evaluating it, when expr is a macro function call. Otherwise, macroexpand1 returns expr.

macroexpand1 quotes its argument. However, if the expansion of a macro function call has side effects, those side effects are executed.

If the expansion of expr yields another macro function call, that macro function call is not expanded.

See also ::=, macros, and macroexpand.

(%i1) g (x) ::= x / 99;
                                           x
(%o1)                             g(x) ::= --
                                           99
(%i2) h (x) ::= buildq ([x], g (x - a));
(%o2)                   h(x) ::= buildq([x], g(x - a))
(%i3) a: 1234;
(%o3)                                1234
(%i4) macroexpand1 (h (y));
(%o4)                              g(y - a)
(%i5) h (y);
                                   y - 1234
(%o5)                              --------
                                      99

Differenza rispetto macroexpand: solo in %o4.

macros
Default value: [].

macros is the list of user-defined macro functions. The macro function definition operator ::= puts a new macro function onto this list, and kill, remove, and remfunction remove macro functions from the list.

See also infolists.

splice (a)
Splices (interpolates) the list named by the atom a into an expression, but only if splice appears within buildq; otherwise, splice is treated as an undefined function. If appearing within buildq as a alone (without splice), a is substituted (not interpolated) as a list into the result. The argument of splice can only be an atom; it cannot be a literal list or an expression which yields a list.

Typically splice supplies the arguments for a function or operator. For a function f, the expression f (splice (a)) within buildq expands to f (a[1], a[2], a[3], ...). For an operator o, the expression "o" (splice (a)) within buildq expands to "o" (a[1], a[2], a[3], ...), where o may be any type of operator (typically one which takes multiple arguments). Note that the operator must be enclosed in double quotes ".

(%i6) buildq ([x: [1, %pi, z - y]], foo (splice (x)) / length (x));
                              foo(1, %pi, z - y)
(%o6)                       -----------------------
                            length([1, %pi, z - y])
(%i7) buildq ([x: [1, %pi]], "/" (splice (x)));
                                       1
(%o7)                                 ---
                                      %pi
(%i8) matchfix ("<>", "<>");
(%o8)                                 <>
(%i9) buildq ([x: [1, %pi, z - y]], "<>" (splice (x)));
(%o9)                          <>1, %pi, z - y<>

Maxima – 201 – Definire funzioni – 2

top2

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

Macros

buildq (L, expr)
Substitutes variables named by the list L into the expression expr, in parallel, without evaluating expr. The resulting expression is simplified, but not evaluated, after buildq carries out the substitution.

The elements of L are symbols or assignment expressions symbol: value, evaluated in parallel. That is, the binding of a variable on the right-hand side of an assignment is the binding of that variable in the context from which buildq was called, not the binding of that variable in the variable list L. If some variable in L is not given an explicit assignment, its binding in buildq is the same as in the context from which buildq was called.

Then the variables named by L are substituted into expr in parallel. That is, the substitution for every variable is determined before any substitution is made, so the substitution for one variable has no effect on any other.

If any variable x appears as splice (x) in expr, then x must be bound to a list, and the list is spliced (interpolated) into expr instead of substituted.

Any variables in expr not appearing in L are carried into the result verbatim, even if they have bindings in the context from which buildq was called.

a is explicitly bound to x, while b has the same binding (namely 29) as in the calling context, and c is carried through verbatim. The resulting expression is not evaluated until the explicit evaluation ''%.

(%i1) (a: 17, b: 29, c: 1729)$

(%i2) buildq ([a: x, b], a + b + c);
(%o2)                             x + c + 29
(%i3) ''%;
(%o3)                              x + 1758

e is bound to a list, which appears as such in the arguments of foo, and interpolated into the arguments of bar.

(%i1) buildq ([e: [a, b, c]], foo (x, e, y));
(%o1)                        foo(x, [a, b, c], y)
(%i2) buildq ([e: [a, b, c]], 2 * splice (e));
(%o2)                               2 a b c

The result is simplified after substitution. If simplification were applied before substitution, these two results would be the same.

(%i1) buildq ([e: [a, b, c]], splice (e) + splice (e));
(%o1)                           2 c + 2 b + 2 a
(%i2) buildq ([e: [a, b, c]], 2 * splice (e));
(%o2)                               2 a b c

The variables in L are bound in parallel; if bound sequentially, the first result would be foo (b, b). Substitutions are carried out in parallel; compare the second result with the result of subst, which carries out substitutions sequentially.

(%i1) buildq ([a: b, b: a], foo (a, b));
(%o1)                              foo(b, a)
(%i2) buildq ([u: v, v: w, w: x, x: y, y: z, z: u],
              bar (u, v, w, x, y, z));
(%o2)                        bar(v, w, x, y, z, u)
(%i3) subst ([u=v, v=w, w=x, x=y, y=z, z=u],
             bar (u, v, w, x, y, z));
(%o3)                        bar(u, u, u, u, u, u)

Construct a list of equations with some variables or expressions on the left-hand side and their values on the right-hand side. macroexpand shows the expression returned by show_values.

(%i1) show_values ([L]) ::= buildq ([L], map ("=", 'L, L));
(%o1)          show_values([L]) ::= buildq([L], map("=", 'L, L))
(%i2) (a: 17, b: 29, c: 1729)$

(%i3) show_values (a, b, c - a - b);
(%o3)                 [a = 17, b = 29, c - b - a = 1683]
(%i4) macroexpand (show_values (a, b, c - a - b));
(%o4)           map(=, '([a, b, c - b - a]), [a, b, c - b - a])

Given a function of several arguments, create another function for which some of the arguments are fixed.

(%i1) curry (f, [a]) :=
             buildq ([f, a], lambda ([[x]], apply (f, append (a, x))))$

(%i2) by3 : curry ("*", 3);
(%o2)               lambda([[x]], apply(*, append([3], x)))
(%i3) by3 (a + b);
(%o3)                              3 (b + a)

macroexpand (expr)
Returns the macro expansion of expr without evaluating it, when expr is a macro function call. Otherwise, macroexpand returns expr.

If the expansion of expr yields another >macro function call, that macro function call is also expanded.

macroexpand quotes its argument. However, if the expansion of a macro function call has side effects, those side effects are executed.

See also ::=, macros, and macroexpand1.

(%i1) g (x) ::= x / 99;
                                           x
(%o1)                             g(x) ::= --
                                           99
(%i2) h (x) ::= buildq ([x], g (x - a));
(%o2)                   h(x) ::= buildq([x], g(x - a))
(%i3) a: 1234;
(%o3)                                1234
(%i4) macroexpand (h (y));
                                     y - a
(%o4)                                -----
                                      99
(%i5) h (y);
                                   y - 1234
(%o5)                              --------
                                      99

Maxima – 200 – Definire funzioni – 1

indri

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);

and

f(1);

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.

Maxima – 199 – Insiemi – 8

DvOX

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

stirling1 (n, m)
Represents the Stirling number of the first kind.

When n and m are nonnegative integers, the magnitude of stirling1 (n, m) is the number of permutations of a set with n members that have m cycles.

stirling1 is a simplifying function. Maxima knows the following identities:

  • stirling1(1, k) = kron_delta(1, k), k >= 0,(see here)
  • stirling1(n, n) = 1, n >= 0 (see here)
  • stirling1(n, n − 1) = −binomial(n, 2), n >= 1, (see here)
  • stirling1(n, 0) = kron d elta(n, 0), n >= 0 (see here and here)
  • stirling1(n, 1) = (−1) ( n − 1)(n − 1)!, n >= 1 (see here)
  • stirling1(n, k) = 0, n >= 0 and k > n.

These identities are applied when the arguments are literal integers or symbols declared as integers, and the first argument is nonnegative. stirling1 does not simplify for non-integer arguments.

(%i1) declare (n, integer)$

(%i2) assume (n >= 0)$

(%i3) stirling1 (n, n);
(%o3)                                  1

stirling2 (n, m)
Represents the Stirling number of the second kind.

When n and m are nonnegative integers, stirling2 (n, m) is the number of ways a set with cardinality n can be partitioned into m disjoint subsets.

  • stirling2 is a simplifying function. Maxima knows the following identities:
  • stirling2(n, 0) = 1, n >= 1 (see here) and stirling2(0,0) = 1)
  • stirling2(n, n) = 1, n >= 0, (see here)
  • stirling2(n, 1) = 1, n >= 1, (see here) and stirling2(0,1) = 0)
  • stirling2(n, 2) = 2 ( n − 1) − 1, n >= 1, (see here)
  • stirling2(n, n − 1) = binomial(n, 2), n >= 1 (see here)
  • stirling2(n, k) = 0, n >= 0 and k > n.

These identities are applied when the arguments are literal integers or symbols declared as integers, and the first argument is nonnegative. stirling2 does not simplify for non-integer arguments.

(%i1) declare (n, integer)$

(%i2) assume (n >= 0)$

(%i3) stirling2 (n, n);
(%o3)                                  1

stirling2 does not simplify for non-integer arguments.

(%i4) stirling2 (%pi, %pi);
(%o4)                         stirling2(%pi, %pi)

subset (a, f)
Returns the subset of the set a that satisfies the predicate f.

subset returns a set which comprises the elements of a for which f returns anything other than false. subset does not apply is to the return value of f.

subset complains if a is not a literal set.

See also partition_set.

(%i5) subset ({1, 2, x, x + y, z, x + y + z}, atom);
(%o5)                            {1, 2, x, z}
(%i6) subset ({1, 2, 7, 8, 9, 14}, evenp);
(%o6)                             {2, 8, 14}

subsetp (a, b)
Returns true if and only if the set a is a subset of b.

subsetp complains if either a or b is not a literal set.

(%i7) subsetp ({1, 2, 3}, {a, 1, b, 2, c, 3});
(%o7)                                true
(%i8) subsetp ({a, 1, b, 2, c, 3}, {1, 2, 3});
(%o8)                                false

symmdifference (a_1, ..., a_n)
Returns the symmetric difference of sets a_1, ..., a_n.

Given two arguments, symmdifference (a, b) is the same as union (setdifference (a, b), setdifference (b, a)).

symmdifference complains if any argument is not a literal set.

(%i9) S_1 : {a, b, c};
(%o9)                              {a, b, c}
(%i10) S_2 : {1, b, c};
(%o10)                             {1, b, c}
(%i11) S_3 : {a, b, z};
(%o11)                             {a, b, z}
(%i12) symmdifference ();
(%o12)                                {}
(%i13) symmdifference (S_1);
(%o13)                             {a, b, c}
(%i14) symmdifference (S_1, S_2);
(%o14)                              {1, a}
(%i15) symmdifference (S_1, S_2, S_3);
(%o15)                             {1, b, z}
(%i16) symmdifference ({}, S_1, S_2, S_3);
(%o16)                             {1, b, z}

union (a_1, ..., a_n)
Returns the union of the sets a_1 through a_n.

union() (with no arguments) returns the empty set.

union complains if any argument is not a literal set.

(%i17) S_1 : {a, b, c + d, %e};
(%o17)                         {%e, a, b, d + c}
(%i18) S_2 : {%pi, %i, %e, c + d};
(%o18)                       {%e, %i, %pi, d + c}
(%i19) S_3 : {17, 29, 1729, %pi, %i};
(%o19)                      {17, 29, 1729, %i, %pi}
(%i20) union ();
(%o20)                                {}
(%i21) union (S_1);
(%o21)                         {%e, a, b, d + c}
(%i22) union (S_1, S_2);
(%o22)                    {%e, %i, %pi, a, b, d + c}
(%i23) union (S_1, S_2, S_3);
(%o23)             {17, 29, 1729, %e, %i, %pi, a, b, d + c}
(%i24) union ({}, S_1, S_2, S_3);
(%o24)             {17, 29, 1729, %e, %i, %pi, a, b, d + c}

Maxima – 198 – Insiemi – 7

top1

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

setify (a)
Constructs a set from the elements of the list a. Duplicate elements of the list a are deleted and the elements are sorted according to the predicate orderlessp.

setify complains if a is not a literal list.

(%i1) setify ([1, 2, 3, a, b, c]);
(%o1)                         {1, 2, 3, a, b, c}
(%i2) setify ([a, b, c, a, b, c]);
(%o2)                              {a, b, c}
(%i3) setify ([7, 13, 11, 1, 3, 9, 5]);
(%o3)                       {1, 3, 5, 7, 9, 11, 13}

setp (a)
Returns true if and only if a is a Maxima set.

setp returns true for unsimplified sets (that is, sets with redundant members) as well as simplified sets.

setp is equivalent to the Maxima function setp(a) := not atom(a) and op(a) = 'set.

(%i4) simp : false;
(%o4)                                false
(%i5) {a, a, a};
(%o5)                              {a, a, a}
(%i6) setp (%);
(%o6)                                true

set_partitions (a)
set_partitions (a, n)
Returns the set of all partitions of a, or a subset of that set.

set_partitions(a, n) returns a set of all decompositions of a into n nonempty disjoint subsets.

set_partitions(a) returns the set of all partitions.

stirling2 returns the cardinality of the set of partitions of a set.

A set of sets P is a partition of a set S when

  1. each member of P is a nonempty set,
  2. distinct members of P are disjoint,
  3. the union of the members of P equals S.

The empty set is a partition of itself, the conditions 1 and 2 being vacuously true.

(%i8) set_partitions ({});
(%o8)                                {{}}

The cardinality of the set of partitions of a set can be found using stirling2.

(%i9) s: {0, 1, 2, 3, 4, 5}$

(%i10) p: set_partitions (s, 3)$

(%i11) cardinality(p) = stirling2 (6, 3);
(%o11)                              90 = 90

Each member of p should have n = 3 members; let’s check.

(%i12) s: {0, 1, 2, 3, 4, 5}$

(%i13) p: set_partitions (s, 3)$

(%i14) map (lambda ([x], apply (union, listify (x))), p);
(%o14)                       {{0, 1, 2, 3, 4, 5}}

ahemmm 😐

some (f, a)
some (f, L_1, ..., L_n)
Returns true if the predicate f is true for one or more given arguments.

Given one set as the second argument, some(f, s) returns true if is(f(a_i)) returns true for one or more a_i in s. some may or may not evaluate f for all a_i in s. Since sets are unordered, some may evaluate f(a_i) in any order.

Given one or more lists as arguments, some(f, L_1, ..., L_n) returns true if is(f(x_1, ..., x_n)) returns true for one or more x_1, ..., x_n in L_1, ..., L_n, respectively. some may or may not evaluate f for some combinations x_1, ..., x n. some evaluates lists in the order of increasing index.

Given an empty set {} or empty lists [] as arguments, some returns false.

When the global flag maperror is true, all lists L_1, ..., L_n must have equal lengths. When maperror is false, list arguments are effectively truncated to the length of the shortest list.

Return values of the predicate f which evaluate (via is) to something other than true or false are governed by the global flag prederror. When prederror is true, such values are treated as false. When prederror is false, such values are treated as unknown.

some applied to a single set. The predicate is a function of one argument.

(%i15) some (integerp, {1, 2, 3, 4, 5, 6});
(%o15)                               true
(%i16) some (atom, {1, 2, sin(3), 4, 5 + y, 6});
(%o16)                               true

some applied to two lists. The predicate is a function of two arguments.

(%i17) some ("=", [a, b, c], [a, b, c]);
(%o17)                               true
(%i18) some ("#", [a, b, c], [a, b, c]);
(%o18)                               false

Return values of the predicate f which evaluate to something other than true or false are governed by the global flag prederror.

(%i19) prederror : false;
(%o19)                               false
(%i20) map (lambda ([a, b], is (a < b)), [x, y, z], [x^2, y^2, z^2]);
(%o20)                    [unknown, unknown, unknown]
(%i21) some ("<", [x, y, z], [x^2, y^2, z^2]);
(%o21)                              unknown
(%i22) some ("<", [x, y, z], [x^2, y^2, z + 1]);
(%o22)                               true
(%i23) prederror : true;
(%o23)                               true
(%i24) some ("<", [x, y, z], [x^2, y^2, z^2]);
(%o24)                               false
(%i25) some ("<", [x, y, z], [x^2, y^2, z + 1]);
(%o25)                               true

Maxima – 197 – Insiemi – 6

wzrQ

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

num_distinct_partitions (n, list)
num_distinct_partitions (n)
Returns the number of distinct integer partitions of n when n is a nonnegative integer.

Otherwise, num_distinct_partitions returns a noun expression.

num_distinct_partitions(n, list) returns a list of the number of distinct partitions of 1, 2, 3, ..., n.

A distinct partition of n is a list of distinct positive integers k_1 , ..., k_m such that n = k_1 + ... + k_m.

(%i1) num_distinct_partitions (12);
(%o1)                                 15
(%i2) num_distinct_partitions (12, list);
(%o2)               [1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15]
(%i3) num_distinct_partitions (n);
(%o3)                     num_distinct_partitions(n)

num_partitions (n)
num_partitions (n, list)
Returns the number of integer partitions of n when n is a nonnegative integer. Otherwise, num_partitions returns a noun expression.

num_partitions(n, list) returns a list of the number of integer partitions of 1, 2, 3, ..., n.

For a nonnegative integer n, num_partitions(n) is equal to cardinality(integer_partitions(n)); however, num_partitions does not actually construct the set of partitions, so it is much faster.

(%i4) num_partitions (5) = cardinality (integer_partitions (5));
(%o4)                                7 = 7
(%i5) num_partitions (8, list);
(%o5)                     [1, 2, 3, 5, 7, 11, 15, 22]
(%i6) num_partitions (n);
(%o6)                          num_partitions(n)

partition_set (a, f)
Partitions the set a according to the predicate f.

partition_set returns a list of two sets. The first set comprises the elements of a for which f evaluates to false, and the second comprises any other elements of a.

partition_set does not apply is to the return value of f.

partition_set complains if a is not a literal set.

See also subset.

(%i7) partition_set ({2, 7, 1, 8, 2, 8}, evenp);
(%o7)                          [{1, 7}, {2, 8}]
(%i8) partition_set ({x, rat(y), rat(y) + z, 1}, lambda ([x], ratp(x)));
(%o8)/R/                     [{1, x}, {y, y + z}]

permutations (a)
Returns a set of all distinct permutations of the members of the list or set a. Each permutation is a list, not a set.

When a is a list, duplicate members of a are included in the permutations.

permutations complains if a is not a literal list or set.

See also random_permutation.

(%i9) permutations ([a, a]);
(%o9)                              {[a, a]}
(%i10) permutations ([a, a, b]);
(%o10)                 {[a, a, b], [a, b, a], [b, a, a]}

powerset (a)
powerset (a, n)
Returns the set of all subsets of a, or a subset of that set.

powerset(a) returns the set of all subsets of the set a. powerset(a) has 2^cardinality(a) members.

powerset(a, n) returns the set of all subsets of a that have cardinality n.

powerset complains if a is not a literal set, or if n is not a nonnegative integer.

(%i11) powerset ({a, b, c});
(%o11)      {{}, {a}, {a, b}, {a, b, c}, {a, c}, {b}, {b, c}, {c}}
(%i12) powerset ({w, x, y, z}, 4);
(%o12)                          {{w, x, y, z}}
(%i13) powerset ({w, x, y, z}, 3);
(%o13)           {{w, x, y}, {w, x, z}, {w, y, z}, {x, y, z}}
(%i14) powerset ({w, x, y, z}, 2);
(%o14)         {{w, x}, {w, y}, {w, z}, {x, y}, {x, z}, {y, z}}
(%i15) powerset ({w, x, y, z}, 1);
(%o15)                       {{w}, {x}, {y}, {z}}
(%i16) powerset ({w, x, y, z}, 0);
(%o16)                               {{}}

random_permutation (a)
Returns a random permutation of the set or list a, as constructed by the Knuth shuffle algorithm.

The return value is a new list, which is distinct from the argument even if all elements happen to be the same. However, the elements of the argument are not copied.

(%i17) random_permutation ([a, b, c, 1, 2, 3]);
(%o17)                        [c, 1, 2, 3, a, b]
(%i18) random_permutation ([a, b, c, 1, 2, 3]);
(%o18)                        [b, 3, 1, c, a, 2]
(%i19) random_permutation ({x + 1, y + 2, z + 3});
(%o19)                       [y + 2, z + 3, x + 1]
(%i20) random_permutation ({x + 1, y + 2, z + 3});
(%o20)                       [x + 1, y + 2, z + 3]

setdifference (a, b)
Returns a set containing the elements in the set a that are not in the set b.

setdifference complains if either a or b is not a literal set.

(%i21) S_1 : {a, b, c, x, y, z};
(%o21)                        {a, b, c, x, y, z}
(%i22) S_2 : {aa, bb, c, x, y, zz};
(%o22)                       {aa, bb, c, x, y, zz}
(%i23) setdifference (S_1, S_2);
(%o23)                             {a, b, z}
(%i24) setdifference (S_2, S_1);
(%o24)                           {aa, bb, zz}
(%i25) setdifference (S_1, S_1);
(%o25)                                {}
(%i26) setdifference (S_1, {});
(%o26)                        {a, b, c, x, y, z}
(%i27) setdifference ({}, S_1);
(%o27)                                {}

setequalp (a, b)
Returns true if sets a and b have the same number of elements and is(x = y) is true for x in the elements of a and y in the elements of b, considered in the order determined by listify. Otherwise, setequalp returns false.

(%i28) setequalp ({1, 2, 3}, {1, 2, 3});
(%o28)                               true
(%i29) setequalp ({a, b, c}, {1, 2, 3});
(%o29)                               false
(%i30) setequalp ({x^2 - y^2}, {(x + y) * (x - y)});
(%o30)                               false

Maxima – 196 – Insiemi – 5

FO9c

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

intersect (a_1, ..., a_n)
intersect is the same as intersection.

intersection (a_1, ..., a_n)
Returns a set containing the elements that are common to the sets a_1 through a_n.

intersection complains if any argument is not a literal set.

(%i1) S_1 : {a, b, c, d};
(%o1)                            {a, b, c, d}
(%i2) S_2 : {d, e, f, g};
(%o2)                            {d, e, f, g}
(%i3) S_3 : {c, d, e, f};
(%o3)                            {c, d, e, f}
(%i4) S_4 : {u, v, w};
(%o4)                              {u, v, w}
(%i5) intersection (S_1, S_2);
(%o5)                                 {d}
(%i6) intersection (S_2, S_3);
(%o6)                              {d, e, f}
(%i7) intersection (S_1, S_2, S_3);
(%o7)                                 {d}
(%i8) intersection (S_1, S_2, S_3, S_4);
(%o8)                                 {}

kron_delta (x1, x2, ..., xp)
Represents the Kronecker delta function.

kron_delta simplifies to 1 when xi and yj are equal for all pairs of arguments, and it simplifies to 0 when xi and yj are not equal for some pair of arguments. Equality is determined using is(equal(xi, xj)) and inequality by is(notequal(xi,xj)). For exactly one argument, kron_delta signals an error.

(%i9) kron_delta(a,a);
(%o9)                                  1
(%i10) kron_delta(a,b,a,b);
(%o10)                         kron_delta(a, b)
(%i11) kron_delta(a,a,b,a+1);
(%o11)                                 0
(%i12) assume(equal(x,y));
(%o12)                           [equal(x, y)]
(%i13) kron_delta(x,y);
(%o13)                                 1

listify (a)
Returns a list containing the members of a when a is a set. Otherwise, listify returns a.

full_listify replaces all set operators in a by list operators.

(%i14) listify ({a, b, c, d});
(%o14)                           [a, b, c, d]
(%i15) listify (F ({a, b, c, d}));
(%o15)                          F({a, b, c, d})

makeset (expr, x, s)
Returns a set with members generated from the expression expr, where x is a list of variables in expr, and s is a set or list of lists. To generate each set member, expr is evaluated with the variables x bound in parallel to a member of s.

Each member of s must have the same length as x. The list of variables x must be a list of symbols, without subscripts. Even if there is only one symbol, x must be a list of one element, and each member of s must be a list of one element.

See also makelist.

(%i16) makeset (i/j, [i, j], [[1, a], [2, b], [3, c], [4, d]]);
                                  1  2  3  4
(%o16)                           {-, -, -, -}
                                  a  b  c  d
(%i17) S : {x, y, z}$

(%i18) S3 : cartesian_product (S, S, S);
(%o18) {[x, x, x], [x, x, y], [x, x, z], [x, y, x], [x, y, y], [x, y, z],
[x, z, x], [x, z, y], [x, z, z], [y, x, x], [y, x, y], [y, x, z], [y, y, x],
[y, y, y], [y, y, z], [y, z, x], [y, z, y], [y, z, z], [z, x, x], [z, x, y],
[z, x, z], [z, y, x], [z, y, y], [z, y, z], [z, z, x], [z, z, y], [z, z, z]}
(%i19) makeset (i + j + k, [i, j, k], S3);
(%o19) {3 x, 3 y, y + 2 x, 2 y + x, 3 z, z + 2 x, z + y + x, z + 2 y, 2 z + x,
                                                                       2 z + y}
(%i20) makeset (sin(x), [x], {[1], [2], [3]});
(%o20)                     {sin(1), sin(2), sin(3)}

moebius (n)
Represents the Moebius function.

When n is product of k distinct primes, moebius(n) simplifies to (−1) k; when n = 1, it simplifies to 1; and it simplifies to 0 for all other positive integers.

moebius distributes over equations, lists, matrices, and sets.

(%i22) moebius (1);
(%o22)                                 1
(%i23) moebius (2 * 3 * 5);
(%o23)                                - 1
(%i24) moebius (11 * 17 * 29 * 31);
(%o24)                                 1
(%i25) moebius (2^32);
(%o25)                                 0
(%i26) moebius (n);
(%o26)                            moebius(n)
(%i27) moebius (n = 12);
(%o27)                          moebius(n) = 0
(%i28) moebius ([11, 11 * 13, 11 * 13 * 15]);
(%o28)                            [- 1, 1, 1]
(%i29) moebius (matrix ([11, 12], [13, 14]));
                                  [ - 1  0 ]
(%o29)                            [        ]
                                  [ - 1  1 ]
(%i30) moebius ({21, 22, 23, 24});
(%o30)                            {- 1, 0, 1}

multinomial_coeff (a_1, ..., a_n)
multinomial_coeff ()
Returns the multinomial coefficient.

When each a_k is a nonnegative integer, the multinomial coefficient gives the number of ways of placing a_1 + ... + a_n distinct objects into n boxes with a_k elements in the k’th box. In general, multinomial_coeff (a_1, ..., a_n) evaluates to (a_1 + ... + a_n)!/(a_1! ... a_n!).

multinomial_coeff() (with no arguments) evaluates to 1.

minfactorial may be able to simplify the value returned by multinomial_coeff.

(%i31) multinomial_coeff (1, 2, x);
                                   (x + 3)!
(%o31)                             --------
                                     2 x!
(%i32) minfactorial (%);
                            (x + 1) (x + 2) (x + 3)
(%o32)                      -----------------------
                                       2
(%i33) multinomial_coeff (-6, 2);

factorial: factorial of negative integer -6 not defined.
 -- an error. To debug this try: debugmode(true);

OOPS! diverso dal Reference 😐

Maxima – 195 – Insiemi – 4

top5.png

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

extremal_subset (s, f, max)
extremal_subset (s, f, min)
Returns the subset of s for which the function f takes on maximum or minimum values.

extremal_subset(s, f, max) returns the subset of the set or list s for which the real-valued function f takes on its maximum value.

extremal_subset(s, f, min) returns the subset of the set or list s for which the real-valued function f takes on its minimum value.

(%i1) extremal_subset ({-2, -1, 0, 1, 2}, abs, max);
(%o1)                              {- 2, 2}
(%i2) extremal_subset ({sqrt(2), 1.57, %pi/2}, sin, min);
(%o2)                              {sqrt(2)}

flatten (expr)
Collects arguments of subexpressions which have the same operator as expr and constructs an expression from these collected arguments.

Subexpressions in which the operator is different from the main operator of expr are copied without modification, even if they, in turn, contain some subexpressions in which the operator is the same as for expr.

It may be possible for flatten to construct expressions in which the number of arguments differs from the declared arguments for an operator; this may provoke an error message from the simplifier or evaluator. flatten does not try to detect such situations.

Expressions with special representations, for example, canonical rational expressions (CRE), cannot be flattened; in such cases, flatten returns its argument unchanged.

Applied to a list, flatten gathers all list elements that are lists.

(%i3) flatten ([a, b, [c, [d, e], f], [[g, h]], i, j]);
(%o3)                   [a, b, c, d, e, f, g, h, i, j]

Applied to a set, flatten gathers all members of set elements that are sets.

(%i4) flatten ({a, {b}, {{c}}});
(%o4)                              {a, b, c}
(%i5) flatten ({a, {[a], {a}}});
(%o5)                              {a, [a]}

flatten is similar to the effect of declaring the main operator n-ary. However, flatten has no effect on subexpressions which have an operator different from the main operator, while an n-ary declaration affects those.

(%i6) expr: flatten (f (g (f (f (x)))));
(%o6)                            f(g(f(f(x))))
(%i7) declare (f, nary);
(%o7)                                done
(%i8) ev (expr);
(%o8)                             f(g(f(x)))

flatten treats subscripted functions the same as any other operator.

(%i9) flatten (f[5] (f[5] (x, y), z));
(%o9)                             f (x, y, z)
                                   5

It may be possible for flatten to construct expressions in which the number of arguments differs from the declared arguments for an operator

(%i10) 'mod (5, 'mod (7, 4));
(%o10)                         mod(5, mod(7, 4))
(%i11) flatten (%);
(%o11)                           mod(5, 7, 4)
(%i12) ''%, nouns;

mod: wrong number of arguments.
 -- an error. To debug this try: debugmode(true);

full_listify (a)
Replaces every set operator in a by a list operator, and returns the result.

full_listify replaces set operators in nested subexpressions, even if the main operator is not set.

listify replaces only the main operator.

(%i13) full_listify ({a, b, {c, {d, e, f}, g}});
(%o13)                     [a, b, [c, [d, e, f], g]]
(%i14) full_listify (F (G ({a, b, H({c, d, e})})));
(%o14)                    F(G([a, b, H([c, d, e])]))

fullsetify (a)
When a is a list, replaces the list operator with a set operator, and applies fullsetify to each member which is a set. When a is not a list, it is returned unchanged.

setify replaces only the main operator.

In line (%o2), the argument of f isn’t converted to a set because the main operator of f([b]) isn’t a list.

(%i1) fullsetify ([a, [a]]);
(%o1)                              {a, {a}}
(%i2) fullsetify ([a, f([b])]);
(%o2)                             {a, f([b])}

identity (x)
Returns x for any argument x.

identity may be used as a predicate when the arguments are already Boolean values.

(%i3) every (identity, [true, true]);
(%o3)                                true

integer_partitions (n)
integer_partitions (n, len)
Returns integer partitions of n, that is, lists of integers which sum to n.

integer_partitions(n) returns the set of all partitions of the integer n. Each partition is a list sorted from greatest to least.

integer_partitions(n, len) returns all partitions that have length len or less; in this case, zeros are appended to each partition with fewer than len terms to make each partition have exactly len terms. Each partition is a list sorted from greatest to least.

A list [a_1 , ..., a_m] is a partition of a nonnegative integer n when (1) each a_i is a nonzero integer, and (2) a_1 + ... + a_m = n. Thus 0 has no partitions.

(%i4) integer_partitions (3);
(%o4)                      {[1, 1, 1], [2, 1], [3]}
(%i5) s: integer_partitions (25)$

(%i6) cardinality (s);
(%o6)                                1958
(%i7) map (lambda ([x], apply ("+", x)), s);
(%o7)                                {25}
(%i8) integer_partitions (5, 3);
(%o8)       {[2, 2, 1], [3, 1, 1], [3, 2, 0], [4, 1, 0], [5, 0, 0]}
(%i9) integer_partitions (5, 2);
(%o9)                      {[3, 2], [4, 1], [5, 0]}

To find all partitions that satisfy a condition, use the function subset; here is an example that finds all partitions of 10 that consist of prime numbers.

(%i10) s: integer_partitions (10)$

(%i11) cardinality (s);
(%o11)                                42
(%i12) xprimep(x) := integerp(x) and (x > 1) and primep(x)$

(%i13) subset (s, lambda ([x], every (xprimep, x)));
(%o13)    {[2, 2, 2, 2, 2], [3, 3, 2, 2], [5, 3, 2], [5, 5], [7, 3]}