MutableSet - Maple Programming Help

Home : Support : Online Help : Programming : Operations : Sets and lists : MutableSet

MutableSet

an object-based mutable set

Description

 • The MutableSet appliable object provides an efficient means to construct, manipulate, and query set objects.
 • Unlike a standard Maple set, adding an element to a MutableSet object does not allocate a new container.  As such, it can be efficiently used in loops to build up a set an element at a time.

Construction

 • The MutableSet command returns a new MutableSet object.
 If called with a single argument that is either a Maple set or a MutableSet object, the returned object contains the elements in the argument.
 Otherwise, the returned object contains each distinct argument as an element.

Modification

 • The following commands modify the content of a MutableSet object. The ms argument denotes a MutableSet object.
 clear(ms) : clear ms
 delete(ms,value) : delete value from ms
 insert(ms,value) : insert value into ms

Set Operation

 The following set commands combine MutableSet objects. Those prefixed with an ampersand (&) operate inplace on the first argument. These commands can be invoked as functions or inline operators. The ms1 and ms2 arguments denote MutableSet objects.
 ms1 intersect ms2, or $\mathrm{ms1}\cap \mathrm{ms2}$, or intersect(ms1,ms2,...) : return a new MutableSet object that is the intersection of the given mutable sets
 ms1 &intersect ms2 or &intersect(ms1,ms2,...) : remove elements of ms1 that are not elements of ms2, etc., updating ms1
 ms1 minus ms2, or $\mathrm{ms1}\setminus \mathrm{ms2}$, or minus(ms1,ms2) : return a new MutableSet object that contains the set difference of ms1 and ms2
 ms1 &minus ms2 or &minus(ms1,ms2) : remove elements of ms2 from ms1, updating ms1
 ms1 union ms2, or $\mathrm{ms1}\cup \mathrm{ms2}$, or union(ms1,ms2,...) : return a new MutableSet object that is the union of the given mutable sets
 ms1 &union ms2 or &union(ms1,ms2,...) : combine the members of ms2, etc., with ms1, updating ms1

Inspection

 The following commands inspect the content of a MutableSet object. The ms, ms1, and ms2 arguments denote MutableSet objects.
 ms = other or =(ms,other): when evaluated in a boolean context, return true if ms and other are both MutableSet objects with identical content, false otherwise
 empty(ms) : return true if ms is empty; false otherwise
 has(ms,expr) : return true if any member of ms contains expr; false otherwise
 hastype(ms,typ) : return true if any member of ms contains an expression of the specified type; false otherwise
 in(value,ms), or value in ms, or $\mathrm{value}\in \mathrm{ms}$ : return true if value is a member of ms; false otherwise
 indets(ms,typ) : return a Maple set of all indeterminates in ms; if the optional typ parameter is specified, then return the expressions of type typ.
 lowerbound(ms) : return the lower index bound (always 1)
 max(ms) : return the maximum element of ms. Behaves like max on sets.
 member(value,ms) : return true if  value is a member of ms; false otherwise.  The three argument form of member is not supported.
 min(ms) : return the minimum element of ms.  Behaves like min on sets.
 numelems(ms) : return the number of elements in ms
 numboccur(ms,expr) : count the number of occurrences of expr in ms, either as an element or within elements.
 subset(ms1,ms2), or ms1 subset ms2, or $\mathrm{ms1}\subseteq \mathrm{ms2}$ : return true if ms1 is a subset of ms2; false otherwise
 upperbound(ms) : return the upper index (same as the output of numelems)

map and subs

 The map and subs commands operate on a MutableSet object; see their help pages for the complete syntax. By default they create a new MutableSet object, but if called with the inplace index option, they update the MutableSet object. The ms argument denotes a MutableSet object.
 map(fcn,ms) : apply a procedure, fcn, to each element of ms
 subs(eqns,ms) : substitute subexpressions in the content of ms

Conversion

 The convert command can be used to convert a MutableSet object to and from other Maple structures.
 convert(ms,typ) : convert a MutableSet to the specified type
 convert(expr,MutableSet) : convert expr to a MutableSet object; expr must be a list, set, or rtable

Indexing

 Integer indices can be used to extract or reassign a single element of a MutableSet. Reassigning an element may change its index.
 Range indices are not supported.

