Go to the first, previous, next, last section, table of contents.

Maxima provides set functions, such as intersection and union, for finite sets that are defined by explicit enumeration. Maxima treats lists and sets as distinct objects. This feature makes it possible to work with sets that have members that are either lists or sets.

In addition to functions for finite sets, Maxima provides some functions related to combinatorics; these include the Stirling numbers, the Bell numbers, and several others.

To construct a set with members `a_1, ..., a_n`

, use the
command `set(a_1, ..., a_n)`

; to construct the empty
set, use `set()`

. If a member is listed more than
once, the simplification process eliminates the redundant member.

(%i1) set(); (%o1) {} (%i2) set(a, b, a); (%o2) {a, b} (%i3) set(a, set(b)); (%o3) {a, {b}} (%i4) set(a, [b]); (%o4) {a, [b]}

Sets are always displayed as brace delimited lists; if you would like to
be able to *input* a set using braces, see Defining sets with braces.

To construct a set from the elements of a list, use `setify`

.

(%i1) setify([b, a]); (%o1) {a, b}

Set members `x`

and `y`

are equal provided `is(x = y)`

evaluates to true. Thus `rat(x)`

and `x`

are equal as set members;
consequently,

(%i1) set(x, rat(x)); (%o1) {x}

Further, since `is((x-1)*(x+1) = x^2 - 1)`

evaluates to false,
`(x-1)*(x+1)`

and `x^2-1`

are distinct set members; thus

(%i1) set((x - 1)*(x + 1), x^2 - 1); 2 (%o1) {(x - 1) (x + 1), x - 1}

To reduce this set to a singleton set, apply `rat`

to each set member:

(%i1) set((x - 1)*(x + 1), x^2 - 1); 2 (%o1) {(x - 1) (x + 1), x - 1} (%i2) map(rat, %); 2 (%o2)/R/ {x - 1}

To remove redundancies from other sets, you may need to use other
simplification functions. Here is an example that uses `trigsimp`

:

(%i1) set(1, cos(x)^2 + sin(x)^2); 2 2 (%o1) {1, sin (x) + cos (x)} (%i2) map(trigsimp, %); (%o2) {1}

A set is simplified when its members are non-redundant and
sorted. The current version of the set functions uses the Maxima function
`orderlessp`

to order sets; however, *future versions of
the set functions might use a different ordering function*.

Some operations on sets, such as substitution, automatically force a re-simplification; for example,

(%i1) s: set (a, b, c)$ (%i2) subst (c=a, s); (%o2) {a, b} (%i3) subst ([a=x, b=x, c=x], s); (%o3) {x} (%i4) map (lambda ([x], x^2), set (-1, 0, 1)); (%o4) {0, 1}

Maxima treats lists and sets as distinct objects;
functions such as `union`

and `intersection`

will signal
an error if any argument is a list. If you need to apply a set
function to a list, use the `setify`

function to convert it
to a set. Thus

(%i1) union ([1, 2], set (a, b)); Function union expects a set, instead found [1,2] -- an error. Quitting. To debug this try debugmode(true); (%i2) union (setify ([1, 2]), set (a, b)); (%o2) {1, 2, a, b}

To extract all set elements of a set `s`

that satisfy a predicate
`f`

, use `subset(s,f)`

. (A *predicate* is a
boolean-valued function.) For example, to find the equations
in a given set that do not depend on a variable `z`

, use

(%i1) subset (set (x + y + z, x - y + 4, x + y - 5), lambda ([e], freeof (z, e))); (%o1) {- y + x + 4, y + x - 5}

The section section Definitions for Sets has a complete list of the set functions in Maxima.

There two ways to to iterate over set members. One way is the use
`map`

; for example:

(%i1) map (f, set (a, b, c)); (%o1) {f(a), f(b), f(c)}

The other way is to use `for `

`x` in `s` do

(%i1) s: set (a, b, c); (%o1) {a, b, c} (%i2) for si in s do print (concat (si, 1)); a1 b1 c1 (%o2) done

The Maxima functions `first`

and `rest`

work
correctly on sets. Applied to a set, `first`

returns the first
displayed element of a set; which element that is may be
implementation-dependent. If `s`

is a set, then
`rest(s)`

is equivalent to `disjoin (first(s), s)`

.
Currently, there are other Maxima functions that work correctly
on sets.
In future versions of the set functions,
`first`

and `rest`

may function differently or not at all.

The set functions use the Maxima function `orderlessp`

to
order set members and the (Lisp-level) function `like`

to test for set
member equality. Both of these functions have known bugs (versions
5.9.2 and earlier) that may manifest if you attempt to use
sets with members that are lists or matrices that contain expressions
in CRE form. An example is

