Cartesian products#

AUTHORS:

  • Nicolas Thiery (2010-03): initial version

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

Bases: UniqueRepresentation, Parent

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
>>> from sage.all import *
>>> G = cartesian_product([GF(Integer(5)), Permutations(Integer(10))])
>>> G.cartesian_factors()
(Finite Field of size 5, Standard permutations of 10)
>>> G.cardinality()
18144000
>>> G.random_element()    # random
(1, [4, 7, 6, 5, 10, 1, 3, 2, 8, 9])
>>> 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)[source]#

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)
>>> from sage.all import *
>>> S1 = Sets().example()
>>> S2 = InfiniteEnumeratedSets().example()
>>> C = cartesian_product([S2, S1, S2])
>>> C._cartesian_product_of_elements([S2.an_element(), S1.an_element(), S2.an_element()])
(42, 47, 42)
class Element[source]#

Bases: ElementWrapperCheckWrappedClass

cartesian_factors()[source]#

Return the tuple of elements that compose this element.

EXAMPLES:

sage: A = cartesian_product([ZZ, RR])
sage: A((1, 1.23)).cartesian_factors()                                  # needs sage.rings.real_mpfr
(1, 1.23000000000000)
sage: type(_)
<... 'tuple'>
>>> from sage.all import *
>>> A = cartesian_product([ZZ, RR])
>>> A((Integer(1), RealNumber('1.23'))).cartesian_factors()                                  # needs sage.rings.real_mpfr
(1, 1.23000000000000)
>>> type(_)
<... 'tuple'>
cartesian_projection(i)[source]#

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
>>> from sage.all import *
>>> 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})
>>> x = C.an_element(); x
(47, 42, 1)
>>> x.cartesian_projection(Integer(1))
42
wrapped_class#

alias of tuple

an_element()[source]#

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)
>>> from sage.all import *
>>> 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})
>>> C.an_element()
(47, 42, 1)
cartesian_factors()[source]#

Return the Cartesian factors of self.

EXAMPLES:

sage: cartesian_product([QQ, ZZ, ZZ]).cartesian_factors()
(Rational Field, Integer Ring, Integer Ring)
>>> from sage.all import *
>>> cartesian_product([QQ, ZZ, ZZ]).cartesian_factors()
(Rational Field, Integer Ring, Integer Ring)
cartesian_projection(i)[source]#

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}
>>> from sage.all import *
>>> 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})
>>> x = C.an_element(); x
(47, 42, 1)
>>> pi = C.cartesian_projection(Integer(1))
>>> pi(x)
42

>>> C.cartesian_projection('hey')
Traceback (most recent call last):
...
ValueError: i (=hey) must be in {0, 1, 2}
construction()[source]#

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))
>>> from sage.all import *
>>> cartesian_product([ZZ, QQ]).construction()
(The cartesian_product functorial construction,
 (Integer Ring, Rational Field))