2 * Introduction to Sets::
3 * Functions and Variables for Sets::
6 @node Introduction to Sets, Functions and Variables for Sets, Sets, Sets
7 @section Introduction to Sets
9 Maxima provides set functions, such as intersection and
10 union, for finite sets that are defined by explicit enumeration.
12 lists and sets as distinct objects. This feature makes it possible to
13 work with sets that have members that are either lists or sets.
15 In addition to functions for finite sets, Maxima provides some
16 functions related to combinatorics; these include the Stirling
17 numbers of the first and second kind, the Bell numbers, multinomial
18 coefficients, partitions of nonnegative integers, and a few others.
19 Maxima also defines a Kronecker delta function.
23 To construct a set with members @code{a_1, ..., a_n}, write
24 @code{set(a_1, ..., a_n)} or @code{@{a_1, ..., a_n@}};
25 to construct the empty set, write @code{set()} or @code{@{@}}.
26 In input, @code{set(...)} and @code{@{ ... @}} are equivalent.
27 Sets are always displayed with curly braces.
29 If a member is listed more than
30 once, simplification eliminates the redundant member.
61 Two would-be elements @var{x} and @var{y} are redundant
62 (i.e., considered the same for the purpose of set construction)
63 if and only if @code{is(@var{x} = @var{y})} yields @code{true}.
64 @c THAT IS BECAUSE THE SET SIMPLIFICATION CODE CALLS THE LISP FUNCTION LIKE,
65 @c AND SO DOES THE CODE TO EVALUATE IS (X = Y).
66 Note that @code{is(equal(@var{x}, @var{y}))} can yield @code{true}
67 while @code{is(@var{x} = @var{y})} yields @code{false};
68 in that case the elements @var{x} and @var{y} are considered distinct.
98 (%i6) is (equal (y, z));
102 (%o7) - ----- + - + -
108 (%o9) @{-----, - + -@}
112 To construct a set from the elements of a list, use @code{setify}.
118 (%i1) setify ([b, a]);
122 Set members @code{x} and @code{y} are equal provided @code{is(x = y)}
123 evaluates to @code{true}. Thus @code{rat(x)} and @code{x} are equal as set
124 members; consequently,
134 Further, since @code{is((x - 1)*(x + 1) = x^2 - 1)} evaluates to @code{false},
135 @code{(x - 1)*(x + 1)} and @code{x^2 - 1} are distinct set members; thus
138 @c {(x - 1)*(x + 1), x^2 - 1};
141 (%i1) @{(x - 1)*(x + 1), x^2 - 1@};
143 (%o1) @{(x - 1) (x + 1), x - 1@}
146 To reduce this set to a singleton set, apply @code{rat} to each set member:
149 @c {(x - 1)*(x + 1), x^2 - 1};
153 (%i1) @{(x - 1)*(x + 1), x^2 - 1@};
155 (%o1) @{(x - 1) (x + 1), x - 1@}
161 To remove redundancies from other sets, you may need to use other
162 simplification functions. Here is an example that uses @code{trigsimp}:
165 @c {1, cos(x)^2 + sin(x)^2};
166 @c map (trigsimp, %);
169 (%i1) @{1, cos(x)^2 + sin(x)^2@};
171 (%o1) @{1, sin (x) + cos (x)@}
172 (%i2) map (trigsimp, %);
176 A set is simplified when its members are non-redundant and
177 sorted. The current version of the set functions uses the Maxima function
178 @code{orderlessp} to order sets; however, @i{future versions of
179 the set functions might use a different ordering function}.
181 Some operations on sets, such as substitution, automatically force a
182 re-simplification; for example,
187 @c subst ([a=x, b=x, c=x], s);
188 @c map (lambda ([x], x^2), set (-1, 0, 1));
191 (%i1) s: @{a, b, c@}$
192 (%i2) subst (c=a, s);
194 (%i3) subst ([a=x, b=x, c=x], s);
196 (%i4) map (lambda ([x], x^2), set (-1, 0, 1));
200 Maxima treats lists and sets as distinct objects;
201 functions such as @code{union} and @code{intersection} complain
202 if any argument is not a set. If you need to apply a set
203 function to a list, use the @code{setify} function to convert it
207 @c union ([1, 2], {a, b});
208 @c union (setify ([1, 2]), {a, b});
211 (%i1) union ([1, 2], @{a, b@});
212 Function union expects a set, instead found [1,2]
213 -- an error. Quitting. To debug this try debugmode(true);
214 (%i2) union (setify ([1, 2]), @{a, b@});
218 To extract all set elements of a set @code{s} that satisfy a predicate
219 @code{f}, use @code{subset(s, f)}. (A @i{predicate} is a
220 boolean-valued function.) For example, to find the equations
221 in a given set that do not depend on a variable @code{z}, use
224 @c subset ({x + y + z, x - y + 4, x + y - 5},
225 @c lambda ([e], freeof (z, e)));
228 (%i1) subset (@{x + y + z, x - y + 4, x + y - 5@},
229 lambda ([e], freeof (z, e)));
230 (%o1) @{- y + x + 4, y + x - 5@}
233 The section @ref{Functions and Variables for Sets} has a complete list of
234 the set functions in Maxima.
236 @opencatbox{Categories:}
240 @subsection Set Member Iteration
242 There two ways to to iterate over set members. One way is the use
243 @code{map}; for example:
246 @c map (f, {a, b, c});
249 (%i1) map (f, @{a, b, c@});
250 (%o1) @{f(a), f(b), f(c)@}
253 The other way is to use @code{for @var{x} in @var{s} do}
257 @c for si in s do print (concat (si, 1));
260 (%i1) s: @{a, b, c@};
262 (%i2) for si in s do print (concat (si, 1));
269 The Maxima functions @code{first} and @code{rest} work
270 correctly on sets. Applied to a set, @code{first} returns the first
271 displayed element of a set; which element that is may be
272 implementation-dependent. If @code{s} is a set, then
273 @code{rest(s)} is equivalent to @code{disjoin(first(s), s)}.
274 Currently, there are other Maxima functions that work correctly
276 In future versions of the set functions,
277 @code{first} and @code{rest} may function differently or not at all.
279 @c WHAT EXACTLY IS THE EFFECT OF ordergreat AND orderless ON THE SET FUNCTIONS ??
280 Maxima's @code{orderless} and @code{ordergreat} mechanisms are
281 incompatible with the set functions. If you need to use either @code{orderless}
282 or @code{ordergreat}, call those functions before constructing any sets,
283 and do not call @code{unorder}.
287 Stavros Macrakis of Cambridge, Massachusetts and Barton Willis of the
288 University of Nebraska at Kearney (UNK) wrote the Maxima set functions and their
291 @node Functions and Variables for Sets, , Introduction to Sets, Sets
292 @section Functions and Variables for Sets
295 @deffn {Function} adjoin (@var{x}, @var{a})
297 Returns the union of the set @var{a} with @code{@{@var{x}@}}.
299 @code{adjoin} complains if @var{a} is not a literal set.
301 @code{adjoin(@var{x}, @var{a})} and @code{union(set(@var{x}), @var{a})}
303 however, @code{adjoin} may be somewhat faster than @code{union}.
305 See also @mref{disjoin}.
310 @c adjoin (c, {a, b});
311 @c adjoin (a, {a, b});
314 (%i1) adjoin (c, @{a, b@});
316 (%i2) adjoin (a, @{a, b@});
320 @opencatbox{Categories:}
327 @deffn {Function} belln (@var{n})
329 Represents the @math{n}-th Bell number.
330 @code{belln(n)} is the number of partitions of a set with @var{n} members.
332 For nonnegative integers @var{n},
333 @code{belln(@var{n})} simplifies to the @math{n}-th Bell number.
334 @code{belln} does not simplify for any other arguments.
336 @code{belln} distributes over equations, lists, matrices, and sets.
340 @code{belln} applied to nonnegative integers.
343 @c makelist (belln (i), i, 0, 6);
344 @c is (cardinality (set_partitions ({})) = belln (0));
345 @c is (cardinality (set_partitions ({1, 2, 3, 4, 5, 6})) =
349 (%i1) makelist (belln (i), i, 0, 6);
350 (%o1) [1, 1, 2, 5, 15, 52, 203]
351 (%i2) is (cardinality (set_partitions (@{@})) = belln (0));
353 (%i3) is (cardinality (set_partitions (@{1, 2, 3, 4, 5, 6@})) =
358 @code{belln} applied to arguments which are not nonnegative integers.
361 @c [belln (x), belln (sqrt(3)), belln (-9)];
364 (%i1) [belln (x), belln (sqrt(3)), belln (-9)];
365 (%o1) [belln(x), belln(sqrt(3)), belln(- 9)]
368 @opencatbox{Categories:}
375 @deffn {Function} cardinality (@var{a})
377 Returns the number of distinct elements of the set @var{a}.
379 @code{cardinality} ignores redundant elements
380 even when simplification is disabled.
386 @c cardinality ({a, a, b, c});
388 @c cardinality ({a, a, b, c});
391 (%i1) cardinality (@{@});
393 (%i2) cardinality (@{a, a, b, c@});
397 (%i4) cardinality (@{a, a, b, c@});
401 @opencatbox{Categories:}
407 @anchor{cartesian_product}
408 @deffn {Function} cartesian_product (@var{b_1}, ... , @var{b_n})
409 Returns a set of lists of the form @code{[@var{x_1}, ..., @var{x_n}]}, where
410 @var{x_1}, ..., @var{x_n} are elements of the sets @var{b_1}, ... , @var{b_n},
413 @code{cartesian_product} complains if any argument is not a literal set.
415 See also @mrefdot{cartesian_product_list}
420 @c cartesian_product ({0, 1});
421 @c cartesian_product ({0, 1}, {0, 1});
422 @c cartesian_product ({x}, {y}, {z});
423 @c cartesian_product ({x}, {-1, 0, 1});
426 (%i1) cartesian_product (@{0, 1@});
428 (%i2) cartesian_product (@{0, 1@}, @{0, 1@});
429 (%o2) @{[0, 0], [0, 1], [1, 0], [1, 1]@}
430 (%i3) cartesian_product (@{x@}, @{y@}, @{z@});
432 (%i4) cartesian_product (@{x@}, @{-1, 0, 1@});
433 (%o4) @{[x, - 1], [x, 0], [x, 1]@}
436 @opencatbox{Categories:}
443 @anchor{cartesian_product_list}
444 @deffn {Function} cartesian_product_list (@var{b_1}, ... , @var{b_n})
445 Returns a list of lists of the form @code{[@var{x_1}, ..., @var{x_n}]}, where
446 @var{x_1}, ..., @var{x_n} are elements of the lists @var{b_1}, ... , @var{b_n}, respectively,
447 comprising all possible combinations of the elements of @var{b_1}, ... , @var{b_n}.
449 The list returned by @code{cartesian_product_list} is equivalent to the
450 following recursive definition.
451 Let @var{L} be the list returned by @code{cartesian_product_list(@var{b_2}, ..., @var{b_n})}.
452 Then @code{cartesian_product_list(@var{b_1}, @var{b_2}, ..., @var{b_n})}
453 (i.e., @var{b_1} in addition to @var{b_2}, ..., @var{b_n})
454 returns a list comprising each element of @var{L} appended to the first element of @var{b_1},
455 each element of @var{L} appended to the second element of @var{b_1},
456 each element of @var{L} appended to the third element of @var{b_1}, etc.
457 The order of the list returned by @code{cartesian_product_list(@var{b_1}, @var{b_2}, ..., @var{b_n})}
458 may therefore be summarized by saying the lesser indices (1, 2, 3, ...) vary more slowly than the greater indices.
460 The list returned by @code{cartesian_product_list} contains duplicate elements
461 if any argument @var{b_1}, ..., @var{b_n} contains duplicates.
462 In this respect, @code{cartesian_product_list} differs from @code{cartesian_product},
463 which returns no duplicates.
464 Also, the ordering of the list returned @code{cartesian_product_list}
465 is determined by the order of the elements of @var{b_1}, ..., @var{b_n}.
466 Again, this differs from @code{cartesian_product},
467 which returns a set (with order determined by @code{orderlessp}).
469 The length of the list returned by @code{cartesian_product_list}
470 is equal to the product of the lengths of the arguments @var{b_1}, ..., @var{b_n}.
472 See also @mrefdot{cartesian_product}
474 @code{cartesian_product_list} complains if any argument is not a list.
478 @code{cartesian_product_list} returns a list of lists comprising all possible combinations.
481 @c cartesian_product_list ([0, 1]);
482 @c cartesian_product_list ([0, 1], [0, 1]);
483 @c cartesian_product_list ([x], [y], [z]);
484 @c cartesian_product_list ([x], [-1, 0, 1]);
485 @c cartesian_product_list ([a, h, e], [c, b, 4]);
488 (%i1) cartesian_product_list ([0, 1]);
490 (%i2) cartesian_product_list ([0, 1], [0, 1]);
491 (%o2) [[0, 0], [0, 1], [1, 0], [1, 1]]
492 (%i3) cartesian_product_list ([x], [y], [z]);
494 (%i4) cartesian_product_list ([x], [-1, 0, 1]);
495 (%o4) [[x, - 1], [x, 0], [x, 1]]
496 (%i5) cartesian_product_list ([a, h, e], [c, b, 4]);
497 (%o5) [[a, c], [a, b], [a, 4], [h, c], [h, b], [h, 4], [e, c],
501 The order of the list returned by @code{cartesian_product_list}
502 may be summarized by saying the lesser indices vary more slowly than the greater indices.
505 @c cartesian_product_list ([1, 2, 3], [a, b], [i, ii]);
509 (%i1) cartesian_product_list ([1, 2, 3], [a, b], [i, ii]);
510 (%o1) [[1, a, i], [1, a, ii], [1, b, i], [1, b, ii], [2, a, i],
511 [2, a, ii], [2, b, i], [2, b, ii], [3, a, i], [3, a, ii],
512 [3, b, i], [3, b, ii]]
515 The list returned by @code{cartesian_product_list} contains duplicate elements
516 if any argument contains duplicates.
519 @c cartesian_product_list ([e, h], [3, 7, 3]);
523 (%i1) cartesian_product_list ([e, h], [3, 7, 3]);
524 (%o1) [[e, 3], [e, 7], [e, 3], [h, 3], [h, 7], [h, 3]]
527 The length of the list returned by @code{cartesian_product_list}
528 is equal to the product of the lengths of the arguments.
531 @c foo: cartesian_product_list ([1, 1, 2, 2, 3], [h, z, h]);
532 @c is (length (foo) = 5*3);
536 (%i1) foo: cartesian_product_list ([1, 1, 2, 2, 3], [h, z, h]);
537 (%o1) [[1, h], [1, z], [1, h], [1, h], [1, z], [1, h], [2, h],
538 [2, z], [2, h], [2, h], [2, z], [2, h], [3, h], [3, z], [3, h]]
539 (%i2) is (length (foo) = 5*3);
543 @opencatbox{Categories:}
551 @deffn {Function} disjoin (@var{x}, @var{a})
552 Returns the set @var{a} without the member @var{x}.
553 If @var{x} is not a member of @var{a}, return @var{a} unchanged.
555 @code{disjoin} complains if @var{a} is not a literal set.
557 @code{disjoin(@var{x}, @var{a})}, @code{delete(@var{x}, @var{a})}, and
558 @code{setdifference(@var{a}, set(@var{x}))} are all equivalent.
559 Of these, @code{disjoin} is generally faster than the others.
564 @c disjoin (a, {a, b, c, d});
565 @c disjoin (a + b, {5, z, a + b, %pi});
566 @c disjoin (a - b, {5, z, a + b, %pi});
569 (%i1) disjoin (a, @{a, b, c, d@});
571 (%i2) disjoin (a + b, @{5, z, a + b, %pi@});
573 (%i3) disjoin (a - b, @{5, z, a + b, %pi@});
574 (%o3) @{5, %pi, b + a, z@}
577 @opencatbox{Categories:}
584 @deffn {Function} disjointp (@var{a}, @var{b})
585 Returns @code{true} if and only if the sets @var{a} and @var{b} are disjoint.
587 @code{disjointp} complains if either @var{a} or @var{b} is not a literal set.
592 @c disjointp ({a, b, c}, {1, 2, 3});
593 @c disjointp ({a, b, 3}, {1, 2, 3});
596 (%i1) disjointp (@{a, b, c@}, @{1, 2, 3@});
598 (%i2) disjointp (@{a, b, 3@}, @{1, 2, 3@});
602 @opencatbox{Categories:}
604 @category{Predicate functions}
610 @deffn {Function} divisors (@var{n})
612 Represents the set of divisors of @var{n}.
614 @code{divisors(@var{n})} simplifies to a set of integers
615 when @var{n} is a nonzero integer.
616 The set of divisors includes the members 1 and @var{n}.
617 The divisors of a negative integer are the divisors of its absolute value.
619 @code{divisors} distributes over equations, lists, matrices, and sets.
623 We can verify that 28 is a perfect number:
624 the sum of its divisors (except for itself) is 28.
628 @c lreduce ("+", args(s)) - 28;
631 (%i1) s: divisors(28);
632 (%o1) @{1, 2, 4, 7, 14, 28@}
633 (%i2) lreduce ("+", args(s)) - 28;
637 @code{divisors} is a simplifying function.
638 Substituting 8 for @code{a} in @code{divisors(a)}
639 yields the divisors without reevaluating @code{divisors(8)}.
648 (%i2) subst (8, a, %);
652 @code{divisors} distributes over equations, lists, matrices, and sets.
656 @c divisors ([a, b, c]);
657 @c divisors (matrix ([a, b], [c, d]));
658 @c divisors ({a, b, c});
661 (%i1) divisors (a = b);
662 (%o1) divisors(a) = divisors(b)
663 (%i2) divisors ([a, b, c]);
664 (%o2) [divisors(a), divisors(b), divisors(c)]
665 (%i3) divisors (matrix ([a, b], [c, d]));
666 [ divisors(a) divisors(b) ]
668 [ divisors(c) divisors(d) ]
669 (%i4) divisors (@{a, b, c@});
670 (%o4) @{divisors(a), divisors(b), divisors(c)@}
673 @opencatbox{Categories:}
680 @deffn {Function} elementp (@var{x}, @var{a})
681 Returns @code{true} if and only if @var{x} is a member of the
684 @code{elementp} complains if @var{a} is not a literal set.
689 @c elementp (sin(1), {sin(1), sin(2), sin(3)});
690 @c elementp (sin(1), {cos(1), cos(2), cos(3)});
693 (%i1) elementp (sin(1), @{sin(1), sin(2), sin(3)@});
695 (%i2) elementp (sin(1), @{cos(1), cos(2), cos(3)@});
699 @opencatbox{Categories:}
701 @category{Predicate functions}
707 @deffn {Function} emptyp (@var{a})
708 Return @code{true} if and only if @var{a} is the empty set or
714 @c map (emptyp, [{}, []]);
715 @c map (emptyp, [a + b, {{}}, %pi]);
718 (%i1) map (emptyp, [@{@}, []]);
720 (%i2) map (emptyp, [a + b, @{@{@}@}, %pi]);
721 (%o2) [false, false, false]
724 @opencatbox{Categories:}
726 @category{Predicate functions}
731 @anchor{equiv_classes}
732 @deffn {Function} equiv_classes (@var{s}, @var{F})
733 Returns a set of the equivalence classes of the set @var{s} with respect
734 to the equivalence relation @var{F}.
736 @var{F} is a function of two variables defined on the Cartesian product of @var{s} with @var{s}.
737 The return value of @var{F} is either @code{true} or @code{false},
738 or an expression @var{expr} such that @code{is(@var{expr})} is either @code{true} or @code{false}.
740 When @var{F} is not an equivalence relation,
741 @code{equiv_classes} accepts it without complaint,
742 but the result is generally incorrect in that case.
744 @c EXCESSIVE DETAIL HERE. PROBABLY JUST CUT THIS
745 @c @var{F} may be a relational operator (built-in or user-defined),
746 @c an ordinary Maxima function, a Lisp function, a lambda expression,
747 @c a macro, or a subscripted function.
751 The equivalence relation is a lambda expression which returns @code{true} or @code{false}.
754 @c equiv_classes ({1, 1.0, 2, 2.0, 3, 3.0},
755 @c lambda ([x, y], is (equal (x, y))));
758 (%i1) equiv_classes (@{1, 1.0, 2, 2.0, 3, 3.0@},
759 lambda ([x, y], is (equal (x, y))));
760 (%o1) @{@{1, 1.0@}, @{2, 2.0@}, @{3, 3.0@}@}
763 The equivalence relation is the name of a relational function
764 which @code{is} evaluates to @code{true} or @code{false}.
767 @c equiv_classes ({1, 1.0, 2, 2.0, 3, 3.0}, equal);
770 (%i1) equiv_classes (@{1, 1.0, 2, 2.0, 3, 3.0@}, equal);
771 (%o1) @{@{1, 1.0@}, @{2, 2.0@}, @{3, 3.0@}@}
774 The equivalence classes are numbers which differ by a multiple of 3.
777 @c equiv_classes ({1, 2, 3, 4, 5, 6, 7},
778 @c lambda ([x, y], remainder (x - y, 3) = 0));
781 (%i1) equiv_classes (@{1, 2, 3, 4, 5, 6, 7@},
782 lambda ([x, y], remainder (x - y, 3) = 0));
783 (%o1) @{@{1, 4, 7@}, @{2, 5@}, @{3, 6@}@}
786 @opencatbox{Categories:}
793 @deffn {Function} every @
794 @fname{every} (@var{f}, @var{s}) @
795 @fname{every} (@var{f}, @var{L_1}, ..., @var{L_n})
797 Returns @code{true} if the predicate @var{f} is @code{true} for all given arguments.
799 Given one set as the second argument,
800 @code{every(@var{f}, @var{s})} returns @code{true}
801 if @code{is(@var{f}(@var{a_i}))} returns @code{true} for all @var{a_i} in @var{s}.
802 @code{every} may or may not evaluate @var{f} for all @var{a_i} in @var{s}.
803 Since sets are unordered,
804 @code{every} may evaluate @code{@var{f}(@var{a_i})} in any order.
806 Given one or more lists as arguments,
807 @code{every(@var{f}, @var{L_1}, ..., @var{L_n})} returns @code{true}
808 if @code{is(@var{f}(@var{x_1}, ..., @var{x_n}))} returns @code{true}
809 for all @var{x_1}, ..., @var{x_n} in @var{L_1}, ..., @var{L_n}, respectively.
810 @code{every} may or may not evaluate
811 @var{f} for every combination @var{x_1}, ..., @var{x_n}.
812 @code{every} evaluates lists in the order of increasing index.
814 Given an empty set @code{@{@}} or empty lists @code{[]} as arguments,
815 @code{every} returns @code{true}.
817 When the global flag @code{maperror} is @code{true}, all lists
818 @var{L_1}, ..., @var{L_n} must have equal lengths.
819 When @code{maperror} is @code{false}, list arguments are
820 effectively truncated to the length of the shortest list.
822 Return values of the predicate @var{f} which evaluate (via @code{is})
823 to something other than @code{true} or @code{false}
824 are governed by the global flag @code{prederror}.
825 When @code{prederror} is @code{true},
826 such values are treated as @code{false},
827 and the return value from @code{every} is @code{false}.
828 When @code{prederror} is @code{false},
829 such values are treated as @code{unknown},
830 and the return value from @code{every} is @code{unknown}.
834 @code{every} applied to a single set.
835 The predicate is a function of one argument.
838 @c every (integerp, {1, 2, 3, 4, 5, 6});
839 @c every (atom, {1, 2, sin(3), 4, 5 + y, 6});
842 (%i1) every (integerp, @{1, 2, 3, 4, 5, 6@});
844 (%i2) every (atom, @{1, 2, sin(3), 4, 5 + y, 6@});
848 @code{every} applied to two lists.
849 The predicate is a function of two arguments.
852 @c every ("=", [a, b, c], [a, b, c]);
853 @c every ("#", [a, b, c], [a, b, c]);
856 (%i1) every ("=", [a, b, c], [a, b, c]);
858 (%i2) every ("#", [a, b, c], [a, b, c]);
862 Return values of the predicate @var{f} which evaluate
863 to something other than @code{true} or @code{false}
864 are governed by the global flag @code{prederror}.
867 @c prederror : false;
868 @c map (lambda ([a, b], is (a < b)), [x, y, z],
870 @c every ("<", [x, y, z], [x^2, y^2, z^2]);
872 @c every ("<", [x, y, z], [x^2, y^2, z^2]);
875 (%i1) prederror : false;
877 (%i2) map (lambda ([a, b], is (a < b)), [x, y, z],
879 (%o2) [unknown, unknown, unknown]
880 (%i3) every ("<", [x, y, z], [x^2, y^2, z^2]);
882 (%i4) prederror : true;
884 (%i5) every ("<", [x, y, z], [x^2, y^2, z^2]);
888 @opencatbox{Categories:}
894 @anchor{extremal_subset}
895 @deffn {Function} extremal_subset @
896 @fname{extremal_subset} (@var{s}, @var{f}, max) @
897 @fname{extremal_subset} (@var{s}, @var{f}, min)
899 Returns the subset of @var{s} for which the function @var{f} takes on maximum or minimum values.
901 @code{extremal_subset(@var{s}, @var{f}, max)} returns the subset of the set or
902 list @var{s} for which the real-valued function @var{f} takes on its maximum value.
904 @code{extremal_subset(@var{s}, @var{f}, min)} returns the subset of the set or
905 list @var{s} for which the real-valued function @var{f} takes on its minimum value.
910 @c extremal_subset ({-2, -1, 0, 1, 2}, abs, max);
911 @c extremal_subset ({sqrt(2), 1.57, %pi/2}, sin, min);
914 (%i1) extremal_subset (@{-2, -1, 0, 1, 2@}, abs, max);
916 (%i2) extremal_subset (@{sqrt(2), 1.57, %pi/2@}, sin, min);
920 @opencatbox{Categories:}
927 @deffn {Function} flatten (@var{expr})
929 Collects arguments of subexpressions which have the same operator as @var{expr}
930 and constructs an expression from these collected arguments.
932 Subexpressions in which the operator is different from the main operator of @code{expr}
933 are copied without modification,
934 even if they, in turn, contain some subexpressions in which the operator is the same as for @code{expr}.
936 It may be possible for @code{flatten} to construct expressions in which the number
937 of arguments differs from the declared arguments for an operator;
938 this may provoke an error message from the simplifier or evaluator.
939 @code{flatten} does not try to detect such situations.
941 Expressions with special representations, for example, canonical rational expressions (CRE),
942 cannot be flattened; in such cases, @code{flatten} returns its argument unchanged.
946 Applied to a list, @code{flatten} gathers all list elements that are lists.
949 @c flatten ([a, b, [c, [d, e], f], [[g, h]], i, j]);
952 (%i1) flatten ([a, b, [c, [d, e], f], [[g, h]], i, j]);
953 (%o1) [a, b, c, d, e, f, g, h, i, j]
956 Applied to a set, @code{flatten} gathers all members of set elements that are sets.
959 @c flatten ({a, {b}, {{c}}});
960 @c flatten ({a, {[a], {a}}});
963 (%i1) flatten (@{a, @{b@}, @{@{c@}@}@});
965 (%i2) flatten (@{a, @{[a], @{a@}@}@});
969 @code{flatten} is similar to the effect of declaring the main operator n-ary.
970 However, @code{flatten} has no effect on subexpressions which have an operator
971 different from the main operator, while an n-ary declaration affects those.
974 @c expr: flatten (f (g (f (f (x)))));
975 @c declare (f, nary);
979 (%i1) expr: flatten (f (g (f (f (x)))));
981 (%i2) declare (f, nary);
987 @code{flatten} treats subscripted functions the same as any other operator.
990 @c flatten (f[5] (f[5] (x, y), z));
993 (%i1) flatten (f[5] (f[5] (x, y), z));
998 It may be possible for @code{flatten} to construct expressions in which the number
999 of arguments differs from the declared arguments for an operator;
1002 @c 'mod (5, 'mod (7, 4));
1007 (%i1) 'mod (5, 'mod (7, 4));
1008 (%o1) mod(5, mod(7, 4))
1012 Wrong number of arguments to mod
1013 -- an error. Quitting. To debug this try debugmode(true);
1016 @opencatbox{Categories:}
1023 @anchor{full_listify}
1024 @deffn {Function} full_listify (@var{a})
1025 Replaces every set operator in @var{a} by a list operator,
1026 and returns the result.
1027 @code{full_listify} replaces set operators in nested subexpressions,
1028 even if the main operator is not @code{set}.
1030 @code{listify} replaces only the main operator.
1035 @c full_listify ({a, b, {c, {d, e, f}, g}});
1036 @c full_listify (F (G ({a, b, H({c, d, e})})));
1039 (%i1) full_listify (@{a, b, @{c, @{d, e, f@}, g@}@});
1040 (%o1) [a, b, [c, [d, e, f], g]]
1041 (%i2) full_listify (F (G (@{a, b, H(@{c, d, e@})@})));
1042 (%o2) F(G([a, b, H([c, d, e])]))
1045 @opencatbox{Categories:}
1052 @deffn {Function} fullsetify (@var{a})
1053 When @var{a} is a list, replaces the list operator with a set operator,
1054 and applies @code{fullsetify} to each member which is a set.
1055 When @var{a} is not a list, it is returned unchanged.
1057 @code{setify} replaces only the main operator.
1061 In line @code{(%o2)}, the argument of @code{f} isn't converted to a set
1062 because the main operator of @code{f([b])} isn't a list.
1065 @c fullsetify ([a, [a]]);
1066 @c fullsetify ([a, f([b])]);
1069 (%i1) fullsetify ([a, [a]]);
1071 (%i2) fullsetify ([a, f([b])]);
1075 @opencatbox{Categories:}
1082 @deffn {Function} identity (@var{x})
1084 Returns @var{x} for any argument @var{x}.
1088 @code{identity} may be used as a predicate when the arguments
1089 are already Boolean values.
1092 @c every (identity, [true, true]);
1095 (%i1) every (identity, [true, true]);
1100 @anchor{integer_partitions}
1101 @deffn {Function} integer_partitions @
1102 @fname{integer_partitions} (@var{n}) @
1103 @fname{integer_partitions} (@var{n}, @var{len})
1105 Returns integer partitions of @var{n}, that is,
1106 lists of integers which sum to @var{n}.
1108 @code{integer_partitions(@var{n})} returns the set of
1109 all partitions of the integer @var{n}.
1110 Each partition is a list sorted from greatest to least.
1112 @code{integer_partitions(@var{n}, @var{len})}
1113 returns all partitions that have length @var{len} or less; in this
1114 case, zeros are appended to each partition with fewer than @var{len}
1115 terms to make each partition have exactly @var{len} terms.
1116 Each partition is a list sorted from greatest to least.
1118 A list @math{[a_1, ..., a_m]} is a partition of a nonnegative integer
1119 @math{n} when (1) each @math{a_i} is a nonzero integer, and (2)
1120 @math{a_1 + ... + a_m = n.} Thus 0 has no partitions.
1125 @c integer_partitions (3);
1126 @c s: integer_partitions (25)$
1128 @c map (lambda ([x], apply ("+", x)), s);
1129 @c integer_partitions (5, 3);
1130 @c integer_partitions (5, 2);
1133 (%i1) integer_partitions (3);
1134 (%o1) @{[1, 1, 1], [2, 1], [3]@}
1135 (%i2) s: integer_partitions (25)$
1136 (%i3) cardinality (s);
1138 (%i4) map (lambda ([x], apply ("+", x)), s);
1140 (%i5) integer_partitions (5, 3);
1141 (%o5) @{[2, 2, 1], [3, 1, 1], [3, 2, 0], [4, 1, 0], [5, 0, 0]@}
1142 (%i6) integer_partitions (5, 2);
1143 (%o6) @{[3, 2], [4, 1], [5, 0]@}
1146 To find all partitions that satisfy a condition, use the function @code{subset};
1147 here is an example that finds all partitions of 10 that consist of prime numbers.
1150 @c s: integer_partitions (10)$
1152 @c xprimep(x) := integerp(x) and (x > 1) and primep(x)$
1153 @c subset (s, lambda ([x], every (xprimep, x)));
1156 (%i1) s: integer_partitions (10)$
1157 (%i2) cardinality (s);
1159 (%i3) xprimep(x) := integerp(x) and (x > 1) and primep(x)$
1160 (%i4) subset (s, lambda ([x], every (xprimep, x)));
1161 (%o4) @{[2, 2, 2, 2, 2], [3, 3, 2, 2], [5, 3, 2], [5, 5], [7, 3]@}
1164 @opencatbox{Categories:}
1171 @deffn {Function} intersect (@var{a_1}, ..., @var{a_n})
1173 @code{intersect} is the same as @code{intersection}, which see.
1175 @opencatbox{Categories:}
1181 @anchor{intersection}
1182 @deffn {Function} intersection (@var{a_1}, ..., @var{a_n})
1183 Returns a set containing the elements that are common to the
1184 sets @var{a_1} through @var{a_n}.
1186 @code{intersection} complains if any argument is not a literal set.
1191 @c S_1 : {a, b, c, d};
1192 @c S_2 : {d, e, f, g};
1193 @c S_3 : {c, d, e, f};
1195 @c intersection (S_1, S_2);
1196 @c intersection (S_2, S_3);
1197 @c intersection (S_1, S_2, S_3);
1198 @c intersection (S_1, S_2, S_3, S_4);
1201 (%i1) S_1 : @{a, b, c, d@};
1202 (%o1) @{a, b, c, d@}
1203 (%i2) S_2 : @{d, e, f, g@};
1204 (%o2) @{d, e, f, g@}
1205 (%i3) S_3 : @{c, d, e, f@};
1206 (%o3) @{c, d, e, f@}
1207 (%i4) S_4 : @{u, v, w@};
1209 (%i5) intersection (S_1, S_2);
1211 (%i6) intersection (S_2, S_3);
1213 (%i7) intersection (S_1, S_2, S_3);
1215 (%i8) intersection (S_1, S_2, S_3, S_4);
1219 @opencatbox{Categories:}
1225 @deffn {Function} kron_delta (@var{x1}, @var{x2}, @dots{}, @var{xp})
1227 Represents the Kronecker delta function.
1229 @code{kron_delta} simplifies to 1 when @var{xi} and @var{yj} are equal
1230 for all pairs of arguments, and it simplifies to 0 when @var{xi} and
1231 @var{yj} are not equal for some pair of arguments. Equality is
1232 determined using @code{is(equal(xi,xj))} and inequality by
1233 @code{is(notequal(xi,xj))}. For exactly one argument, @code{kron_delta}
1240 @c kron_delta(a,b,a,b);
1241 @c kron_delta(a,a,b,a+1);
1242 @c assume(equal(x,y));
1246 (%i1) kron_delta(a,a);
1248 (%i2) kron_delta(a,b,a,b);
1249 (%o2) kron_delta(a, b)
1250 (%i3) kron_delta(a,a,b,a+1);
1252 (%i4) assume(equal(x,y));
1254 (%i5) kron_delta(x,y);
1262 @deffn {Function} listify (@var{a})
1264 Returns a list containing the members of @var{a} when @var{a} is a set.
1265 Otherwise, @code{listify} returns @var{a}.
1267 @code{full_listify} replaces all set operators in @var{a} by list operators.
1272 @c listify ({a, b, c, d});
1273 @c listify (F ({a, b, c, d}));
1276 (%i1) listify (@{a, b, c, d@});
1278 (%i2) listify (F (@{a, b, c, d@}));
1279 (%o2) F(@{a, b, c, d@})
1282 @opencatbox{Categories:}
1289 @deffn {Function} makeset (@var{expr}, @var{x}, @var{s})
1291 Returns a set with members generated from the expression @var{expr},
1292 where @var{x} is a list of variables in @var{expr},
1293 and @var{s} is a set or list of lists.
1294 To generate each set member,
1295 @var{expr} is evaluated with the variables @var{x} bound in parallel to a member of @var{s}.
1297 Each member of @var{s} must have the same length as @var{x}.
1298 The list of variables @var{x} must be a list of symbols, without subscripts.
1299 Even if there is only one symbol, @var{x} must be a list of one element,
1300 and each member of @var{s} must be a list of one element.
1302 @c FOLLOWING EQUIVALENT EXPRESSION IS REALLY TOO COMPLICATED, JUST SKIP IT FOR NOW
1303 @c @code{makeset(@var{expr}, @var{x}, @var{s})} returns the same result as
1304 @c @code{setify(map(lambda([L], sublis(map("=", ''@var{x}, L), ''@var{expr})), args(@var{s})))}.
1306 See also @mref{makelist}.
1311 @c makeset (i/j, [i, j], [[1, a], [2, b], [3, c], [4, d]]);
1313 @c S3 : cartesian_product (S, S, S);
1314 @c makeset (i + j + k, [i, j, k], S3);
1315 @c makeset (sin(x), [x], {[1], [2], [3]});
1318 (%i1) makeset (i/j, [i, j], [[1, a], [2, b], [3, c], [4, d]]);
1320 (%o1) @{-, -, -, -@}
1322 (%i2) S : @{x, y, z@}$
1323 (%i3) S3 : cartesian_product (S, S, S);
1324 (%o3) @{[x, x, x], [x, x, y], [x, x, z], [x, y, x], [x, y, y],
1325 [x, y, z], [x, z, x], [x, z, y], [x, z, z], [y, x, x],
1326 [y, x, y], [y, x, z], [y, y, x], [y, y, y], [y, y, z],
1327 [y, z, x], [y, z, y], [y, z, z], [z, x, x], [z, x, y],
1328 [z, x, z], [z, y, x], [z, y, y], [z, y, z], [z, z, x],
1329 [z, z, y], [z, z, z]@}
1330 (%i4) makeset (i + j + k, [i, j, k], S3);
1331 (%o4) @{3 x, 3 y, y + 2 x, 2 y + x, 3 z, z + 2 x, z + y + x,
1332 z + 2 y, 2 z + x, 2 z + y@}
1333 (%i5) makeset (sin(x), [x], @{[1], [2], [3]@});
1334 (%o5) @{sin(1), sin(2), sin(3)@}
1337 @opencatbox{Categories:}
1344 @deffn {Function} moebius (@var{n})
1346 Represents the Moebius function.
1348 When @var{n} is product of @math{k} distinct primes,
1349 @code{moebius(@var{n})} simplifies to @math{(-1)^k};
1350 when @math{@var{n} = 1}, it simplifies to 1;
1351 and it simplifies to 0 for all other positive integers.
1353 @code{moebius} distributes over equations, lists, matrices, and sets.
1359 @c moebius (2 * 3 * 5);
1360 @c moebius (11 * 17 * 29 * 31);
1363 @c moebius (n = 12);
1364 @c moebius ([11, 11 * 13, 11 * 13 * 15]);
1365 @c moebius (matrix ([11, 12], [13, 14]));
1366 @c moebius ({21, 22, 23, 24});
1371 (%i2) moebius (2 * 3 * 5);
1373 (%i3) moebius (11 * 17 * 29 * 31);
1375 (%i4) moebius (2^32);
1379 (%i6) moebius (n = 12);
1380 (%o6) moebius(n) = 0
1381 (%i7) moebius ([11, 11 * 13, 11 * 13 * 15]);
1383 (%i8) moebius (matrix ([11, 12], [13, 14]));
1387 (%i9) moebius (@{21, 22, 23, 24@});
1391 @opencatbox{Categories:}
1397 @anchor{multinomial_coeff}
1398 @deffn {Function} multinomial_coeff @
1399 @fname{multinomial_coeff} (@var{a_1}, ..., @var{a_n}) @
1400 @fname{multinomial_coeff} ()
1402 Returns the multinomial coefficient.
1404 When each @var{a_k} is a nonnegative integer, the multinomial coefficient
1405 gives the number of ways of placing @code{@var{a_1} + ... + @var{a_n}}
1406 distinct objects into @math{n} boxes with @var{a_k} elements in the
1407 @math{k}'th box. In general, @code{multinomial_coeff (@var{a_1}, ..., @var{a_n})}
1408 evaluates to @code{(@var{a_1} + ... + @var{a_n})!/(@var{a_1}! ... @var{a_n}!)}.
1410 @code{multinomial_coeff()} (with no arguments) evaluates to 1.
1412 @code{minfactorial} may be able to simplify the value returned by @code{multinomial_coeff}.
1417 @c multinomial_coeff (1, 2, x);
1418 @c minfactorial (%);
1419 @c multinomial_coeff (-6, 2);
1420 @c minfactorial (%);
1423 (%i1) multinomial_coeff (1, 2, x);
1427 (%i2) minfactorial (%);
1428 (x + 1) (x + 2) (x + 3)
1429 (%o2) -----------------------
1431 (%i3) multinomial_coeff (-6, 2);
1435 (%i4) minfactorial (%);
1439 @opencatbox{Categories:}
1445 @anchor{num_distinct_partitions}
1446 @deffn {Function} num_distinct_partitions @
1447 @fname{num_distinct_partitions} (@var{n}) @
1448 @fname{num_distinct_partitions} (@var{n}, list)
1450 Returns the number of distinct integer partitions of @var{n}
1451 when @var{n} is a nonnegative integer.
1452 Otherwise, @code{num_distinct_partitions} returns a noun expression.
1454 @code{num_distinct_partitions(@var{n}, list)} returns a
1455 list of the number of distinct partitions of 1, 2, 3, ..., @var{n}.
1457 A distinct partition of @var{n} is
1458 a list of distinct positive integers @math{k_1}, ..., @math{k_m}
1459 such that @math{@var{n} = k_1 + ... + k_m}.
1464 @c num_distinct_partitions (12);
1465 @c num_distinct_partitions (12, list);
1466 @c num_distinct_partitions (n);
1469 (%i1) num_distinct_partitions (12);
1471 (%i2) num_distinct_partitions (12, list);
1472 (%o2) [1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15]
1473 (%i3) num_distinct_partitions (n);
1474 (%o3) num_distinct_partitions(n)
1477 @opencatbox{Categories:}
1483 @anchor{num_partitions}
1484 @deffn {Function} num_partitions @
1485 @fname{num_partitions} (@var{n}) @
1486 @fname{num_partitions} (@var{n}, list)
1488 Returns the number of integer partitions of @var{n}
1489 when @var{n} is a nonnegative integer.
1490 Otherwise, @code{num_partitions} returns a noun expression.
1492 @code{num_partitions(@var{n}, list)} returns a
1493 list of the number of integer partitions of 1, 2, 3, ..., @var{n}.
1495 For a nonnegative integer @var{n}, @code{num_partitions(@var{n})} is equal to
1496 @code{cardinality(integer_partitions(@var{n}))}; however, @code{num_partitions}
1497 does not actually construct the set of partitions, so it is much faster.
1502 @c num_partitions (5) = cardinality (integer_partitions (5));
1503 @c num_partitions (8, list);
1504 @c num_partitions (n);
1507 (%i1) num_partitions (5) = cardinality (integer_partitions (5));
1509 (%i2) num_partitions (8, list);
1510 (%o2) [1, 1, 2, 3, 5, 7, 11, 15, 22]
1511 (%i3) num_partitions (n);
1512 (%o3) num_partitions(n)
1515 @opencatbox{Categories:}
1523 @anchor{partition_set}
1524 @deffn {Function} partition_set (@var{a}, @var{f})
1526 Partitions the set @var{a} according to the predicate @var{f}.
1528 @code{partition_set} returns a list of two sets.
1529 The first set comprises the elements of @var{a} for which @var{f} evaluates to @code{false},
1530 and the second comprises any other elements of @var{a}.
1531 @code{partition_set} does not apply @code{is} to the return value of @var{f}.
1533 @code{partition_set} complains if @var{a} is not a literal set.
1535 See also @mref{subset}.
1540 @c partition_set ({2, 7, 1, 8, 2, 8}, evenp);
1541 @c partition_set ({x, rat(y), rat(y) + z, 1},
1542 @c lambda ([x], ratp(x)));
1545 (%i1) partition_set (@{2, 7, 1, 8, 2, 8@}, evenp);
1546 (%o1) [@{1, 7@}, @{2, 8@}]
1547 (%i2) partition_set (@{x, rat(y), rat(y) + z, 1@},
1548 lambda ([x], ratp(x)));
1549 (%o2)/R/ [@{1, x@}, @{y, y + z@}]
1552 @opencatbox{Categories:}
1558 @anchor{permutations}
1559 @deffn {Function} permutations (@var{a})
1561 Returns a set of all distinct permutations of the members of
1562 the list or set @var{a}. Each permutation is a list, not a set.
1564 When @var{a} is a list, duplicate members of @var{a} are included
1565 in the permutations.
1567 @code{permutations} complains if @var{a} is not a literal list or set.
1569 See also @mref{random_permutation}.
1574 @c permutations ([a, a]);
1575 @c permutations ([a, a, b]);
1578 (%i1) permutations ([a, a]);
1580 (%i2) permutations ([a, a, b]);
1581 (%o2) @{[a, a, b], [a, b, a], [b, a, a]@}
1584 @opencatbox{Categories:}
1592 @deffn {Function} powerset @
1593 @fname{powerset} (@var{a}) @
1594 @fname{powerset} (@var{a}, @var{n})
1596 Returns the set of all subsets of @var{a}, or a subset of that set.
1598 @code{powerset(@var{a})} returns the set of all subsets of the set @var{a}.
1599 @code{powerset(@var{a})} has @code{2^cardinality(@var{a})} members.
1601 @code{powerset(@var{a}, @var{n})} returns the set of all subsets of @var{a} that have
1602 cardinality @var{n}.
1604 @code{powerset} complains if @var{a} is not a literal set,
1605 or if @var{n} is not a nonnegative integer.
1610 @c powerset ({a, b, c});
1611 @c powerset ({w, x, y, z}, 4);
1612 @c powerset ({w, x, y, z}, 3);
1613 @c powerset ({w, x, y, z}, 2);
1614 @c powerset ({w, x, y, z}, 1);
1615 @c powerset ({w, x, y, z}, 0);
1618 (%i1) powerset (@{a, b, c@});
1619 (%o1) @{@{@}, @{a@}, @{a, b@}, @{a, b, c@}, @{a, c@}, @{b@}, @{b, c@}, @{c@}@}
1620 (%i2) powerset (@{w, x, y, z@}, 4);
1621 (%o2) @{@{w, x, y, z@}@}
1622 (%i3) powerset (@{w, x, y, z@}, 3);
1623 (%o3) @{@{w, x, y@}, @{w, x, z@}, @{w, y, z@}, @{x, y, z@}@}
1624 (%i4) powerset (@{w, x, y, z@}, 2);
1625 (%o4) @{@{w, x@}, @{w, y@}, @{w, z@}, @{x, y@}, @{x, z@}, @{y, z@}@}
1626 (%i5) powerset (@{w, x, y, z@}, 1);
1627 (%o5) @{@{w@}, @{x@}, @{y@}, @{z@}@}
1628 (%i6) powerset (@{w, x, y, z@}, 0);
1632 @opencatbox{Categories:}
1638 @anchor{random_permutation}
1639 @deffn {Function} random_permutation (@var{a})
1641 Returns a random permutation of the set or list @var{a},
1642 as constructed by the Knuth shuffle algorithm.
1644 The return value is a new list, which is distinct
1645 from the argument even if all elements happen to be the same.
1646 However, the elements of the argument are not copied.
1651 @c random_permutation ([a, b, c, 1, 2, 3]);
1652 @c random_permutation ([a, b, c, 1, 2, 3]);
1653 @c random_permutation ({x + 1, y + 2, z + 3});
1654 @c random_permutation ({x + 1, y + 2, z + 3});
1657 (%i1) random_permutation ([a, b, c, 1, 2, 3]);
1658 (%o1) [c, 1, 2, 3, a, b]
1659 (%i2) random_permutation ([a, b, c, 1, 2, 3]);
1660 (%o2) [b, 3, 1, c, a, 2]
1661 (%i3) random_permutation (@{x + 1, y + 2, z + 3@});
1662 (%o3) [y + 2, z + 3, x + 1]
1663 (%i4) random_permutation (@{x + 1, y + 2, z + 3@});
1664 (%o4) [x + 1, y + 2, z + 3]
1667 @opencatbox{Categories:}
1674 @anchor{setdifference}
1675 @deffn {Function} setdifference (@var{a}, @var{b})
1677 Returns a set containing the elements in the set @var{a} that are
1678 not in the set @var{b}.
1680 @code{setdifference} complains if either @var{a} or @var{b} is not a literal set.
1685 @c S_1 : {a, b, c, x, y, z};
1686 @c S_2 : {aa, bb, c, x, y, zz};
1687 @c setdifference (S_1, S_2);
1688 @c setdifference (S_2, S_1);
1689 @c setdifference (S_1, S_1);
1690 @c setdifference (S_1, {});
1691 @c setdifference ({}, S_1);
1694 (%i1) S_1 : @{a, b, c, x, y, z@};
1695 (%o1) @{a, b, c, x, y, z@}
1696 (%i2) S_2 : @{aa, bb, c, x, y, zz@};
1697 (%o2) @{aa, bb, c, x, y, zz@}
1698 (%i3) setdifference (S_1, S_2);
1700 (%i4) setdifference (S_2, S_1);
1701 (%o4) @{aa, bb, zz@}
1702 (%i5) setdifference (S_1, S_1);
1704 (%i6) setdifference (S_1, @{@});
1705 (%o6) @{a, b, c, x, y, z@}
1706 (%i7) setdifference (@{@}, S_1);
1710 @opencatbox{Categories:}
1717 @deffn {Function} setequalp (@var{a}, @var{b})
1719 Returns @code{true} if sets @var{a} and @var{b} have the same number of elements
1720 @c $SETEQUALP CALLS THE LISP FUNCTION LIKE,
1721 @c AND SO DOES THE CODE TO EVALUATE IS (X = Y).
1722 and @code{is(@var{x} = @var{y})} is @code{true}
1723 for @code{x} in the elements of @var{a}
1724 and @code{y} in the elements of @var{b},
1725 considered in the order determined by @code{listify}.
1726 Otherwise, @code{setequalp} returns @code{false}.
1731 @c setequalp ({1, 2, 3}, {1, 2, 3});
1732 @c setequalp ({a, b, c}, {1, 2, 3});
1733 @c setequalp ({x^2 - y^2}, {(x + y) * (x - y)});
1736 (%i1) setequalp (@{1, 2, 3@}, @{1, 2, 3@});
1738 (%i2) setequalp (@{a, b, c@}, @{1, 2, 3@});
1740 (%i3) setequalp (@{x^2 - y^2@}, @{(x + y) * (x - y)@});
1744 @opencatbox{Categories:}
1746 @category{Predicate functions}
1752 @deffn {Function} setify (@var{a})
1754 Constructs a set from the elements of the list @var{a}. Duplicate
1755 elements of the list @var{a} are deleted and the elements
1756 are sorted according to the predicate @code{orderlessp}.
1758 @code{setify} complains if @var{a} is not a literal list.
1763 @c setify ([1, 2, 3, a, b, c]);
1764 @c setify ([a, b, c, a, b, c]);
1765 @c setify ([7, 13, 11, 1, 3, 9, 5]);
1768 (%i1) setify ([1, 2, 3, a, b, c]);
1769 (%o1) @{1, 2, 3, a, b, c@}
1770 (%i2) setify ([a, b, c, a, b, c]);
1772 (%i3) setify ([7, 13, 11, 1, 3, 9, 5]);
1773 (%o3) @{1, 3, 5, 7, 9, 11, 13@}
1776 @opencatbox{Categories:}
1783 @deffn {Function} setp (@var{a})
1785 Returns @code{true} if and only if @var{a} is a Maxima set.
1787 @code{setp} returns @code{true} for unsimplified sets (that is, sets with redundant members)
1788 as well as simplified sets.
1790 @c NOT SURE WE NEED TO MENTION THIS. OK FOR NOW
1791 @code{setp} is equivalent to the Maxima function
1792 @code{setp(a) := not atom(a) and op(a) = 'set}.
1810 @opencatbox{Categories:}
1812 @category{Predicate functions}
1817 @anchor{set_partitions}
1818 @deffn {Function} set_partitions @
1819 @fname{set_partitions} (@var{a}) @
1820 @fname{set_partitions} (@var{a}, @var{n})
1822 Returns the set of all partitions of @var{a}, or a subset of that set.
1824 @code{set_partitions(@var{a}, @var{n})} returns a set of all
1825 decompositions of @var{a} into @var{n} nonempty disjoint subsets.
1827 @code{set_partitions(@var{a})} returns the set of all partitions.
1829 @code{stirling2} returns the cardinality of the set of partitions of a set.
1831 A set of sets @math{P} is a partition of a set @math{S} when
1835 each member of @math{P} is a nonempty set,
1837 distinct members of @math{P} are disjoint,
1839 the union of the members of @math{P} equals @math{S}.
1844 The empty set is a partition of itself, the conditions 1 and 2 being vacuously true.
1847 @c set_partitions ({});
1850 (%i1) set_partitions (@{@});
1854 The cardinality of the set of partitions of a set can be found using @code{stirling2}.
1857 @c s: {0, 1, 2, 3, 4, 5}$
1858 @c p: set_partitions (s, 3)$
1859 @c cardinality(p) = stirling2 (6, 3);
1862 (%i1) s: @{0, 1, 2, 3, 4, 5@}$
1863 (%i2) p: set_partitions (s, 3)$
1864 (%i3) cardinality(p) = stirling2 (6, 3);
1868 Each member of @code{p} should have @var{n} = 3 members; let's check.
1871 @c s: {0, 1, 2, 3, 4, 5}$
1872 @c p: set_partitions (s, 3)$
1873 @c map (cardinality, p);
1876 (%i1) s: @{0, 1, 2, 3, 4, 5@}$
1877 (%i2) p: set_partitions (s, 3)$
1878 (%i3) map (cardinality, p);
1882 Finally, for each member of @code{p}, the union of its members should
1883 equal @code{s}; again let's check.
1886 @c s: {0, 1, 2, 3, 4, 5}$
1887 @c p: set_partitions (s, 3)$
1888 @c map (lambda ([x], apply (union, listify (x))), p);
1891 (%i1) s: @{0, 1, 2, 3, 4, 5@}$
1892 (%i2) p: set_partitions (s, 3)$
1893 (%i3) map (lambda ([x], apply (union, listify (x))), p);
1894 (%o3) @{@{0, 1, 2, 3, 4, 5@}@}
1897 @opencatbox{Categories:}
1904 @deffn {Function} some @
1905 @fname{some} (@var{f}, @var{a}) @
1906 @fname{some} (@var{f}, @var{L_1}, ..., @var{L_n})
1908 Returns @code{true} if the predicate @var{f} is @code{true} for one or more given arguments.
1910 Given one set as the second argument,
1911 @code{some(@var{f}, @var{s})} returns @code{true}
1912 if @code{is(@var{f}(@var{a_i}))} returns @code{true} for one or more @var{a_i} in @var{s}.
1913 @code{some} may or may not evaluate @var{f} for all @var{a_i} in @var{s}.
1914 Since sets are unordered,
1915 @code{some} may evaluate @code{@var{f}(@var{a_i})} in any order.
1917 Given one or more lists as arguments,
1918 @code{some(@var{f}, @var{L_1}, ..., @var{L_n})} returns @code{true}
1919 if @code{is(@var{f}(@var{x_1}, ..., @var{x_n}))} returns @code{true}
1920 for one or more @var{x_1}, ..., @var{x_n} in @var{L_1}, ..., @var{L_n}, respectively.
1921 @code{some} may or may not evaluate
1922 @var{f} for some combinations @var{x_1}, ..., @var{x_n}.
1923 @code{some} evaluates lists in the order of increasing index.
1925 Given an empty set @code{@{@}} or empty lists @code{[]} as arguments,
1926 @code{some} returns @code{false}.
1928 When the global flag @code{maperror} is @code{true}, all lists
1929 @var{L_1}, ..., @var{L_n} must have equal lengths.
1930 When @code{maperror} is @code{false}, list arguments are
1931 effectively truncated to the length of the shortest list.
1933 Return values of the predicate @var{f} which evaluate (via @code{is})
1934 to something other than @code{true} or @code{false}
1935 are governed by the global flag @code{prederror}.
1936 When @code{prederror} is @code{true},
1937 such values are treated as @code{false}.
1938 When @code{prederror} is @code{false},
1939 such values are treated as @code{unknown}.
1943 @code{some} applied to a single set.
1944 The predicate is a function of one argument.
1947 @c some (integerp, {1, 2, 3, 4, 5, 6});
1948 @c some (atom, {1, 2, sin(3), 4, 5 + y, 6});
1951 (%i1) some (integerp, @{1, 2, 3, 4, 5, 6@});
1953 (%i2) some (atom, @{1, 2, sin(3), 4, 5 + y, 6@});
1957 @code{some} applied to two lists.
1958 The predicate is a function of two arguments.
1961 @c some ("=", [a, b, c], [a, b, c]);
1962 @c some ("#", [a, b, c], [a, b, c]);
1965 (%i1) some ("=", [a, b, c], [a, b, c]);
1967 (%i2) some ("#", [a, b, c], [a, b, c]);
1971 Return values of the predicate @var{f} which evaluate
1972 to something other than @code{true} or @code{false}
1973 are governed by the global flag @code{prederror}.
1976 @c prederror : false;
1977 @c map (lambda ([a, b], is (a < b)), [x, y, z],
1978 @c [x^2, y^2, z^2]);
1979 @c some ("<", [x, y, z], [x^2, y^2, z^2]);
1980 @c some ("<", [x, y, z], [x^2, y^2, z + 1]);
1981 @c prederror : true;
1982 @c some ("<", [x, y, z], [x^2, y^2, z^2]);
1983 @c some ("<", [x, y, z], [x^2, y^2, z + 1]);
1986 (%i1) prederror : false;
1988 (%i2) map (lambda ([a, b], is (a < b)), [x, y, z],
1990 (%o2) [unknown, unknown, unknown]
1991 (%i3) some ("<", [x, y, z], [x^2, y^2, z^2]);
1993 (%i4) some ("<", [x, y, z], [x^2, y^2, z + 1]);
1995 (%i5) prederror : true;
1997 (%i6) some ("<", [x, y, z], [x^2, y^2, z^2]);
1999 (%i7) some ("<", [x, y, z], [x^2, y^2, z + 1]);
2003 @opencatbox{Categories:}
2011 @deffn {Function} stirling1 (@var{n}, @var{m})
2013 Represents the Stirling number of the first kind.
2015 When @var{n} and @var{m} are nonnegative
2016 integers, the magnitude of @code{stirling1 (@var{n}, @var{m})} is the number of
2017 permutations of a set with @var{n} members that have @var{m} cycles.
2019 @code{stirling1} is a simplifying function.
2020 Maxima knows the following identities:
2024 @math{stirling1(1,k) = kron_delta(1,k), k >= 0},(see @url{https://dlmf.nist.gov/26.8.E2})
2026 @math{stirling1(n,n) = 1, n >= 0} (see @url{https://dlmf.nist.gov/26.8.E1})
2028 @math{stirling1(n,n-1) = -binomial(n,2), n >= 1}, (see @url{https://dlmf.nist.gov/26.8.E16})
2030 @math{stirling1(n,0) = kron_delta(n,0), n >=0} (see @url{https://dlmf.nist.gov/26.8.E14} and
2031 @url{https://dlmf.nist.gov/26.8.E1})
2033 @math{stirling1(n,1) =(-1)^(n-1) (n-1)!, n >= 1} (see @url{https://dlmf.nist.gov/26.8.E14})
2035 @math{stirling1(n,k) = 0, n >= 0} and @math{k > n}.
2038 These identities are applied when the arguments are literal integers
2039 or symbols declared as integers, and the first argument is nonnegative.
2040 @code{stirling1} does not simplify for non-integer arguments.
2046 @c declare (n, integer)$
2048 @c stirling1 (n, n);
2051 (%i1) declare (n, integer)$
2052 (%i2) assume (n >= 0)$
2053 (%i3) stirling1 (n, n);
2058 @opencatbox{Categories:}
2065 @deffn {Function} stirling2 (@var{n}, @var{m})
2067 Represents the Stirling number of the second kind.
2069 When @var{n} and @var{m} are nonnegative
2070 integers, @code{stirling2 (@var{n}, @var{m})} is the number of ways a set with
2071 cardinality @var{n} can be partitioned into @var{m} disjoint subsets.
2073 @code{stirling2} is a simplifying function.
2074 Maxima knows the following identities.
2077 @item @math{stirling2(n,0) = 1, n >= 1} (see @url{https://dlmf.nist.gov/26.8.E17} and stirling2(0,0) = 1)
2078 @item @math{stirling2(n,n) = 1, n >= 0}, (see @url{https://dlmf.nist.gov/26.8.E4})
2079 @item @math{stirling2(n,1) = 1, n >= 1}, (see @url{https://dlmf.nist.gov/26.8.E17} and stirling2(0,1) = 0)
2080 @item @math{stirling2(n,2) = 2^(n-1) -1, n >= 1}, (see @url{https://dlmf.nist.gov/26.8.E17})
2081 @item @math{stirling2(n,n-1) = binomial(n,2), n>= 1} (see @url{https://dlmf.nist.gov/26.8.E16})
2082 @item @math{stirling2(n,k) = 0, n >= 0} and @math{k > n}.
2085 These identities are applied when the arguments are literal integers
2086 or symbols declared as integers, and the first argument is nonnegative.
2087 @code{stirling2} does not simplify for non-integer arguments.
2092 @c declare (n, integer)$
2094 @c stirling2 (n, n);
2097 (%i1) declare (n, integer)$
2098 (%i2) assume (n >= 0)$
2099 (%i3) stirling2 (n, n);
2103 @code{stirling2} does not simplify for non-integer arguments.
2106 @c stirling2 (%pi, %pi);
2109 (%i1) stirling2 (%pi, %pi);
2110 (%o1) stirling2(%pi, %pi)
2113 @opencatbox{Categories:}
2120 @deffn {Function} subset (@var{a}, @var{f})
2122 Returns the subset of the set @var{a} that satisfies the predicate @var{f}.
2124 @code{subset} returns a set which comprises the elements of @var{a}
2125 for which @var{f} returns anything other than @code{false}.
2126 @code{subset} does not apply @code{is} to the return value of @var{f}.
2128 @code{subset} complains if @var{a} is not a literal set.
2130 See also @mref{partition_set}.
2135 @c subset ({1, 2, x, x + y, z, x + y + z}, atom);
2136 @c subset ({1, 2, 7, 8, 9, 14}, evenp);
2139 (%i1) subset (@{1, 2, x, x + y, z, x + y + z@}, atom);
2140 (%o1) @{1, 2, x, z@}
2141 (%i2) subset (@{1, 2, 7, 8, 9, 14@}, evenp);
2145 @opencatbox{Categories:}
2152 @deffn {Function} subsetp (@var{a}, @var{b})
2154 Returns @code{true} if and only if the set @var{a} is a subset of @var{b}.
2156 @code{subsetp} complains if either @var{a} or @var{b} is not a literal set.
2161 @c subsetp ({1, 2, 3}, {a, 1, b, 2, c, 3});
2162 @c subsetp ({a, 1, b, 2, c, 3}, {1, 2, 3});
2165 (%i1) subsetp (@{1, 2, 3@}, @{a, 1, b, 2, c, 3@});
2167 (%i2) subsetp (@{a, 1, b, 2, c, 3@}, @{1, 2, 3@});
2171 @opencatbox{Categories:}
2173 @category{Predicate functions}
2178 @anchor{symmdifference}
2179 @deffn {Function} symmdifference (@var{a_1}, @dots{}, @var{a_n})
2181 Returns the symmetric difference of sets @var{a_1}, @dots{}, @var{a_n}.
2183 Given two arguments, @code{symmdifference (@var{a}, @var{b})} is the same as
2184 @code{union (setdifference (@var{a}, @var{b}), setdifference (@var{b},
2187 @code{symmdifference} complains if any argument is not a literal set.
2195 @c symmdifference ();
2196 @c symmdifference (S_1);
2197 @c symmdifference (S_1, S_2);
2198 @c symmdifference (S_1, S_2, S_3);
2199 @c symmdifference ({}, S_1, S_2, S_3);
2202 (%i1) S_1 : @{a, b, c@};
2204 (%i2) S_2 : @{1, b, c@};
2206 (%i3) S_3 : @{a, b, z@};
2208 (%i4) symmdifference ();
2210 (%i5) symmdifference (S_1);
2212 (%i6) symmdifference (S_1, S_2);
2214 (%i7) symmdifference (S_1, S_2, S_3);
2216 (%i8) symmdifference (@{@}, S_1, S_2, S_3);
2220 @opencatbox{Categories:}
2227 @deffn {Function} union (@var{a_1}, ..., @var{a_n})
2228 Returns the union of the sets @var{a_1} through @var{a_n}.
2230 @code{union()} (with no arguments) returns the empty set.
2232 @code{union} complains if any argument is not a literal set.
2237 @c S_1 : {a, b, c + d, %e};
2238 @c S_2 : {%pi, %i, %e, c + d};
2239 @c S_3 : {17, 29, 1729, %pi, %i};
2242 @c union (S_1, S_2);
2243 @c union (S_1, S_2, S_3);
2244 @c union ({}, S_1, S_2, S_3);
2247 (%i1) S_1 : @{a, b, c + d, %e@};
2248 (%o1) @{%e, a, b, d + c@}
2249 (%i2) S_2 : @{%pi, %i, %e, c + d@};
2250 (%o2) @{%e, %i, %pi, d + c@}
2251 (%i3) S_3 : @{17, 29, 1729, %pi, %i@};
2252 (%o3) @{17, 29, 1729, %i, %pi@}
2256 (%o5) @{%e, a, b, d + c@}
2257 (%i6) union (S_1, S_2);
2258 (%o6) @{%e, %i, %pi, a, b, d + c@}
2259 (%i7) union (S_1, S_2, S_3);
2260 (%o7) @{17, 29, 1729, %e, %i, %pi, a, b, d + c@}
2261 (%i8) union (@{@}, S_1, S_2, S_3);
2262 (%o8) @{17, 29, 1729, %e, %i, %pi, a, b, d + c@}
2265 @opencatbox{Categories:}