Cartesian Products#
- class sage.combinat.cartesian_product.CartesianProduct_iters(*iters)#
Bases:
EnumeratedSetFromIterator
Cartesian product of finite sets.
This class will soon be deprecated (see github issue #18411 and github issue #19195). One should instead use the functorial construction
cartesian_product
. The main differences in behavior are:construction:
CartesianProduct
takes as many argument as there are factors whereascartesian_product
takes a single list (or iterable) of factors;representation of elements: elements are represented by plain Python list for
CartesianProduct
versus a custom element class forcartesian_product
;membership testing: because of the above, plain Python lists are not considered as elements of a
cartesian_product
.
All of these is illustrated in the examples below.
EXAMPLES:
sage: F1 = ['a', 'b'] sage: F2 = [1, 2, 3, 4] sage: F3 = Permutations(3) # optional - sage.combinat sage: from sage.combinat.cartesian_product import CartesianProduct_iters sage: C = CartesianProduct_iters(F1, F2, F3) # optional - sage.combinat sage: c = cartesian_product([F1, F2, F3]) # optional - sage.combinat sage: type(C.an_element()) # optional - sage.combinat <class 'list'> sage: type(c.an_element()) # optional - sage.combinat <class 'sage.sets.cartesian_product.CartesianProduct_with_category.element_class'> sage: l = ['a', 1, Permutation([3,2,1])] # optional - sage.combinat sage: l in C # optional - sage.combinat True sage: l in c # optional - sage.combinat False sage: elt = c(l) # optional - sage.combinat sage: elt # optional - sage.combinat ('a', 1, [3, 2, 1]) sage: elt in c # optional - sage.combinat True sage: elt.parent() is c # optional - sage.combinat True
- cardinality()#
Returns the number of elements in the Cartesian product of everything in *iters.
EXAMPLES:
sage: from sage.combinat.cartesian_product import CartesianProduct_iters sage: CartesianProduct_iters(range(2), range(3)).cardinality() 6 sage: CartesianProduct_iters(range(2), range(3)).cardinality() 6 sage: CartesianProduct_iters(range(2), range(3), range(4)).cardinality() 24
This works correctly for infinite objects:
sage: CartesianProduct_iters(ZZ, QQ).cardinality() +Infinity sage: CartesianProduct_iters(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: from sage.combinat.cartesian_product import CartesianProduct_iters sage: CartesianProduct_iters(ZZ, []).is_finite() True sage: CartesianProduct_iters(4,4).is_finite() Traceback (most recent call last): ... ValueError: unable to determine whether this product is finite
- list()#
Returns
EXAMPLES:
sage: from sage.combinat.cartesian_product import CartesianProduct_iters sage: CartesianProduct_iters(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_iters('dog', 'cat').list() [['d', 'c'], ['d', 'a'], ['d', 't'], ['o', 'c'], ['o', 'a'], ['o', 't'], ['g', 'c'], ['g', 'a'], ['g', 't']]
- random_element()#
Return a random element from the Cartesian product of *iters.
EXAMPLES:
sage: from sage.combinat.cartesian_product import CartesianProduct_iters sage: c = CartesianProduct_iters('dog', 'cat').random_element() sage: c in CartesianProduct_iters('dog', 'cat') True
- unrank(x)#
For finite Cartesian products, we can reduce unrank to the constituent iterators.
EXAMPLES:
sage: from sage.combinat.cartesian_product import CartesianProduct_iters sage: C = CartesianProduct_iters(range(1000), range(1000), range(1000)) sage: C[238792368] [238, 792, 368]
Check for github issue #15919:
sage: FF = IntegerModRing(29) sage: C = CartesianProduct_iters(FF, FF, FF) sage: C.unrank(0) [0, 0, 0]