Exemple #1
0
def export_magma_output(infilename, outfilename=None, verbose=False):
    r"""
    Convert Magma search output to a curves file.

    INPUT:

    - ``infilename`` (string) -- name of file containing Magma output

    - ``outfilename`` (string, default ``None``) -- name of output file

    - ``verbose`` (boolean, default ``False``) -- verbosity flag.
    """
    if outfilename:
        outfile = file(outfilename, mode="w")

    def output(L):
        if outfilename:
            outfile.write(L)
        if verbose:
            sys.stdout.write(L)

    K = None

    for field_label, cond_label, iso_label, num, ai in magma_output_iter(
            infilename):
        ec = {}
        ec['field_label'] = field_label
        if not K:
            K = HilbertNumberField(field_label)
        ec['conductor_label'] = cond_label
        ec['iso_label'] = iso_label
        ec['number'] = num
        N = K.ideal(cond_label)
        norm = N.norm()
        hnf = N.pari_hnf()
        ec['conductor_ideal'] = "[%i,%s,%s]" % (norm, hnf[1][0], hnf[1][1])
        ec['conductor_norm'] = norm
        ec['ainvs'] = [[str(c) for c in list(a)] for a in ai]
        ec['cm'] = '?'
        ec['base_change'] = []
        output(make_curves_line(ec) + "\n")
def export_magma_output(infilename, outfilename=None, verbose=False):
    r"""
    Convert Magma search output to a curves file.

    INPUT:

    - ``infilename`` (string) -- name of file containing Magma output

    - ``outfilename`` (string, default ``None``) -- name of output file

    - ``verbose`` (boolean, default ``False``) -- verbosity flag.
    """
    if outfilename:
        outfile = file(outfilename, mode="w")

    def output(L):
        if outfilename:
            outfile.write(L)
        if verbose:
            sys.stdout.write(L)

    K = None

    for field_label, cond_label, iso_label, num, ai in magma_output_iter(infilename):
        ec = {}
        ec['field_label'] = field_label
        if not K:
            K = HilbertNumberField(field_label)
        ec['conductor_label'] = cond_label
        ec['iso_label'] = iso_label
        ec['number'] = num
        N = K.ideal(cond_label)
        norm = N.norm()
        hnf = N.pari_hnf()
        ec['conductor_ideal'] = "[%i,%s,%s]" % (norm, hnf[1][0], hnf[1][1])
        ec['conductor_norm'] = norm
        ec['ainvs'] = [[str(c) for c in list(a)] for a in ai]
        ec['cm'] = '?'
        ec['base_change'] = []
        output(make_curves_line(ec) + "\n")
