Iterator
CartesianProduct
generate a Cartesian product of lists and sets
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
CartesianProduct(L, opts)
L
-
seq({list,set}); lists and sets
opts
(optional) equation(s) of the form option = value; specify options for the CartesianProduct 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 CartesianProduct command returns an iterator that generates the Cartesian product of a sequence of lists and sets. The k-th element of the output Array contains an element from the k-th set/list in the input.
The L parameter is a sequence of lists and sets.
If L contains an empty list or set, the iterator returns nothing.
If L is the empty sequence the iterator returns an empty Array.
Internally this iterator uses a loopless, reflected, mixed-radix Gray code for the iterator; see MixedRadixGrayCode.
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 Cartesian product of a list and two sets.
M≔CartesianProduct⁡1,2,2,a,b,c,d,e:
Print the elements
Print⁡M,showrank:
1: 1 a c 2: 2 a c 3: 2 a c 4: 2 b c 5: 2 b c 6: 1 b c 7: 1 b d 8: 2 b d 9: 2 b d 10: 2 a d 11: 2 a d 12: 1 a d 13: 1 a e 14: 2 a e 15: 2 a e 16: 2 b e 17: 2 b e 18: 1 b e
Generate the entire sequence.
seq⁡v,v=M
1ac,2ac,2ac,2bc,2bc,1bc,1bd,2bd,2bd,2ad,2ad,1ad,1ae,2ae,2ae,2be,2be,1be
Compute the number of iterations.
Number⁡M
18
Return the element with rank equal to 4.
Unrank⁡M,4
2bc
Copy the iterator, but start with rank equal to 4.
N≔Object⁡M,rank=4:
seq⁡v,v=N
2bc,2bc,1bc,1bd,2bd,2bd,2ad,2ad,1ad,1ae,2ae,2ae,2be,2be,1be
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.
The Iterator[CartesianProduct] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
The Iterator[CartesianProduct] command was updated in Maple 2022.
The L parameter was updated in Maple 2022.
See Also
combinat[cartprod]
Download Help Document