Sets and Lists

Calling Sequence


{es}
[es]


Description


•

A set is an unordered sequence of distinct expressions enclosed in braces {}, representing a set in the mathematical sense.

•

A list is an ordered sequence of expressions enclosed in square brackets []. The ordering of the expressions is the ordering of es.

•

Note that es may be empty so that the empty set is represented by {} and the empty list is represented by [].

•

The number of elements of a set or list S is given by numelems(S).


Accessing the elements


•

The elements of a set or list can be extracted via the selection operation. Thus if S is a set or list then S[i] selects the ith element. Alternatively you may use op(i,S). The elements are numbered from 1 so S[1] extracts the first element. Negative subscripts can also be used. S[1] selects the last element, S[2] the second last, and so on.

•

Multiple elements of a list or set S can be extracted. The selection op(i..j,S) selects the subsequence (S[i],S[i+1],...,S[j]). If S is a set then the selection S[i..j] selects the subset {op(i..j,S)}. If S is a list then the selection S[i..j] selects the sublist [op(i..j,S)]. Negative subscripts can also be used. Thus S[2..2] selects all elements except the first and last.

•

Lists and sets can be nested, in which case selection can be done in one of two ways: S[i][j]...[n] or S[i,j,...,n].

•

To extract the contents of a list or set L as a sequence, use the op(L) function or the empty selection operator L[].



Modifying the elements


Note: The following modifications to a list or set cause a new list or set to be created rather than modifying the original inplace. To save space and time, when the number of data or the number of modifications to them are large, use of an Array may be preferable.
•

Appending an element x to a list L is done by [op(L), x]. Inserting an element x to a set S is done using the union operator S union {x} ($S\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\cup \phantom{\rule[0.0ex]{0.5em}{0.0ex}}\left\{x\right\}$).

•

Replacing the ith element of a list L by x can be done by subsop(i=x, L).

•

The operation L[i] := x also works for reasons of convenience, but only for short lists. It is not recommended to use this operation unless you know that the list is small.

•

Deleting the ith element of a list L is subsop(i=NULL, L). Deleting an element x from a set S is done using the minus operator S minus {x} ($S\setminus \left\{x\right\}$).



Set and list operations


•

To test if an element x is in a list or set L, use either member(x,L) or x in L ($x\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\in \phantom{\rule[0.0ex]{0.5em}{0.0ex}}L$).

•

To test if a set S is a subset of a set T, use the subset operator S subset T ($S\subseteq T$).

•

To find the intersection of two sets S and T, use S intersect T ($S\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\cap \phantom{\rule[0.0ex]{0.5em}{0.0ex}}T$).

•

To find the union of two sets S and T, use S union T ($S\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\cup \phantom{\rule[0.0ex]{0.5em}{0.0ex}}T$).

•

To find the difference of two sets S and T, use S minus T ($S\setminus T$).

•

Further list operations can be found in the package ListTools.



Set ordering


Sets have a deterministic ordering that, for most objects, is not based on runtime properties. This means that when {b,c,a} is input, the order will be fixed to {a,b,c} no matter when you created that set. A notable exception to this rule is when a set contains multiple mutable objects of the same type. For example, two vectors inside a set could appear in either order in different sessions.
In general, object ordering is first determined by quickly identifiable features like its class or size. The default comparison order checks properties in the following order:
1.

object id: this causes the same kind of datastructures to be grouped together. For example integers will come before exact rationals, floats, and complex numbers.

2.

object length: this causes objects of the same size to be grouped together. The "length" is a property of the underlying datastructure. As a result, {2^200, 2^100} is not in increasing numerical order and {bb, aaaaaaaa} is not in alphabetical order; in both cases the shorter data structure occurs first.

3.

lexicographical or numerical order: this orders numbers in increasing numerical order and strings in lexicographic order.

4.

recurse on components: this causes compound objects to be ordered according to the above properties of their child components. For example {f(z),g(a)} will pass the above properties as both f(z) and g(a) are function objects of the same length. So the next level of ordering will compare the names of each function, f and g, which will fall into rule 3. Note that the ordering of expressions like {x+1,1+y} may still be session dependent if the unique representation of x+1 is in the order of 1+x, or if the second expression is y+1 instead of 1+y.

5.

address: in some cases the object will be identical in every way, such as the local name x and the global name x. When this happens the machine address of the given object will be used. This at least ensures the set order will be consistent within the running session.

Note: This order could be changed in future versions of Maple, and/or objects could be represented in a different way in future versions. You should not assume that sets will be maintained in any particular order.
The objectaddress based set ordering used in Maple 11 and earlier versions can be obtained by using the commandline option setsort=0. Similarly, the new set order can be enabled with setsort=1. Variations of setsort=0..8 are available if you want to purposefully introduce ordering differences into your code to test its robustness. Settings 2 through 8 apply the same rules 15 listed above but alter whether each subordering is increasing or decreasing. For more information on commandline options, see the maple help page.



Thread Safety


•