(%i1) set ([x], [rat (x)]); Maxima encountered a Lisp error: CAR: #:X13129 is not a LIST Automatically continuing. To reenable the Lisp debugger set *debugger-hook* to nil.

This command causes Maxima to halt with an error (the error message depends on which version of Lisp your Maxima uses). Another example is

(%i1) setify ([[rat(a)], [rat(b)]]); Maxima encountered a Lisp error: CAR: #:A13129 is not a LIST Automatically continuing. To reenable the Lisp debugger set *debugger-hook* to nil.

These bugs are caused by bugs in `orderlessp`

and `like`

; they
are not caused by bugs in the set functions. To illustrate, try the commands

(%i1) orderlessp ([rat(a)], [rat(b)]); Maxima encountered a Lisp error: CAR: #:B13130 is not a LIST Automatically continuing. To reenable the Lisp debugger set *debugger-hook* to nil. (%i2) is ([rat(a)] = [rat(a)]); (%o2) false

Until these bugs are fixed, do not construct sets with members that are lists or matrices containing expressions in CRE form; a set with a member in CRE form, however, shouldn't be a problem:

(%i1) set (x, rat (x)); (%o1) {x}

Maxima's `orderlessp`

has another bug that can cause problems
with set functions, namely that the ordering predicate `orderlessp`

is
not transitive. The simplest known example that shows this is

(%i1) q: x^2$ (%i2) r: (x + 1)^2$ (%i3) s: x*(x + 2)$ (%i4) orderlessp (q, r); (%o4) true (%i5) orderlessp (r, s); (%o5) true (%i6) orderlessp (q, s); (%o6) false

This bug can cause trouble will all set functions as well as with
Maxima functions in general. It's likely, but not certain, that
if all set members are either in CRE form or have been simplified
using `ratsimp`

, this bug will not manifest.

Maxima's `orderless`

and `ordergreat`

mechanisms are
incompatible with the set functions. If you need to use either `orderless`

or `ordergreat`

, issue these commands before constructing any sets
and do not use the `unorder`

command.

You may encounter two other minor bugs.
Maxima versions 5.5 and earlier had a bug in the `tex`

function that
makes the empty set incorrectly translate to TeX; this bug is fixed in
the Maxima 5.9.0. Additionally, the `setup_autoload`

function in
Maxima 5.9.0 is broken; a fix is in the `nset-init.lisp`

file
located in the directory `maxima/share/contrib/nset`

.

Maxima's sign function has a bug that may cause the Kronecker delta function to misbehave; for example:

(%i1) kron_delta (1/sqrt(2), sqrt(2)/2); (%o1) 0

The correct value is 1; the bug is related to the `sign`

bug

(%i1) sign (1/sqrt(2) - sqrt(2)/2); (%o1) pos

If you find something that you think might be a set function bug, please
report it to the Maxima bug database. See `bug_report`

.

If you'd like to be able to input sets using braces, you may do so by declaring the left brace to be a matchfix operator; this is done using the commands

(%i1) matchfix("{","}")$ (%i2) "{" ([a]) := apply (set, a)$

Now we can define sets using braces; thus

(%i1) matchfix("{","}")$ (%i2) "{" ([a]) := apply (set, a)$ (%i3) {}; (%o3) {} (%i4) {a, {a, b}}; (%o4) {a, {a, b}}

To always allow this form of set input, place the two commands in lines
(%i1) and (%i2) in your `maxima-init.mac`

file.

In addition to functions for finite sets, Maxima provides some functions related to combinatorics; these include the Stirling numbers of the first and second kind, the Bell numbers, multinomial coefficients, partitions of nonnegative integers, and a few others. Maxima also defines a Kronecker delta function.

Stavros Macrakis of Cambridge, Massachusetts and Barton Willis of the University of Nebraska at Kearney (UNK) wrote the Maxima set functions and their documentation.

__Function:__**adjoin***(*`x`,`a`)-
Adjoin
`x`to the set`a`and return a set. Thus`adjoin(`

and`x`,`a`)`union(set(x),a)`

are equivalent; however, using`adjoin`

may be somewhat faster than using`union`

. If`a`isn't a set, signal an error.(%i1) adjoin (c, set (a, b)); (%o1) {a, b, c} (%i2) adjoin (a, set (a, b)); (%o2) {a, b}

See also

`disjoin`

.

__Function:__**belln***(*`n`)-
For nonnegative integers
`n`, return the n-th Bell number. If`s`

is a set with`n`

members,`belln(n)`

is the number of partitions of`s`

. For example:(%i1) makelist (belln (i), i, 0, 6); (%o1) [1, 1, 2, 5, 15, 52, 203] (%i2) is (cardinality (set_partitions (set ())) = belln (0)); (%o2) true (%i3) is (cardinality (set_partitions (set (1, 2, 3, 4, 5, 6))) = belln (6)); (%o3) true

