Cartesian Products

sage.combinat.cartesian_product.CartesianProduct(*iters)

Returns the combinatorial class of the Cartesian product of *iters.

EXAMPLES:

sage: cp = CartesianProduct([1,2], [3,4]); cp
Cartesian product of [1, 2], [3, 4]
sage: cp.list()
[[1, 3], [1, 4], [2, 3], [2, 4]]

Note that you must not use a generator-type object that is returned by a function (using “yield”). They cannot be copied or rewound (you cannot jump back to the beginning), but this is necessary to construct the cartesian product:

sage: def a(n): yield 1*n; yield 2*n
sage: def b(): yield 'a'; yield 'b'
sage: CartesianProduct(a(3), b()).list()
Traceback (most recent call last):
...
ValueError: generators are not allowed, see the
documentation (type "CartesianProduct?") for a workaround

You either create a list of all values or you use sage.combinat.misc.IterableFunctionCall to make a (copy-able) iterator:

sage: from sage.combinat.misc import IterableFunctionCall
sage: CartesianProduct(IterableFunctionCall(a, 3), IterableFunctionCall(b)).list()
[[3, 'a'], [3, 'b'], [6, 'a'], [6, 'b']]

See the documentation for IterableFunctionCall for more information.

class sage.combinat.cartesian_product.CartesianProduct_iters(*iters)

Bases: sage.combinat.combinat.CombinatorialClass

TESTS:

sage: import sage.combinat.cartesian_product as cartesian_product
sage: cp = cartesian_product.CartesianProduct_iters([1,2],[3,4]); cp
Cartesian product of [1, 2], [3, 4]
sage: loads(dumps(cp)) == cp
True
cardinality()

Returns the number of elements in the cartesian product of everything in *iters.

EXAMPLES:

sage: CartesianProduct(range(2), range(3)).cardinality()
6
sage: CartesianProduct(range(2), xrange(3)).cardinality()
6
sage: CartesianProduct(range(2), xrange(3), xrange(4)).cardinality()
24

This works correctly for infinite objects:

sage: CartesianProduct(ZZ, QQ).cardinality()
+Infinity
sage: CartesianProduct(ZZ, []).cardinality()
0
is_finite()

The cartesian product is finite if all of its inputs are finite, or if any input is empty.

EXAMPLES:

sage: CartesianProduct(ZZ, []).is_finite()
True
sage: CartesianProduct(4,4).is_finite()
Traceback (most recent call last):
...
ValueError: Unable to determine whether this product is finite
list()

Returns

EXAMPLES:

sage: CartesianProduct(range(3), range(3)).list()
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
sage: CartesianProduct('dog', 'cat').list()
[['d', 'c'],
 ['d', 'a'],
 ['d', 't'],
 ['o', 'c'],
 ['o', 'a'],
 ['o', 't'],
 ['g', 'c'],
 ['g', 'a'],
 ['g', 't']]
random_element()

Returns a random element from the cartesian product of *iters.

EXAMPLES:

sage: CartesianProduct('dog', 'cat').random_element()
['d', 'a']
unrank(x)

For finite cartesian products, we can reduce unrank to the constituent iterators.

EXAMPLES:

sage: C = CartesianProduct(xrange(1000), xrange(1000), xrange(1000))
sage: C[238792368]
[238, 792, 368]

Previous topic

Alternating Sign Matrices

Next topic

Combinations

This Page