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