Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/databases/conway.py
6915 views
1
"""
2
Frank Luebeck's tables of Conway polynomials over finite fields
3
"""
4
5
6
## On 3/29/07, David Joyner <[email protected]> wrote:
7
## > I guessed what you would do to create the *.bz2 file
8
## > and I think it turned out to be right. (You did things the simplest way,
9
## > as far as I can see.) The file is posted at
10
## > http://sage.math.washington.edu/home/wdj/patches/conway_table.py.bz2
11
## > I think what you did was (a) take the data file from
12
## > http://www.math.rwth-aachen.de/~Frank.Luebeck/data/ConwayPol/CPimport.txt
13
## > (b) renamed the file conway_table.py
14
## > (c) used an editor to make a few changes to the first few and last
15
## > few lines of the data file,
16
## > (d) packed it using bzip2.
17
## >
18
## > I did this and placed the *bz2 file in the correct directory
19
## > (as determined by conway.py). Here's a test
20
## >
21
## > sage: R = PolynomialRing(GF(2),"x")
22
## > sage: R(ConwayPolynomials()[2][3])
23
## > x^3 + x + 1
24
## >
25
## > If I am reading conway.py correctly then this test shows that the
26
## > file I created is what you want.
27
## >
28
## > If you agree, then I will add the new data (you forwarded by
29
## > separate emails) to it.
30
## >
31
## > Please let me know if this is reasonable.
32
33
## Yes, you did exactly the right thing, which I verified as follows:
34
35
## (1) delete SAGE_ROOT/data/conway_polynomials/*
36
## (2) put your conway_polynomial.py.bz2 file in a new directory
37
## /home/was/s/data/src/conway/
38
## (3) Start Sage:
39
## sage: c = ConwayPolynomials(read_only=False)
40
## sage: c._init() # builds database
41
## sage: c.polynomial(3,10)
42
## [2, 1, 0, 0, 2, 2, 2, 0, 0, 0, 1]
43
## (4) restart and test:
44
## sage: conway_polynomial(3,10)
45
## x^10 + 2*x^6 + 2*x^5 + 2*x^4 + x + 2
46
47
48
#*****************************************************************************
49
#
50
# Sage: Copyright (C) 2005 William Stein <[email protected]>
51
#
52
# Distributed under the terms of the GNU General Public License (GPL)
53
#
54
# This code is distributed in the hope that it will be useful,
55
# but WITHOUT ANY WARRANTY; without even the implied warranty of
56
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
57
# General Public License for more details.
58
#
59
# The full text of the GPL is available at:
60
#
61
# http://www.gnu.org/licenses/
62
#*****************************************************************************
63
64
import os
65
66
import sage.databases.db # very important that this be fully qualified
67
_CONWAYDATA = "%s/conway_polynomials/"%sage.misc.misc.SAGE_DATA
68
69
70
class ConwayPolynomials(sage.databases.db.Database):
71
def __init__(self, read_only=True):
72
"""
73
Initialize the database.
74
75
INPUT:
76
77
- ``read_only`` - bool (default: True), if True, then
78
the database is read_only and changes cannot be committed to
79
disk.
80
81
TESTS::
82
83
sage: c = ConwayPolynomials()
84
sage: c
85
Frank Luebeck's database of Conway polynomials
86
sage: c.read_only
87
True
88
"""
89
sage.databases.db.Database.__init__(self,
90
name="conway_polynomials", read_only=read_only)
91
92
def _init(self):
93
if not os.path.exists(_CONWAYDATA):
94
raise RuntimeError, "In order to initialize the database, the directory must exist."%_CONWAYDATA
95
os.chdir(_CONWAYDATA)
96
if os.system("bunzip2 -k conway_table.py.bz2"):
97
raise RuntimeError, "error decompressing table"
98
from conway_table import conway_polynomials
99
for X in conway_polynomials:
100
(p, n, v) = tuple(X)
101
if not self.has_key(p):
102
self[p] = {}
103
self[p][n] = v
104
self[p] = self[p] # so database knows it was changed
105
os.unlink("conway_table.pyc")
106
os.unlink("conway_table.py")
107
self.commit()
108
109
def __repr__(self):
110
"""
111
Return a description of this database.
112
113
TESTS:
114
115
sage: c = ConwayPolynomials()
116
sage: c.__repr__()
117
"Frank Luebeck's database of Conway polynomials"
118
"""
119
return "Frank Luebeck's database of Conway polynomials"
120
121
122
def polynomial(self, p, n):
123
"""
124
Return the Conway polynomial of degree ``n`` over ``GF(p)``,
125
or raise a RuntimeError if this polynomial is not in the
126
database.
127
128
.. NOTE::
129
130
See also the global function ``conway_polynomial`` for
131
a more user-friendly way of accessing the polynomial.
132
133
INPUT:
134
135
- ``p`` -- prime number
136
137
- ``n`` -- positive integer
138
139
OUTPUT:
140
141
List of Python int's giving the coefficients of the corresponding
142
Conway polynomial in ascending order of degree.
143
144
EXAMPLES::
145
146
sage: c = ConwayPolynomials()
147
sage: c.polynomial(3, 21)
148
[1, 2, 0, 2, 0, 1, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
149
sage: c.polynomial(97, 128)
150
Traceback (most recent call last):
151
...
152
RuntimeError: Conway polynomial over F_97 of degree 128 not in database.
153
"""
154
try:
155
return self[int(p)][int(n)]
156
except KeyError:
157
raise RuntimeError, "Conway polynomial over F_%s of degree %s not in database."%(p,n)
158
159
def has_polynomial(self, p, n):
160
"""
161
Return True if the database of Conway polynomials contains the
162
polynomial of degree ``n`` over ``GF(p)``.
163
164
INPUT:
165
166
- ``p`` -- prime number
167
168
- ``n`` -- positive integer
169
170
EXAMPLES::
171
172
sage: c = ConwayPolynomials()
173
sage: c.has_polynomial(97, 12)
174
True
175
sage: c.has_polynomial(60821, 5)
176
False
177
"""
178
return self.has_key(int(p)) and self[int(p)].has_key(int(n))
179
180
def primes(self):
181
"""
182
Return the list of prime numbers ``p`` for which the database of
183
Conway polynomials contains polynomials over ``GF(p)``.
184
185
EXAMPLES::
186
187
sage: c = ConwayPolynomials()
188
sage: P = c.primes()
189
sage: 2 in P
190
True
191
sage: next_prime(10^7) in P
192
False
193
"""
194
return list(self.keys())
195
196
def degrees(self, p):
197
"""
198
Return the list of integers ``n`` for which the database of Conway
199
polynomials contains the polynomial of degree ``n`` over ``GF(p)``.
200
201
EXAMPLES::
202
203
sage: c = ConwayPolynomials()
204
sage: c.degrees(60821)
205
[1, 2, 3, 4]
206
sage: c.degrees(next_prime(10^7))
207
[]
208
"""
209
p = int(p)
210
if not p in self.primes():
211
return []
212
return self[p].keys()
213
214
215
216