Example #1
0
def cmp_label(lab1, lab2):
    from sage.databases.cremona import parse_cremona_label, class_to_int
    a, b, c = parse_cremona_label(lab1)
    id1 = int(a), class_to_int(b), int(c)
    a, b, c = parse_cremona_label(lab2)
    id2 = int(a), class_to_int(b), int(c)
    return cmp(id1, id2)
Example #2
0
def encode_hecke_orbit(label):
    level, weight, char_orbit_label, hecke_orbit_label = label.split('.')
    level = int(level)
    weight = int(weight)
    char_orbit = class_to_int(char_orbit_label)
    hecke_orbit = class_to_int(hecke_orbit_label)
    return level + (weight << 24) + (char_orbit << 36) + (hecke_orbit << 52)
Example #3
0
def curve_cmp(E1, E2):
    r"""
    Comparison function for elliptic curves over `Q`.

    Order by label if in the database, else first by conductor, then
    by c_invariants.
    """
    t = cmp(E1.conductor(), E2.conductor())
    if t:
        return t

    # Now they have the same conductor
    try:
        from sage.databases.cremona import parse_cremona_label, class_to_int

        k1 = parse_cremona_label(E1.label())
        k2 = parse_cremona_label(E2.label())
        t = cmp(class_to_int(k1[1]), class_to_int(k2[1]))
        if t:
            return t
        return cmp(k1[2], k2[2])
    except RuntimeError:  # if not in database, label() will fail
        pass

    return cmp(E1.ainvs(), E2.ainvs())
Example #4
0
def add_numeric_iso_labels(min_conductor_norm=0,
                           max_conductor_norm=None,
                           fix=False):
    r""" One-off utility to add a numeric conversion of the letter-coded
    class labels 'a'->0', 'z'->25, 'ba'->26, etc. for sorting
    purposes.
    """
    from sage.databases.cremona import class_to_int
    count = 0
    query = {}
    query['conductor_norm'] = {'$gte': int(min_conductor_norm)}
    if max_conductor_norm:
        query['conductor_norm']['$lte'] = int(max_conductor_norm)
    else:
        max_conductor_norm = oo
    curves_to_fix = nfcurves.find(query)
    print("%s curves to examine of conductor norm between %s and %s." %
          (curves_to_fix.count(), min_conductor_norm, max_conductor_norm))
    for c in curves_to_fix:
        count = count + 1
        if count % 100 == 0: print("%s: %s" % (count, c['label']))
        fix_data = {}
        lab = c['iso_label']
        if 'CM' in lab:
            nlab = -1 - class_to_int(lab[2:])
            print("converting label %s to %s" % (lab, nlab))
        else:
            nlab = class_to_int(lab.lower())
        fix_data['iso_nlabel'] = nlab
        #print("using fixed data %s for form %s" % (fix_data,c['label']))
        if fix:
            nfcurves.update_one({'_id': c['_id']}, {"$set": fix_data},
                                upsert=True)
Example #5
0
def add_numeric_iso_labels(min_conductor_norm=0, max_conductor_norm=None, fix=False):
    r""" One-off utility to add a numeric conversion of the letter-coded
    class labels 'a'->0', 'z'->25, 'ba'->26, etc. for sorting
    purposes.
    """
    from sage.databases.cremona import class_to_int
    count = 0
    query = {}
    query['conductor_norm'] = {'$gte' : int(min_conductor_norm)}
    if max_conductor_norm:
        query['conductor_norm']['$lte'] = int(max_conductor_norm)
    else:
        max_conductor_norm = oo
    curves_to_fix = nfcurves.find(query)
    print("%s curves to examine of conductor norm between %s and %s."
          % (curves_to_fix.count(),min_conductor_norm,max_conductor_norm))
    for c in curves_to_fix:
        count = count+1
        if count%100==0: print("%s: %s" % (count, c['label']))
        fix_data = {}
        lab = c['iso_label']
        if 'CM' in lab:
            nlab = -1 - class_to_int(lab[2:])
            print("converting label %s to %s" % (lab, nlab))
        else:
            nlab = class_to_int(lab.lower())
        fix_data['iso_nlabel'] = nlab
        #print("using fixed data %s for form %s" % (fix_data,c['label']))
        if fix:
            nfcurves.update_one({'_id': c['_id']}, {"$set": fix_data}, upsert=True)
Example #6
0
def extended_class_to_int(k):
    if k == 'a':
        return 0
    elif k[0] == 'a':
        return -class_to_int(k[1:])
    else:
        return class_to_int(k)
Example #7
0
def extended_class_to_int(k):
    if k == 'a':
        return 0
    elif k[0] == 'a':
        return -class_to_int(k[1:])
    else:
        return class_to_int(k)
Example #8
0
def encode_hecke_orbit(label):
    level, weight, char_orbit_label, hecke_orbit_label = label.split('.')
    level = int(level)
    weight = int(weight)
    char_orbit = class_to_int(char_orbit_label)
    hecke_orbit = class_to_int(hecke_orbit_label)
    return level + (weight << 24) + (char_orbit << 36) + (hecke_orbit << 52)
Example #9
0
def cmp_label(lab1, lab2):
    from sage.databases.cremona import parse_cremona_label, class_to_int
    a, b, c = parse_cremona_label(lab1)
    id1 = int(a), class_to_int(b), int(c)
    a, b, c = parse_cremona_label(lab2)
    id2 = int(a), class_to_int(b), int(c)
    return cmp(id1, id2)
