コード例 #1
0
ファイル: hmf_check_find.py プロジェクト: sehlen/lmfdb
def find_curves(field_label='2.2.5.1', min_norm=0, max_norm=None, label=None, outfilename=None, verbose=False, effort=500):
    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.
    """
    print("Checking forms over {}, norms from {} to {}".format(field_label,min_norm,max_norm))
    if outfilename:
        print("Output of curves found to {}".format(outfilename))
    else:
        print("No curve search or output, just checking")
    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
    if label:
        print("looking for {} only".format(label))
        query['short_label'] = label # e.g. '91.1-a'
    else:
        query['level_norm'] = {'$gte': int(min_norm)}
        if max_norm:
            query['level_norm']['$lte'] = int(max_norm)
    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(1000)]
    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 = parse_ainvs(K.K(), 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]
            f_aplist = [int(a) for a in f['hecke_eigenvalues']]
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags) if flag]
            nap = min(len(aplist), len(f_aplist))
            if aplist[:nap] == f_aplist[:nap]:
                nok += 1
                if verbose:
                    print("Curve {} and newform agree! (checked {} ap)".format(ec['short_label'],nap))
            else:
                print("Curve {} does NOT agree with newform".format(ec['short_label']))
                if verbose:
                    for P,aPf,aPc in zip(good_primes[:nap], f_aplist[:nap], aplist[:nap]):
                        if aPf!=aPc:
                            print("P = {} with norm {}".format(P,P.norm().factor()))
                            print("ap from curve: %s" % aPc)
                            print("ap from  form: %s" % aPf)
                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()
    # (unless outfilename is None in which case just dump the missing labels to a file)

    if outfilename:
        outfile = file(outfilename, mode="w")
    else:
        t = file("curves_missing.{}".format(field_label), mode="w")
        for c in missing_curves:
            t.write(c)
            t.write("\n")
        t.close()
        return

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

    bad_p = []
    #if field_label=='4.4.1600.1': bad_p = [7**2,13**2,29**2]
    if field_label=='4.4.2304.1': bad_p = [19**2,29**2]
    if field_label=='4.4.4225.1': bad_p = [17**2,23**2]
    if field_label=='4.4.7056.1': bad_p = [29**2,31**2]
    if field_label=='4.4.7168.1': bad_p = [29**2]
    if field_label=='4.4.9248.1': bad_p = [23**2]
    if field_label=='4.4.11025.1': bad_p = [17**2,37**2,43**2]
    if field_label=='4.4.13824.1': bad_p = [19**2]
    if field_label=='4.4.12400.1': bad_p = [23**2]
    if field_label=='4.4.180769.1': bad_p = [23**2]
    if field_label=='6.6.905177.1': bad_p = [2**3]
    bad_p = []

    effort0 = effort
    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")

            print("Conductor = %s" % form['level_ideal'].replace(" ",""))
            N = K.ideal(form['level_label'])
            neigs = len(form['hecke_eigenvalues'])
            Plist = [P['ideal'] for P in K.primes_iter(neigs)]
            goodP = [(i, P) for i, P in enumerate(Plist)
                     if not P.divides(N)
                     and not P.norm() in bad_p
                     and P.residue_class_degree()==1]
            aplist = [int(form['hecke_eigenvalues'][i]) for i, P in goodP]
            Plist = [P for i,P in goodP]
            nap = len(Plist)
            neigs0 = min(nap,100)
            effort=effort0
            if verbose:
                print("Using %s ap from Hilbert newform and effort %s" % (neigs0,effort))
                if bad_p:
                    print("( excluding primes with norms {})".format(bad_p))
            #inds = list(set([randint(0,nap-1) for _ in range(neigs0)]))
            inds = range(neigs0)
            Plist0 = [Plist[i] for i in inds]
            aplist0 = [aplist[i] for i in inds]
            curves = EllipticCurveSearch(K.K(), Plist0, N, aplist0, effort)
            # rep = 0
            allrep=0
            while not curves and allrep<10:
                allrep += 1
                effort*=2
                # if rep<2:
                #     rep += 1
                # else:
                #     rep = 1
                #     effort *=2
                if verbose:
                    print("No curves found by Magma, trying again with effort %s..." % effort)
                curves = EllipticCurveSearch(K.K(), Plist0, N, aplist0, effort)
                if verbose:
                    if curves:
                        print("Success!")
                    else:
                        print("Still no success")
            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'] = ";".join([",".join([str(c) for c in list(a)]) for a in ai])
            #print ec['ainvs']
            ec['cm'] = '?'
            ec['base_change'] = []
            output(make_curves_line(ec) + "\n")
            if outfilename:
                outfile.flush()
コード例 #2
0
ファイル: bmf_check.py プロジェクト: SamSchiavone/lmfdb
def check_curves(field_label='2.0.4.1',
                 min_norm=0,
                 max_norm=None,
                 label=None,
                 check_ap=False,
                 verbose=False):
    r"""Go through all Bianchi Modular Forms with the given field label,
    assumed imaginary quadratic (i.e. '2.0.d.1' with d in
    {4,8,3,7,11}), check whether an elliptic curve exists with the
    same label.  If so, and if check_ap is True, check that the a_P agree.

    """
    if field_label not in fields:
        print("No BMF data available for field {}".format(field_label))
        return
    else:
        K = field_from_label(field_label)
    print("Checking forms over {}, norms from {} to {}".format(
        field_label, min_norm, max_norm))
    query = {}
    query['field_label'] = field_label
    query['dimension'] = 1  # only look at rational newforms
    if label:
        print("looking for {} only".format(label))
        query['short_label'] = label  # e.g. '91.1-a'
    else:
        query['level_norm'] = {'$gte': int(min_norm)}
        if max_norm:
            query['level_norm']['$lte'] = int(max_norm)
    cursor = forms.search(query, sort=['level_norm'])
    labels = [f['short_label'] for f in cursor]
    nforms = len(labels)
    print("found {} newforms".format(nforms))
    labels = [lab for lab in labels if lab not in false_curves[field_label]]
    nforms = len(labels)
    print(
        "  of which {} should have associated curves (not false ones)".format(
            nforms))
    nfound = 0
    nnotfound = 0
    nok = 0
    missing_curves = []
    mismatches = []

    primes = list(primes_iter(K, maxnorm=1000)) if check_ap else []
    curve_ap = {}  # curve_ap[conductor_label] will be a dict iso -> ap
    form_ap = {}  # form_ap[conductor_label]  will be a dict iso -> ap

    # Now 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.

    print("checking {} newforms".format(nforms))
    n = 0
    for curve_label in labels:
        n += 1
        if n % 100 == 0:
            perc = 100.0 * n / nforms
            print("{} forms checked ({}%)".format(n, perc))
        # We find the forms again since otherwise the cursor might timeout during the loop.
        label = "-".join([field_label, curve_label])
        if verbose:
            print("newform and isogeny class label {}".format(label))
        f = forms.lucky({'label': label})
        if f:
            if verbose:
                print("found newform with label {}".format(label))
        else:
            print("no newform in database has label {}!".format(label))
            continue
        ec = nfcurves.lucky({'class_label': label, 'number': 1})
        if ec:
            if verbose:
                print("curve with label %s found in the database" %
                      curve_label)
            nfound += 1
            if not check_ap:
                continue
            ainvsK = parse_ainvs(K, ec['ainvs'])
            if verbose:
                print("E = {}".format(ainvsK))
            E = EllipticCurve(ainvsK)
            if verbose:
                print("constructed elliptic curve {}".format(E.ainvs()))
            good_flags = [E.has_good_reduction(P) for P in primes]
            good_primes = [P for (P, flag) in zip(primes, good_flags) if flag]
            if verbose:
                print("{} good primes".format(len(good_primes)))
            f_aplist = f['hecke_eigs']
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags) if flag]
            nap = len(f_aplist)
            if verbose:
                print("recovered {} ap from BMF".format(nap))
            aplist = [
                E.reduction(P).trace_of_frobenius() for P in good_primes[:nap]
            ]
            if verbose:
                print("computed {} ap from elliptic curve".format(nap))
            if aplist[:nap] == f_aplist[:nap]:
                nok += 1
                if verbose:
                    print("Curve {} and newform agree! (checked {} ap)".format(
                        ec['short_label'], nap))
            else:
                print("Curve {} does NOT agree with newform".format(
                    ec['short_label']))
                mismatches.append(label)
                if verbose:
                    for P, aPf, aPc in zip(good_primes[:nap], f_aplist[:nap],
                                           aplist[:nap]):
                        if aPf != aPc:
                            print("P = {} with norm {}".format(
                                P,
                                P.norm().factor()))
                            print("ap from curve: %s" % aPc)
                            print("ap from  form: %s" % aPf)
                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:
        pass
    if mismatches:
        print("{} form-curve pairs had inconsistent ap:".format(
            len(mismatches)))
        print(mismatches)
コード例 #3
0
ファイル: hmf_check_find.py プロジェクト: sehlen/lmfdb
def check_curve_labels(field_label='2.2.5.1', min_norm=0, max_norm=None, fix=False, verbose=False):
    r""" Go through all curves with the given field label, assumed totally
    real, test whether a Hilbert Modular Form exists with the same
    label.
    """
    query = {}
    query['field_label'] = field_label
    query['number'] = 1  # only look at first curve in each isogeny class
    query['conductor_norm'] = {'$gte': int(min_norm)}
    if max_norm:
        query['conductor_norm']['$lte'] = int(max_norm)
    else:
        max_norm = 'infinity'
    cursor = nfcurves.find(query)
    nfound = 0
    nnotfound = 0
    nok = 0
    bad_curves = []
    K = HilbertNumberField(field_label)
    primes = [P['ideal'] for P in K.primes_iter(30)]
    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 curves (one per isogeny class), check that
    # there is a Hilbert newform 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 ec in cursor:
        hmf_label = "-".join([ec['field_label'], ec['conductor_label'], ec['iso_label']])
        f = forms.find_one({'field_label': field_label, 'label': hmf_label})
        if f:
            if verbose:
                print("hmf with label %s found" % hmf_label)
            nfound += 1
            ainvsK = parse_ainvs(K.K(), 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[:10]]
            f_aplist = [int(a) for a in f['hecke_eigenvalues'][:30]]
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags) if flag][:10]
            if aplist == f_aplist:
                nok += 1
                if verbose:
                    print("Curve %s and newform agree!" % ec['short_label'])
            else:
                bad_curves.append(ec['short_label'])
                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 hmf with label %s found!" % hmf_label)
            nnotfound += 1

    # Report progress:

    n = nfound + nnotfound
    if nnotfound:
        print("Out of %s forms, %s were found and %s were not found" % (n, nfound, nnotfound))
    else:
        print("Out of %s classes of curve, all %s had newforms with the same label" % (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))
        # print("Bad curves: %s" % bad_curves)

    # Step 2: for each conductor_label for which there was a
    # discrepancy, create a dict giving the permutation curve -->
    # newform, so remap[conductor_label][iso_label] = form_label

    remap = {}
    for level in curve_ap.keys():
        remap[level] = {}
        c_dat = curve_ap[level]
        f_dat = form_ap[level]
        for a in c_dat.keys():
            aplist = c_dat[a]
            for b in f_dat.keys():
                if aplist == f_dat[b]:
                    remap[level][a] = b
                    break
    if verbose:
        print("remap: %s" % remap)

    # Step 3, for through all curves with these bad conductors and
    # create new labels for them, update the database with these (if
    # fix==True)

    for level in remap.keys():
        perm = remap[level]
        print("Fixing iso labels for conductor %s using map %s" % (level, perm))
        query = {}
        query['field_label'] = field_label
        query['conductor_label'] = level
        cursor = nfcurves.find(query)
        for ec in cursor:
            iso = ec['iso_label']
            if iso in perm:
                new_iso = perm[iso]
                if verbose:
                    print("--mapping class %s to class %s" % (iso, new_iso))
                num = str(ec['number'])
                newlabeldata = {}
                newlabeldata['iso_label'] = new_iso
                newlabeldata['short_class_label'] = '-'.join([level, new_iso])
                newlabeldata['class_label'] = '-'.join([field_label,
                                                        newlabeldata['short_class_label']])
                newlabeldata['short_label'] = ''.join([newlabeldata['short_class_label'], num])
                newlabeldata['label'] = '-'.join([field_label,
                                                  newlabeldata['short_label']])
                if verbose:
                    print("new data fields: %s" % newlabeldata)
                if fix:
                    nfcurves.update({'_id': ec['_id']}, {"$set": newlabeldata}, upsert=True)
コード例 #4
0
ファイル: hmf_check_find.py プロジェクト: sehlen/lmfdb
def find_curve_labels(field_label='2.2.5.1', min_norm=0, max_norm=None, outfilename=None, verbose=False, effort=1000):
    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.
    """
    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()]
    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" % curve_label)
            nfound += 1
            ainvsK = parse_ainvs(K.K(), 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!" % 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 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

    if outfilename==None:
        return

    # Step 2: for each newform for which there was no curve, create a
    # Magma file containing code to search for such a curve.

    # First output Magma code to define the field and primes:
    output_magma_field(field_label, K.K(), primes, outfilename)
    if verbose:
        print("...output definition of field and primes finished")

    for nf_label in missing_curves:
        if verbose:
            print("Curve %s is missing..." % 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, outputting Magma search code")
            output_magma_curve_search(K, form, outfilename, verbose=verbose, effort=effort)
コード例 #5
0
ファイル: hmf_check_find.py プロジェクト: rupertm2/lmfdb
def find_curves(field_label='2.2.5.1',
                min_norm=0,
                max_norm=None,
                label=None,
                outfilename=None,
                verbose=False,
                effort=500):
    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.
    """
    print("Checking forms over {}, norms from {} to {}".format(
        field_label, min_norm, max_norm))
    if outfilename:
        print("Output of curves found to {}".format(outfilename))
    else:
        print("No curve search or output, just checking")
    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
    if label:
        print("looking for {} only".format(label))
        query['short_label'] = label  # e.g. '91.1-a'
    else:
        query['level_norm'] = {'$gte': int(min_norm)}
        if max_norm:
            query['level_norm']['$lte'] = int(max_norm)
    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(1000)]
    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 = parse_ainvs(K.K(), 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]
            f_aplist = [int(a) for a in f['hecke_eigenvalues']]
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags) if flag]
            nap = min(len(aplist), len(f_aplist))
            if aplist[:nap] == f_aplist[:nap]:
                nok += 1
                if verbose:
                    print("Curve {} and newform agree! (checked {} ap)".format(
                        ec['short_label'], nap))
            else:
                print("Curve {} does NOT agree with newform".format(
                    ec['short_label']))
                if verbose:
                    for P, aPf, aPc in zip(good_primes[:nap], f_aplist[:nap],
                                           aplist[:nap]):
                        if aPf != aPc:
                            print("P = {} with norm {}".format(
                                P,
                                P.norm().factor()))
                            print("ap from curve: %s" % aPc)
                            print("ap from  form: %s" % aPf)
                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()
    # (unless outfilename is None in which case just dump the missing labels to a file)

    if outfilename:
        outfile = file(outfilename, mode="w")
    else:
        t = file("curves_missing.{}".format(field_label), mode="w")
        for c in missing_curves:
            t.write(c)
            t.write("\n")
        t.close()
        return

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

    bad_p = []
    #if field_label=='4.4.1600.1': bad_p = [7**2,13**2,29**2]
    if field_label == '4.4.2304.1': bad_p = [19**2, 29**2]
    if field_label == '4.4.4225.1': bad_p = [17**2, 23**2]
    if field_label == '4.4.7056.1': bad_p = [29**2, 31**2]
    if field_label == '4.4.7168.1': bad_p = [29**2]
    if field_label == '4.4.9248.1': bad_p = [23**2]
    if field_label == '4.4.11025.1': bad_p = [17**2, 37**2, 43**2]
    if field_label == '4.4.13824.1': bad_p = [19**2]
    if field_label == '4.4.12400.1': bad_p = [23**2]
    if field_label == '4.4.180769.1': bad_p = [23**2]
    if field_label == '6.6.905177.1': bad_p = [2**3]
    bad_p = []

    effort0 = effort
    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")

            print("Conductor = %s" % form['level_ideal'].replace(" ", ""))
            N = K.ideal(form['level_label'])
            neigs = len(form['hecke_eigenvalues'])
            Plist = [P['ideal'] for P in K.primes_iter(neigs)]
            goodP = [(i, P) for i, P in enumerate(Plist)
                     if not P.divides(N) and not P.norm() in bad_p
                     and P.residue_class_degree() == 1]
            aplist = [int(form['hecke_eigenvalues'][i]) for i, P in goodP]
            Plist = [P for i, P in goodP]
            nap = len(Plist)
            neigs0 = min(nap, 100)
            effort = effort0
            if verbose:
                print("Using %s ap from Hilbert newform and effort %s" %
                      (neigs0, effort))
                if bad_p:
                    print("( excluding primes with norms {})".format(bad_p))
            #inds = list(set([randint(0,nap-1) for _ in range(neigs0)]))
            inds = range(neigs0)
            Plist0 = [Plist[i] for i in inds]
            aplist0 = [aplist[i] for i in inds]
            curves = EllipticCurveSearch(K.K(), Plist0, N, aplist0, effort)
            # rep = 0
            allrep = 0
            while not curves and allrep < 10:
                allrep += 1
                effort *= 2
                # if rep<2:
                #     rep += 1
                # else:
                #     rep = 1
                #     effort *=2
                if verbose:
                    print(
                        "No curves found by Magma, trying again with effort %s..."
                        % effort)
                curves = EllipticCurveSearch(K.K(), Plist0, N, aplist0, effort)
                if verbose:
                    if curves:
                        print("Success!")
                    else:
                        print("Still no success")
            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'] = ";".join(
                [",".join([str(c) for c in list(a)]) for a in ai])
            #print ec['ainvs']
            ec['cm'] = '?'
            ec['base_change'] = []
            output(make_curves_line(ec) + "\n")
            if outfilename:
                outfile.flush()
コード例 #6
0
ファイル: hmf_check_find.py プロジェクト: rupertm2/lmfdb
def find_curve_labels(field_label='2.2.5.1',
                      min_norm=0,
                      max_norm=None,
                      outfilename=None,
                      verbose=False,
                      effort=1000):
    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.
    """
    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()]
    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" % curve_label)
            nfound += 1
            ainvsK = parse_ainvs(K.K(), 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!" % 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 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

    if outfilename == None:
        return

    # Step 2: for each newform for which there was no curve, create a
    # Magma file containing code to search for such a curve.

    # First output Magma code to define the field and primes:
    output_magma_field(field_label, K.K(), primes, outfilename)
    if verbose:
        print("...output definition of field and primes finished")

    for nf_label in missing_curves:
        if verbose:
            print("Curve %s is missing..." % 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, outputting Magma search code")
            output_magma_curve_search(K,
                                      form,
                                      outfilename,
                                      verbose=verbose,
                                      effort=effort)
コード例 #7
0
ファイル: hmf_check_find.py プロジェクト: rupertm2/lmfdb
def check_curve_labels(field_label='2.2.5.1',
                       min_norm=0,
                       max_norm=None,
                       fix=False,
                       verbose=False):
    r""" Go through all curves with the given field label, assumed totally
    real, test whether a Hilbert Modular Form exists with the same
    label.
    """
    query = {}
    query['field_label'] = field_label
    query['number'] = 1  # only look at first curve in each isogeny class
    query['conductor_norm'] = {'$gte': int(min_norm)}
    if max_norm:
        query['conductor_norm']['$lte'] = int(max_norm)
    else:
        max_norm = 'infinity'
    cursor = nfcurves.find(query)
    nfound = 0
    nnotfound = 0
    nok = 0
    bad_curves = []
    K = HilbertNumberField(field_label)
    primes = [P['ideal'] for P in K.primes_iter(30)]
    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 curves (one per isogeny class), check that
    # there is a Hilbert newform 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 ec in cursor:
        hmf_label = "-".join(
            [ec['field_label'], ec['conductor_label'], ec['iso_label']])
        f = forms.find_one({'field_label': field_label, 'label': hmf_label})
        if f:
            if verbose:
                print("hmf with label %s found" % hmf_label)
            nfound += 1
            ainvsK = parse_ainvs(K.K(), 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[:10]
            ]
            f_aplist = [int(a) for a in f['hecke_eigenvalues'][:30]]
            f_aplist = [ap for ap, flag in zip(f_aplist, good_flags)
                        if flag][:10]
            if aplist == f_aplist:
                nok += 1
                if verbose:
                    print("Curve %s and newform agree!" % ec['short_label'])
            else:
                bad_curves.append(ec['short_label'])
                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 hmf with label %s found!" % hmf_label)
            nnotfound += 1

    # Report progress:

    n = nfound + nnotfound
    if nnotfound:
        print("Out of %s forms, %s were found and %s were not found" %
              (n, nfound, nnotfound))
    else:
        print(
            "Out of %s classes of curve, all %s had newforms with the same label"
            % (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))
        # print("Bad curves: %s" % bad_curves)

    # Step 2: for each conductor_label for which there was a
    # discrepancy, create a dict giving the permutation curve -->
    # newform, so remap[conductor_label][iso_label] = form_label

    remap = {}
    for level in curve_ap.keys():
        remap[level] = {}
        c_dat = curve_ap[level]
        f_dat = form_ap[level]
        for a in c_dat.keys():
            aplist = c_dat[a]
            for b in f_dat.keys():
                if aplist == f_dat[b]:
                    remap[level][a] = b
                    break
    if verbose:
        print("remap: %s" % remap)

    # Step 3, for through all curves with these bad conductors and
    # create new labels for them, update the database with these (if
    # fix==True)

    for level in remap.keys():
        perm = remap[level]
        print("Fixing iso labels for conductor %s using map %s" %
              (level, perm))
        query = {}
        query['field_label'] = field_label
        query['conductor_label'] = level
        cursor = nfcurves.find(query)
        for ec in cursor:
            iso = ec['iso_label']
            if iso in perm:
                new_iso = perm[iso]
                if verbose:
                    print("--mapping class %s to class %s" % (iso, new_iso))
                num = str(ec['number'])
                newlabeldata = {}
                newlabeldata['iso_label'] = new_iso
                newlabeldata['short_class_label'] = '-'.join([level, new_iso])
                newlabeldata['class_label'] = '-'.join(
                    [field_label, newlabeldata['short_class_label']])
                newlabeldata['short_label'] = ''.join(
                    [newlabeldata['short_class_label'], num])
                newlabeldata['label'] = '-'.join(
                    [field_label, newlabeldata['short_label']])
                if verbose:
                    print("new data fields: %s" % newlabeldata)
                if fix:
                    nfcurves.update({'_id': ec['_id']}, {"$set": newlabeldata},
                                    upsert=True)
コード例 #8
0
ファイル: hmf_check_find.py プロジェクト: JRSijsling/lmfdb
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)]
            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()
コード例 #9
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)
    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)]
            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()