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 nonnegative 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 nonnegative 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 nonnegative 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 nonnegative 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 nonnegative 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 nonnegative 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))
random_element(*args)[source]

Return a random element of this Cartesian product.

The extra arguments are passed down to each of the factors of the Cartesian product.

EXAMPLES:

sage: C = cartesian_product([Permutations(10)]*5)
sage: C.random_element()           # random
([2, 9, 4, 7, 1, 8, 6, 10, 5, 3],
 [8, 6, 5, 7, 1, 4, 9, 3, 10, 2],
 [5, 10, 3, 8, 2, 9, 1, 4, 7, 6],
 [9, 6, 10, 3, 2, 1, 5, 8, 7, 4],
 [8, 5, 2, 9, 10, 3, 7, 1, 4, 6])

sage: C = cartesian_product([ZZ]*10)
sage: c1 = C.random_element()
sage: c1                   # random
(3, 1, 4, 1, 1, -3, 0, -4, -17, 2)
sage: c2 = C.random_element(4,7)
sage: c2                   # random
(6, 5, 6, 4, 5, 6, 6, 4, 5, 5)
sage: all(4 <= i <= 7 for i in c2)
True
>>> from sage.all import *
>>> C = cartesian_product([Permutations(Integer(10))]*Integer(5))
>>> C.random_element()           # random
([2, 9, 4, 7, 1, 8, 6, 10, 5, 3],
 [8, 6, 5, 7, 1, 4, 9, 3, 10, 2],
 [5, 10, 3, 8, 2, 9, 1, 4, 7, 6],
 [9, 6, 10, 3, 2, 1, 5, 8, 7, 4],
 [8, 5, 2, 9, 10, 3, 7, 1, 4, 6])

>>> C = cartesian_product([ZZ]*Integer(10))
>>> c1 = C.random_element()
>>> c1                   # random
(3, 1, 4, 1, 1, -3, 0, -4, -17, 2)
>>> c2 = C.random_element(Integer(4),Integer(7))
>>> c2                   # random
(6, 5, 6, 4, 5, 6, 6, 4, 5, 5)
>>> all(Integer(4) <= i <= Integer(7) for i in c2)
True