Example #10
0
def add_numeric_label_suffixes(min_level_norm=0, max_level_norm=None, fix=False):
    r""" One-off utility to add a numeric conversion of the letter-coded
    label suffixes 'a'->0', 'z'->25, 'ba'->26, etc. for sorting
    purposes.
    """
    from sage.databases.cremona import class_to_int
    count = 0
    query = {}
    query['level_norm'] = {'$gte' : int(min_level_norm)}
    if max_level_norm:
        query['level_norm']['$lte'] = int(max_level_norm)
    else:
        max_level_norm = oo
    forms_to_fix = forms.find(query)
    print("%s forms to examine of level norm between %s and %s."
          % (forms_to_fix.count(),min_level_norm,max_level_norm))
    for f in forms_to_fix:
        count = count+1
        if count%100==0: print("%s: %s" % (count, f['label']))
        fix_data = {}
        lab = f['label_suffix']
        fix_data['label_nsuffix'] = class_to_int(lab)
        #print("using fixed data %s for form %s" % (fix_data,f['label']))
        if fix:
            forms.update({'label': f['label']}, {"$set": fix_data}, upsert=True)
Example #11
0
def label_to_number(modulus, number, all=False):
    """
    Takes the second part of a character label and converts it to the second
    part of a Conrey label.  This could be trivial (just casting to an int)
    or could require converting from an orbit label to a number.

    If the label is invalid, returns 0.
    """
    try:
        number = int(number)
    except ValueError:
        # encoding Galois orbit
        if modulus < 10000:
            try:
                orbit_label = '{0}.{1}'.format(modulus, 1 + class_to_int(number))
            except ValueError:
                raise ValueError("Dirichlet Character of this label not found in database")
            else:
                number = db.char_dir_orbits.lucky({'orbit_label': orbit_label}, 'galois_orbit')
                if number is None:
                    raise ValueError("Dirichlet Character of this label not found in database")
                if not all:
                    number = number[0]
        else:
            raise ValueError("The modulus cannot be larger than 10,000")
    else:
        if number <= 0:
            raise ValueError("The number after the '.' cannot be negative")
        elif gcd(modulus, number) != 1:
            raise ValueError("The two numbers either side of '.' must be coprime")
        elif number > modulus:
            raise ValueError("The number after the '.' must be less than the number before")
    return number
Example #12
0
def curve_key(E1):
    r"""
    Comparison key for elliptic curves over `\QQ`.

    The key is a tuple:

    - if the curve is in the database: (conductor, 0, label, number)

    - otherwise: (conductor, 1, a_invariants)

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.ell_egros import curve_key
        sage: E = EllipticCurve_from_j(1728)
        sage: curve_key(E)
        (32, 0, 0, 2)
        sage: E = EllipticCurve_from_j(1729)
        sage: curve_key(E)
        (2989441, 1, (1, 0, 0, -36, -1))
    """
    try:
        from sage.databases.cremona import parse_cremona_label, class_to_int
        N, l, k = parse_cremona_label(E1.label())
        return (N, 0, class_to_int(l), k)
    except RuntimeError:
        return (E1.conductor(), 1, E1.ainvs())
Example #13
0
def curve_key(E1):
    r"""
    Comparison key for elliptic curves over `\QQ`.

    The key is a tuple:

    - if the curve is in the database: (conductor, 0, label, number)

    - otherwise: (conductor, 1, a_invariants)

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.ell_egros import curve_key
        sage: E = EllipticCurve_from_j(1728)
        sage: curve_key(E)
        (32, 0, 0, 2)
        sage: E = EllipticCurve_from_j(1729)
        sage: curve_key(E)
        (2989441, 1, (1, 0, 0, -36, -1))
    """
    try:
        from sage.databases.cremona import parse_cremona_label, class_to_int
        N, l, k = parse_cremona_label(E1.label())
        return (N, 0, class_to_int(l), k)
    except LookupError:
        return (E1.conductor(), 1, E1.ainvs())
Example #14
0
def test_conductor_pair(p, N1, N2):
    import sage.databases.cremona as c
    for E1 in cremona_optimal_curves([N1]):
        if N1 == N2:
            cl1 = c.class_to_int(c.parse_cremona_label(E1.label())[1])
        else:
            cl1 = 0
        for E2 in cremona_optimal_curves([N2]):
            if N1 == N2:
                cl2 = c.class_to_int(c.parse_cremona_label(E2.label())[1])
            else:
                cl2 = 0
            if N1 != N2 or cl2 < cl1:
                res, info = test_cong(p, E1, E2)
                if res:
                    report(res, info, p, E1.label(), E2.label())
Example #15
0
def label_to_number(modulus, number, all=False):
    """
    Takes the second part of a character label and converts it to the second
    part of a Conrey label.  This could be trivial (just casting to an int)
    or could require converting from an orbit label to a number.

    If the label is invalid, returns 0.
    """
    try:
        number = int(number)
    except ValueError:
        # encoding Galois orbit
        if modulus < 10000:
            try:
                orbit_label = '{0}.{1}'.format(modulus, 1 + class_to_int(number))
            except ValueError:
                return 0
            else:
                number = db.char_dir_orbits.lucky({'orbit_label':orbit_label}, 'galois_orbit')
                if number is None:
                    return 0
                if not all:
                    number = number[0]
        else:
            return 0
    else:
        if number <= 0 or gcd(modulus, number) != 1 or number > modulus:
            return 0
    return number
Example #16
0
def add_numeric_label_suffixes(min_level_norm=0,
                               max_level_norm=None,
                               fix=False):
    r""" One-off utility to add a numeric conversion of the letter-coded
    label suffixes 'a'->0', 'z'->25, 'ba'->26, etc. for sorting
    purposes.
    """
    from sage.databases.cremona import class_to_int
    count = 0
    query = {}
    query['level_norm'] = {'$gte': int(min_level_norm)}
    if max_level_norm:
        query['level_norm']['$lte'] = int(max_level_norm)
    else:
        max_level_norm = oo
    forms_to_fix = forms.find(query)
    print("%s forms to examine of level norm between %s and %s." %
          (forms_to_fix.count(), min_level_norm, max_level_norm))
    for f in forms_to_fix:
        count = count + 1
        if count % 100 == 0: print("%s: %s" % (count, f['label']))
        fix_data = {}
        lab = f['label_suffix']
        fix_data['label_nsuffix'] = class_to_int(lab)
        #print("using fixed data %s for form %s" % (fix_data,f['label']))
        if fix:
            forms.update({'label': f['label']}, {"$set": fix_data},
                         upsert=True)
