Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/sets/cartesian_product.py
4072 views
1
"""
2
Cartesian products
3
4
AUTHORS:
5
6
- Nicolas Thiery (2010-03): initial version
7
8
"""
9
#*****************************************************************************
10
# Copyright (C) 2008 Nicolas Thiery <nthiery at users.sf.net>,
11
# Mike Hansen <[email protected]>,
12
# Florent Hivert <[email protected]>
13
#
14
# Distributed under the terms of the GNU General Public License (GPL)
15
# http://www.gnu.org/licenses/
16
#*****************************************************************************
17
from sage.misc.misc import attrcall
18
from sage.misc.cachefunc import cached_method
19
from sage.categories.sets_cat import Sets
20
from sage.structure.parent import Parent
21
from sage.structure.unique_representation import UniqueRepresentation
22
23
class CartesianProduct(UniqueRepresentation, Parent):
24
"""
25
A class implementing a raw data structure for cartesian products
26
of sets (and elements thereof). See
27
:const:`~sage.categories.cartesian_product.cartesian_product` for
28
how to construct full fledge cartesian products.
29
"""
30
31
def __init__(self, sets, category, flatten = False):
32
r"""
33
INPUT:
34
35
- ``sets`` -- a tuple of parents
36
- ``category`` -- a subcategory of ``Sets().CartesianProducts()``
37
- ``flatten`` -- a boolean (default: ``False``)
38
39
``flatten`` is current ignored, and reserved for future use.
40
41
TESTS::
42
43
sage: from sage.sets.cartesian_product import CartesianProduct
44
sage: C = CartesianProduct((QQ, ZZ, ZZ), category = Sets().CartesianProducts())
45
sage: C
46
The cartesian product of (Rational Field, Integer Ring, Integer Ring)
47
sage: C.an_element()
48
(1/2, 1, 1)
49
sage: TestSuite(C).run()
50
"""
51
self._sets = sets
52
Parent.__init__(self, category = category)
53
54
def _repr_(self):
55
"""
56
EXAMPLES::
57
58
sage: cartesian_product([QQ, ZZ, ZZ]) # indirect doctest
59
The cartesian product of (Rational Field, Integer Ring, Integer Ring)
60
"""
61
return "The cartesian product of %s"%(self._sets,)
62
63
def _sets_keys(self):
64
"""
65
Returns the indices of the summands of ``self``
66
as per
67
:meth:`Sets.CartesianProducts.ParentMethods._sets_keys()
68
<sage.categories.sets_cat.Sets.CartesianProducts.ParentMethods._sets_keys>`.
69
70
EXAMPLES::
71
72
sage: cartesian_product([QQ, ZZ, ZZ])._sets_keys()
73
[0, 1, 2]
74
"""
75
return range(len(self._sets))
76
77
@cached_method
78
def summand_projection(self, i):
79
"""
80
Returns the natural projection onto the `i`-th summand of self
81
as per
82
:meth:`Sets.CartesianProducts.ParentMethods.summand_projection()
83
<sage.categories.sets_cat.Sets.CartesianProducts.ParentMethods.summand_projection>`.
84
85
INPUTS:
86
87
- ``i`` -- the index of a summand of self
88
89
EXAMPLES::
90
91
sage: C = Sets().CartesianProducts().example(); C
92
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})
93
sage: x = C.an_element(); x
94
(47, 42, 1)
95
sage: pi = C.summand_projection(1)
96
sage: pi(x)
97
42
98
"""
99
assert i in self._sets_keys()
100
return attrcall("summand_projection", i)
101
102
def _cartesian_product_of_elements(self, elements):
103
"""
104
Returns the cartesian product of the given ``elements``,
105
as per
106
:meth:`Sets.CartesianProducts.ParentMethods._cartesian_product_of_elements()`.
107
<sage.categories.sets_cat.Sets.CartesianProducts.ParentMethods._cartesian_product_of_elements>`.
108
109
INPUT:
110
111
- ``elements`` - a tuple with one element of each summand of self
112
113
EXAMPLES::
114
115
sage: S1 = Sets().example()
116
sage: S2 = InfiniteEnumeratedSets().example()
117
sage: C = cartesian_product([S2, S1, S2])
118
sage: C._cartesian_product_of_elements([S2.an_element(), S1.an_element(), S2.an_element()])
119
(42, 47, 42)
120
"""
121
elements = tuple(elements)
122
assert len(elements) == len(self._sets)
123
# assert all(zip(self._sets[i], elements, operator.__contains__))
124
return self(tuple(elements))
125
126
an_element = Sets.CartesianProducts.ParentMethods.an_element
127
128
from sage.structure.element_wrapper import ElementWrapper
129
class Element(ElementWrapper):
130
131
def summand_projection(self, i):
132
"""
133
Returns the projection of ``self`` on the `i`-th
134
summand of the cartesian product, as per
135
:meth:`Sets.CartesianProducts.ElementMethods.summand_projection()
136
<sage.categories.sets_cat.Sets.CartesianProducts.ElementMethods.summand_projection>`.
137
138
INPUTS:
139
140
- ``i`` -- the index of a summand of the cartesian product
141
142
EXAMPLES::
143
144
sage: C = Sets().CartesianProducts().example(); C
145
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})
146
sage: x = C.an_element(); x
147
(47, 42, 1)
148
sage: x.summand_projection(1)
149
42
150
"""
151
return self.value[i]
152
153
wrapped_class = tuple
154
155