When

`n`isn't a nonnegative integer,`belln(n)`

doesn't simplify.(%i1) [belln (x), belln (sqrt(3)), belln (-9)]; (%o1) [belln(x), belln(sqrt(3)), belln(- 9)]

The function

`belln`

threads over equalities, lists, matrices, and sets.

__Function:__**cardinality***(*`a`)-
Return the number of distinct elements of the set
`a`.(%i1) cardinality (set ()); (%o1) 0 (%i2) cardinality (set (a, a, b, c)); (%o2) 3 (%i3) cardinality (set (a, a, b, c)), simp: false; (%o3) 3

In line (%o3), we see that cardinality works correctly even when simplification has been turned off.

__Function:__**cartesian_product***(*`b_1`, ... ,`b_n`)-
Return a set of lists of the form
`[`

, where`x_1`, ...,`x_n`]

, ...,`x_1`in`b_1`

. Signal an error when any`x_n`in`b_n``b_k`isn't a set.(%i1) cartesian_product (set (0, 1)); (%o1) {[0], [1]} (%i2) cartesian_product (set (0, 1), set (0, 1)); (%o2) {[0, 0], [0, 1], [1, 0], [1, 1]} (%i3) cartesian_product (set (x), set (y), set (z)); (%o3) {[x, y, z]} (%i4) cartesian_product (set (x), set (-1, 0, 1)); (%o4) {[x, - 1], [x, 0], [x, 1]}

__Function:__**disjoin***(*`x`,`a`)-
Remove
`x`from the set`a`and return a set. If`x`isn't a member of`a`, return`a`. Each of the following do the same thing:`disjoin(`

,`x`,`a`)`delete(`

, and`x`,`a`)`setdifference(`

; however,`a`,set(`x`))`disjoin`

is generally the fastest way to remove a member from a set. Signal an error if`a`isn't a set.

__Function:__**disjointp***(*`a`,`b`)-
Return
`true`

if the sets`a`and`b`are disjoint. Signal an error if either`a`or`b`isn't a set.

__Function:__**divisors***(*`n`)-
When
`n`is a nonzero integer, return the set of its divisors. The set of divisors includes the members 1 and`n`. The divisors of a negative integer are the divisors of its absolute value.We can verify that 28 is a perfect number.

(%i1) s: divisors(28); (%o1) {1, 2, 4, 7, 14, 28} (%i2) lreduce ("+", args(s)) - 28; (%o2) 28

The function divisors works by simplification; you shouldn't need to manually re-evaluate after a substitution. For example:

(%i1) divisors (a); (%o1) divisors(a) (%i2) subst (8, a, %); (%o2) {1, 2, 4, 8}

The function divisors threads over equalities, lists, matrices, and sets. Here is an example of threading over a list and an equality.

(%i1) divisors ([a, b, c=d]); (%o1) [divisors(a), divisors(b), divisors(c) = divisors(d)]

__Function:__**elementp***(*`x`,`a`)-
Return
`true`

if and only if`x`is a member of the set`a`. Signal an error if`a`isn't a set.

__Function:__**emptyp***(*`a`)-
Return
`true`

if and only if`a`is the empty set or the empty list.(%i1) map (emptyp, [set (), []]); (%o1) [true, true] (%i2) map (emptyp, [a + b, set (set ()), %pi]); (%o2) [false, false, false]

__Function:__**equiv_classes***(*`s`,`f`)-
Return a set of the equivalence classes of
`s`with respect to the equivalence relation`f`. The function`f`should be a boolean-valued function defined on the cartesian product of`s`with`s`. Further, the function`f`should be an equivalence relation;`equiv_classes`

, however, doesn't check that it is.(%i1) equiv_classes (set (a, b, c), lambda ([x, y], is (x=y))); (%o1) {{a}, {b}, {c}}

Actually,

`equiv_classes (`

automatically applies the Maxima function`s`,`f`)`is`

after applying the function`f`; accordingly, we can restate the previous example more briefly.(%i1) equiv_classes (set (a, b, c), "="); (%o1) {{a}, {b}, {c}}

Here is another example.

(%i1) equiv_classes (set (1, 2, 3, 4, 5, 6, 7), lambda ([x, y], remainder (x - y, 3) = 0)); (%o1) {{1, 4, 7}, {2, 5}, {3, 6}}

__Function:__**every***(*`f`,`a`)__Function:__**every***(*`f`,`L_1`, ...,`L_n`)-
The first argument

`f`should be a predicate (a function that evaluates to true, false, or unknown).Given one set as the second argument,

`every (`

