Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/sets/non_negative_integers.py
8817 views
1
"""
2
Non Negative Integers
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2009 Florent Hivert <[email protected]>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#*****************************************************************************
10
11
from sage.structure.parent import Parent
12
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
13
from sage.structure.unique_representation import UniqueRepresentation
14
from sage.rings.integer import Integer
15
16
class NonNegativeIntegers(UniqueRepresentation, Parent):
17
r"""
18
The enumerated set of non negative integers.
19
20
This class implements the set of non negative integers, as an
21
enumerated set (see :class:`InfiniteEnumeratedSets
22
<sage.categories.infinite_enumerated_sets.InfiniteEnumeratedSets>`).
23
24
EXAMPLES::
25
26
sage: NN = NonNegativeIntegers()
27
sage: NN
28
Non negative integers
29
sage: NN.cardinality()
30
+Infinity
31
sage: TestSuite(NN).run()
32
sage: NN.list()
33
Traceback (most recent call last):
34
...
35
NotImplementedError: infinite list
36
sage: NN.element_class
37
<type 'sage.rings.integer.Integer'>
38
sage: it = iter(NN)
39
sage: [it.next(), it.next(), it.next(), it.next(), it.next()]
40
[0, 1, 2, 3, 4]
41
sage: NN.first()
42
0
43
44
Currently, this is just a "facade" parent; namely its elements are
45
plain Sage ``Integers`` with ``Integer Ring`` as parent::
46
47
sage: x = NN(15); type(x)
48
<type 'sage.rings.integer.Integer'>
49
sage: x.parent()
50
Integer Ring
51
sage: x+3
52
18
53
54
In a later version, there will be an option to specify whether the
55
elements should have ``Integer Ring`` or ``Non negative integers``
56
as parent::
57
58
sage: NN = NonNegativeIntegers(facade = False) # todo: not implemented
59
sage: x = NN(5) # todo: not implemented
60
sage: x.parent() # todo: not implemented
61
Non negative integers
62
63
This runs generic sanity checks on ``NN``::
64
65
sage: TestSuite(NN).run()
66
67
TODO: do not use ``NN`` any more in the doctests for
68
``NonNegativeIntegers``.
69
"""
70
71
def __init__(self, category=None):
72
"""
73
TESTS::
74
75
sage: NN = NonNegativeIntegers()
76
sage: NN
77
Non negative integers
78
sage: NN.category()
79
Category of facade infinite enumerated sets
80
sage: TestSuite(NN).run()
81
"""
82
from sage.rings.integer_ring import ZZ
83
Parent.__init__(self, facade = ZZ, category = InfiniteEnumeratedSets().or_subcategory(category) )
84
85
def _repr_(self):
86
"""
87
TESTS::
88
89
sage: NonNegativeIntegers() # indirect doctest
90
Non negative integers
91
"""
92
return "Non negative integers"
93
94
def __contains__(self, elt):
95
"""
96
EXAMPLES::
97
98
sage: NN = NonNegativeIntegers()
99
sage: 1 in NN
100
True
101
sage: -1 in NN
102
False
103
sage: x in NN
104
False
105
sage: None in NN
106
False
107
"""
108
try:
109
i = Integer(elt)
110
return i >= Integer(0) and i == elt
111
except TypeError:
112
return False
113
114
def _element_constructor_(self, i):
115
"""
116
Constructs an element of self from an integer, testing that
117
this integer is indeed non negative.
118
119
EXAMPLES::
120
121
sage: NN = NonNegativeIntegers()
122
sage: NN._element_constructor_(42)
123
42
124
sage: NN._element_constructor_(-5)
125
Traceback (most recent call last):
126
...
127
ValueError: Value -5 in not in Non negative integers.
128
sage: NN._element_constructor_(x)
129
Traceback (most recent call last):
130
...
131
ValueError: Value x in not in Non negative integers.
132
133
This is used upon coercion attempts::
134
135
sage: n = NN(42); n # indirect doctest
136
42
137
sage: type(n)
138
<type 'sage.rings.integer.Integer'>
139
sage: n.parent()
140
Integer Ring
141
sage: NN(-1)
142
Traceback (most recent call last):
143
...
144
ValueError: Value -1 in not in Non negative integers.
145
146
For fast construction of elements without tests, please use
147
instead ``from_integer``::
148
149
sage: NN.from_integer(42)
150
42
151
sage: NN.from_integer(-5) # Don't do that at home kids!
152
-5
153
"""
154
if i in self:
155
return self.from_integer(i)
156
else:
157
raise ValueError, "Value %s in not in %s."%(i, self)
158
159
from_integer = Integer
160
161
Element = Integer
162
163
def __iter__(self):
164
"""
165
EXAMPLES::
166
167
sage: NN = NonNegativeIntegers()
168
sage: g = iter(NN)
169
sage: g.next(), g.next(), g.next(), g.next()
170
(0, 1, 2, 3)
171
"""
172
i = 0
173
while True:
174
yield self.from_integer(i)
175
i += 1
176
# Uncomment the following two lines to catch infinite loops when debugging
177
#if i > 200:
178
# raise ValueError, "Infinite loop during DEBUG! TODO: remove me"
179
180
def an_element(self):
181
"""
182
EXAMPLES::
183
184
sage: NonNegativeIntegers().an_element()
185
42
186
"""
187
return self.from_integer(Integer(42))
188
189
def some_elements(self):
190
"""
191
EXAMPLES::
192
193
sage: NonNegativeIntegers().some_elements()
194
[0, 1, 3, 42]
195
"""
196
return [Integer(0), Integer(1), Integer(3), Integer(42)]
197
198
def next(self, o):
199
"""
200
EXAMPLES::
201
202
sage: NN = NonNegativeIntegers()
203
sage: NN.next(3)
204
4
205
"""
206
return self.from_integer(o+1)
207
208
def unrank(self, rnk):
209
"""
210
EXAMPLES::
211
212
sage: NN = NonNegativeIntegers()
213
sage: NN.unrank(100)
214
100
215
"""
216
return self.from_integer(rnk)
217
218