Exemple #3
0
def find_curves(field_label='2.2.5.1',
                min_norm=0,
                max_norm=None,
                outfilename=None,
                verbose=False):
    r""" Go through all Hilbert Modular Forms with the given field label,
    assumed totally real, for level norms in the given range, test
    whether an elliptic curve exists with the same label; if not, find
    the curves using Magma; output these to a file.
    """
    query = {}
    query['field_label'] = field_label
    if fields.find({'label': field_label}).count() == 0:
        if verbose:
            print("No HMF data for field %s" % field_label)
        return None

    query['dimension'] = 1  # only look at rational newforms
    query['level_norm'] = {'$gte': int(min_norm)}
    if max_norm:
        query['level_norm']['$lte'] = int(max_norm)
    else:
        max_norm = 'infinity'
    cursor = forms.find(query)
    nfound = 0
    nnotfound = 0
    nok = 0
    missing_curves = []
    K = HilbertNumberField(field_label)
    primes = [P['ideal'] for P in K.primes_iter(100)]
    curve_ap = {}  # curve_ap[conductor_label] will be a dict iso -> ap
    form_ap = {}  # form_ap[conductor_label]  will be a dict iso -> ap

    # Step 1: look at all newforms, check that there is an elliptic
    # curve of the same label, and if so compare ap-lists.  The
    # dicts curve_ap and form_ap store these when there is
    # disagreement: e.g. curve_ap[conductor_label][iso_label] =
    # aplist.

    for f in cursor:
        curve_label = f['label']
        ec = nfcurves.find_one({
            'field_label': field_label,
            'class_label': curve_label,
            'number': 1
        })
        if ec:
            if verbose:
                print("curve with label %s found in the database" %
                      curve_label)
            nfound += 1
            ainvsK = [K.K()([QQ(str(c)) for c in ai]) for ai in ec['ainvs']]
            E = EllipticCurve(ainvsK)
            good_flags = [E.has_good_reduction(P) for P in primes]
            good_primes = [P for (P, flag) in zip(primes, good_flags) if flag]
            aplist = [
                E.reduction(P).trace_of_frobenius() for P in good_primes[:30]
            ]
            f_aplist = [int(a) for a in f['hecke_eigenvalues'][:40]]
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags)
                        if flag][:30]
            if aplist == f_aplist:
                nok += 1
                if verbose:
                    print("Curve %s and newform agree!" % ec['short_label'])
            else:
                print("Curve %s does NOT agree with newform" %
                      ec['short_label'])
                if verbose:
                    print("ap from curve: %s" % aplist)
                    print("ap from  form: %s" % f_aplist)
                if not ec['conductor_label'] in curve_ap:
                    curve_ap[ec['conductor_label']] = {}
                    form_ap[ec['conductor_label']] = {}
                curve_ap[ec['conductor_label']][ec['iso_label']] = aplist
                form_ap[ec['conductor_label']][f['label_suffix']] = f_aplist
        else:
            if verbose:
                print("No curve with label %s found in the database!" %
                      curve_label)
            missing_curves.append(f['short_label'])
            nnotfound += 1

    # Report progress:

    n = nfound + nnotfound
    if nnotfound:
        print(
            "Out of %s newforms, %s curves were found in the database and %s were not found"
            % (n, nfound, nnotfound))
    else:
        print(
            "Out of %s newforms, all %s had curves with the same label and ap"
            % (n, nfound))
    if nfound == nok:
        print("All curves agree with matching newforms")
    else:
        print("%s curves agree with matching newforms, %s do not" %
              (nok, nfound - nok))
    if nnotfound:
        print("Missing curves: %s" % missing_curves)
    else:
        return

    # Step 2: for each newform for which there was no curve, call interface to Magma's EllipticCurveSearch()

    if outfilename:
        outfile = file(outfilename, mode="w")

    def output(L):
        if outfilename:
            outfile.write(L)
        if verbose:
            sys.stdout.write(L)

    for nf_label in missing_curves:
        if verbose:
            print("Curve %s is missing from the database..." % nf_label)
        form = forms.find_one({
            'field_label': field_label,
            'short_label': nf_label
        })
        if not form:
            print("... form %s not found!" % nf_label)
        else:
            if verbose:
                print("... found form, calling Magma search")

            N = K.ideal(form['level_label'])
            Plist = [P['ideal'] for P in K.primes_iter(30)]
            goodP = [(i, P) for i, P in enumerate(Plist) if not P.divides(N)]
            label = form['short_label']
            aplist = [int(form['hecke_eigenvalues'][i]) for i, P in goodP]
            curves = EllipticCurveSearch(K.K(), Plist, N, aplist[:5])
            #curves = EllipticCurveSearch(K.K(), [], N, [])
            if curves:
                E = curves[0]
                print("%s curves found by Magma, first is %s" %
                      (len(curves), E))
            else:
                print("No curves found by Magma (using %s ap)" % len(aplist))

        ec = {}
        ec['field_label'] = field_label
        ec['conductor_label'] = form['level_label']
        ec['iso_label'] = form['label_suffix']
        ec['number'] = int(1)
        ec['conductor_ideal'] = form['level_ideal'].replace(" ", "")
        ec['conductor_norm'] = form['level_norm']
        ai = E.ainvs()
        ec['ainvs'] = [[str(c) for c in list(a)] for a in ai]
        ec['cm'] = '?'
        ec['base_change'] = []
        output(make_curves_line(ec) + "\n")