Iteration

 The MutableSet object exports a ModuleIterator command; that allows a MutableSet object to be used in loops and iterations (seq, add, etc.).

Examples

Construction

 Create a MutableSet with three elements.
 > $\mathrm{M1}≔\mathrm{MutableSet}\left(a,b,c\right)$
 ${\mathrm{M1}}{≔}{\mathrm{MutableSet}}{}\left({a}{,}{b}{,}{c}\right)$ (1)
 Copy $\mathrm{M1}$.
 > $\mathrm{M2}≔\mathrm{MutableSet}\left(\mathrm{M1}\right)$
 ${\mathrm{M2}}{≔}{\mathrm{MutableSet}}{}\left({a}{,}{b}{,}{c}\right)$ (2)
 Create a MutableSet from a Maple set.
 > $\mathrm{M3}≔\mathrm{MutableSet}\left(\left\{b,c,d\right\}\right)$
 ${\mathrm{M3}}{≔}{\mathrm{MutableSet}}{}\left({b}{,}{c}{,}{d}\right)$ (3)

Modification

 Delete the b element from $\mathrm{M1}$ and insert a d element.
 > $\mathrm{delete}\left(\mathrm{M1},b\right)$
 ${\mathrm{MutableSet}}{}\left({a}{,}{c}\right)$ (4)
 > $\mathrm{insert}\left(\mathrm{M1},d\right)$
 ${\mathrm{MutableSet}}{}\left({a}{,}{c}{,}{d}\right)$ (5)
 Add $c$ to each element in $\mathrm{M2}$; do so inplace.
 > ${\mathrm{map}}_{\mathrm{inplace}}\left(\mathrm{+},\mathrm{M2},c\right)$
 ${\mathrm{MutableSet}}{}\left({a}{+}{c}{,}{b}{+}{c}{,}{2}{}{c}\right)$ (6)
 Replace all occurrences of $c$ inside $\mathrm{M2}$ with $d$; do so inplace.
 > ${\mathrm{subs}}_{\mathrm{inplace}}\left(c=d,\mathrm{M2}\right)$
 ${\mathrm{MutableSet}}{}\left({2}{}{d}{,}{a}{+}{d}{,}{b}{+}{d}\right)$ (7)