Example #17
0
def label_to_number(modulus, number, all=False):
    """
    Takes the second part of a character label and converts it to the second
    part of a Conrey label.  This could be trivial (just casting to an int)
    or could require converting from an orbit label to a number.

    If the label is invalid, returns 0.
    """
    try:
        number = int(number)
    except ValueError:
        # encoding Galois orbit
        if modulus < 10000:
            try:
                orbit_label = '{0}.{1}'.format(modulus,
                                               1 + class_to_int(number))
            except ValueError:
                return 0
            else:
                number = db.char_dir_orbits.lucky({'orbit_label': orbit_label},
                                                  'galois_orbit')
                if number is None:
                    return 0
                if not all:
                    number = number[0]
        else:
            return 0
    else:
        if number <= 0 or gcd(modulus, number) != 1 or number > modulus:
            return 0
    return number
Example #18
0
def sorting_label(d1):
    """
    Provide a sorting key.
    """
    from sage.databases.cremona import parse_cremona_label, class_to_int
    a, b, c = parse_cremona_label(d1["label"])
    return (int(a), class_to_int(b), int(c))
Example #19
0
def cmp_label(lab1, lab2):
    """
    EXAMPLES::

    cmp_label('24a5', '33a1')
    -1
    cmp_label('11a1', '11a1')
    0
    """
    from sage.databases.cremona import parse_cremona_label, class_to_int
    a, b, c = parse_cremona_label(lab1)
    id1 = int(a), class_to_int(b), int(c)
    a, b, c = parse_cremona_label(lab2)
    id2 = int(a), class_to_int(b), int(c)
    if id1 == id2:
        return 0
    return -1 if id1 < id2 else 1
Example #20
0
def fix_labels(field_label, min_level_norm=0, max_level_norm=None, fix_forms=False, fix_curves=False, reverse=False):
    r""" One-off utility to correct labels 'aa'->'ba'->'ca', ..., 'az'->'bz'->'cz'
    """
    from sage.databases.cremona import class_to_int
    count = 0
    query = {}
    query['field_label'] = field_label
    query['level_norm'] = {'$gte' : int(min_level_norm)}
    if max_level_norm:
        query['level_norm']['$lte'] = int(max_level_norm)
    else:
        max_level_norm = oo
    forms_to_fix = forms.find(query)
    print("%s forms to examine of level norm between %s and %s."
          % (forms_to_fix.count(),min_level_norm,max_level_norm))
    if forms_to_fix.count() == 0:
        return None
    for f in forms_to_fix:
        count = count+1
        if count%100==0: print("%s: %s" % (count, f['label']))
        fix_data = {}
        lab = f['label_suffix']
        if len(lab)==1:
            continue
        if f['label'][-2:] != lab:
            print("Incorrect label_suffix %s in form %s" % (lab,f['label']))
            return
        oldlab = lab
        lab = fix_one_label(lab, reverse=reverse)
        fix_data['label_suffix'] = lab
        fix_data['label'] = f['label'].replace(oldlab,lab)
        fix_data['short_label'] = f['short_label'].replace(oldlab,lab)
        fix_data['label_nsuffix'] = class_to_int(lab)
        print("using fixed data %s for form %s" % (fix_data,f['label']))
        if fix_forms:
            forms.update({'label': f['label']}, {"$set": fix_data}, upsert=True)

        # find associated elliptic curve and fix that too (where appropriate)
        if f['deg']==2 and f['dimension']==1:
            label = f['label']
            for e in nfcurves.find({'class_label':f['label']}):
                fix_data = {}
                fix_data['iso_label'] = lab
                fix_data['label'] = e['label'].replace(oldlab,lab)
                fix_data['short_label'] = e['short_label'].replace(oldlab,lab)
                fix_data['class_label'] = e['class_label'].replace(oldlab,lab)
                fix_data['short_class_label'] = e['short_class_label'].replace(oldlab,lab)
                print("using fixed data %s for curve %s" % (fix_data,e['label']))
                if fix_curves:
                    res = nfcurves.update_one({'_id': e['_id']}, {"$set": fix_data}, upsert=True)
                    assert res.matched_count==1
        else:
            print("No elliptic curve to fix")
