Iterator
MixedRadixTuples
generate mixed-radix tuples
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
MixedRadixTuples(radices, opts)
radices
-
list(nonnegint)
opts
(optional) equation(s) of the form option = value; specify options for the MixedRadixTuples command
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.
The MixedRadixTuples command returns an iterator that generates all tuples of a mixed-radix number.
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.
with⁡Iterator:
Construct an iterator that generates the tuples with the radices 2,4,3.
M≔MixedRadixTuples⁡2,4,3:
seq⁡v,v=M
000,100,010,110,020,120,030,130,001,101,011,111,021,121,031,131,002,102,012,112,022,122,032,132
Compute the number of iterations.
Number⁡M
24
Return the element with rank equal to 4.
Unrank⁡M,4
110
Copy the iterator, but start with rank equal to 4.
N≔Object⁡M,rank=4:
seq⁡v,v=N
110,020,120,030,130,001,101,011,111,021,121,031,131,002,102,012,112,022,122,032,132
Making Change
Given 7 pennies (1-cent coin), 5 nickels (5-cents), 3 dimes (10-cents), and 3 quarters (25-cents), list all ways to change 75 cents.
Assign a procedure that converts a Vector of the number of coins to a string. The contents of the Vector are the number of pennies, nickels, dimes, and quarters, in that order.
Change := proc(V) local i; sprintf("%d pennies + %d nickels " "+ %d dimes + %d quarters " "= 75 cents" , seq(i, i=V)); end proc:
Assign a predicate that returns true if the Vector V has the exact change.
is_exact≔V↦evalb⁡V1+5⋅V2+10⋅V3+25⋅V4=75:
Iterate through all all possibilities, accepting those that are exact, and transforming the Vector to the desired string.
iter≔MixedRadixTuples⁡7,5,3,3:
forchnginiterdoifis_exact⁡chngthenprintf⁡%s\n,Change⁡chngendifenddo:
5 pennies + 4 nickels + 0 dimes + 2 quarters = 75 cents 5 pennies + 2 nickels + 1 dimes + 2 quarters = 75 cents 0 pennies + 3 nickels + 1 dimes + 2 quarters = 75 cents 5 pennies + 0 nickels + 2 dimes + 2 quarters = 75 cents 0 pennies + 1 nickels + 2 dimes + 2 quarters = 75 cents
Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 2; generating all tuples and permutations, p. 2, sec. 7.2.1.1, generating all n-tuples, algorithm M, mixed-radix generation.
The Iterator[MixedRadixTuples] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
The Iterator[MixedRadixTuples] command was updated in Maple 2022.
The radices parameter was updated in Maple 2022.
See Also
convert[base]
Iterator[MixedRadixGrayCode]
Download Help Document