Details on the Iterator object - Maple Help

Home : Support : Online Help : Mathematics : Discrete Mathematics : Combinatorics : Iterator : Details on the Iterator object

Details of the Iterator object

 Common Methods Each iterator constructed by this package is a Maple object with the following common methods. ModuleApply : makes the object an appliable object factory. It calls the Object constructor and passes its arguments to the object's ModuleCopy method. ModuleCopy(self,proto,...) : used to copy an object; see ModuleCopy. ModuleIterator(self) : returns the two procedures, hasNext and getNext used to implement an iterator; see ModuleIterator. The iterators constructed by this package violate one of the specifications: calling hasNext consecutively, without a call to getNext, does not necessarily return the same value. Each call to hasNext advances the iterator. The reason for this is efficiency; these iterators reuse an Array as output, so there is no need to call getNext. length(self) : returns the length of the output array of the iterator self. For most iterators, this is a fixed value, but for a few this may change with each iteration. anditer(p,self,...) : applies the predicate p to each iteration and returns the and of all the evaluations. It returns false immediately if any evaluation returns false. Optional arguments are passed to the predicate, p. The iterator is reset upon exiting. oriter(p,self,...) : applies the predicate p to each iteration and returns the or of all the evaluations. It returns true immediately if any evaluation returns true. Optional arguments are passed to the predicate, p. The iterator is reset upon exiting. output(self) : returns the Array used to store the output result. Print(self,num,opts) : prints num iterations of self. The option showrank prints the rank of each iteration. Reset(self,rank) : reset self to the given rank. If the iterator does not have an Unrank method, or if rank is omitted, the iterator is reset to the first rank.
 Standard Methods Many of the iterators have the following methods. Number(self) : return the total number of iterations that the iterator would generate if it started from rank one. Rank(self,L) : compute the rank of L.  If L is not passed, use the current rank of self. Unrank(self,rnk) : return the output of self corresponding to rank rnk.
 Caveat Because these iterators use state variables that are local to the instantiated object, the same object cannot be reused as in iterator in a nested loop. Doing so will raise an error indicating that the iterator has already been started. To use two or more of these objects in nested loops, create duplicates using the Object function. See the example, below.

Examples

 > $\mathrm{with}\left(\mathrm{Iterator}\right):$

Construct an iterator that generates all permutations of the integers from one to five.

 > $P≔\mathrm{Permute}\left(5\right):$

Compute the number of iterations.

 > $\mathrm{Number}\left(P\right)$
 ${120}$ (1)

Test whether the sum of the first and last elements of any permutation equals three, then display the permutation and its rank that meet the criteria.

 > $\mathrm{oriter}\left(V→\mathrm{evalb}\left({V}_{1}+{V}_{-1}=3\right),P\right)$
 ${\mathrm{true}}$ (2)
 > $\mathrm{output}\left(P\right)$
 $\left[\begin{array}{ccccc}{1}& {3}& {4}& {5}& {2}\end{array}\right]$ (3)
 > $\mathrm{Rank}\left(P\right)$
 ${10}$ (4)

Verify all permutations sum to 15.

 > $\mathrm{anditer}\left(\left(V,s\right)→\mathrm{evalb}\left(\mathrm{add}\left(V\right)=s\right),P,15\right)$
 ${\mathrm{true}}$ (5)

Display the length of the output.

 > $\mathrm{length}\left(P\right)$
 ${5}$ (6)

Print the first 10 iterations, with rank.

 > $\mathrm{Print}\left(P,10,'\mathrm{showrank}'\right):$
 1: 1 2 3 4 5  2: 1 2 3 5 4  3: 1 2 4 3 5  4: 1 2 4 5 3  5: 1 2 5 3 4  6: 1 2 5 4 3  7: 1 3 2 4 5  8: 1 3 2 5 4  9: 1 3 4 2 5 10: 1 3 4 5 2

Using the same iterator in concurrent loops raises an error.

 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}p\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}P\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}q\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}P\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$

To avoid this, copy the object and use it in the inner loop. Because P stopped before completing due to the error, it needs to be reset.

 > $\mathrm{Reset}\left(P\right):$
 > $Q≔\mathrm{Object}\left(P\right):$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}p\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}P\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}q\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}Q\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$

Compatibility

 • The Details of the Iterator object command was updated in Maple 2021.