## Maxima – 195 – Insiemi – 4 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 (f (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], }
(%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]}``````

Posta un commento o usa questo indirizzo per il trackback.