returns`f`,`a`)`true`

if

returns`f`(`a_i`)`true`

for all`a_i`in`a`. Since sets are unordered,`every`

is free to evaluate

in any order.`f`(`a_i`)`every`

may or may not evaluate`f`for all`a_i`in`a`. Because the order of evaluation isn't specified, the predicate`f`should not have side-effects or signal errors for any input.Given one or more lists as arguments,

`every (`

returns`f`,`L_1`, ...,`L_n`)`true`

if

returns`f`(`x_1`, ...,`x_n`)`true`

for all`x_1`, ...,`x_n`in`L_1`, ...,`L_n`, respectively.`every`

may or may not evaluate`f`for every combination`x_1`, ...,`x_n`. Since lists are ordered,`every`

evaluates in the order of increasing index.To use

`every`

on multiple set arguments, they should first be converted to an ordered sequence so that their relative alignment becomes well-defined.If the global flag

`maperror`

is`true`

(the default), all lists`L_1`, ...,`L_n`must have equal lengths -- otherwise,`every`

signals an error. When`maperror`

is false, the list arguments are effectively truncated each to the length of the shortest list.The Maxima function

`is`

automatically applied after evaluating the predicate`f`.(%i1) every ("=", [a, b], [a, b]); (%o1) true (%i2) every ("#", [a, b], [a, b]); (%o2) false

__Function:__**extremal_subset***(*`s`,`f`, max)__Function:__**extremal_subset***(*`s`,`f`, min)-
When the third argument is max, return the subset of the set or
list
`s`for which the real-valued function`f`takes on its greatest value; when the third argument is min, return the subset for which`f`takes on its least value.(%i1) extremal_subset (set (-2, -1, 0, 1, 2), abs, max); (%o1) {- 2, 2} (%i2) extremal_subset (set (sqrt(2), 1.57, %pi/2), sin, min); (%o2) {sqrt(2)}

__Function:__**flatten***(*`e`)-
Flatten essentially evaluates an expression as if its main operator had
been declared n-ary; there is, however, one difference -- flatten doesn't
recurse into other function arguments. For example:
(%i1) expr: flatten (f (g (f (f (x))))); (%o1) f(g(f(f(x)))) (%i2) declare (f, nary); (%o2) done (%i3) ev (expr); (%o3) f(g(f(x)))

Applied to a set, flatten gathers all members of set elements that are sets; for example:

(%i1) flatten (set (a, set (b), set (set (c)))); (%o1) {a, b, c} (%i2) flatten (set (a, set ([a], set (a)))); (%o2) {a, [a]}

Flatten works correctly when the main operator is a subscripted function

(%i1) flatten (f[5] (f[5] (x))); (%o1) f (x) 5

To flatten an expression, the main operator must be defined for zero or more arguments; if this isn't the case, Maxima will halt with an error. Expressions with special representations, for example CRE expressions, can't be flattened; in this case, flatten returns its argument unchanged.

__Function:__**full_listify***(*`a`)-
If
`a`is a set, convert`a`to a list and apply`full_listify`

to each list element.To convert just the top-level operator of a set to a list, see listify.

__Function:__**fullsetify***(*`a`)-
If
`a`is a list, convert`a`to a set and apply`fullsetify`

to each set member.(%i1) fullsetify ([a, [a]]); (%o1) {a, {a}} (%i2) fullsetify ([a, f([b])]); (%o2) {a, f([b])}

In line (%o2), the argument of

`f`

isn't converted to a set because the main operator of`f([b])`

isn't a list.To convert just the top-level operator of a list to a set, see setify.

__Function:__**identity***(*`x`)-
The identity function evaluates to its argument for all inputs. To determine if every member of a set is

`true`

, you can use(%i1) every (identity, [true, true]); (%o1) true

__Function:__**integer_partitions***(*`n`)__Function:__**integer_partitions***(*`n`,`len`)-
If the optional second argument
`len`isn't specified, return the set of all partitions of the integer`n`. When`len`is specified, return 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. In either case, each partition is a list sorted from greatest to least.We say a list [a_1, ..., a_m] is a partition of a nonnegative integer n provided (1) each a_i is a nonzero integer and (2) a_1 + ... + a_m = n. Thus 0 has no partitions.

