 MixedRadixGrayCode - Maple Help

Iterator Calling Sequence MixedRadixGrayCode(radices, opts) Parameters

 radices - list(nonnegint) opts - (optional) equation(s) of the form option = value; specify options for the MixedRadixGrayCode command Options

 • append_change = truefalse
 True means append an integer indicating the change to the output Array. The magnitude of the integer is the index that changed, the sign is the direction of the change. The default is false.
 • compile = truefalse
 True means compile the iterator. The default is true.
 • rank = nonnegint
 Specify the starting rank of the iterator. The default is one. Passing a value greater than one causes the iterator to skip the lower ranks; this can be useful when parallelizing iterators. The starting rank reverts to one when the iterator is reset, reused, or copied. Description

 • The MixedRadixGrayCode command returns an iterator that generates a mixed-radix Gray code.
 • The radices parameter is a list of positive integers that specify the radices of the number. The k-th integer is the radix at the k-th index. Methods

In addition to the common iterator methods, this iterator object has the following methods. The self parameter is the iterator object.

 • Number(self): return the number of iterations required to step through the iterator, assuming it started at rank one.
 • Rank(self,L): return the rank of the current iteration. Optionally pass L, a list or one-dimensional rtable, and return its rank.
 • Unrank(self,rnk): return a one-dimensional Array corresponding to the iterator output with rank rnk. Examples

 > $\mathrm{with}\left(\mathrm{Iterator}\right):$
 > $M≔\mathrm{MixedRadixGrayCode}\left(\left[2,4,3\right]\right):$
 > $\mathrm{seq}\left({m}_{[]},m=M\right)$
 $\left[\begin{array}{ccc}{0}& {0}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {0}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {1}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {1}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {1}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {1}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {0}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {0}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {0}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {0}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {1}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {1}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {2}\end{array}\right]$ (1)

Compute the number of iterations.

 > $\mathrm{Number}\left(M\right)$
 ${24}$ (2)

Return the element with rank equal to 4.

 > $\mathrm{Unrank}\left(M,4\right)$
 $\left[\begin{array}{ccc}{0}& {1}& {0}\end{array}\right]$ (3)

 > $N≔\mathrm{Object}\left(M,\mathrm{rank}=4\right):$
 > $\mathrm{seq}\left({v}_{[]},v=N\right)$
 $\left[\begin{array}{ccc}{0}& {1}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {1}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {1}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {0}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {0}& {1}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {0}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {0}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {1}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {1}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {2}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {2}\end{array}\right]$ (4)

Create a new iterator and use the append_change option to include the index that changed.

 > $N≔\mathrm{MixedRadixGrayCode}\left(\left[3,4\right],\mathrm{append_change}\right):$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}V\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}N\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{printf}\left("%d : %d",{V}_{1..2},{V}_{3}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$
 0 0 : 0 1 0 : 1 2 0 : 1 2 1 : 2 1 1 : -1 0 1 : -1 0 2 : 2 1 2 : 1 2 2 : 1 2 3 : 2 1 3 : -1 0 3 : -1

Generate all factors of an integer, such that the ratio of adjacent factors is a prime or the inverse of a prime.

 > facts := proc(n :: posint) local F,T,m,iter,p; uses Iterator;     # Factor n into the form [[p1,e1],...,[pm,em]],     # where n = p1^e1*...*pm^em.     F := op(2,ifactors(n));     m := numelems(F);     # Assign a procedure that converts a Vector (of exponents)     # to an integer, using F.     T := proc(V)     local i;         mul(F[i,1]^V[i],i=1..m);     end proc;     # Iterate through all factors.     seq(T(p), p=MixedRadixGrayCode([seq(f+1,f=F)])); end proc:
 > $\mathrm{facts}\left(72\right)$
 ${1}{,}{2}{,}{4}{,}{8}{,}{24}{,}{12}{,}{6}{,}{3}{,}{9}{,}{18}{,}{36}{,}{72}$ (5) References

 Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 2; generating all tuples and permutations, sec. 7.2.1.1, Generating all n-tuples, algorithm  H, loopless reflected mixed-radix Gray generation, p. 20. Compatibility

 • The Iterator[MixedRadixGrayCode] command was introduced in Maple 2016.