Overview of the Iterator package - Maple Programming Help

Home : Support : Online Help : Mathematics : Packages : Iterator

Overview of the Iterator package

 Calling Sequence Iterator[command](arguments) command(arguments)

Description

 • The Iterator package exports constructors of efficient iterators over discrete structures.
 • Each iterator is an object with a ModuleIterator method. It can be used in for loops and in seq, add, or mul commands.
 • To reduce memory usage the iterators use a mutable data structure, a one-dimensional Array, as the output.
 • All constructors provide a compile option that is true by default.  When true, the returned iterator is compiled.
 • See Iterator[Details] for details of an iterator object.

All Commands

The following commands are available.

Subpackages

The following subpackages are available.

 procedures for converting between permutations and inversion tables procedures for operating on mixed-radix tuples procedure for converting between tree representations

Combinations

 generate all (s,t)-combinations of zeros and ones in near-perfect order generate t-combinations of a set generate feasible ways to fill a rucksack generate combinations in the lexicographic revolving-door Gray code

Conversions

 subpackage for converting between permutations and inversion tables subpackage for converting between tree representations

Gray Codes

 generate n-bit binary Gray code generate all (s,t)-combinations of zeros and ones in near-perfect order generate a mixed-radix Gray code generate combinations in the lexicographic revolving-door Gray code

Necklaces

 generate Lyndon words that form a de Bruijn sequence generate Lyndon words generate necklaces generate prenecklaces

Parentheses

 generate positions of left-parentheses in pairs of nested parentheses generate pairs of nested parentheses

Partitions

 generate bounded compositions that sum to a constant generate multicombinations generate partitions of a multiset generate partitions of an integer generate fixed-size partitions of an integer generate partitions of an integer in part-count form generate fixed-size partitions of a set generate set partitions with restricted growth strings in Gray code order generate set partitions with restricted growth strings

Permutations

 generate permutations of a list generate permutations with restrictions

Products

 generate a Cartesian product of lists and sets create the product of iterators

Support

 compute the starting ranks and iterations suitable for parallelizing an iterator

Trees

 generate binary trees of a given size generate oriented forests of a given size subpackage for converting between tree representations

Example Index

 • Index of interesting examples. The link goes to the help page; look in its Examples section for the example.

 solve an alphametic puzzle (cryptarithm) compute number of contingency tables solve Dudney's century puzzle compute matrix permanent with Ryser's algorithm compute number of distinct ranks of a poker hand solve simplified Dudney's century puzzle split a list of floats into nearly equal sublists.  Demonstrates the creation and use of parallelized iterators. create a Young rectangle (specialization of a Young tableau)

Examples

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

Use Permute to construct an iterator over all permutations of the list [1,2,2,3].

 > $P≔\mathrm{Permute}\left(\left[1,2,2,3\right]\right):$

Use a for-loop to iterate over the permutations.

 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}p\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}P\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{printf}\left("%d\n",p\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 1 2 2 3 1 2 3 2 1 3 2 2 2 1 2 3 2 1 3 2 2 2 1 3 2 2 3 1 2 3 1 2 2 3 2 1 3 1 2 2 3 2 1 2 3 2 2 1

Use a seq command to create the entire sequence.

 > $\mathrm{seq}\left(p\left[\right],p=P\right)$
 $\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {3}& {2}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {3}& {2}& {2}\end{array}\right]{,}\left[\begin{array}{cccc}{2}& {1}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{2}& {1}& {3}& {2}\end{array}\right]{,}\left[\begin{array}{cccc}{2}& {2}& {1}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{2}& {2}& {3}& {1}\end{array}\right]{,}\left[\begin{array}{cccc}{2}& {3}& {1}& {2}\end{array}\right]{,}\left[\begin{array}{cccc}{2}& {3}& {2}& {1}\end{array}\right]{,}\left[\begin{array}{cccc}{3}& {1}& {2}& {2}\end{array}\right]{,}\left[\begin{array}{cccc}{3}& {2}& {1}& {2}\end{array}\right]{,}\left[\begin{array}{cccc}{3}& {2}& {2}& {1}\end{array}\right]$ (1)

