Ticket #8251: 8251_Jones_pickle.patch

File 8251_Jones_pickle.patch, 4.0 KB (added by wjp, 9 years ago)
  • sage/databases/jones.py

    # HG changeset patch
    # User Willem Jan Palenstijn <wjp@usecode.org>
    # Date 1294554583 -3600
    # Node ID faf3acfa70f6bdae1c9d911ee42393a6b769271c
    # Parent  af388485d7ae58022ddbcb57db96ce89f3646c64
    #8251: Store Jones database as list of python ints instead of polynomials
    
    diff -r af388485d7ae -r faf3acfa70f6 sage/databases/jones.py
    a b  
    8585        return "John Jones's table of number fields with bounded ramification and degree <= 6"
    8686   
    8787    def _load(self, path, filename):
     88        """
     89        Load number fields from a .gp file from John Jones's web page.
     90        This is called by _init, and should not be called directly.
     91
     92        TESTS: This is how the database is created from scratch::
     93
     94                sage: J = JonesDatabase()
     95                sage: J._init()   # not tested, indirect doctest
     96                ...
     97        """
    8898        print filename
    8999        i = 0
    90100        while filename[i].isalpha():
     
    96106        S.sort()
    97107        data = open(path + "/" + filename).read()
    98108        data = data.replace("^","**")
    99         x = PolynomialRing(RationalField(), 'x').gen()
     109        x = PolynomialRing(IntegerRing(), 'x').gen()
    100110        v = eval(data)
     111
     112        # We store the polynomials defining the number fields as
     113        # lists of python ints, to be completely independent of the
     114        # version of sage used to save/load jones.sobj.
     115        v = [ [ int(c) for c in p ] for p in v ]
    101116        s = tuple(S)
    102117        if self.root.has_key(s):
     118            def _cmp_nf(x,y):
     119                """
     120                Compare two lists as polynomials.
     121                NB: This is rather slow, but the _load() function is
     122                only used when re-creating the database from scratch.
     123                """
     124                R = PolynomialRing(RationalField(), 'x')
     125                return R(x).__cmp__(R(y))
    103126            self.root[s] += v
    104             self.root[s].sort()
     127            self.root[s].sort(cmp=_cmp_nf)
    105128        else:
    106129            self.root[s] = v
    107130           
     
    147170            os.makedirs(JONESDATA)
    148171        save(self.root, JONESDATA+ "/jones.sobj")
    149172
     173    def _load_db(self):
     174        """
     175        Load the Jones database from file.
     176        This raises a RuntimeError if the optional package with the database
     177        hasn't been installed.
     178
     179        TESTS::
     180       
     181            sage: J = JonesDatabase()              # optional - jones_database
     182            sage: len(J.get(163))# indirect doctest, optional - jones_database
     183            3
     184
     185        """
     186        if self.root is not None:
     187            return
     188
     189        if os.path.exists(JONESDATA+ "/jones.sobj"):
     190            self.root = load(JONESDATA+ "/jones.sobj")
     191        else:
     192            raise RuntimeError, "You must install the Jones database optional package."
     193        if not isinstance(self.root.iteritems().next()[1][0],list):
     194            self.root = None
     195            raise RuntimeError, "You must update the Jones database optional package to the latest version."
     196
    150197    def unramified_outside(self, S, d=None, var='a'):
    151198        """
    152199        Return all fields in the database of degree d unramified
     
    215262            ValueError: S must be a list of primes
    216263        """
    217264        if self.root is None:
    218             if os.path.exists(JONESDATA+ "/jones.sobj"):
    219                 self.root = load(JONESDATA+ "/jones.sobj")
    220             else:
    221                 raise RuntimeError, "You must install the Jones database optional package."
     265            self._load_db()
    222266        try:
    223267            S = list(S)
    224268        except TypeError:
     
    229273        s = tuple(S)
    230274        if not self.root.has_key(s):
    231275            return []
    232         return [NumberField(f, var, check=False) for f in self.root[s]]
     276        R = PolynomialRing(RationalField(),'x')
     277        return [NumberField(R(f), var, check=False) for f in self.root[s]]
    233278
    234279    def ramified_at(self, S, d=None, var='a'):
    235280        """