Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/databases/conway.py
8814 views
1
"""
2
Frank Luebeck's tables of Conway polynomials over finite fields
3
"""
4
5
#*****************************************************************************
6
#
7
# Sage: Copyright (C) 2005 William Stein <[email protected]>
8
# Copyright (C) 2013 R. Andrew Ohana <[email protected]>
9
#
10
# Distributed under the terms of the GNU General Public License (GPL)
11
#
12
# This code is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
# General Public License for more details.
16
#
17
# The full text of the GPL is available at:
18
#
19
# http://www.gnu.org/licenses/
20
#*****************************************************************************
21
22
import collections, os
23
from sage.misc.misc import SAGE_SHARE
24
25
_CONWAYDATA = os.path.join(SAGE_SHARE, 'conway_polynomials',
26
'conway_polynomials.sobj')
27
_conwaydict = None
28
29
class DictInMapping(collections.Mapping):
30
def __init__(self, dict):
31
"""
32
Places dict into a non-mutable mapping.
33
34
TESTS::
35
36
sage: from sage.databases.conway import DictInMapping
37
sage: d = {}
38
sage: m = DictInMapping(d); m
39
{}
40
sage: d[0] = 1; m
41
{0: 1}
42
sage: m[2] = 3
43
Traceback (most recent call last):
44
...
45
TypeError: 'DictInMapping' object does not support item assignment
46
"""
47
self._store = dict
48
49
def __getitem__(self, key):
50
"""
51
TESTS::
52
53
sage: from sage.databases.conway import DictInMapping
54
sage: DictInMapping({'foo': 'bar'})['foo']
55
'bar'
56
"""
57
return self._store[key]
58
59
def __len__(self):
60
"""
61
TESTS::
62
63
sage: from sage.databases.conway import DictInMapping
64
sage: d = {}
65
sage: m = DictInMapping(d); len(m)
66
0
67
sage: d['foo'] = 'bar'; len(m)
68
1
69
"""
70
return len(self._store)
71
72
def __iter__(self):
73
"""
74
TESTS::
75
76
sage: from sage.databases.conway import DictInMapping
77
sage: iter(DictInMapping({'foo': 'bar'})).next()
78
'foo'
79
"""
80
return iter(self._store)
81
82
def __repr__(self):
83
"""
84
TESTS::
85
86
sage: from sage.databases.conway import DictInMapping
87
sage: DictInMapping({'foo': 'bar'})
88
{'foo': 'bar'}
89
"""
90
return repr(self._store)
91
92
class ConwayPolynomials(collections.Mapping):
93
def __init__(self):
94
"""
95
Initialize the database.
96
97
TESTS::
98
99
sage: c = ConwayPolynomials()
100
sage: c
101
Frank Luebeck's database of Conway polynomials
102
"""
103
global _conwaydict
104
if _conwaydict is None:
105
if not os.path.exists(_CONWAYDATA):
106
raise RuntimeError('In order to initialize the database, '
107
+ '%s must exist.'%_CONWAYDATA)
108
from sage.structure.sage_object import load
109
_conwaydict = load(_CONWAYDATA)
110
self._store = _conwaydict
111
112
def __repr__(self):
113
"""
114
Return a description of this database.
115
116
TESTS::
117
118
sage: c = ConwayPolynomials()
119
sage: c.__repr__()
120
"Frank Luebeck's database of Conway polynomials"
121
"""
122
return "Frank Luebeck's database of Conway polynomials"
123
124
def __getitem__(self, key):
125
"""
126
If key is a pair of integers ``p,n``, return the Conway
127
polynomial of degree ``n`` over ``GF(p)``.
128
129
If key is an integer ``p``, return a non-mutable mapping
130
whose keys are the degrees of the polynomial values.
131
132
TESTS::
133
134
sage: c = ConwayPolynomials()
135
sage: c[60859]
136
{1: (60856, 1), 2: (3, 60854, 1),
137
3: (60856, 8, 0, 1), 4: (3, 32881, 3, 0, 1)}
138
sage: c[60869, 3]
139
(60867, 2, 0, 1)
140
"""
141
try:
142
return DictInMapping(self._store[key])
143
except KeyError as err:
144
try:
145
if isinstance(key, (tuple, list)):
146
if len(key) == 2:
147
return self._store[key[0]][key[1]]
148
except KeyError:
149
pass
150
raise err
151
152
def __len__(self):
153
"""
154
Return the number of polynomials in this database.
155
156
TESTS::
157
158
sage: c = ConwayPolynomials()
159
sage: len(c)
160
35352
161
"""
162
try:
163
return self._len
164
except AttributeError:
165
pass
166
self._len = sum(len(a) for a in self._store.itervalues())
167
return self._len
168
169
def __iter__(self):
170
"""
171
Return an iterator over the keys of this database.
172
173
TESTS::
174
175
sage: c = ConwayPolynomials()
176
sage: itr = iter(c)
177
sage: itr.next()
178
(65537, 4)
179
sage: itr.next()
180
(2, 1)
181
"""
182
for a,b in self._store.iteritems():
183
for c in b:
184
yield a,c
185
186
def polynomial(self, p, n):
187
"""
188
Return the Conway polynomial of degree ``n`` over ``GF(p)``,
189
or raise a RuntimeError if this polynomial is not in the
190
database.
191
192
.. NOTE::
193
194
See also the global function ``conway_polynomial`` for
195
a more user-friendly way of accessing the polynomial.
196
197
INPUT:
198
199
- ``p`` -- prime number
200
201
- ``n`` -- positive integer
202
203
OUTPUT:
204
205
List of Python int's giving the coefficients of the corresponding
206
Conway polynomial in ascending order of degree.
207
208
EXAMPLES::
209
210
sage: c = ConwayPolynomials()
211
sage: c.polynomial(3, 21)
212
(1, 2, 0, 2, 0, 1, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
213
sage: c.polynomial(97, 128)
214
Traceback (most recent call last):
215
...
216
RuntimeError: Conway polynomial over F_97 of degree 128 not in database.
217
"""
218
try:
219
return self[p,n]
220
except KeyError:
221
raise RuntimeError("Conway polynomial over F_%s of degree %s not in database."%(p,n))
222
223
def has_polynomial(self, p, n):
224
"""
225
Return True if the database of Conway polynomials contains the
226
polynomial of degree ``n`` over ``GF(p)``.
227
228
INPUT:
229
230
- ``p`` -- prime number
231
232
- ``n`` -- positive integer
233
234
EXAMPLES::
235
236
sage: c = ConwayPolynomials()
237
sage: c.has_polynomial(97, 12)
238
True
239
sage: c.has_polynomial(60821, 5)
240
False
241
"""
242
return (p,n) in self
243
244
def primes(self):
245
"""
246
Return the list of prime numbers ``p`` for which the database of
247
Conway polynomials contains polynomials over ``GF(p)``.
248
249
EXAMPLES::
250
251
sage: c = ConwayPolynomials()
252
sage: P = c.primes()
253
sage: 2 in P
254
True
255
sage: next_prime(10^7) in P
256
False
257
"""
258
return self._store.keys()
259
260
def degrees(self, p):
261
"""
262
Return the list of integers ``n`` for which the database of Conway
263
polynomials contains the polynomial of degree ``n`` over ``GF(p)``.
264
265
EXAMPLES::
266
267
sage: c = ConwayPolynomials()
268
sage: c.degrees(60821)
269
[1, 2, 3, 4]
270
sage: c.degrees(next_prime(10^7))
271
[]
272
"""
273
if p not in self._store:
274
return []
275
return self._store[p].keys()
276
277
def __reduce__(self):
278
"""
279
TESTS::
280
281
sage: c = ConwayPolynomials()
282
sage: loads(dumps(c)) == c
283
True
284
"""
285
return (ConwayPolynomials, ())
286
287