Cartesian Product - Maple Help

Online Help

All Products    Maple    MapleSim


Iterator

  

CartesianProduct

  

generate a Cartesian product of lists and sets

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

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

• 

compile = truefalse

  

True means compile the iterator. The default is true.

• 

rank = nonnegint

  

Specify the starting rank of the iterator. The default is one. 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. For efficiency, each list/set must have at least two elements, otherwise an error is raised.

• 

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

withIterator:

Construct an iterator that generates the Cartesian product of a list and two sets.

MCartesianProduct1,2,2,a,b,c,d,e:

Print the elements

PrintM,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.

seqv,v=M

1ac,2ac,2ac,2bc,2bc,1bc,1bd,2bd,2bd,2ad,2ad,1ad,1ae,2ae,2ae,2be,2be,1be

(1)

Compute the number of iterations.

NumberM

18

(2)

Return the element with rank equal to 4.

UnrankM,4

2bc

(3)

Copy the iterator, but start with rank equal to 4.

NObjectM,rank=4:

seqv,v=N

2bc,2bc,1bc,1bd,2bd,2bd,2ad,2ad,1ad,1ae,2ae,2ae,2be,2be,1be

(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.

• 

For more information on Maple 2016 changes, see Updates in Maple 2016.

See Also

combinat[cartprod]

Iterator