|
Calling Sequence
|
|
CartesianProduct(L, opts)
|
|
Parameters
|
|
L
|
-
|
seq({list,set}); lists and sets
|
opts
|
-
|
(optional) equation(s) of the form option = value; specify options for the CartesianProduct command
|
|
|
|
|
Options
|
|
|
True means compile the iterator. The default is true.
|
|
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 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.
|
|
|
|
Examples
|
|
Construct an iterator that generates the Cartesian product of a list and two sets.
>
|
|
Print the elements
>
|
|
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.
| (1) |
Compute the number of iterations.
Return the element with rank equal to 4.
Copy the iterator, but start with rank equal to 4.
>
|
|
| (4) |
|
|
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[CartesianProduct] command was introduced in Maple 2016.
|
•
|
The Iterator[CartesianProduct] command was updated in Maple 2022.
|
•
|
The L parameter was updated in Maple 2022.
|
|
|
|