source: sage/databases/db_class_polynomials.py @ 4749:a40686a42843

Revision 4749:a40686a42843, 2.9 KB checked in by Justin C. Walker <justin@…>, 6 years ago (diff)

Minor bug fix: uninitialized variable in error message ("s" -> "level") in _dbpath().

Line 
1"""
2Database of Hilbert Polynomials
3"""
4
5#######################################################################
6
7#  SAGE: System for Algebra and Geometry Experimentation   
8#
9#  Copyright (C) 2006 David Kohel <kohel@maths.usyd.edu.au>
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
19import bz2, os
20import sage.misc.misc
21from sage.rings.integer import Integer
22from sage.rings.integer_ring import IntegerRing
23from sage.rings.polynomial.polynomial_ring import PolynomialRing
24
25DB_HOME = '%s/kohel'%sage.misc.misc.SAGE_DATA
26
27disc_length = 7
28level_length = 3
29
30def _dbz_to_integers(name):
31    file = '%s/%s'%(DB_HOME, name)
32    if not os.path.exists(file):
33        raise RuntimeError, "Class polynomial database file %s not available"%file
34    data = bz2.decompress(open(file).read())
35    data = "[" + data.replace("\n",",") + "]"
36    return eval(data)
37
38def _pad_int_str(s,n):
39    return "0"*(n-len(str(s))) + str(s)
40
41class ClassPolynomialDatabase:
42    def _dbpath(self,disc,level=1):
43        if level != 1:
44            raise NotImplementedError, "Level (= %s) > 1 not yet implemented."%level
45        n1 = 5000*((abs(disc)-1)//5000)
46        s1 = _pad_int_str(n1+1,disc_length)
47        s2 = _pad_int_str(n1+5000,disc_length)
48        subdir = "%s-%s"%(s1,s2)
49        discstr = _pad_int_str(abs(disc),disc_length)
50        return "PolHeeg/%s/%s/pol.%s.dbz"%(self.model,subdir,discstr)
51
52    def __getitem__(self,disc,level=1,var='x'):
53        classpol = self._dbpath(disc,level)
54        try:
55            coeff_list = _dbz_to_integers(classpol)
56        except RuntimeError, msg:
57            print msg
58            raise RuntimeError, \
59                  "No database entry for class polynomial of discriminant %s"%disc
60        P = PolynomialRing(IntegerRing(),names=var)
61        return P(list(coeff_list))
62
63class HilbertClassPolynomialDatabase(ClassPolynomialDatabase):
64    """
65    The database of Hilbert class polynomials.
66    """
67    def __init__(self):
68        """
69        Initialize the database.
70        """
71        self.model = "Cls"
72   
73    def __repr__(self):
74        return "Hilbert class polynomial database"
75       
76class AtkinClassPolynomialDatabase(ClassPolynomialDatabase):
77    """
78    The database of Atkin class polynomials.
79    """
80    def __repr__(self):
81        return "Atkin class polynomial database"
82
83class WeberClassPolynomialDatabase(ClassPolynomialDatabase):
84    """
85    The database of Weber class polynomials.
86    """
87    def __repr__(self):
88        return "Weber class polynomial database"
89
90class DedekindEtaClassPolynomialDatabase(ClassPolynomialDatabase):
91    """
92    The database of Dedekind eta class polynomials.
93    """
94    def __repr__(self):
95        return "Dedekind eta class polynomial database"
Note: See TracBrowser for help on using the repository browser.