Note the use of the square brackets, [], to instantiate the Vector that is assigned to p.  Without them, all values in the final expression sequence equal the last value because the p' evaluates to the Vector rather than its content.  Here is what happens when the square brackets are omitted.

 > $\mathrm{seq}\left(p,p=P\right)$
 $\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{1}& {2}& {2}& {3}\end{array}\right]$ (2)

Using hasNext and getNext

 • Use Combination to generate all triplets of the integers 0 to 4. Extract the two procedures, hasNext and getNext, from the ModuleIerator method of the iterator object and use them in a while-loop.
 > $M≔\mathrm{Combination}\left(5,3\right):$
 > $\mathrm{hasNext},\mathrm{getNext}≔\mathrm{ModuleIterator}\left(M\right):$
 > $\mathbf{while}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{hasNext}\left(\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(\mathrm{getNext}\left(\right)\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 $\left[\begin{array}{ccc}{0}& {1}& {2}\end{array}\right]$
 $\left[\begin{array}{ccc}{0}& {1}& {3}\end{array}\right]$
 $\left[\begin{array}{ccc}{0}& {2}& {3}\end{array}\right]$
 $\left[\begin{array}{ccc}{1}& {2}& {3}\end{array}\right]$
 $\left[\begin{array}{ccc}{0}& {1}& {4}\end{array}\right]$
 $\left[\begin{array}{ccc}{0}& {2}& {4}\end{array}\right]$
 $\left[\begin{array}{ccc}{1}& {2}& {4}\end{array}\right]$
 $\left[\begin{array}{ccc}{0}& {3}& {4}\end{array}\right]$
 $\left[\begin{array}{ccc}{1}& {3}& {4}\end{array}\right]$
 $\left[\begin{array}{ccc}{2}& {3}& {4}\end{array}\right]$ (3)

Concurrent iterators

Construct an iterator over the 2-permutations of the list $\left[1,1,2\right]$, use Object to create an identical, but independent, second iterator, and use both iterators in a dual-loop.

 > $P≔\mathrm{Permute}\left(\left[1,1,2\right],2\right):$
 > $Q≔\mathrm{Object}\left(P\right):$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}p\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}P\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}q\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}Q\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(p,q\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}:$
 $\left[\begin{array}{cc}{1}& {1}\end{array}\right]{,}\left[\begin{array}{cc}{1}& {1}\end{array}\right]$
 $\left[\begin{array}{cc}{1}& {1}\end{array}\right]{,}\left[\begin{array}{cc}{1}& {2}\end{array}\right]$
 $\left[\begin{array}{cc}{1}& {1}\end{array}\right]{,}\left[\begin{array}{cc}{2}& {1}\end{array}\right]$
 $\left[\begin{array}{cc}{1}& {2}\end{array}\right]{,}\left[\begin{array}{cc}{1}& {1}\end{array}\right]$
 $\left[\begin{array}{cc}{1}& {2}\end{array}\right]{,}\left[\begin{array}{cc}{1}& {2}\end{array}\right]$
 $\left[\begin{array}{cc}{1}& {2}\end{array}\right]{,}\left[\begin{array}{cc}{2}& {1}\end{array}\right]$
 $\left[\begin{array}{cc}{2}& {1}\end{array}\right]{,}\left[\begin{array}{cc}{1}& {1}\end{array}\right]$
 $\left[\begin{array}{cc}{2}& {1}\end{array}\right]{,}\left[\begin{array}{cc}{1}& {2}\end{array}\right]$
 $\left[\begin{array}{cc}{2}& {1}\end{array}\right]{,}\left[\begin{array}{cc}{2}& {1}\end{array}\right]$ (4)

Compatibility

 • The Iterator package was introduced in Maple 2016.