Operation

 Create two MutableSet objects, then form their intersection.
 > $\mathrm{M1}≔\mathrm{MutableSet}\left(a,b,c,d\right)$
 ${\mathrm{M1}}{≔}{\mathrm{MutableSet}}{}\left({a}{,}{b}{,}{c}{,}{d}\right)$ (8)
 > $\mathrm{M2}≔\mathrm{MutableSet}\left(c,d,e,f\right)$
 ${\mathrm{M2}}{≔}{\mathrm{MutableSet}}{}\left({c}{,}{d}{,}{e}{,}{f}\right)$ (9)
 > $\mathrm{M3}≔\mathrm{M1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∩\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{M2}$
 ${\mathrm{M3}}{≔}{\mathrm{MutableSet}}{}\left({c}{,}{d}\right)$ (10)
 Use the inplace form to update $\mathrm{M1}$ with the intersection of $\mathrm{M1}$ and $\mathrm{M2}$.
 > $\mathrm{M1}&intersect\mathrm{M2}:$
 > $\mathrm{M1}$
 ${\mathrm{MutableSet}}{}\left({c}{,}{d}\right)$ (11)

Inspection

 Use member to determine whether a given element is a member of $\mathrm{M1}$.
 > $\mathrm{M1}≔\mathrm{MutableSet}\left(a,b,c,4\right)$
 ${\mathrm{M1}}{≔}{\mathrm{MutableSet}}{}\left({4}{,}{a}{,}{b}{,}{c}\right)$ (12)
 > $\mathrm{member}\left(b,\mathrm{M1}\right)$
 ${\mathrm{true}}$ (13)
 Use has to test for the occurrence of a subexpression in any of the members of the set.
 > $\mathrm{has}\left(\mathrm{M1},c\right)$
 ${\mathrm{true}}$ (14)
 > $\mathrm{indets}\left(\mathrm{M1},\mathrm{numeric}\right)$
 $\left\{{4}\right\}$ (15)

Conversion

 Create a MutableSet object by converting a Vector.
 > $\mathrm{M1}≔\mathrm{convert}\left(\mathrm{Vector}\left(10,\mathrm{symbol}=v\right),\mathrm{MutableSet}\right)$
 ${\mathrm{M1}}{≔}{\mathrm{MutableSet}}{}\left({{v}}_{{1}}{,}{{v}}_{{2}}{,}{{v}}_{{3}}{,}{{v}}_{{4}}{,}{{v}}_{{5}}{,}{{v}}_{{6}}{,}{{v}}_{{7}}{,}{{v}}_{{8}}{,}{{v}}_{{9}}{,}{{v}}_{{10}}\right)$ (16)
 Convert M1 to a standard set.
 > $\mathrm{convert}\left(\mathrm{M1},\mathrm{set}\right)$
 $\left\{{{v}}_{{1}}{,}{{v}}_{{2}}{,}{{v}}_{{3}}{,}{{v}}_{{4}}{,}{{v}}_{{5}}{,}{{v}}_{{6}}{,}{{v}}_{{7}}{,}{{v}}_{{8}}{,}{{v}}_{{9}}{,}{{v}}_{{10}}\right\}$ (17)

Indexing

 Use indexing to extract each element of a MutableSet.
 > $\mathrm{M1}≔\mathrm{MutableSet}\left(a,b,c,d\right)$
 ${\mathrm{M1}}{≔}{\mathrm{MutableSet}}{}\left({a}{,}{b}{,}{c}{,}{d}\right)$ (18)
 > $\mathrm{seq}\left(i={\mathrm{M1}}_{i},i=1..\mathrm{numelems}\left(\mathrm{M1}\right)\right)$
 ${1}{=}{a}{,}{2}{=}{b}{,}{3}{=}{c}{,}{4}{=}{d}$ (19)
 Reassign the value of the second index. Note that in the updated MutableSet, the new value is assigned a different index.
 > ${\mathrm{M1}}_{2}≔23:$
 > $\mathrm{seq}\left(i={\mathrm{M1}}_{i},i=1..\mathrm{numelems}\left(\mathrm{M1}\right)\right)$
 ${1}{=}{23}{,}{2}{=}{a}{,}{3}{=}{c}{,}{4}{=}{d}$ (20)

Iteration

 Iterate through each element in M1.
 > $\left[\mathrm{seq}\left(x,x\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∈\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{M1}\right)\right]$
 $\left[{23}{,}{a}{,}{c}{,}{d}\right]$ (21)
 > $\mathrm{add}\left(x,x\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∈\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{M2}\right)$
 ${c}{+}{d}{+}{e}{+}{f}$ (22)
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}x\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{M2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(x\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}$
 ${c}$
 ${d}$
 ${e}$
 ${f}$ (23)

Usage

 The MutableSet object provides an efficient way to construct a set an element at a time, in a loop.  Doing so with standard Maple sets is inefficient in that each addition creates a new set; as such the operation is $\mathrm{O}\left({n}^{2}\right)$ in time and memory, where $n$ is the number of elements. The following compares the memory and time required to create a set of ten thousand elements using standard sets and a MutableSet object.
 > MapleSets := proc(n :: posint)  local i, s;     s := {};     for i to n do         s := s union {i};     end do:     numelems(s); end proc: MutableSets := proc(n :: posint) local i, s;     s := MutableSet();     for i to n do         insert(s,-i);     end do;     numelems(s); end proc:
 > $\mathrm{CodeTools}:-\mathrm{Usage}\left(\mathrm{MapleSets}\left(10000\right)\right)$
 memory used=382.91MiB, alloc change=25.48MiB, cpu time=1.55s, real time=1.28s, gc time=576.83ms
 ${10000}$ (24)
 > $\mathrm{CodeTools}:-\mathrm{Usage}\left(\mathrm{MutableSets}\left(10000\right)\right)$
 memory used=1.15MiB, alloc change=0 bytes, cpu time=23.00ms, real time=22.00ms, gc time=0ns
 ${10000}$ (25)

Compatibility

 • The MutableSet object was introduced in Maple 2016.