def find_curves(field_label='2.2.5.1', min_norm=0, max_norm=None, outfilename=None, verbose=False):
    r""" Go through all Hilbert Modular Forms with the given field label,
    assumed totally real, for level norms in the given range, test
    whether an elliptic curve exists with the same label; if not, find
    the curves using Magma; output these to a file.
    """
    query = {}
    query['field_label'] = field_label
    if fields.find({'label': field_label}).count() == 0:
        if verbose:
            print("No HMF data for field %s" % field_label)
        return None

    query['dimension'] = 1  # only look at rational newforms
    query['level_norm'] = {'$gte': int(min_norm)}
    if max_norm:
        query['level_norm']['$lte'] = int(max_norm)
    else:
        max_norm = 'infinity'
    cursor = forms.find(query)
    cursor.sort([('level_norm', pymongo.ASCENDING)])
    labels = [f['label'] for f in cursor]
    nfound = 0
    nnotfound = 0
    nok = 0
    missing_curves = []
    K = HilbertNumberField(field_label)
    primes = [P['ideal'] for P in K.primes_iter(100)]
    curve_ap = {}  # curve_ap[conductor_label] will be a dict iso -> ap
    form_ap = {}  # form_ap[conductor_label]  will be a dict iso -> ap

    # Step 1: look at all newforms, check that there is an elliptic
    # curve of the same label, and if so compare ap-lists.  The
    # dicts curve_ap and form_ap store these when there is
    # disagreement: e.g. curve_ap[conductor_label][iso_label] =
    # aplist.

    for curve_label in labels:
        # We find the forms again since otherwise the cursor might timeout during the loop.
        f = forms.find_one({'label': curve_label})
        ec = nfcurves.find_one({'field_label': field_label, 'class_label': curve_label, 'number': 1})
        if ec:
            if verbose:
                print("curve with label %s found in the database" % curve_label)
            nfound += 1
            ainvsK = [K.K()([QQ(str(c)) for c in ai]) for ai in ec['ainvs']]
            E = EllipticCurve(ainvsK)
            good_flags = [E.has_good_reduction(P) for P in primes]
            good_primes = [P for (P, flag) in zip(primes, good_flags) if flag]
            aplist = [E.reduction(P).trace_of_frobenius() for P in good_primes[:30]]
            f_aplist = [int(a) for a in f['hecke_eigenvalues'][:40]]
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags) if flag][:30]
            if aplist == f_aplist:
                nok += 1
                if verbose:
                    print("Curve %s and newform agree!" % ec['short_label'])
            else:
                print("Curve %s does NOT agree with newform" % ec['short_label'])
                if verbose:
                    print("ap from curve: %s" % aplist)
                    print("ap from  form: %s" % f_aplist)
                if not ec['conductor_label'] in curve_ap:
                    curve_ap[ec['conductor_label']] = {}
                    form_ap[ec['conductor_label']] = {}
                curve_ap[ec['conductor_label']][ec['iso_label']] = aplist
                form_ap[ec['conductor_label']][f['label_suffix']] = f_aplist
        else:
            if verbose:
                print("No curve with label %s found in the database!" % curve_label)
            missing_curves.append(f['short_label'])
            nnotfound += 1

    # Report progress:

    n = nfound + nnotfound
    if nnotfound:
        print("Out of %s newforms, %s curves were found in the database and %s were not found" % (n, nfound, nnotfound))
    else:
        print("Out of %s newforms, all %s had curves with the same label and ap" % (n, nfound))
    if nfound == nok:
        print("All curves agree with matching newforms")
    else:
        print("%s curves agree with matching newforms, %s do not" % (nok, nfound - nok))
    if nnotfound:
        print("%s missing curves" % len(missing_curves))
    else:
        return

    # Step 2: for each newform for which there was no curve, call interface to Magma's EllipticCurveSearch()

    if outfilename:
        outfile = file(outfilename, mode="w")
    def output(L):
        if outfilename:
            outfile.write(L)
        if verbose:
            sys.stdout.write(L)

    for nf_label in missing_curves:
        if verbose:
            print("Curve %s is missing from the database..." % nf_label)
        form = forms.find_one({'field_label': field_label, 'short_label': nf_label})
        if not form:
            print("... form %s not found!" % nf_label)
        else:
            if verbose:
                print("... found form, calling Magma search")

            N = K.ideal(form['level_label'])
            neigs = len(form['hecke_eigenvalues'])
            if verbose:
                print("Using %s ap from Hilbert newform" % neigs)
            Plist = [P['ideal'] for P in K.primes_iter(neigs)]
            goodP = [(i, P) for i, P in enumerate(Plist) if not P.divides(N)]
            label = form['short_label']
            aplist = [int(form['hecke_eigenvalues'][i]) for i, P in goodP]
            curves = EllipticCurveSearch(K.K(), Plist, N, aplist)
            if not curves:
                if verbose:
                    print("No curves found by Magma, trying again...")
                curves = EllipticCurveSearch(K.K(), Plist, N, aplist)
                if verbose:
                    if curves:
                        print("Success!")
                    else:
                        print("Still no success, giving up")
            #curves = EllipticCurveSearch(K.K(), [], N, [])
            E = None
            if curves:
                E = curves[0]
                print("%s curves for %s found, first is %s" % (len(curves),nf_label,E.ainvs()))
            else:
                print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
                print("!!! No curves for %s found (using %s ap) !!!" % (nf_label,len(aplist)))
                print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

        if E!=None:
            ec = {}
            ec['field_label'] = field_label
            ec['conductor_label'] = form['level_label']
            ec['iso_label'] = form['label_suffix']
            ec['number'] = int(1)
            ec['conductor_ideal'] = form['level_ideal'].replace(" ","")
            ec['conductor_norm'] = form['level_norm']
            ai = E.ainvs()
            ec['ainvs'] = [[str(c) for c in list(a)] for a in ai]
            ec['cm'] = '?'
            ec['base_change'] = []
            output(make_curves_line(ec) + "\n")
            if outfilename:
                outfile.flush()