Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/rings/finite_rings/finite_field_prime_modn.py
4122 views
1
"""
2
Finite Prime Fields
3
4
AUTHORS:
5
6
- William Stein: initial version
7
- Martin Albrecht (2008-01): refactoring
8
9
TESTS::
10
11
sage: k = GF(3)
12
sage: TestSuite(k).run()
13
"""
14
15
#*****************************************************************************
16
# Copyright (C) 2006 William Stein <[email protected]>
17
# Copyright (C) 2008 Martin Albrecht <[email protected]>
18
#
19
# Distributed under the terms of the GNU General Public License (GPL)
20
#
21
# This code is distributed in the hope that it will be useful,
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24
# General Public License for more details.
25
#
26
# The full text of the GPL is available at:
27
#
28
# http://www.gnu.org/licenses/
29
#*****************************************************************************
30
31
32
from sage.rings.finite_rings.finite_field_base import FiniteField as FiniteField_generic
33
from sage.categories.finite_fields import FiniteFields
34
_FiniteFields = FiniteFields()
35
36
import sage.rings.finite_rings.integer_mod_ring as integer_mod_ring
37
import sage.rings.integer as integer
38
import sage.rings.finite_rings.integer_mod as integer_mod
39
import sage.rings.arith as arith
40
41
from sage.rings.integer_ring import ZZ
42
from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic
43
import sage.structure.factorization as factorization
44
from sage.structure.parent import Parent
45
46
class FiniteField_prime_modn(FiniteField_generic, integer_mod_ring.IntegerModRing_generic):
47
def __init__(self, p, name=None, check=True):
48
"""
49
Return a new finite field of order $p$ where $p$ is prime.
50
51
INPUT:
52
53
- p -- an integer >= 2
54
- ``name`` -- ignored
55
- ``check`` -- bool (default: True); if False, do not
56
check p for primality
57
58
EXAMPLES::
59
60
sage: FiniteField(3)
61
Finite Field of size 3
62
63
sage: FiniteField(next_prime(1000))
64
Finite Field of size 1009
65
"""
66
p = integer.Integer(p)
67
if check and not arith.is_prime(p):
68
raise ArithmeticError, "p must be prime"
69
self.__char = p
70
self._IntegerModRing_generic__factored_order = factorization.Factorization([(p,1)], integer.Integer(1))
71
self._kwargs = {}
72
# FiniteField_generic does nothing more than IntegerModRing_generic, and
73
# it saves a non trivial overhead
74
integer_mod_ring.IntegerModRing_generic.__init__(self, p, category = _FiniteFields)
75
76
def __reduce__(self):
77
"""
78
EXAMPLES::
79
80
sage: k = FiniteField(5); type(k)
81
<class 'sage.rings.finite_rings.finite_field_prime_modn.FiniteField_prime_modn_with_category'>
82
sage: k is loads(dumps(k))
83
True
84
"""
85
return self._factory_data[0].reduce_data(self)
86
87
def __cmp__(self, other):
88
r"""
89
Compare \code{self} with \code{other}. Two finite prime fields
90
are considered equal if their characteristic is equal.
91
92
EXAMPLES::
93
94
sage: K = FiniteField(3)
95
sage: copy(K) == K
96
True
97
"""
98
if not isinstance(other, FiniteField_prime_modn):
99
return cmp(type(self), type(other))
100
# elif other.__class__ != FiniteField_prime_modn:
101
# return -cmp(other, self)
102
return cmp(self.__char, other.__char)
103
104
def __richcmp__(left, right, op):
105
r"""
106
Compare \code{self} with \code{right}.
107
108
EXAMPLES::
109
110
sage: k = GF(2)
111
sage: j = GF(3)
112
sage: k == j
113
False
114
115
sage: GF(2) == copy(GF(2))
116
True
117
"""
118
return left._richcmp_helper(right, op)
119
120
def _is_valid_homomorphism_(self, codomain, im_gens):
121
"""
122
This is called implicitly by the hom constructor.
123
124
EXAMPLES::
125
126
sage: k = GF(73^2,'a')
127
sage: f = k.modulus()
128
sage: r = f.change_ring(k).roots()
129
sage: k.hom([r[0][0]]) # indirect doctest
130
Ring endomorphism of Finite Field in a of size 73^2
131
Defn: a |--> 72*a + 3
132
"""
133
try:
134
return im_gens[0] == codomain._coerce_(self.gen(0))
135
except TypeError:
136
return False
137
138
def _coerce_map_from_(self, S):
139
"""
140
This is called implicitly by arithmetic methods.
141
142
EXAMPLES::
143
144
sage: k = GF(7)
145
sage: e = k(6)
146
sage: e * 2 # indirect doctest
147
5
148
sage: 12 % 7
149
5
150
sage: ZZ.residue_field(7).hom(GF(7))(1) # See trac 11319
151
1
152
sage: K.<w> = QuadraticField(337) # See trac 11319
153
sage: pp = K.ideal(13).factor()[0][0]
154
sage: RF13 = K.residue_field(pp)
155
sage: RF13.hom([GF(13)(1)])
156
Ring morphism:
157
From: Residue field of Fractional ideal (w - 18)
158
To: Finite Field of size 13
159
Defn: 1 |--> 1
160
"""
161
if S is int:
162
return integer_mod.Int_to_IntegerMod(self)
163
elif S is ZZ:
164
return integer_mod.Integer_to_IntegerMod(self)
165
elif isinstance(S, IntegerModRing_generic):
166
from sage.rings.residue_field import ResidueField_generic
167
if S.characteristic() == self.characteristic() and \
168
(not isinstance(S, ResidueField_generic) or S.degree() == 1):
169
try:
170
return integer_mod.IntegerMod_to_IntegerMod(S, self)
171
except TypeError:
172
pass
173
to_ZZ = ZZ.coerce_map_from(S)
174
if to_ZZ is not None:
175
return integer_mod.Integer_to_IntegerMod(self) * to_ZZ
176
177
def characteristic(self):
178
r"""
179
Return the characteristic of \code{self}.
180
181
EXAMPLES::
182
183
sage: k = GF(7)
184
sage: k.characteristic()
185
7
186
"""
187
return self.__char
188
189
def modulus(self):
190
"""
191
Return the minimal polynomial of self, which is always $x - 1$.
192
193
EXAMPLES::
194
195
sage: k = GF(199)
196
sage: k.modulus()
197
x + 198
198
"""
199
try:
200
return self.__modulus
201
except AttributeError:
202
x = self['x'].gen()
203
self.__modulus = x - 1
204
return self.__modulus
205
206
def is_prime_field(self):
207
"""
208
Return True
209
210
EXAMPLES::
211
212
sage: k.<a> = GF(3)
213
sage: k.is_prime_field()
214
True
215
216
sage: k.<a> = GF(3^2)
217
sage: k.is_prime_field()
218
False
219
"""
220
return True
221
222
def polynomial(self, name=None):
223
"""
224
Returns the polynomial \var{name}.
225
226
EXAMPLES::
227
228
sage: k.<a> = GF(3)
229
sage: k.polynomial()
230
x
231
"""
232
if name is None:
233
name = self.variable_name()
234
try:
235
return self.__polynomial[name]
236
except AttributeError:
237
from sage.rings.finite_rings.constructor import FiniteField
238
R = FiniteField(self.characteristic())[name]
239
f = self[name]([0,1])
240
try:
241
self.__polynomial[name] = f
242
except (KeyError, AttributeError):
243
self.__polynomial = {}
244
self.__polynomial[name] = f
245
return f
246
247
def order(self):
248
"""
249
Return the order of this finite field.
250
251
EXAMPLES::
252
253
sage: k = GF(5)
254
sage: k.order()
255
5
256
"""
257
return self.__char
258
259
def gen(self, n=0):
260
"""
261
Return generator of this finite field as an extension of its
262
prime field.
263
264
NOTE: If you want a primitive element for this finite field
265
instead, use \code{self.multiplicative_generator()}.
266
267
EXAMPLES::
268
269
sage: k = GF(13)
270
sage: k.gen()
271
1
272
sage: k.gen(1)
273
Traceback (most recent call last):
274
...
275
IndexError: only one generator
276
"""
277
if n != 0:
278
raise IndexError, "only one generator"
279
return self(1)
280
281
def __iter__(self):
282
"""
283
EXAMPLES::
284
285
sage: list(GF(7))
286
[0, 1, 2, 3, 4, 5, 6]
287
288
We can even start iterating over something that would be too big
289
to actually enumerate::
290
291
sage: K = GF(next_prime(2^256))
292
sage: all = iter(K)
293
sage: all.next()
294
0
295
sage: all.next()
296
1
297
sage: all.next()
298
2
299
"""
300
yield self(0)
301
i = one = self(1)
302
while i:
303
yield i
304
i += one
305
306
def degree(self):
307
"""
308
Returns the degree of the finite field, which is a positive
309
integer.
310
311
EXAMPLES::
312
313
sage: FiniteField(3).degree()
314
1
315
"""
316
return integer.Integer(1)
317
318