Example #21
0
def fix_labels(field_label, min_level_norm=0, max_level_norm=None, fix_forms=False, fix_curves=False, reverse=False):
    r""" One-off utility to correct labels 'aa'->'ba'->'ca', ..., 'az'->'bz'->'cz'
    """
    from sage.databases.cremona import class_to_int
    count = 0
    query = {}
    query['field_label'] = field_label
    query['level_norm'] = {'$gte' : int(min_level_norm)}
    if max_level_norm:
        query['level_norm']['$lte'] = int(max_level_norm)
    else:
        max_level_norm = oo
    forms_to_fix = forms.find(query)
    print("%s forms to examine of level norm between %s and %s."
          % (forms_to_fix.count(),min_level_norm,max_level_norm))
    if forms_to_fix.count() == 0:
        return None
    for f in forms_to_fix:
        count = count+1
        if count%100==0: print("%s: %s" % (count, f['label']))
        fix_data = {}
        lab = f['label_suffix']
        if len(lab)==1:
            continue
        if f['label'][-2:] != lab:
            print("Incorrect label_suffix %s in form %s" % (lab,f['label']))
            return
        oldlab = lab
        lab = fix_one_label(lab, reverse=reverse)
        fix_data['label_suffix'] = lab
        fix_data['label'] = f['label'].replace(oldlab,lab)
        fix_data['short_label'] = f['short_label'].replace(oldlab,lab)
        fix_data['label_nsuffix'] = class_to_int(lab)
        print("using fixed data %s for form %s" % (fix_data,f['label']))
        if fix_forms:
            forms.update({'label': f['label']}, {"$set": fix_data}, upsert=True)

        # find associated elliptic curve and fix that too (where appropriate)
        if f['deg']==2 and f['dimension']==1:
            label = f['label']
            for e in nfcurves.find({'class_label':f['label']}):
                fix_data = {}
                fix_data['iso_label'] = lab
                fix_data['label'] = e['label'].replace(oldlab,lab)
                fix_data['short_label'] = e['short_label'].replace(oldlab,lab)
                fix_data['class_label'] = e['class_label'].replace(oldlab,lab)
                fix_data['short_class_label'] = e['short_class_label'].replace(oldlab,lab)
                print("using fixed data %s for curve %s" % (fix_data,e['label']))
                if fix_curves:
                    res = nfcurves.update_one({'_id': e['_id']}, {"$set": fix_data}, upsert=True)
                    assert res.matched_count==1
        else:
            print("No elliptic curve to fix")
Example #22
0
def curve_cmp(E1, E2):
    r"""
    Comparison function for elliptic curves over `\QQ`.

    Order by label if in the database, else first by conductor, then
    by c_invariants.

    Deprecated, please use instead `curve_key`.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.ell_egros import curve_cmp
        sage: E1 = EllipticCurve_from_j(1728)
        sage: E2 = EllipticCurve_from_j(1729)
        sage: curve_cmp(E1,E2)
        doctest:...: DeprecationWarning: Please use 'curve_key' instead.
        See http://trac.sagemath.org/21142 for details.
        -1
    """
    from sage.misc.superseded import deprecation
    deprecation(21142, "Please use 'curve_key' instead.")
    t = cmp(E1.conductor(), E2.conductor())
    if t:
        return t

    # Now they have the same conductor
    try:
        from sage.databases.cremona import parse_cremona_label, class_to_int
        k1 = parse_cremona_label(E1.label())
        k2 = parse_cremona_label(E2.label())
        t = cmp(class_to_int(k1[1]),class_to_int(k2[1]))
        if t:
            return t
        return cmp(k1[2], k2[2])
    except RuntimeError: # if not in database, label() will fail
        pass

    return cmp(E1.ainvs(),E2.ainvs())
Example #23
0
def curve_cmp(E1, E2):
    r"""
    Comparison function for elliptic curves over `\QQ`.

    Order by label if in the database, else first by conductor, then
    by c_invariants.

    Deprecated, please use instead `curve_key`.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.ell_egros import curve_cmp
        sage: E1 = EllipticCurve_from_j(1728)
        sage: E2 = EllipticCurve_from_j(1729)
        sage: curve_cmp(E1,E2)
        doctest:...: DeprecationWarning: Please use 'curve_key' instead.
        See http://trac.sagemath.org/21142 for details.
        -1
    """
    from sage.misc.superseded import deprecation
    deprecation(21142, "Please use 'curve_key' instead.")
    t = cmp(E1.conductor(), E2.conductor())
    if t:
        return t

    # Now they have the same conductor
    try:
        from sage.databases.cremona import parse_cremona_label, class_to_int
        k1 = parse_cremona_label(E1.label())
        k2 = parse_cremona_label(E2.label())
        t = cmp(class_to_int(k1[1]), class_to_int(k2[1]))
        if t:
            return t
        return cmp(k1[2], k2[2])
    except RuntimeError:  # if not in database, label() will fail
        pass

    return cmp(E1.ainvs(), E2.ainvs())
Example #24
0
def curve_cmp(E1, E2):
    r"""
    Comparison function for elliptic curves over `Q`.

    Order by label if in the database, else first by conductor, then
    by c_invariants.
    """
    t = cmp(E1.conductor(), E2.conductor())
    if t:
        return t

    # Now they have the same conductor
    try:
        from sage.databases.cremona import parse_cremona_label, class_to_int
        k1 = parse_cremona_label(E1.label())
        k2 = parse_cremona_label(E2.label())
        t = cmp(class_to_int(k1[1]), class_to_int(k2[1]))
        if t:
            return t
        return cmp(k1[2], k2[2])
    except RuntimeError:  # if not in database, label() will fail
        pass

    return cmp(E1.ainvs(), E2.ainvs())
Example #25
0
def parse_character(inp,
                    query,
                    qfield,
                    level_field='level',
                    conrey_field='char_labels'):
    pair = inp.split('.')
    if len(pair) != 2:
        raise ValueError("It must be of the form N.i")
    level, orbit = pair
    level = int(level)
    if level_field in query and query[level_field] != level:
        raise ValueError("Inconsistent level")
    query[level_field] = level
    if orbit.isalpha():
        query[qfield] = class_to_int(
            orbit) + 1  # we don't store the prim_orbit_label
    else:
        if conrey_field is None:
            raise ValueError(
                "You must use the orbit label when searching by primitive character"
            )
        query[conrey_field] = {'$contains': int(orbit)}
Example #26
0
def numerify_iso_label(lab):
    from sage.databases.cremona import class_to_int
    if 'CM' in lab:
        return -1 - class_to_int(lab[2:])
    else:
        return class_to_int(lab.lower())
