Ticket #3869: sage-trac3869.patch

File sage-trac3869.patch, 2.8 kB (added by cremona, 5 months ago)
  • a/sage/databases/cremona.py

    old new  
    4141import sage.schemes.elliptic_curves.constructor as elliptic 
    4242import sage.databases.db   # very important that this be fully qualified 
    4343import sage.misc.misc 
     44 
     45import re 
     46import string 
    4447 
    4548_map = {'allcurves':'a', 'degphi':'b', 'allbsd':'c', 'allgens':'d'} 
    4649 
     
    245248    #iso = iso.lower() 
    246249    return conductor, iso, num 
    247250 
     251def split_code(key): 
     252    """ 
     253    Splits class+curve id string into its two parts. 
    248254 
    249      
     255    EXAMPLES: 
     256        sage: import sage.databases.cremona as cremona 
     257        sage: cremona.split_code('ba2') 
     258        ('ba', '2') 
     259    """  
     260    cu = re.split("[a-z]*",key)[1] 
     261    cl =  re.split("[0-9]*",key)[0] 
     262    return (cl,cu) 
     263 
     264def class_to_int(k): 
     265    """ 
     266    Converts class id string into an integer. 
     267 
     268    EXAMPLES: 
     269        sage: import sage.databases.cremona as cremona 
     270        sage: cremona.class_to_int('ba') 
     271        26 
     272    """  
     273    kk = [string.ascii_lowercase.index(ch) for ch in list(k)] 
     274    kk.reverse() 
     275    return sum([kk[i]*26**i for i in range(len(kk))]) 
     276 
     277def cmp_code(key1,key2): 
     278    """ 
     279    Comparison function for curve id strings. 
     280 
     281    NOTE: Not the same as standard lexicographic order! 
     282 
     283    EXAMPLES: 
     284        sage: import sage.databases.cremona as cremona 
     285        sage: cremona.cmp_code('ba1','z1') 
     286        1 
     287 
     288        By contrast: 
     289        sage: cmp('ba1','z1')                            
     290        -1 
     291         
     292    """ 
     293    cl1,cu1 = split_code(key1) 
     294    cl2,cu2 = split_code(key2) 
     295    d = class_to_int(cl1)-class_to_int(cl2) 
     296    if d!=0:  return d 
     297    return cmp(cu1,cu2) 
     298   
    250299 
    251300class LargeCremonaDatabase(sage.databases.db.Database): 
    252301    """ 
     
    466515        """ 
    467516        for N in conductors: 
    468517            K = self.allcurves(N).keys() 
    469             K.sort(
     518            K.sort(cmp_code
    470519            for e in K: 
    471520                yield self.elliptic_curve(str(N) + e) 
    472521 
     
    481530        classes = [] 
    482531        A = self.allcurves(conductor) 
    483532        K = A.keys() 
    484         K.sort(
     533        K.sort(cmp_code
    485534        for k in K: 
    486535            v = A[k] 
    487536            # test if not first curve in class 
     
    516565            generator that iterates over EllipticCurve objects. 
    517566        """ 
    518567        for N in conductors: 
    519             for id in self.curves(N).keys(): 
     568            K = self.curves(N).keys() 
     569            K.sort(cmp_code) 
     570            for id in K: 
    520571                yield self.elliptic_curve(str(N) + id) 
    521572 
    522573    def list(self, conductors):