The [] and {} constructors are thread safe as of Maple 15.



Examples


>

$\left\{x\,y\,y\right\}$

$\left\{{x}{\,}{y}\right\}$
 (1) 
>

$\left\{y\,x\,y\right\}$

$\left\{{x}{\,}{y}\right\}$
 (2) 
>

$\left[x\,y\,y\right]$

$\left[{x}{\,}{y}{\,}{y}\right]$
 (3) 
>

$\left[y\,x\,y\right]$

$\left[{y}{\,}{x}{\,}{y}\right]$
 (4) 
>

$L:=\left[\mathrm{seq}\left({x}_{i}\,i\=1..4\right)\right]$

${L}{:=}\left[{{x}}_{{1}}{\,}{{x}}_{{2}}{\,}{{x}}_{{3}}{\,}{{x}}_{{4}}\right]$
 (5) 
>

$\mathrm{numelems}\left(L\right)$

Extract list elements
$\left[{{x}}_{{1}}{\,}{{x}}_{{2}}\right]$
 (8) 
$\left[{{x}}_{{2}}{\,}{{x}}_{{3}}{\,}{{x}}_{{4}}\right]$
 (9) 
Extract the contents of a list as a sequence
>

$\mathrm{op}\left(L\right)$

${{x}}_{{1}}{\,}{{x}}_{{2}}{\,}{{x}}_{{3}}{\,}{{x}}_{{4}}$
 (10) 
${{x}}_{{1}}{\,}{{x}}_{{2}}{\,}{{x}}_{{3}}{\,}{{x}}_{{4}}$
 (11) 
>

$\mathrm{op}\left({L}_{2..3}\right)$

${{x}}_{{2}}{\,}{{x}}_{{3}}$
 (12) 
>

$\mathrm{op}\left(2..3\,L\right)$

${{x}}_{{2}}{\,}{{x}}_{{3}}$
 (13) 
Append a new element to a list
>

$L:=\left[\mathrm{op}\left(L\right)\,{x}_{5}\right]$

${L}{:=}\left[{{x}}_{{1}}{\,}{{x}}_{{2}}{\,}{{x}}_{{3}}{\,}{{x}}_{{4}}{\,}{{x}}_{{5}}\right]$
 (14) 
Delete an element from a list
>

$\mathrm{subsop}\left(2\=\mathrm{NULL}\,L\right)$

$\left[{{x}}_{{1}}{\,}{{x}}_{{3}}{\,}{{x}}_{{4}}{\,}{{x}}_{{5}}\right]$
 (15) 
Test for list membership
>

$\mathrm{member}\left({x}_{1}\,L\right)$

Nested lists
>

$L:=\left[1\,\left[2\,3\right]\,\left[4\,\left[5\,6\right]\,7\right]\,8\,9\right]$

${L}{:=}\left[{1}{\,}\left[{2}{\,}{3}\right]{\,}\left[{4}{\,}\left[{5}{\,}{6}\right]{\,}{7}\right]{\,}{8}{\,}{9}\right]$
 (17) 
>

${L}_{3\,2\,1}$

Sets of single letter variables are always sorted in alphabetical order.
>

$\left\{B\,b\,c\,a\,A\right\}$

$\left\{{A}{\,}{B}{\,}{a}{\,}{b}{\,}{c}\right\}$
 (20) 
Sets of small integers are always in numerical order.
>

$S:=\left\{5\,4\,3\,2\,1\right\}$

${S}{:=}\left\{{1}{\,}{2}{\,}{3}{\,}{4}{\,}{5}\right\}$
 (21) 
>

$S\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\∪\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\left\{3\,6\right\}$

$\left\{{1}{\,}{2}{\,}{3}{\,}{4}{\,}{5}{\,}{6}\right\}$
 (22) 
Set operators do not change the set. They create a new set.
$\left\{{1}{\,}{2}{\,}{3}{\,}{4}{\,}{5}\right\}$
 (23) 
Set operators do not work on lists.
>

$\left\{1\,2\,3\right\}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\∩\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\left\{2\,3\,4\right\}$

$\left\{{2}{\,}{3}\right\}$
 (24) 
>

$\left[1\,2\,3\right]\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\∩\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\left\{2\,3\,4\right\}$

The in operator ($\in$) tests for set or list membership.
>

$\mathrm{evalb}\left(1\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\∈\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\left\{1\,2\,3\right\}\right)$

>

$\mathrm{evalb}\left(4\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\∈\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\left[1\,2\,3\right]\right)$

>

$S:=\left\{1\,2\right\}$

${S}{:=}\left\{{1}{\,}{2}\right\}$
 (27) 
>

$T:=\left\{1\,2\,3\right\}$

${T}{:=}\left\{{1}{\,}{2}{\,}{3}\right\}$
 (28) 
$\left\{{3}\right\}$
 (30) 


See Also


Array, convert, in, intersect, list Maple Debugger command, ListTools, maple, member, minus, nops, op, selection, seq, subset, type/list, type/set, union