Example #27
0
def minimal_conrey_in_character_orbit(level, weight, char_orbit):
    if isinstance(char_orbit, basestring):
        char_orbit = class_to_int(char_orbit) + 1
    res = db.mf_newspaces.lucky({'level': level, 'weight': weight, 'char_orbit_index':char_orbit}, projection='char_labels')
    return None if res is None else res[0]
Example #28
0
def cmp_label(lab1, lab2):
    a, b, c = parse_cremona_label(lab1)
    id1 = int(a), class_to_int(b), int(c)
    a, b, c = parse_cremona_label(lab2)
    id2 = int(a), class_to_int(b), int(c)
    return cmp(id1, id2)
Example #29
0
def import_data(hmf_filename, fileprefix=None, ferrors=None, test=True):
    if fileprefix == None:
        fileprefix = "."
    hmff = file(os.path.join(fileprefix, hmf_filename))

    if ferrors == None:
        if Dan_test:
            ferrors = file(
                '/Users/d_yasaki/repos/lmfdb/lmfdb/scripts/hmf/fixing-permuted-primes/import_data.err',
                'a')
        else:
            ferrors = file('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:][:-2])
    v = hmff.readline()
    assert v[:4] == 'n :='
    n = int(v[4:][:-2])
    v = hmff.readline()
    assert v[:4] == 'd :='
    d = int(v[4:][:-2])

    magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
    magma.eval('ZF := Integers(F);')

    # Find the corresponding field in the database of number fields
    dkey = make_disc_key(ZZ(d))[1]
    sig = "%s,%s" % (n, 0)
    print("Finding field with signature %s and disc_key %s ..." % (sig, dkey))
    fields_matching = fields.find({"disc_abs_key": dkey, "signature": sig})
    cnt = fields_matching.count()
    print("Found %s fields" % cnt)
    assert cnt >= 1
    field_label = None
    co = str(coeffs)[1:-1].replace(" ", "")
    for i in range(cnt):
        nf = fields_matching.next()
        print("Comparing coeffs %s with %s" % (nf['coeffs'], co))
        if nf['coeffs'] == co:
            field_label = nf['label']
    assert field_label is not None
    print "...found!"

    # Find the field in the HMF database
    print("Finding field %s in HMF..." % field_label)
    F_hmf = hmf_fields.find_one({"label": field_label})
    if Dan_test:
        assert F_hmf is not None  # Assuming the hmf field is already there!
    if F_hmf is None:
        # Create list of ideals
        print "...adding!"
        ideals = eval(preparse(magma.eval('nice_idealstr(F);')))
        ideals_str = [str(c) for c in ideals]
        if test:
            print("Would now insert data for %s into hmf_fields" % field_label)
        else:
            hmf_fields.insert_one({
                "label": field_label,
                "degree": n,
                "discriminant": d,
                "ideals": ideals_str
            })
        F_hmf = hmf_fields.find_one({"label": field_label})
    else:
        print "...found!"

    print "Computing ideals..."
    ideals_str = F_hmf['ideals']
    ideals = [eval(preparse(c)) for c in ideals_str]
    ideals_norms = [c[0] for c in ideals]
    magma.eval('ideals_str := [' +
               ''.join(F_hmf['ideals']).replace('][', '], [') + ']')
    magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

    # Add the list of primes
    print "Computing primes..."
    v = hmff.readline()  # Skip line
    v = hmff.readline()
    assert v[:9] == 'PRIMES :='
    primes_str = v[10:][:-2]
    primes_array = [str(t) for t in eval(preparse(primes_str))]
    magma.eval('primes_array := ' + primes_str)
    magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_array];')
    magma.eval('primes_indices := [Index(ideals, pp) : pp in primes];')
    try:
        assert magma(
            '&and[primes_indices[j] gt primes_indices[j-1] : j in [2..#primes_indices]]'
        )
        resort = False
    except AssertionError:
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Primes reordered!"
        resort = True
        magma.eval('_, sigma := Sort(primes_indices, func<x,y | x-y>);')
        magma.eval(
            'perm := [[xx : xx in x] : x in CycleDecomposition(sigma) | #x gt 1]'
        )
        # Check at least they have the same norm
        magma.eval(
            'for tau in perm do assert #{Norm(ideals[primes_indices[t]]) : t in tau} eq 1; end for;'
        )
        primes_resort = eval(magma.eval('Eltseq(sigma)'))
        primes_resort = [c - 1 for c in primes_resort]

    primes_indices = eval(magma.eval('primes_indices'))
    primes_str = [ideals_str[j - 1] for j in primes_indices]
    assert len(primes_array) == len(primes_str)
    print "...comparing..."
    for i in range(len(primes_array)):
        assert magma('ideal<ZF | {F!x : x in ' + primes_array[i] + '}> eq ' +
                     'ideal<ZF | {F!x : x in ' + primes_str[i] + '}>;')
    if resort:
        # Important also to resort the list of primes themselves!
        # not just the a_pp's
        primes_str = [primes_str[i] for i in primes_resort]
    if Dan_test:
        assert 'primes' in F_hmf  # DY:  want to make sure the fields are not touched!
    if 'primes' in F_hmf:  # Nothing smart: make sure it is the same
        assert F_hmf['primes'] == primes_str
    else:
        F_hmf['primes'] = primes_str
        if test:
            print("Would now save primes string %s... into hmf_fields" %
                  primes_str[:100])
        else:
            hmf_fields.replace_one(F_hmf)
            print "...saved!"

    # Collect levels
    v = hmff.readline()
    if v[:9] == 'LEVELS :=':
        # Skip this line since we do not use the list of levels
        v = hmff.readline()
    for i in range(3):
        if v[:11] != 'NEWFORMS :=':
            v = hmff.readline()
        else:
            break

    # Finally, newforms!
    print "Starting newforms!"
    while v != '':
        v = hmff.readline()[1:-3]
        if len(v) == 0:
            break
        data = eval(preparse(v))
        level_ideal = data[0]
        level_norm = data[0][0]
        label_suffix = fix_one_label(data[1])
        weight = [2 for i in range(n)]
        label_nsuffix = class_to_int(label_suffix)

        level_ind = int(
            magma('Index(ideals, ideal<ZF | {F!x : x in ' + str(level_ideal) +
                  '}>)')) - 1  # Magma counts starting at 1
        level_ideal = ideals_str[level_ind]
        assert magma('ideal<ZF | {F!x : x in ' + str(level_ideal) + '}> eq ' +
                     'ideal<ZF | {F!x : x in ' + str(data[0]) + '}>;')
        level_dotlabel = level_ind - ideals_norms.index(
            level_norm) + 1  # Start counting at 1
        assert level_dotlabel > 0
        level_label = str(level_norm) + '.' + str(level_dotlabel)

        label = construct_full_label(field_label, weight, level_label,
                                     label_suffix)
        short_label = level_label + '-' + label_suffix

        if len(data) == 3:
            hecke_polynomial = x
            hecke_eigenvalues = data[2]
        else:
            hecke_polynomial = data[2]
            hecke_eigenvalues = data[3]

        if resort:
            hecke_eigenvalues = [hecke_eigenvalues[i] for i in primes_resort]

        # Constrain eigenvalues to size 2MB
        hecke_eigenvalues = [str(c) for c in hecke_eigenvalues]
        leftout = 0
        while sum([len(s) for s in hecke_eigenvalues]) > 2000000:
            hecke_eigenvalues = hecke_eigenvalues[:-1]
            leftout += 1
            # commented code below throws an error.  use above.
            # just be safe and remove one eigenvalue at a time.
            # Aurel's code will adjust and remove extra when needed.
            #q = primes_resort[len(hecke_eigenvalues)]
            #while primes_resort[len(hecke_eigenvalues)] == q:
            #    # Remove all with same norm
            #    leftout += 1
            #    hecke_eigenvalues = hecke_eigenvalues[:-1]

        if leftout > 0:
            print "Left out", leftout

        info = {
            "label": label,
            "short_label": short_label,
            "field_label": field_label,
            "level_norm": int(level_norm),
            "level_ideal": str(level_ideal),
            "level_label": level_label,
            "weight": str(weight),
            "label_suffix": label_suffix,
            "label_nsuffix": label_nsuffix,
            "dimension": hecke_polynomial.degree(),
            "hecke_polynomial": str(hecke_polynomial),
            "hecke_eigenvalues": hecke_eigenvalues
        }  # DY: don't deal with AL right now.
        #,
        #"AL_eigenvalues": [[str(a[0]), str(a[1])] for a in AL_eigenvalues]}
        print info['label']

        existing_forms = hmf_forms.find({"label": label})
        assert existing_forms.count() <= 1
        if existing_forms.count() == 0:
            if test:
                print("Would now insert form data %s into hmf_forms" % info)
            else:
                hmf_forms.insert_one(info)
        else:
            existing_form = existing_forms.next()
            assert info['hecke_polynomial'] == existing_form[
                'hecke_polynomial']
            try:
                assert info['hecke_eigenvalues'] == existing_form[
                    'hecke_eigenvalues']
                print "...duplicate"
            except AssertionError:
                print "...Hecke eigenvalues do not match!  Checking for permutation"
                assert set(info['hecke_eigenvalues'] +
                           ['0', '1', '-1']) == set(
                               existing_form['hecke_eigenvalues'] +
                               [u'0', u'1', u'-1'])
                # Add 0,1,-1 to allow for Atkin-Lehner eigenvalues, if not computed
                print "As sets, ignoring 0,1,-1, the eigenvalues match!"
                if test:
                    print("Would now replace permuted form data %s with %s" %
                          (existing_form, info))
                else:
                    existing_form['hecke_eigenvalues'] = info[
                        'hecke_eigenvalues']
                    hmf_forms.save(existing_form)
