A set is constructed similarly to a list, but instead of square brackets, curly braces are used. What differentiates a set from a list is that in a set, there is no prescribed order and there are no duplicates. The order may change from session to session, but within one session, the order of a set will be fixed.
[> rnd_seq := 5, 6, 6, 10, 10, 3, 1, 1, 1, 6, 4, 7, 8, 4, 10, 3, 10, 4, 5, 3;
$rnd_seq := 5, 6, 6, 10, 10, 3, 1, 1, 1, 6, 4, 7, 8, 4, 10, 3, 10, 4, 5, 3;$
[> S := {rnd_seq};
$S := \{1, 3, 4, 5, 6, 7, 8, 10\}$
To determine if an expression is in a set, you need only use the member function. Internally, this can be done using a binary search, and is therefore significantly faster than finding an entry in a list.
[> member( 8, S ); # Is '8' in the set?
$true$
[> member( 9, S ); # Is '9' in the set?
There is a second operator that tests for membership, and that is the in operator:
[> 8 in S; # Is '8' in the set?
$8 \in \{1, 3, 4, 5, 6, 7, 8, 10\}$
[> 9 in T; # Is '9' in the set T?
You can force an evaluation of such a Boolean statement by using the evalb( ... ) function, but this is generally not necessary; it is easier to just use member(...).
$9 \in T$
You will notice that this operator does not immediate evaluate. This is because Maple delays evaluation until a decision is necessary. For example, in the second case, $T$ may not yet have been defined.
If is the condition of a conditional statement, then as soon as the conditional statement, the membership test will be evaluated:
[> x := 42: [> if x in \{1, 3, 5, 7, 9\} then printf( "%d is a single-digit odd number\n", x ); else printf( "%d is not a single-digit odd number\n", x ); end if;
42 is not a single-digit odd number
Suppose you want to apply a function onto the entries of a set. The map(...) function allows this:
To be completed...
Note that if you map a function onto a set and that mapping takes two or more entries to the same result, then any duplicate entries will be eliminated. Also, the fifth entry may not have its image under the map continue to appear in the fifth entry.
Most operators are symbols, but the operators for sets are the corresponding English words: union, minus and subset. If both operands of a union are sets, the result is the union of the two sets (all entries appearing in one or the other); if both operands of an intersect are sets, the result is the intersection of the two sets (all entries in common); if both operands of a minus are sets, the result is the set containing those elements in the first set that are not in the second; and if both operands of a subset are sets, then the result is true if the left-hand operand is a subset of the second, and false otherwise. All of these are very fast.
[> S1 := {-9, -8, -5, -3, -1, 0, 2, 3, 4, 6, 7, 9}: [> S2 := {-10, -8, -6, -5, -2, -1, 1, 2, 3, 6, 7, 8, 10}: [> S1 union S2;
$\{-10, -8, -6, -5, -2, -1, 1, 2, 3, 6, 7, 8, 10\}$
[> S1 intersect S2;
$\{-8, -5, -1, 2, 3, 6, 7\}$
[> S1 minus S2;
$\{-9, -3, 0, 4, 9\}$
[> {-3, 0, 3} subset S1;
$true$
[> {-3, 0, 3} subset S2;
$false$
If both operands of union, intersect or subset are not sets, the result returns generally returns unevaluated:
[> T1 union T2;
$T1 \cup T2$
[> T1 intersect T2;
$T1 \cap T2$
[> T1 minus {1, 2, 3};
$T1 \setminus \{1, 2, 3\}$
[> {-3, 0, 3} subset T1;
$\{-3, 0, 3\} \subseteq T1$
However, Maple will make certain simplifications:
[> T union T union {};
$T$
[> T intersect T;
$T$
[> T intersect {};
${}$
[> T minus T;
${}$
[> {} minus T;
${}$
[> T minus {};
$T$
[> {-3, 0, 3} subset T1;
$\{-3, 0, 3\} \subseteq T1$
Later, we will see that Boolean expressions involving in and subset can be used as the condition in conditional (if) statements or while loops.