# Cartesian products#

AUTHORS:

• Nicolas Thiery (2010-03): initial version

class sage.sets.cartesian_product.CartesianProduct(sets, category, flatten=False)#

A class implementing a raw data structure for Cartesian products of sets (and elements thereof). See cartesian_product for how to construct full fledged Cartesian products.

EXAMPLES:

sage: G = cartesian_product([GF(5), Permutations(10)])
sage: G.cartesian_factors()
(Finite Field of size 5, Standard permutations of 10)
sage: G.cardinality()
18144000
sage: G.random_element()    # random
(1, [4, 7, 6, 5, 10, 1, 3, 2, 8, 9])
sage: G.category()
Join of Category of finite monoids
and Category of Cartesian products of monoids
and Category of Cartesian products of finite enumerated sets

_cartesian_product_of_elements(elements)#

Return the Cartesian product of the given elements.

This implements Sets.CartesianProducts.ParentMethods._cartesian_product_of_elements(). INPUT:

• elements – an iterable (e.g. tuple, list) with one element of each Cartesian factor of self

Warning

This is meant as a fast low-level method. In particular, no coercion is attempted. When coercion or sanity checks are desirable, please use instead self(elements) or self._element_constructor_(elements).

EXAMPLES:

sage: S1 = Sets().example()
sage: S2 = InfiniteEnumeratedSets().example()
sage: C = cartesian_product([S2, S1, S2])
sage: C._cartesian_product_of_elements([S2.an_element(), S1.an_element(), S2.an_element()])
(42, 47, 42)

class Element#
cartesian_factors()#

Return the tuple of elements that compose this element.

EXAMPLES:

sage: A = cartesian_product([ZZ, RR])
sage: A((1, 1.23)).cartesian_factors()
(1, 1.23000000000000)
sage: type(_)
<... 'tuple'>

cartesian_projection(i)#

Return the projection of self on the $$i$$-th factor of the Cartesian product, as per Sets.CartesianProducts.ElementMethods.cartesian_projection().

INPUT:

• i – the index of a factor of the Cartesian product

EXAMPLES:

sage: C = Sets().CartesianProducts().example(); C
The Cartesian product of (Set of prime numbers (basic implementation), An example of an infinite enumerated set: the non negative integers, An example of a finite enumerated set: {1,2,3})
sage: x = C.an_element(); x
(47, 42, 1)
sage: x.cartesian_projection(1)
42

wrapped_class#

alias of tuple

an_element()#

EXAMPLES:

sage: C = Sets().CartesianProducts().example(); C
The Cartesian product of (Set of prime numbers (basic implementation),
An example of an infinite enumerated set: the non negative integers,
An example of a finite enumerated set: {1,2,3})
sage: C.an_element()
(47, 42, 1)

cartesian_factors()#

Return the Cartesian factors of self.

EXAMPLES:

sage: cartesian_product([QQ, ZZ, ZZ]).cartesian_factors()
(Rational Field, Integer Ring, Integer Ring)

cartesian_projection(i)#

Return the natural projection onto the $$i$$-th Cartesian factor of self as per Sets.CartesianProducts.ParentMethods.cartesian_projection().

INPUT:

• i – the index of a Cartesian factor of self

EXAMPLES:

sage: C = Sets().CartesianProducts().example(); C
The Cartesian product of (Set of prime numbers (basic implementation), An example of an infinite enumerated set: the non negative integers, An example of a finite enumerated set: {1,2,3})
sage: x = C.an_element(); x
(47, 42, 1)
sage: pi = C.cartesian_projection(1)
sage: pi(x)
42

sage: C.cartesian_projection('hey')
Traceback (most recent call last):
...
ValueError: i (=hey) must be in {0, 1, 2}

construction()#

Return the construction functor and its arguments for this Cartesian product.

OUTPUT:

A pair whose first entry is a Cartesian product functor and its second entry is a list of the Cartesian factors.

EXAMPLES:

sage: cartesian_product([ZZ, QQ]).construction()
(The cartesian_product functorial construction,
(Integer Ring, Rational Field))