(%i1) integer_partitions (3); (%o1) {[1, 1, 1], [2, 1], [3]} (%i2) s: integer_partitions (25)$ (%i3) cardinality (s); (%o3) 1958 (%i4) map (lambda ([x], apply ("+", x)), s); (%o4) {25} (%i5) integer_partitions (5, 3); (%o5) {[2, 2, 1], [3, 1, 1], [3, 2, 0], [4, 1, 0], [5, 0, 0]} (%i6) integer_partitions (5, 2); (%o6) {[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.(%i1) s: integer_partitions (10)$ (%i2) xprimep(x) := integerp(x) and (x > 1) and primep(x)$ (%i3) subset (s, lambda ([x], every (xprimep, x))); (%o3) {[2, 2, 2, 2, 2], [3, 3, 2, 2], [5, 3, 2], [5, 5], [7, 3]}

(Notice that

`primep(1)`

is true in Maxima. This disagrees with most definitions of prime.)

__Function:__**intersect***(*`a_1`, ...,`a_n`)-
Return a set containing the elements that are common to the
sets
`a_1`through`a_n`. The function`intersect`

must receive one or more arguments. Signal an error if any of`a_1`through`a_n`isn't a set. See also intersection.

__Function:__**intersection***(*`a_1`, ...,`a_n`)-
Return a set containing the elements that are common to the
sets
`a_1`through`a_n`. The function`intersection`

must receive one or more arguments. Signal an error if any of`a_1`through`a_n`isn't a set. See also intersect.

__Function:__**kron_delta***(*`x`,`y`)-
The Kronecker delta function;
`kron_delta (`

simplifies to 1 when`x`,`y`)`is(x = y)`

is true and it simplifies to zero when`sign (|`

is`x`-`y`|)`pos`

. When`sign (|`

is zero and`x`-`y`|)

isn't a floating point number (neither a double nor a bfloat), return 0. Otherwise, return a noun form.`x`-`y`The function,

`kron_delta`

is declared to be symmetric; thus, for example,`kron_delta(x, y) - kron_delta(y, x)`

simplifies to zero.Here are a few examples.

(%i1) [kron_delta (a, a), kron_delta (a + 1, a)]; (%o1) [1, 0] (%i2) kron_delta (a, b); (%o2) kron_delta(a, b)

Assuming that

`a > b`

makes`sign (|a - b|)`

evaluate to`pos`

; thus(%i1) assume (a > b)$ (%i2) kron_delta (a, b); (%o2) 0

If we instead assume that

`x >= y`

, then`sign (|x - y|)`

evaluates to`pz`

; in this case,`kron_delta (x, y)`

doesn't simplify(%i1) assume(x >= y)$ (%i2) kron_delta (x, y); (%o2) kron_delta(x, y)

Finally, since

`1/10 - 0.1`

evaluates to a floating point number, we have(%i1) kron_delta (1/10, 0.1); 1 (%o1) kron_delta(--, 0.1) 10

If you want

`kron_delta (1/10, 0.1)`

to evaluate to 1, apply`float`

.(%i1) float (kron_delta (1/10, 0.1)); (%o1) 1

__Function:__**listify***(*`a`)-
If
`a`is a set, return a list containing the members of`a`; when`a`isn't a set, return`a`. To convert a set and all of its members to lists, see full_listify.

__Function:__**lreduce***(*`f`,`s`)__Function:__**lreduce***(*`f`,`s`,`init`)-
The function
`lreduce`

(left reduce) extends a 2-arity function to an n-arity function by composition; an example should make this clear. When the optional argument`init`isn't defined, we have(%i1) lreduce (f, [1, 2, 3]); (%o1) f(f(1, 2), 3) (%i2) lreduce (f, [1, 2, 3, 4]); (%o2) f(f(f(1, 2), 3), 4)

Notice that the function

`f`is first applied to the`leftmost`

list elements (thus the name lreduce). When`init`is defined, the second argument to the inner most function evaluation is`init`; for example:(%i1) lreduce (f, [1, 2, 3], 4); (%o1) f(f(f(4, 1), 2), 3)

The function

`lreduce`

makes it easy to find the product or sum of the elements of a list.(%i1) lreduce ("+", args (set (a, b))); (%o1) b + a (%i2) lreduce ("*", args (set (1, 2, 3, 4, 5))); (%o2) 120

See also See rreduce, See xreduce, and See tree_reduce.

__Function:__**makeset***(*`e`,`v`,`s`)-
This function is similar to
`makelist`

, but`makeset`

allows multiple substitutions. The first argument`e`is an expression; the second argument`v`is a list of variables; and`s`is a list or set of values for the variables`v`. Each member of`s`must have the same length as`v`. We have`makeset (`

is the set`e`,`v`,`s`)`{z | z = substitute(v -> s_i) and s_i in s}`

.(%i1) makeset (i/j, [i, j], [[a, b], [c, d]]); a c (%o1) {-, -} b d (%i2) ind: set (0, 1, 2, 3)$ (%i3) makeset (i^2 + j^2 + k^2, [i, j, k], cartesian_product (ind, ind, ind)); (%o3) {0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 17, 18, 19, 22, 27}

