Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/databases/db_class_polynomials.py
6915 views
1
"""
2
Database of Hilbert Polynomials
3
"""
4
5
#######################################################################
6
7
# Sage: System for Algebra and Geometry Experimentation
8
#
9
# Copyright (C) 2006 David Kohel <[email protected]>
10
#
11
# Distributed under the terms of the GNU General Public License (GPL)
12
#
13
# The full text of the GPL is available at:
14
#
15
# http://www.gnu.org/licenses/
16
#######################################################################
17
18
19
import bz2, os
20
import sage.misc.misc
21
from sage.rings.integer_ring import IntegerRing
22
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
23
24
dblocation = '%s/kohel'%sage.misc.misc.SAGE_DATA
25
26
disc_length = 7
27
level_length = 3
28
29
def _dbz_to_integers(name):
30
file = '%s/%s'%(dblocation, name)
31
if not os.path.exists(file):
32
raise RuntimeError, "Class polynomial database file %s not available"%file
33
data = bz2.decompress(open(file).read())
34
data = "[" + data.replace("\n",",") + "]"
35
return eval(data)
36
37
def _pad_int_str(s,n):
38
return "0"*(n-len(str(s))) + str(s)
39
40
class ClassPolynomialDatabase:
41
def _dbpath(self,disc,level=1):
42
"""
43
TESTS:
44
sage: db = HilbertClassPolynomialDatabase()
45
sage: db.__getitem__(-23,level=2)
46
Traceback (most recent call last):
47
...
48
NotImplementedError: Level (= 2) > 1 not yet implemented.
49
"""
50
if level != 1:
51
raise NotImplementedError, "Level (= %s) > 1 not yet implemented."%level
52
n1 = 5000*((abs(disc)-1)//5000)
53
s1 = _pad_int_str(n1+1,disc_length)
54
s2 = _pad_int_str(n1+5000,disc_length)
55
subdir = "%s-%s"%(s1,s2)
56
discstr = _pad_int_str(abs(disc),disc_length)
57
return "PolHeeg/%s/%s/pol.%s.dbz"%(self.model,subdir,discstr)
58
59
def __getitem__(self,disc,level=1,var='x'):
60
classpol = self._dbpath(disc,level)
61
try:
62
coeff_list = _dbz_to_integers(classpol)
63
except RuntimeError, msg:
64
print msg
65
raise RuntimeError, \
66
"No database entry for class polynomial of discriminant %s"%disc
67
P = PolynomialRing(IntegerRing(),names=var)
68
return P(list(coeff_list))
69
70
class HilbertClassPolynomialDatabase(ClassPolynomialDatabase):
71
"""
72
The database of Hilbert class polynomials.
73
74
EXAMPLES::
75
76
sage: db = HilbertClassPolynomialDatabase()
77
sage: db[-4] # optional Kohel database required
78
x - 1728
79
sage: db[-7] # optional
80
x + 3375
81
sage: f = db[-23]; f # optional
82
x^3 + 3491750*x^2 - 5151296875*x + 12771880859375
83
sage: f.discriminant().factor() # optional
84
-1 * 5^18 * 7^12 * 11^4 * 17^2 * 19^2 * 23
85
sage: db[-23] # optional
86
x^3 + 3491750*x^2 - 5151296875*x + 12771880859375
87
"""
88
def __init__(self):
89
"""
90
Initialize the database.
91
"""
92
self.model = "Cls"
93
94
def __repr__(self):
95
return "Hilbert class polynomial database"
96
97
######################################################
98
# None of the following are implemented yet.
99
######################################################
100
101
class AtkinClassPolynomialDatabase(ClassPolynomialDatabase):
102
"""
103
The database of Atkin class polynomials.
104
"""
105
def __repr__(self):
106
return "Atkin class polynomial database"
107
108
class WeberClassPolynomialDatabase(ClassPolynomialDatabase):
109
"""
110
The database of Weber class polynomials.
111
"""
112
def __repr__(self):
113
return "Weber class polynomial database"
114
115
class DedekindEtaClassPolynomialDatabase(ClassPolynomialDatabase):
116
"""
117
The database of Dedekind eta class polynomials.
118
"""
119
def __repr__(self):
120
return "Dedekind eta class polynomial database"
121
122
123