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 if you have a generator-type object that is returned by a function, then you should use IterableFunctionCall class defined in sage.combinat.misc.

  sage: def a(n): yield 1*n; yield 2*n
  sage: def b(): yield 'a'; yield 'b'
  sage: CartesianProduct(a(3), b()).list()
  [[3, 'a'], [3, 'b']]
  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

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
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']

Previous topic

Alternating Sign Matrices

Next topic

Combinations

This Page