__Function:__**moebius***(*`n`)-
The Moebius function; when
`n`is product of k distinct primes,`moebius(`

evaluates to (-1)^k; it evaluates to 1 when`n`)`n`= 1; and it evaluates to 0 for all other positive integers. The Moebius function threads over equalities, lists, matrices, and sets.

__Function:__**multinomial_coeff***(*`a_1`, ...,`a_n`)__Function:__**multinomial_coeff***()*-
Return the multinomial coefficient. When each
`a_k`is a nonnegative integer, the multinomial coefficient gives the number of ways of placing

distinct objects into n boxes with`a_1`+ ... +`a_n``a_k`elements in the k'th box. In general,`multinomial (`

evaluates to`a_1`, ...,`a_n`)`(`

. Given no arguments,`a_1`+ ... +`a_n`)!/(`a_1`! ...`a_n`!)`multinomial()`

evaluates to 1. A user may use`minfactorial`

to simplify the value returned by`multinomial_coeff`

; for example:(%i1) multinomial_coeff (1, 2, x); (x + 3)! (%o1) -------- 2 x! (%i2) minfactorial (%); (x + 1) (x + 2) (x + 3) (%o2) ----------------------- 2 (%i3) multinomial_coeff (-6, 2); (- 4)! (%o3) -------- 2 (- 6)! (%i4) minfactorial (%); (%o4) 10

__Function:__**num_distinct_partitions***(*`n`)__Function:__**num_distinct_partitions***(*`n`,`a`)-
When

`n`is a nonnegative integer, return the number of distinct integer partitions of`n`.If the optional parameter

`a`has the value`list`

, return a list of the number of distinct partitions of 1,2,3, ... , n. If`n`isn't a nonnegative integer, return a noun form.Definition: If

`n`= k_1 + ... + k_m, where k_1 through k_m are distinct positive integers, we call k_1 + ... + k_m a distinct partition of`n`.

__Function:__**num_partitions***(*`n`)__Function:__**num_partitions***(*`n`,`a`)-
When
`n`is a nonnegative integer, return the number of partitions of`n`. If the optional parameter`a`has the value`list`

, return a list of the number of partitions of 1,2,3, ... , n. If`n`isn't a nonnegative integer, return a noun form.(%i1) num_partitions (5) = cardinality (integer_partitions (5)); (%o1) 7 = 7 (%i2) num_partitions (8, list); (%o2) [1, 1, 2, 3, 5, 7, 11, 15, 22] (%i3) num_partitions (n); (%o3) num_partitions(n)

For a nonnegative integer

`n`,`num_partitions (`

is equal to`n`)`cardinality (integer_partitions (`

; however, calling`n`))`num_partitions`

is much faster.

__Function:__**partition_set***(*`a`,`f`)-
Return a list of two sets; the first set is the subset of
`a`for which the predicate`f`evaluates to false and the second is the subset of`a`for which`f`evaluates to true. If`a`isn't a set, signal an error. See also subset.(%i1) partition_set (set (2, 7, 1, 8, 2, 8), evenp); (%o1) [{1, 7}, {2, 8}] (%i2) partition_set (set (x, rat(y), rat(y) + z, 1), lambda ([x], ratp(x))); (%o2)/R/ [{1, x}, {y, y + z}]

__Function:__**permutations***(*`a`)-
Return 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*not*deleted before finding the permutations. Thus(%i1) permutations ([a, a]); (%o1) {[a, a]} (%i2) permutations ([a, a, b]); (%o2) {[a, a, b], [a, b, a], [b, a, a]}

If

`a`isn't a list or set, signal an error.

__Function:__**powerset***(*`a`)__Function:__**powerset***(*`a`,`n`)-
When the optional second argument
`n`isn't defined, return the set of all subsets of the set`a`.`powerset(`

has`a`)`2^cardinality(`

members. Given a second argument,`a`)`powerset(`

returns the set of all subsets of`a`,`n`)`a`that have cardinality`n`. Signal an error if`a`isn't a set; additionally signal an error if`n`isn't a positive integer.

__Function:__**rreduce***(*`f`,`s`)__Function:__**rreduce***(*`f`,`s`,`init`)-
The function
`rreduce`

(right reduce) extends a 2-arity function to an n-arity function by composition; an example should make this clear. When the optional argument`init`isn't defined, we have(%i1) rreduce (f, [1, 2, 3]); (%o1) f(1, f(2, 3)) (%i2) rreduce (f, [1, 2, 3, 4]); (%o2) f(1, f(2, f(3, 4)))

Notice that the function

`f`is first applied to the rightmost list elements (thus the name rreduce). When`init`is defined, the second argument to the inner most function evaluation is`init`; for example:(%i1) rreduce (f, [1, 2, 3], 4); (%o1) f(1, f(2, f(3, 4)))