def get_form_data_from_file(nf, data_dir=NF_DIR, max_dim=50, verbose=False):
    """Fill in data for an incomplete WebNewform object by reading from a
    data file.  If a suitable file does not exist, the original
    WebNewform object is returned unchanged, with a message output.

    The data files only contain the extra data for dimensions less
    than some bound.  If the newform's dimension is greater then we do
    nothing.

    """
    if not nf.field_poly is None:
        return nf
    fname = label = nf.label
    datafile = "/".join([data_dir, fname])
    try:
        data = read_dtp(datafile, verbose=False)
    except FileNotFoundError:
        fname = fname[:fname.rindex(".")]
        datafile = "/".join([data_dir, fname])
        try:
            data = read_dtp(datafile, verbose=False)
        except FileNotFoundError:
            print("No file {} or {} found: no data available".format(datafile,label))
            return nf

    if verbose:
        print("Successfully read data for {} from file {}".format(label, fname))
        
    # Now we have data.  It is a dict with a single key (N,k,o) where
    # o is the character orbit number and value so we just extract the
    # single value for this key, which is another dict:

    data = list(data.values())[0]

    # This dict has keys ['dims', 'traces', 'ALs', 'polys',
    # 'eigdata'], and each value is a list, one per newform, so we
    # need to extract just one item from the relevant lists, according
    # to the newform we want.  This is determined by the 4th (last)
    # part of the label we have, which we need to convert to an
    # integer (from 0).

    # NB eigdata is a list of dicts (see later comment for details)
    # but *only* for components of dimension>1
    
    nf_index = class_to_int(label.split(".")[3])
    dims = data['dims']
    nf_eigdata_index = nf_index - dims.count(1)
    if verbose: # debug
        print("Newform label = {}".format(label))
        print("nf_index = {}".format(nf_index))
        print("len(polys) = {}".format(len(data['polys'])))
        print("len(dims) = {}".format(len(dims)))
        print("#dims>1 = {}".format(len(dims)-dims.count(1)))
        print("len(eigdata) = {}".format(len(data['eigdata'])))
        print("nf_eigdata_index = {}".format(nf_eigdata_index))
    dim = data['dims'][nf_index]
    assert dim == nf.dim
    nf.field_poly = data['polys'][nf_index]

    eigdata = data['eigdata'][nf_eigdata_index]

    # eigdata is a dict with keys  ['poly', 'basis', 'n', 'm', 'ans', 'char']
    chi_gens, chi_vals = eigdata['char']
    nf.hecke_ring_character_values = list(zip(chi_gens, chi_vals))
    # we will not need to access the char_order since this space has
    # already been selected to have an appropriate character.

    if verbose:
        print("hecke_ring_character_values = {}".format(nf.hecke_ring_character_values))
    
    Qx = PolynomialRing(QQ,'x')
    nf.hecke_field = K = NumberField(Qx(nf.field_poly), 'a_')
    nf.betas = [K(b) for b in eigdata['basis']]
    nf.an = eigdata['ans']
    nf.ap = [nf.an[p-1] for p in primes(len(nf.an))]

    if verbose: # debug
        print("We have {} a_n and {} a_p for newforms {}".format(len(nf.an), len(nf.ap), label))
    return nf
