Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/sets/non_negative_integers.py
4057 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
"""
106
try:
107
return Integer(elt) >= Integer(0)
108
except TypeError:
109
return False
110
111
def _element_constructor_(self, i):
112
"""
113
Constructs an element of self from an integer, testing that
114
this integer is indeed non negative.
115
116
EXAMPLES::
117
118
sage: NN = NonNegativeIntegers()
119
sage: NN._element_constructor_(42)
120
42
121
sage: NN._element_constructor_(-5)
122
Traceback (most recent call last):
123
...
124
ValueError: Value -5 in not in Non negative integers.
125
sage: NN._element_constructor_(x)
126
Traceback (most recent call last):
127
...
128
ValueError: Value x in not in Non negative integers.
129
130
This is used upon coercion attempts::
131
132
sage: n = NN(42); n # indirect doctest
133
42
134
sage: type(n)
135
<type 'sage.rings.integer.Integer'>
136
sage: n.parent()
137
Integer Ring
138
sage: NN(-1)
139
Traceback (most recent call last):
140
...
141
ValueError: Value -1 in not in Non negative integers.
142
143
For fast construction of elements without tests, please use
144
instead ``from_integer``::
145
146
sage: NN.from_integer(42)
147
42
148
sage: NN.from_integer(-5) # Don't do that at home kids!
149
-5
150
"""
151
if i in self:
152
return self.from_integer(i)
153
else:
154
raise ValueError, "Value %s in not in %s."%(i, self)
155
156
from_integer = Integer
157
158
Element = Integer
159
160
def __iter__(self):
161
"""
162
EXAMPLES::
163
164
sage: NN = NonNegativeIntegers()
165
sage: g = iter(NN)
166
sage: g.next(), g.next(), g.next(), g.next()
167
(0, 1, 2, 3)
168
"""
169
i = 0
170
while True:
171
yield self.from_integer(i)
172
i += 1
173
# Uncomment the following two lines to catch infinite loops when debugging
174
#if i > 200:
175
# raise ValueError, "Infinite loop during DEBUG! TODO: remove me"
176
177
def an_element(self):
178
"""
179
EXAMPLES::
180
181
sage: NonNegativeIntegers().an_element()
182
42
183
"""
184
return self.from_integer(Integer(42))
185
186
def some_elements(self):
187
"""
188
EXAMPLES::
189
190
sage: NonNegativeIntegers().some_elements()
191
[0, 1, 3, 42]
192
"""
193
return [Integer(0), Integer(1), Integer(3), Integer(42)]
194
195
def next(self, o):
196
"""
197
EXAMPLES::
198
199
sage: NN = NonNegativeIntegers()
200
sage: NN.next(3)
201
4
202
"""
203
return self.from_integer(o+1)
204
205
def unrank(self, rnk):
206
"""
207
EXAMPLES::
208
209
sage: NN = NonNegativeIntegers()
210
sage: NN.unrank(100)
211
100
212
"""
213
return self.from_integer(rnk)
214
215