Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Cartesian products
AUTHORS:
- Nicolas Thiery (2010-03): initial version """ #***************************************************************************** # Copyright (C) 2008 Nicolas Thiery <nthiery at users.sf.net>, # Mike Hansen <mhansen@gmail.com>, # Florent Hivert <Florent.Hivert@univ-rouen.fr> # # Distributed under the terms of the GNU General Public License (GPL) # http://www.gnu.org/licenses/ #*****************************************************************************
""" A class implementing a raw data structure for Cartesian products of sets (and elements thereof). See :obj:`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
.. automethod:: _cartesian_product_of_elements """ r""" INPUT:
- ``sets`` -- a tuple of parents - ``category`` -- a subcategory of ``Sets().CartesianProducts()`` - ``flatten`` -- a boolean (default: ``False``)
``flatten`` is current ignored, and reserved for future use.
No other keyword arguments (``kwargs``) are accepted.
TESTS::
sage: from sage.sets.cartesian_product import CartesianProduct sage: C = CartesianProduct((QQ, ZZ, ZZ), category = Sets().CartesianProducts()) sage: C The Cartesian product of (Rational Field, Integer Ring, Integer Ring) sage: C.an_element() (1/2, 1, 1) sage: TestSuite(C).run() sage: cartesian_product([ZZ, ZZ], blub=None) Traceback (most recent call last): ... TypeError: __init__() got an unexpected keyword argument 'blub' """
r""" Construct an element of a Cartesian product from a list or iterable
INPUT:
- ``x`` -- a list (or iterable)
Each component of `x` is converted to the corresponding Cartesian factor.
EXAMPLES::
sage: C = cartesian_product([GF(5), GF(3)]) sage: x = C((1,3)); x (1, 0) sage: x.parent() The Cartesian product of (Finite Field of size 5, Finite Field of size 3) sage: x[0].parent() Finite Field of size 5 sage: x[1].parent() Finite Field of size 3
An iterable is also accepted as input::
sage: C(i for i in range(2)) (0, 1)
TESTS::
sage: C((1,3,4)) Traceback (most recent call last): ... ValueError: (1, 3, 4) should be of length 2 """ "{} should be of length {}".format(x, len(self._sets)))
""" EXAMPLES::
sage: cartesian_product([QQ, ZZ, ZZ]) # indirect doctest The Cartesian product of (Rational Field, Integer Ring, Integer Ring) """
""" Check if ``x`` is contained in ``self``.
EXAMPLES::
sage: C = cartesian_product([list(range(5)), list(range(5))]) sage: (1, 1) in C True sage: (1, 6) in C False """ and all(elt in self._sets[i] for i,elt in enumerate(x)) )
""" Return the Cartesian factors of ``self``.
.. SEEALSO::
:meth:`Sets.CartesianProducts.ParentMethods.cartesian_factors() <sage.categories.sets_cat.Sets.CartesianProducts.ParentMethods.cartesian_factors>`.
EXAMPLES::
sage: cartesian_product([QQ, ZZ, ZZ]).cartesian_factors() (Rational Field, Integer Ring, Integer Ring) """
""" Return the indices of the Cartesian factors of ``self`` as per :meth:`Sets.CartesianProducts.ParentMethods._sets_keys() <sage.categories.sets_cat.Sets.CartesianProducts.ParentMethods._sets_keys>`.
EXAMPLES::
sage: cartesian_product([QQ, ZZ, ZZ])._sets_keys() {0, 1, 2} sage: cartesian_product([ZZ]*100)._sets_keys() {0, ..., 99} """
def cartesian_projection(self, i): """ Return the natural projection onto the `i`-th Cartesian factor of ``self`` as per :meth:`Sets.CartesianProducts.ParentMethods.cartesian_projection() <sage.categories.sets_cat.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} """
""" Return the Cartesian product of the given ``elements``.
This implements :meth:`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) """
r""" 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)) """
r""" Return ``True`` if ``S`` coerces into this Cartesian product.
TESTS::
sage: Z = cartesian_product([ZZ]) sage: Q = cartesian_product([QQ]) sage: Z.has_coerce_map_from(Q) # indirect doctest False sage: Q.has_coerce_map_from(Z) # indirect doctest True """
r""" Return the projection of ``self`` on the `i`-th factor of the Cartesian product, as per :meth:`Sets.CartesianProducts.ElementMethods.cartesian_projection() <sage.categories.sets_cat.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
sage: x.summand_projection(1) doctest:...: DeprecationWarning: summand_projection is deprecated. Please use cartesian_projection instead. See http://trac.sagemath.org/10963 for details. 42 """
r""" Iterate over the components of 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 = C.an_element(); c (47, 42, 1) sage: for i in c: ....: print(i) 47 42 1 """
r""" Return the number of factors in the cartesian product from which ``self`` comes.
EXAMPLES::
sage: C = cartesian_product([ZZ, QQ, CC]) sage: e = C.random_element() sage: len(e) 3 """
r""" 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'> """
|