Example #31
0
def sorting_label(lab1):
    """
    Provide a sorting key.
    """
    a, b, c = parse_cremona_label(lab1)
    return (int(a), class_to_int(b), int(c))
def import_data(hmf_filename, fileprefix=None, ferrors=None, test=True):
    if fileprefix==None:
        fileprefix="."
    hmff = file(os.path.join(fileprefix,hmf_filename))

    if ferrors==None:
        if Dan_test:
            ferrors = file('/Users/d_yasaki/repos/lmfdb/lmfdb/scripts/hmf/fixing-permuted-primes/import_data.err', 'a')
        else:
            ferrors = file('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:][:-2])
    v = hmff.readline()
    assert v[:4] == 'n :='
    n = int(v[4:][:-2])
    v = hmff.readline()
    assert v[:4] == 'd :='
    d = int(v[4:][:-2])

    magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
    magma.eval('ZF := Integers(F);')

    # Find the corresponding field in the database of number fields
    dkey = make_disc_key(ZZ(d))[1]
    sig = "%s,%s" % (n,0)
    print("Finding field with signature %s and disc_key %s ..." % (sig,dkey))
    fields_matching = fields.find({"disc_abs_key": dkey, "signature": sig})
    cnt = fields_matching.count()
    print("Found %s fields" % cnt)
    assert cnt >= 1
    field_label = None
    co = str(coeffs)[1:-1].replace(" ","")
    for i in range(cnt):
        nf = fields_matching.next()
        print("Comparing coeffs %s with %s" % (nf['coeffs'], co))
        if nf['coeffs'] == co:
            field_label = nf['label']
    assert field_label is not None
    print "...found!"

    # Find the field in the HMF database
    print("Finding field %s in HMF..." % field_label)
    F_hmf = hmf_fields.find_one({"label": field_label})
    if Dan_test:
        assert F_hmf is not None  # Assuming the hmf field is already there!
    if F_hmf is None:
        # Create list of ideals
        print "...adding!"
        ideals = eval(preparse(magma.eval('nice_idealstr(F);')))
        ideals_str = [str(c) for c in ideals]
        if test:
            print("Would now insert data for %s into hmf_fields" % field_label)
        else:
            hmf_fields.insert_one({"label": field_label,
                                   "degree": n,
                                   "discriminant": d,
                                   "ideals": ideals_str})
        F_hmf = hmf_fields.find_one({"label": field_label})
    else:
        print "...found!"

    print "Computing ideals..."
    ideals_str = F_hmf['ideals']
    ideals = [eval(preparse(c)) for c in ideals_str]
    ideals_norms = [c[0] for c in ideals]
    magma.eval('ideals_str := [' + ''.join(F_hmf['ideals']).replace('][', '], [') + ']')
    magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

    # Add the list of primes
    print "Computing primes..."
    v = hmff.readline()  # Skip line
    v = hmff.readline()
    assert v[:9] == 'PRIMES :='
    primes_str = v[10:][:-2]
    primes_array = [str(t) for t in eval(preparse(primes_str))]
    magma.eval('primes_array := ' + primes_str)
    magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_array];')
    magma.eval('primes_indices := [Index(ideals, pp) : pp in primes];')
    try:
        assert magma('&and[primes_indices[j] gt primes_indices[j-1] : j in [2..#primes_indices]]')
        resort = False
    except AssertionError:
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Primes reordered!"
        resort = True
        magma.eval('_, sigma := Sort(primes_indices, func<x,y | x-y>);')
        magma.eval('perm := [[xx : xx in x] : x in CycleDecomposition(sigma) | #x gt 1]')
        # Check at least they have the same norm
        magma.eval('for tau in perm do assert #{Norm(ideals[primes_indices[t]]) : t in tau} eq 1; end for;')
        primes_resort = eval(magma.eval('Eltseq(sigma)'))
        primes_resort = [c - 1 for c in primes_resort]

    primes_indices = eval(magma.eval('primes_indices'))
    primes_str = [ideals_str[j - 1] for j in primes_indices]
    assert len(primes_array) == len(primes_str)
    print "...comparing..."
    for i in range(len(primes_array)):
        assert magma('ideal<ZF | {F!x : x in ' + primes_array[i] + '}> eq '
                     + 'ideal<ZF | {F!x : x in ' + primes_str[i] + '}>;')
    if resort:
        # Important also to resort the list of primes themselves!
        # not just the a_pp's
        primes_str = [primes_str[i] for i in primes_resort]
    if Dan_test:
        assert 'primes' in F_hmf  # DY:  want to make sure the fields are not touched!
    if 'primes' in F_hmf:  # Nothing smart: make sure it is the same
        assert F_hmf['primes'] == primes_str
    else:
        F_hmf['primes'] = primes_str
        if test:
            print("Would now save primes string %s... into hmf_fields" % primes_str[:100])
        else:
            hmf_fields.replace_one(F_hmf)
            print "...saved!"

    # Collect levels
    v = hmff.readline()
    if v[:9] == 'LEVELS :=':
        # Skip this line since we do not use the list of levels
        v = hmff.readline()
    for i in range(3):
        if v[:11] != 'NEWFORMS :=':
            v = hmff.readline()
        else:
            break

    # Finally, newforms!
    print "Starting newforms!"
    while v != '':
        v = hmff.readline()[1:-3]
        if len(v) == 0:
            break
        data = eval(preparse(v))
        level_ideal = data[0]
        level_norm = data[0][0]
        label_suffix = fix_one_label(data[1])
        weight = [2 for i in range(n)]
        label_nsuffix = class_to_int(label_suffix)

        level_ind = int(magma('Index(ideals, ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>)')
                        ) - 1  # Magma counts starting at 1
        level_ideal = ideals_str[level_ind]
        assert magma('ideal<ZF | {F!x : x in ' + str(level_ideal) + '}> eq '
                     + 'ideal<ZF | {F!x : x in ' + str(data[0]) + '}>;')
        level_dotlabel = level_ind - ideals_norms.index(level_norm) + 1   # Start counting at 1
        assert level_dotlabel > 0
        level_label = str(level_norm) + '.' + str(level_dotlabel)

        label = construct_full_label(field_label, weight, level_label, label_suffix)
        short_label = level_label + '-' + label_suffix

        if len(data) == 3:
            hecke_polynomial = x
            hecke_eigenvalues = data[2]
        else:
            hecke_polynomial = data[2]
            hecke_eigenvalues = data[3]

        if resort:
            hecke_eigenvalues = [hecke_eigenvalues[i] for i in primes_resort]

        # Constrain eigenvalues to size 2MB
        hecke_eigenvalues = [str(c) for c in hecke_eigenvalues]
        leftout = 0
        while sum([len(s) for s in hecke_eigenvalues]) > 2000000:
            hecke_eigenvalues = hecke_eigenvalues[:-1]
            leftout += 1
            # commented code below throws an error.  use above.
            # just be safe and remove one eigenvalue at a time.
            # Aurel's code will adjust and remove extra when needed.
            #q = primes_resort[len(hecke_eigenvalues)]
            #while primes_resort[len(hecke_eigenvalues)] == q:
            #    # Remove all with same norm 
            #    leftout += 1
            #    hecke_eigenvalues = hecke_eigenvalues[:-1]
            
        if leftout > 0:
            print "Left out", leftout

        info = {"label": label,
                "short_label": short_label,
                "field_label": field_label,
                "level_norm": int(level_norm),
                "level_ideal": str(level_ideal),
                "level_label": level_label,
                "weight": str(weight),
                "label_suffix": label_suffix,
                "label_nsuffix" : label_nsuffix,
                "dimension": hecke_polynomial.degree(),
                "hecke_polynomial": str(hecke_polynomial),
                "hecke_eigenvalues": hecke_eigenvalues}  # DY: don't deal with AL right now.
                #,
                #"AL_eigenvalues": [[str(a[0]), str(a[1])] for a in AL_eigenvalues]}
        print info['label']

        existing_forms = hmf_forms.find({"label": label})
        assert existing_forms.count() <= 1
        if existing_forms.count() == 0:
            if test:
                print("Would now insert form data %s into hmf_forms" % info)
            else:
                hmf_forms.insert_one(info)
        else:
            existing_form = existing_forms.next()
            assert info['hecke_polynomial'] == existing_form['hecke_polynomial']
            try:
                assert info['hecke_eigenvalues'] == existing_form['hecke_eigenvalues']
                print "...duplicate"
            except AssertionError:
                print "...Hecke eigenvalues do not match!  Checking for permutation"
                assert set(info['hecke_eigenvalues'] + ['0','1','-1']) == set(existing_form['hecke_eigenvalues'] + [u'0',u'1',u'-1'])
                # Add 0,1,-1 to allow for Atkin-Lehner eigenvalues, if not computed
                print "As sets, ignoring 0,1,-1, the eigenvalues match!"
                if test:
                    print("Would now replace permuted form data %s with %s" % (existing_form, info))
                else:
                    existing_form['hecke_eigenvalues'] = info['hecke_eigenvalues']
                    hmf_forms.save(existing_form)
def numerify_iso_label(lab):
    from sage.databases.cremona import class_to_int
    return class_to_int(lab.lower())
Example #34
0
def numerify_iso_label(lab):
    from sage.databases.cremona import class_to_int
    return class_to_int(lab.lower())
Example #35
0
def numerify_iso_label(lab):
    from sage.databases.cremona import class_to_int
    if 'CM' in lab:
        return -1 - class_to_int(lab[2:])
    else:
        return class_to_int(lab.lower())
Example #36
0
def cmp_label(lab1, lab2):
    a, b, c = parse_cremona_label(lab1)
    id1 = int(a), class_to_int(b), int(c)
    a, b, c = parse_cremona_label(lab2)
    id2 = int(a), class_to_int(b), int(c)
    return cmp(id1, id2)