The function

`rreduce`

makes it easy to find the product or sum of the elements of a list.(%i1) rreduce ("+", args (set (a, b))); (%o1) b + a (%i2) rreduce ("*", args (set (1, 2, 3, 4, 5))); (%o2) 120

See also See lreduce, See tree_reduce, and See xreduce.

__Function:__**setdifference***(*`a`,`b`)-
Return a set containing the elements in the set
`a`that are not in the set`b`. Signal an error if`a`or`b`is not a set.

__Function:__**setify***(*`a`)-
Construct 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`

. Signal an error if`a`

isn't a list.

__Function:__**setp***(*`a`)-
Return true if and only if
`a`is a Maxima set. The function`setp`

checks that the operator of its argument is set; it doesn't check that its argument is a*simplified*set. Thus(%i1) setp (set (a, a)), simp: false; (%o1) true

The function

`setp`

could be coded in Maxima as`setp(a) := is (inpart (a, 0) = set)`

.

__Function:__**set_partitions***(*`a`)__Function:__**set_partitions***(*`a`,`n`)-
When the optional argument
`n`is defined, return a set of all decompositions of`a`into`n``nonempty`disjoint subsets. When`n`isn't defined, return the set of all partitions.We say a set P is a partition of a set S provided

- each member of P is a nonempty set,
- distinct members of P are disjoint,
- 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); thus

(%i1) set_partitions (set ()); (%o1) {{}}

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

`stirling2`

; thus(%i1) s: set (0, 1, 2, 3, 4, 5)$ (%i2) p: set_partitions (s, 3)$ (%o3) 90 = 90 (%i4) cardinality(p) = stirling2 (6, 3);

Each member of

`p`

should have 3 members; let's check.(%i1) s: set (0, 1, 2, 3, 4, 5)$ (%i2) p: set_partitions (s, 3)$ (%o3) {3} (%i4) map (cardinality, p);

Finally, for each member of

`p`

, the union of its members should equal`s`

; again let's check.(%i1) s: set (0, 1, 2, 3, 4, 5)$ (%i2) p: set_partitions (s, 3)$ (%o3) {{0, 1, 2, 3, 4, 5}} (%i4) map (lambda ([x], apply (union, listify (x))), p);

__Function:__**some***(*`f`,`a`)__Function:__**some***(*`f`,`L_1`, ...,`L_n`)-
The first argument

`f`should be a predicate (a function that evaluates to true, false, or unknown).Given one set as the second argument,

`some (`

returns`f`,`a`)`true`

if

returns`f`(`a_i`)`true`

for at least one`a_i`in`a`. Since sets are unordered,`some`

is free to evaluate

in any order.`f`(`a_i`)`some`

may or may not evaluate`f`for all`a_i`in`a`. Because the order of evaluation isn't specified, the predicate`f`should not have side-effects or signal errors for any input. To use`some`

on multiple set arguments, they should first be converted to an ordered sequence so that their relative alignment becomes well-defined.Given one or more lists as arguments,

`some (`

returns`f`,`L_1`, ...,`L_n`)`true`

if

returns`f`(`x_1`, ...,`x_n`)`true`

for at least one`x_1`, ...,`x_n`in`L_1`, ...,`L_n`, respectively.`some`

may or may not evaluate`f`for every combination`x_1`, ...,`x_n`. Since lists are ordered,`some`

evaluates in the order of increasing index.If the global flag

`maperror`

is true (the default), all lists`L_1`, ...,`L_n`must have equal lengths -- otherwise,`some`

signals an error. When`maperror`

is false, the list arguments are effectively truncated each to the length of the shortest list.The Maxima function

`is`

is automatically applied after evaluating the predicate`f`.(%i1) some ("<", [a, b, 5], [1, 2, 8]); (%o1) true (%i2) some ("=", [2, 3], [2, 7]); (%o2) true

__Function:__**stirling1***(*`n`,`m`)-
The Stirling number of the first kind. When
`n`and`m`are nonnegative integers, the magnitude of`stirling1 (`

is the number of permutations of a set with`n`,`m`)`n`members that have`m`cycles. For details, see Graham, Knuth and Patashnik*Concrete Mathematics*. We use a recursion relation to define`stirling1 (`

for`n`,`m`)`m`less than 0; we do not extend it for`n`less than 0 or for non-integer arguments.The function

`stirling1`

works by simplification; it knows the basic special values (see Donald Knuth,*The Art of Computer Programming,*third edition, Volume 1, Section 1.2.6, Equations 48, 49, and 50). For Maxima to apply these rules, the arguments must be declared to be integer and the first argument must nonnegative. For example:(%i1) declare (n, integer)$ (%i2) assume (n >= 0)$ (%i3) stirling1 (n, n); (%o3) 1

`stirling1`

does not simplify for non-integer arguments.(%i1) stirling1 (sqrt(2), sqrt(2)); (%o1) stirling1(sqrt(2), sqrt(2))

Maxima knows a few other special values; for example:

(%i1) declare (n, integer)$ (%i2) assume (n >= 0)$ (%i3) stirling1 (n + 1, n); n (n + 1) (%o3) --------- 2 (%i4) stirling1 (n + 1, 1); (%o4) n!

__Function:__**stirling2***(*`n`,`m`)-
The Stirling number of the second kind. When
`n`and`m`are nonnegative integers,`stirling2 (`

is the number of ways a set with cardinality`n`,`m`)`n`can be partitioned into`m`disjoint subsets. We use a recursion relation to define`stirling2 (`

for`n`,`m`)`m`less than 0; we do not extend it for`n`less than 0 or for non-integer arguments.The function

`stirling2`

works by simplification; it knows the basic special values (see Donald Knuth,*The Art of Computer Programming,*third edition, Volume 1, Section 1.2.6, Equations 48, 49, and 50). For Maxima to apply these rules, the arguments must be declared to be integer and the first argument must nonnegative. For example:(%i1) declare (n, integer)$ (%i2) assume (n >= 0)$ (%i3) stirling2 (n, n); (%o3) 1

`stirling2`

does not simplify for non-integer arguments.(%i1) stirling2 (%pi, %pi); (%o1) stirling2(%pi, %pi)

Maxima knows a few other special values.

(%i1) declare (n, integer)$ (%i2) assume (n >= 0)$ (%i3) stirling2 (n + 9, n + 8); (n + 8) (n + 9) (%o3) --------------- 2 (%i4) stirling2 (n + 1, 2); n (%o4) 2 - 1

__Function:__**subset***(*`a`,`f`)-
Return the subset of the set
`a`that satisfies the predicate`f`. For example:(%i1) subset (set (1, 2, x, x + y, z, x + y + z), atom); (%o1) {1, 2, x, z} (%i2) subset (set (1, 2, 7, 8, 9, 14), evenp); (%o2) {2, 8, 14}

The second argument to

`subset`

must be a predicate (a boolean-valued function of one argument) if the first argument to`subset`

isn't a set, signal an error. See also partition_set.

__Function:__**subsetp***(*`a`,`b`)-
Return true if and only if the set
`a`is a subset of`b`. Signal an error if`a`or`b`is not a set.

__Function:__**symmdifference***(*`a_1`, ...,`a_n`)-
Return the set of members that occur in exactly one
set
`a_k`. Signal an error if any argument`a_k`isn't a set. Given two arguments,`symmdifference (`

is the same as`a`,`b`)`union (setdifference (`

.`a`,`b`), setdifference (`b`,`a`))

__Function:__**tree_reduce***(*`f`,`s`)__Function:__**tree_reduce***(*`f`,`s`,`init`)-
The function

`tree_reduce`

extends a associative binary operator f : S x S -> S from two arguments to any number of arguments using a minimum depth tree. An example should make this clear.(%i1) tree_reduce (f, [a, b, c, d]); (%o1) f(f(a, b), f(c, d))

Given an odd number of arguments,

`tree_reduce`

favors the left side of the tree; for example:(%i1) tree_reduce (f, [a, b, c, d, e]); (%o1) f(f(f(a, b), f(c, d)), e)

For addition of floating point numbers, using

`tree_reduce`

may give a sum that has a smaller rounding error than using either`rreduce`

or`lreduce`

.

__Function:__**union***(*`a_1`, ...,`a_n`)-
Return the union of the sets
`a_1`through`a_n`. When`union`

receives no arguments, it returns the empty set. Signal an error when one or more arguments to`union`

is not a set.

__Function:__**xreduce***(*`f`,`s`)__Function:__**xreduce***(*`f`,`s`,`init`)-
This function is similar to both

`lreduce`

and`rreduce`

except that`xreduce`

is free to use either left or right associativity; in particular when`f`is an associative function and Maxima has a built-in evaluator for it,`xreduce`

may use the n-ary function; these n-ary functions include addition`+`

, multiplication`*`

,`and`

,`or`

,`max`

,`min`

, and`append`

. For these operators, we generally expect using`xreduce`

to be faster than using either`rreduce`

or`lreduce`

. When`f`isn't n-ary,`xreduce`

uses left-associativity.Floating point addition is not associative; nevertheless,

`xreduce`

uses Maxima's n-ary addition when the set or list`s`contains floating point numbers.

Go to the first, previous, next, last section, table of contents.