Module: sage.combinat.cartesian_product
Cartesian Products
Module-level Functions
) |
Returns the combinatorial class of the cartesian product of *iters.
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(): yield 1; yield 2 sage: def b(): yield 'a'; yield 'b' sage: CartesianProduct(a(), b()).list() [[1, 'a'], [1, 'b']] sage: from sage.combinat.misc import IterableFunctionCall sage: CartesianProduct(IterableFunctionCall(a), IterableFunctionCall(b)).list() [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
Class: CartesianProduct_iters
self) |
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
Functions: count,
iterator,
list,
random_element
self) |
Returns the number of elements in the cartesian product of everything in *iters.
sage: CartesianProduct(range(2), range(3)).count() 6 sage: CartesianProduct(range(2), xrange(3)).count() 6 sage: CartesianProduct(range(2), xrange(3), xrange(4)).count() 24
self) |
An iterator for the elements in the cartesian product of the iterables *iters.
From Recipe 19.9 in the Python Cookbook by Alex Martelli and David Ascher.
sage: [e for e in CartesianProduct(range(3), range(3))] [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]] sage: [e for e in CartesianProduct('dog', 'cat')] [['d', 'c'], ['d', 'a'], ['d', 't'], ['o', 'c'], ['o', 'a'], ['o', 't'], ['g', 'c'], ['g', 'a'], ['g', 't']]
self) |
Returns
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']]
self) |
Returns a random element from the cartesian product of *iters.
sage: CartesianProduct('dog', 'cat').random_element() ['d', 'a']
Special Functions: __contains__,
__init__,
__repr__
self, x) |
sage: cp = CartesianProduct([1,2],[3,4]) sage: [1,3] in cp True sage: [1,2] in cp False sage: [1, 3, 1] in cp False
self) |
sage: CartesianProduct(range(2), range(3)) Cartesian product of [0, 1], [0, 1, 2]