def find_Galois_squarefull_forms():
    forms_labels = []
    for F in hmf_fields.find():
        Fcoeff = fields.find_one({'label' : F['label']})['coeffs']
        magma.eval('F<w> := NumberField(Polynomial([' + str(Fcoeff) + ']));')
        if magma('IsNormal(F) and Degree(F) mod 2 eq 1;'):
            magma.eval('ZF := Integers(F);')
            for f in hmf_forms.find({'field_label' : F['label']}):
                magma.eval('NN := ideal<ZF | SequenceToSet(' + f['level_ideal'] + ')>;');
                if magma('Min([2] cat [ff[2] : ff in Factorization(NN)]) ge 2;'):
                    forms_labels.append(f['label'])
                    print(f['label'])
    return forms_labels
def find_Galois_squarefull_forms():
    forms_labels = []
    for F in hmf_fields.find():
        Fcoeff = fields.find_one({'label' : F['label']})['coeffs']
        magma.eval('F<w> := NumberField(Polynomial([' + str(Fcoeff) + ']));')
        if magma('IsNormal(F) and Degree(F) mod 2 eq 1;'):
            magma.eval('ZF := Integers(F);')
            for f in hmf_forms.find({'field_label' : F['label']}):
                magma.eval('NN := ideal<ZF | SequenceToSet(' + f['level_ideal'] + ')>;');
                if magma('Min([2] cat [ff[2] : ff in Factorization(NN)]) ge 2;'):
                    forms_labels.append(f['label'])
                    print f['label']
    return forms_labels
예제 #3
0
def has_magma():
    """
    Test if Magma is available.

    EXAMPLES::

        sage: from sage.doctest.external import has_magma
        sage: has_magma() # random, optional - magma
        True
    """
    from sage.interfaces.magma import magma
    try:
        magma('2+3')
        return True
    except Exception:
        return False
예제 #4
0
파일: external.py 프로젝트: Babyll/sage
def has_magma():
    """
    Test if Magma is available.

    EXAMPLES::

        sage: from sage.doctest.external import has_magma
        sage: has_magma() # random
        True
    """
    from sage.interfaces.magma import magma
    try:
        magma('2+3')
        return True
    except Exception:
        return False
예제 #5
0
    def _magma_init_(self, magma):
        """
        Internal function. Returns a string to initialize this
        conic in the Magma subsystem.

        EXAMPLES::

            sage: C = Conic(QQ, [1,2,3])
            sage: C._magma_init_(magma)                     # optional - magma
            'Conic([_sage_ref...|1/1,2/1,3/1,0/1,0/1,0/1])'
            sage: C = Conic(GF(41), [-1,2,5])               # optional - magma
            sage: C._magma_init_(magma)                     # optional - magma
            'Conic([_sage_ref...|GF(41)!40,GF(41)!2,GF(41)!5,GF(41)!0,GF(41)!0,GF(41)!0])'
            sage: F.<a> = GF(25)
            sage: C = Conic([3,0,1,4,a,2])
            sage: C
            Projective Conic Curve over Finite Field in a of size 5^2 defined by -2*x^2 - y^2 + x*z + (a)*y*z + 2*z^2
            sage: magma(C)                                  # optional - magma
            Conic over GF(5^2) defined by
            3*X^2 + 4*Y^2 + X*Z + a*Y*Z + 2*Z^2
            sage: magma(Conic([1/2,2/3,-4/5,6/7,8/9,-10/11])) # optional - magma
            Conic over Rational Field defined by
            1/2*X^2 + 2/3*X*Y + 6/7*Y^2 - 4/5*X*Z + 8/9*Y*Z - 10/11*Z^2
            sage: R.<x> = Frac(QQ['x'])
            sage: magma(Conic([x,1+x,1-x]))                 # optional - magma
            Conic over Univariate rational function field over Rational Field defined by
            x*X^2 + (x + 1)*Y^2 + (-x + 1)*Z^2
            sage: P.<x> = QQ[]
            sage: K.<b> = NumberField(x^3+x+1)
            sage: magma(Conic([b,1,2]))                     # optional - magma
            Conic over Number Field with defining polynomial x^3 + x + 1 over the Rational Field defined by
            b*X^2 + Y^2 + 2*Z^2
        """
        kmn = magma(self.base_ring())._ref()
        coeffs = self.coefficients()
        magma_coeffs = [
            coeffs[i]._magma_init_(magma) for i in [0, 3, 5, 1, 4, 2]
        ]
        return 'Conic([%s|%s])' % (kmn, ','.join(magma_coeffs))
예제 #6
0
파일: con_field.py 프로젝트: chos9/sage
    def _magma_init_(self, magma):
        """
        Internal function. Returns a string to initialize this
        conic in the Magma subsystem.

        EXAMPLES::

            sage: C = Conic(QQ, [1,2,3])
            sage: C._magma_init_(magma)                     # optional - magma
            'Conic([_sage_ref...|1/1,2/1,3/1,0/1,0/1,0/1])'
            sage: C = Conic(GF(41), [-1,2,5])               # optional - magma
            sage: C._magma_init_(magma)                     # optional - magma
            'Conic([_sage_ref...|GF(41)!40,GF(41)!2,GF(41)!5,GF(41)!0,GF(41)!0,GF(41)!0])'
            sage: F.<a> = GF(25)
            sage: C = Conic([3,0,1,4,a,2])
            sage: C
            Projective Conic Curve over Finite Field in a of size 5^2 defined by -2*x^2 - y^2 + x*z + (a)*y*z + 2*z^2
            sage: magma(C)                                  # optional - magma
            Conic over GF(5^2) defined by
            3*X^2 + 4*Y^2 + X*Z + a*Y*Z + 2*Z^2
            sage: magma(Conic([1/2,2/3,-4/5,6/7,8/9,-10/11])) # optional - magma
            Conic over Rational Field defined by
            1/2*X^2 + 2/3*X*Y + 6/7*Y^2 - 4/5*X*Z + 8/9*Y*Z - 10/11*Z^2
            sage: R.<x> = Frac(QQ['x'])
            sage: magma(Conic([x,1+x,1-x]))                 # optional - magma
            Conic over Univariate rational function field over Rational Field defined by
            x*X^2 + (x + 1)*Y^2 + (-x + 1)*Z^2
            sage: P.<x> = QQ[]
            sage: K.<b> = NumberField(x^3+x+1)
            sage: magma(Conic([b,1,2]))                     # optional - magma
            Conic over Number Field with defining polynomial x^3 + x + 1 over the Rational Field defined by
            b*X^2 + Y^2 + 2*Z^2
        """
        kmn = magma(self.base_ring())._ref()
        coeffs = self.coefficients()
        magma_coeffs = [coeffs[i]._magma_init_(magma) for i in [0, 3, 5, 1, 4, 2]]
        return 'Conic([%s|%s])' % (kmn,','.join(magma_coeffs))
예제 #7
0
def spanning_maass_products(ring, weight, subspaces = None, lazy_rank_check = False) :
    r"""This function is intended to quickly find good generators. This may be
    calculated in low precision and then ported to higher.
    
    the maass spaces of all subspaces are expected to be ordered the same way as
    ``ring.type()._maass_generator_preimages``.
    """
    ## Outline :
    ##  - get the basis as polynomials form the ring
    ##  - get the maass products, convert them to polynomials and try to 
    ##  construct a basis
    ##  - get the maass space associated to this weight and get its coordinates
    ##  - return a basis containing of maass forms and the products

    if subspaces is None :
        subspaces = dict()
        
    assert ring.base_ring() is QQ or ring.base_ring() is ZZ, \
           "%s doesn't have rational base field" % ring
    
    dim = ring.graded_submodule(weight).dimension()
    precision = ring.fourier_expansion_precision()
    maass_lift_func = ring.type()._maass_generators
    maass_lift_preimage_func = ring.type()._maass_generator_preimages
                
    lift_polys = []; preims = []
    monomials = set()
    
    for k in xrange(min((weight // 2) - ((weight // 2) % 2), weight - 4), 3, -2) :
        if k not in subspaces :
            subspaces[k] = ring.graded_submodule(k)
        if weight - k not in subspaces :
            subspaces[weight - k] = ring.graded_submodule(weight - k)
        
        try :
            kgs = zip(subspaces[k].maass_space()._provided_basis(), maass_lift_preimage_func(k))
            ogs = zip(subspaces[weight - k].maass_space()._provided_basis(), maass_lift_preimage_func(weight - k))
        except ValueError :
            continue
        
        for f_coords,f_pre in kgs :
            for g_coords,g_pre in ogs :
                assert f_coords.parent().graded_ambient() is ring
                assert g_coords.parent().graded_ambient() is ring
 
                f = ring(f_coords)
                g = ring(g_coords)
                    
                fg_poly = (f*g)._reduce_polynomial()
                monomials = monomials.union(set(fg_poly.monomials()))
                
                M = matrix( QQ, len(lift_polys) + 1,
                            [ poly.monomial_coefficient(m)
                              for poly in lift_polys + [fg_poly]
                              for m in monomials] )
                
                # TODO : Use linbox
                try :
                    if magma(M).Rank() <= len(lift_polys) :
                        continue
                except TypeError :
                    for i in xrange(10) :
                        Mp = matrix(Qp(random_prime(10**10), 10), M)
                        if Mp.rank() > len(lift_polys) : break
                    else :
                        if lazy_rank_check :
                            continue
                        elif M.rank() <= len(lift_polys) :
                            continue
                
                lift_polys.append(fg_poly)
                preims.append([f_pre, g_pre])
                        
                if len(lift_polys) == dim :
                    break
                
    if len(lift_polys) == dim :
        ss = construct_from_maass_products(ring, weight, zip(preims, lift_polys), True, False, True)
        poly_coords = [[0]*i + [1] + (len(lift_polys) - i - 1)*[0] for i in xrange(len(lift_polys))]
    else :
        # The products of two Maass lifts don't span this space
        # we hence have to consider the Maass lifts
        ss = ring.graded_submodule(weight)
        poly_coords = [ss.coordinates(ring(p)) for p in lift_polys]

    maass_lifts = maass_lift_func(weight, precision)
    preims[0:0] = [[p] for p in maass_lift_preimage_func(weight)]
    all_coords = map(ss.coordinates, maass_lifts) + poly_coords
    
    M = matrix(QQ, all_coords).transpose()

    nmb_mls = len(maass_lifts)
    for i in xrange(10) :
        Mp = matrix(Qp(random_prime(10**10), 10), M)
        pvs = Mp.pivots()
        if pvs[:nmb_mls] == range(nmb_mls) and \
           len(pvs) == dim :
            break
    else :
        pvs = M.pivots()
        

    if len(pvs) == dim :
        return [(preims[i], ring(ss(all_coords[i])).polynomial()) for i in pvs]
    else :        
        raise RuntimeError, "The products of at most two Maass lifts don't span " + \
                            "this space"
예제 #8
0
def construct_from_maass_products(ring, weight, products, is_basis = True,
                                  provides_maass_spezialschar = False,
                                  is_integral = False,
                                  lazy_rank_check = True) :
    r"""
    Pass the return value of spanning_maass_products of a space of Siegel modular
    forms of same type. This will return a space using these forms.
    Whenever ``is_basis`` is False the the products are first filtered to yield a
    basis.
    """
    assert QQ.has_coerce_map_from(ring.base_ring()) or ring.base_ring() is ZZ, \
           "%s doesn't have rational base field" % ring
           
    dim = ring.graded_submodule(weight).dimension()

    ## if the products don't provide a basis, we have to choose one
    if not is_basis :
        ## we prefer to use Maass lifts, since they are very cheap
        maass_forms = []; non_maass_forms = []
        for p in products :
            if len(p[0]) == 1 :
                maass_forms.append(p)
            else :
                non_maass_forms.append(p)
        
        
        monomials = set()
        lift_polys = []
        products = []
        for lifts, lift_poly in maass_forms + non_maass_forms :
            red_poly = ring(ring.relations().ring()(lift_poly))._reduce_polynomial()
            monomials = monomials.union(set(red_poly.monomials()))
                
            M = matrix( QQ, len(lift_polys) + 1,
                        [ poly.monomial_coefficient(m)
                          for poly in lift_polys + [lift_poly]
                          for m in monomials] )                
                                    
            # TODO : Use linbox
            try :
                if magma(M).Rank() > len(lift_polys) :
                    break
            except TypeError :
                for i in xrange(10) :
                    Mp = matrix(Qp(random_prime(10**10), 10), M)
                    if Mp.rank() > len(lift_polys) : break
                else :
                    if lazy_rank_check :
                        continue
                    elif M.rank() <= len(lift_polys) :
                        continue
                    
            lift_polys.append(red_poly)
            products.append((lifts, red_poly))

            if len(products) == dim :
                break
        else :
            raise ValueError, "products don't provide a basis"
    
    basis = []
     
    if provides_maass_spezialschar :
        maass_form_indices = []
    for i, (lifts, lift_poly) in enumerate(products) :
        e = ring(ring.relations().ring()(lift_poly))

        if len(lifts) == 1 :
            l = lifts[0]
            e._set_fourier_expansion(
                SiegelModularFormG2MaassLift(l[0], l[1], ring.fourier_expansion_precision(),
                                             is_integral = is_integral) )
        elif len(lifts) == 2 :
            (l0, l1) = tuple(lifts)
            e._set_fourier_expansion(
                EquivariantMonoidPowerSeries_LazyMultiplication(
                    SiegelModularFormG2MaassLift(l0[0], l0[1], ring.fourier_expansion_precision(),
                                                 is_integral = is_integral),
                    SiegelModularFormG2MaassLift(l1[0], l1[1], ring.fourier_expansion_precision(),
                                                 is_integral = is_integral) ) ) 

        else :
            e._set_fourier_expansion( 
                prod( SiegelModularFormG2MaassLift(l[0], l[1], ring.precision(),
                                                   is_integral = is_integral)
                      for l in lifts) )            
        basis.append(e)
        
        if provides_maass_spezialschar and len(lifts) == 1 :
            maass_form_indices.append(i)
    
    ss = ring._submodule(basis, grading_indices = (weight,), is_heckeinvariant = True)
    if provides_maass_spezialschar : 
        maass_coords = [ ss([0]*i + [1] + [0]*(dim-i-1))
                         for i in maass_form_indices ]
        ss.maass_space.set_cache( 
          SiegelModularFormG2Submodule_maassspace(ss, map(ss, maass_coords)) )
                                 
    return ss
예제 #9
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)
예제 #10
0
파일: con_field.py 프로젝트: chos9/sage
    def has_rational_point(self, point = False,
                           algorithm = 'default', read_cache = True):
        r"""
        Returns True if and only if the conic ``self``
        has a point over its base field `B`.

        If ``point`` is True, then returns a second output, which is
        a rational point if one exists.

        Points are cached whenever they are found. Cached information
        is used if and only if ``read_cache`` is True.

        ALGORITHM:

        The parameter ``algorithm`` specifies the algorithm
        to be used:

         - ``'default'`` -- If the base field is real or complex,
           use an elementary native Sage implementation.

         - ``'magma'`` (requires Magma to be installed) --
           delegates the task to the Magma computer algebra
           system.

        EXAMPLES:

            sage: Conic(RR, [1, 1, 1]).has_rational_point()
            False
            sage: Conic(CC, [1, 1, 1]).has_rational_point()
            True

            sage: Conic(RR, [1, 2, -3]).has_rational_point(point = True)
            (True, (1.73205080756888 : 0.000000000000000 : 1.00000000000000))

        Conics over polynomial rings can not be solved yet without Magma::

            sage: R.<t> = QQ[]
            sage: C = Conic([-2,t^2+1,t^2-1])
            sage: C.has_rational_point()
            Traceback (most recent call last):
            ...
            NotImplementedError: has_rational_point not implemented for conics over base field Fraction Field of Univariate Polynomial Ring in t over Rational Field

        But they can be solved with Magma::

            sage: C.has_rational_point(algorithm='magma') # optional - magma
            True
            sage: C.has_rational_point(algorithm='magma', point=True) # optional - magma
            (True, (t : 1 : 1))

            sage: D = Conic([t,1,t^2])
            sage: D.has_rational_point(algorithm='magma') # optional - magma
            False

        TESTS:

        One of the following fields comes with an embedding into the complex
        numbers, one does not. Check that they are both handled correctly by
        the Magma interface.::

            sage: K.<i> = QuadraticField(-1)
            sage: K.coerce_embedding()
            Generic morphism:
              From: Number Field in i with defining polynomial x^2 + 1
              To:   Complex Lazy Field
              Defn: i -> 1*I
            sage: Conic(K, [1,1,1]).rational_point(algorithm='magma') # optional - magma
            (-i : 1 : 0)

            sage: x = QQ['x'].gen()
            sage: L.<i> = NumberField(x^2+1, embedding=None)
            sage: Conic(L, [1,1,1]).rational_point(algorithm='magma') # optional - magma
            (-i : 1 : 0)
            sage: L == K
            False
        """
        if read_cache:
            if self._rational_point is not None:
                if point:
                    return True, self._rational_point
                else:
                    return True

        B = self.base_ring()

        if algorithm == 'magma':
            from sage.interfaces.magma import magma
            M = magma(self)
            b = M.HasRationalPoint().sage()
            if not point:
                return b
            if not b:
                return False, None
            M_pt = M.HasRationalPoint(nvals=2)[1]

            # Various attempts will be made to convert `pt` to
            # a Sage object. The end result will always be checked
            # by self.point().

            pt = [M_pt[1], M_pt[2], M_pt[3]]

            # The first attempt is to use sequences. This is efficient and
            # succeeds in cases where the Magma interface fails to convert
            # number field elements, because embeddings between number fields
            # may be lost on conversion to and from Magma.
            # This should deal with all absolute number fields.
            try:
                return True, self.point([B(c.Eltseq().sage()) for c in pt])
            except TypeError:
                pass

            # The second attempt tries to split Magma elements into
            # numerators and denominators first. This is neccessary
            # for the field of rational functions, because (at the moment of
            # writing) fraction field elements are not converted automatically
            # from Magma to Sage.
            try:
                return True, self.point( \
                  [B(c.Numerator().sage()/c.Denominator().sage()) for c in pt])
            except (TypeError, NameError):
                pass

            # Finally, let the Magma interface handle conversion.
            try:
                return True, self.point([B(c.sage()) for c in pt])
            except (TypeError, NameError):
                pass

            raise NotImplementedError, "No correct conversion implemented for converting the Magma point %s on %s to a correct Sage point on self (=%s)" % (M_pt, M, self)

        if algorithm != 'default':
            raise ValueError, "Unknown algorithm: %s" % algorithm

        if is_ComplexField(B):
            if point:
                [_,_,_,d,e,f] = self._coefficients
                if d == 0:
                    return True, self.point([0,1,0])
                return True, self.point([0, ((e**2-4*d*f).sqrt()-e)/(2*d), 1],
                                        check = False)
            return True
        if is_RealField(B):
            D, T = self.diagonal_matrix()
            [a, b, c] = [D[0,0], D[1,1], D[2,2]]
            if a == 0:
                ret = True, self.point(T*vector([1,0,0]), check = False)
            elif a*c <= 0:
                ret = True, self.point(T*vector([(-c/a).sqrt(),0,1]),
                                       check = False)
            elif b == 0:
                ret = True, self.point(T*vector([0,1,0]), check = False)
            elif b*c <= 0:
                ret = True, self.point(T*vector([0,(-c/b).sqrt(),0,1]),
                                       check = False)
            else:
                ret = False, None
            if point:
                return ret
            return ret[0]
        raise NotImplementedError, "has_rational_point not implemented for " \
                                   "conics over base field %s" % B
예제 #11
0
    def has_rational_point(self, point = False,
                           algorithm = 'default', read_cache = True):
        r"""
        Returns True if and only if the conic ``self``
        has a point over its base field `B`.

        If ``point`` is True, then returns a second output, which is
        a rational point if one exists.

        Points are cached whenever they are found. Cached information
        is used if and only if ``read_cache`` is True.

        ALGORITHM:

        The parameter ``algorithm`` specifies the algorithm
        to be used:

         - ``'default'`` -- If the base field is real or complex,
           use an elementary native Sage implementation.

         - ``'magma'`` (requires Magma to be installed) --
           delegates the task to the Magma computer algebra
           system.

        EXAMPLES::

            sage: Conic(RR, [1, 1, 1]).has_rational_point()
            False
            sage: Conic(CC, [1, 1, 1]).has_rational_point()
            True

            sage: Conic(RR, [1, 2, -3]).has_rational_point(point = True)
            (True, (1.73205080756888 : 0.000000000000000 : 1.00000000000000))

        Conics over polynomial rings can be solved internally::

            sage: R.<t> = QQ[]
            sage: C = Conic([-2,t^2+1,t^2-1])
            sage: C.has_rational_point()
            True

        And they can also be solved with Magma::

            sage: C.has_rational_point(algorithm='magma') # optional - magma
            True
            sage: C.has_rational_point(algorithm='magma', point=True) # optional - magma
            (True, (-t : 1 : 1))

            sage: D = Conic([t,1,t^2])
            sage: D.has_rational_point(algorithm='magma') # optional - magma
            False

        TESTS:

        One of the following fields comes with an embedding into the complex
        numbers, one does not. Check that they are both handled correctly by
        the Magma interface. ::

            sage: K.<i> = QuadraticField(-1)
            sage: K.coerce_embedding()
            Generic morphism:
              From: Number Field in i with defining polynomial x^2 + 1 with i = 1*I
              To:   Complex Lazy Field
              Defn: i -> 1*I
            sage: Conic(K, [1,1,1]).rational_point(algorithm='magma') # optional - magma
            (-i : 1 : 0)

            sage: x = QQ['x'].gen()
            sage: L.<i> = NumberField(x^2+1, embedding=None)
            sage: Conic(L, [1,1,1]).rational_point(algorithm='magma') # optional - magma
            (-i : 1 : 0)
            sage: L == K
            False
        """
        if read_cache:
            if self._rational_point is not None:
                if point:
                    return True, self._rational_point
                else:
                    return True

        B = self.base_ring()

        if algorithm == 'magma':
            from sage.interfaces.magma import magma
            M = magma(self)
            b = M.HasRationalPoint().sage()
            if not point:
                return b
            if not b:
                return False, None
            M_pt = M.HasRationalPoint(nvals=2)[1]

            # Various attempts will be made to convert `pt` to
            # a Sage object. The end result will always be checked
            # by self.point().

            pt = [M_pt[1], M_pt[2], M_pt[3]]

            # The first attempt is to use sequences. This is efficient and
            # succeeds in cases where the Magma interface fails to convert
            # number field elements, because embeddings between number fields
            # may be lost on conversion to and from Magma.
            # This should deal with all absolute number fields.
            try:
                return True, self.point([B(c.Eltseq().sage()) for c in pt])
            except TypeError:
                pass

            # The second attempt tries to split Magma elements into
            # numerators and denominators first. This is necessary
            # for the field of rational functions, because (at the moment of
            # writing) fraction field elements are not converted automatically
            # from Magma to Sage.
            try:
                return True, self.point( \
                  [B(c.Numerator().sage()/c.Denominator().sage()) for c in pt])
            except (TypeError, NameError):
                pass

            # Finally, let the Magma interface handle conversion.
            try:
                return True, self.point([B(c.sage()) for c in pt])
            except (TypeError, NameError):
                pass

            raise NotImplementedError("No correct conversion implemented for converting the Magma point %s on %s to a correct Sage point on self (=%s)" % (M_pt, M, self))

        if algorithm != 'default':
            raise ValueError("Unknown algorithm: %s" % algorithm)

        if is_ComplexField(B):
            if point:
                [_,_,_,d,e,f] = self._coefficients
                if d == 0:
                    return True, self.point([0,1,0])
                return True, self.point([0, ((e**2-4*d*f).sqrt()-e)/(2*d), 1],
                                        check = False)
            return True
        if is_RealField(B):
            D, T = self.diagonal_matrix()
            [a, b, c] = [D[0,0], D[1,1], D[2,2]]
            if a == 0:
                ret = True, self.point(T*vector([1,0,0]), check = False)
            elif a*c <= 0:
                ret = True, self.point(T*vector([(-c/a).sqrt(),0,1]),
                                       check = False)
            elif b == 0:
                ret = True, self.point(T*vector([0,1,0]), check = False)
            elif b*c <= 0:
                ret = True, self.point(T*vector([0,(-c/b).sqrt(),0,1]),
                                       check = False)
            else:
                ret = False, None
            if point:
                return ret
            return ret[0]
        raise NotImplementedError("has_rational_point not implemented for " \
                                   "conics over base field %s" % B)
def import_data(hmf_filename, fileprefix=None, ferrors=None, test=True):
    if fileprefix is None:
        fileprefix = "."
    hmff = open(os.path.join(fileprefix, hmf_filename))

    if ferrors is None:
        ferrors = open('/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 = next(fields_matching)
        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 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({
                "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 '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.save(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 = data[1]
        weight = [2 for i in range(n)]

        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]

        # Sort out Atkin-Lehner involutions
        magma.eval('NN := ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>;')
        magma.eval('NNfact := Factorization(NN);')
        ALind = eval(
            preparse(
                magma.eval(
                    '[Index(primes, pp[1])-1 : pp in NNfact | Index(primes,pp[1]) gt 0];'
                )))
        AL_eigsin = [hecke_eigenvalues[c] for c in ALind]
        try:
            assert all([abs(int(c)) <= 1 for c in AL_eigsin])
        except Exception:
            ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        AL_eigenvalues = []
        ees = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))
        for i in range(len(AL_eigsin)):
            if ees[i] >= 2:
                if hecke_eigenvalues[ALind[i]] != 0:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
            else:
                try:
                    if abs(int(AL_eigsin[i])) != 1:
                        ferrors.write(
                            str(n) + '/' + str(d) + ' ' + label + '\n')
                    else:
                        AL_eigenvalues.append(
                            [primes_str[ALind[i]], -AL_eigsin[i]])
                except TypeError:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        assert magma(
            '[Valuation(NN, ideal<ZF | {F!x : x in a}>) gt 0 : a in [' +
            str(''.join([a[0]
                         for a in AL_eigenvalues])).replace('][', '], [') +
            ']];')

        # 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:
            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,
            "dimension": hecke_polynomial.degree(),
            "hecke_polynomial": str(hecke_polynomial),
            "hecke_eigenvalues": hecke_eigenvalues,
            "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(info)
        else:
            existing_form = next(existing_forms)
            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 construct_from_maass_products(ring,
                                  weight,
                                  products,
                                  is_basis=True,
                                  provides_maass_spezialschar=False,
                                  is_integral=False,
                                  lazy_rank_check=True):
    r"""
    Pass the return value of spanning_maass_products of a space of Siegel modular
    forms of same type. This will return a space using these forms.
    Whenever ``is_basis`` is False the the products are first filtered to yield a
    basis.
    """
    assert QQ.has_coerce_map_from(ring.base_ring()) or ring.base_ring() is ZZ, \
           "%s doesn't have rational base field" % ring

    dim = ring.graded_submodule(weight).dimension()

    ## if the products don't provide a basis, we have to choose one
    if not is_basis:
        ## we prefer to use Maass lifts, since they are very cheap
        maass_forms = []
        non_maass_forms = []
        for p in products:
            if len(p[0]) == 1:
                maass_forms.append(p)
            else:
                non_maass_forms.append(p)

        monomials = set()
        lift_polys = []
        products = []
        for lifts, lift_poly in maass_forms + non_maass_forms:
            red_poly = ring(
                ring.relations().ring()(lift_poly))._reduce_polynomial()
            monomials = monomials.union(set(red_poly.monomials()))

            M = matrix(QQ,
                       len(lift_polys) + 1, [
                           poly.monomial_coefficient(m)
                           for poly in lift_polys + [lift_poly]
                           for m in monomials
                       ])

            # TODO : Use linbox
            try:
                if magma(M).Rank() > len(lift_polys):
                    break
            except TypeError:
                for i in xrange(10):
                    Mp = matrix(Qp(random_prime(10**10), 10), M)
                    if Mp.rank() > len(lift_polys): break
                else:
                    if lazy_rank_check:
                        continue
                    elif M.rank() <= len(lift_polys):
                        continue

            lift_polys.append(red_poly)
            products.append((lifts, red_poly))

            if len(products) == dim:
                break
        else:
            raise ValueError, "products don't provide a basis"

    basis = []

    if provides_maass_spezialschar:
        maass_form_indices = []
    for i, (lifts, lift_poly) in enumerate(products):
        e = ring(ring.relations().ring()(lift_poly))

        if len(lifts) == 1:
            l = lifts[0]
            e._set_fourier_expansion(
                SiegelModularFormG2MaassLift(
                    l[0],
                    l[1],
                    ring.fourier_expansion_precision(),
                    is_integral=is_integral))
        elif len(lifts) == 2:
            (l0, l1) = tuple(lifts)
            e._set_fourier_expansion(
                EquivariantMonoidPowerSeries_LazyMultiplication(
                    SiegelModularFormG2MaassLift(
                        l0[0],
                        l0[1],
                        ring.fourier_expansion_precision(),
                        is_integral=is_integral),
                    SiegelModularFormG2MaassLift(
                        l1[0],
                        l1[1],
                        ring.fourier_expansion_precision(),
                        is_integral=is_integral)))

        else:
            e._set_fourier_expansion(
                prod(
                    SiegelModularFormG2MaassLift(
                        l[0], l[1], ring.precision(), is_integral=is_integral)
                    for l in lifts))
        basis.append(e)

        if provides_maass_spezialschar and len(lifts) == 1:
            maass_form_indices.append(i)

    ss = ring._submodule(basis,
                         grading_indices=(weight, ),
                         is_heckeinvariant=True)
    if provides_maass_spezialschar:
        maass_coords = [
            ss([0] * i + [1] + [0] * (dim - i - 1)) for i in maass_form_indices
        ]
        ss.maass_space.set_cache(
            SiegelModularFormG2Submodule_maassspace(ss, map(ss, maass_coords)))

    return ss
예제 #14
0
def import_data(hmf_filename):
    hmff = file(hmf_filename)

    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
    print "Finding field..."
    fields_matching = fields.find({
        "signature": [n, int(0)],
        "discriminant": d
    })
    cnt = fields_matching.count()
    assert cnt >= 1
    field_label = None
    for i in range(cnt):
        nf = fields_matching.next()
        if nf['coefficients'] == coeffs:
            field_label = nf['label']
    assert field_label is not None
    print "...found!"

    # Find the field in the HMF database
    print "Finding field in HMF..."
    F_hmf = hmf_fields.find_one({"label": field_label})
    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]
        hmf_fields.insert({
            "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 'primes' in F_hmf:  # Nothing smart: make sure it is the same
        assert F_hmf['primes'] == primes_str
    else:
        F_hmf['primes'] = primes_str
        hmf_fields.save(F_hmf)
    print "...saved!"

    # Collect levels
    v = hmff.readline()
    if v[:9] == 'LEVELS :=':
        # skip this line, 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 = data[1]
        weight = [2 for i in range(n)]

        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 = parse_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]

        # Sort out Atkin-Lehner involutions
        magma.eval('NN := ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>;')
        magma.eval('NNfact := Factorization(NN);')
        ALind = eval(
            preparse(
                magma.eval(
                    '[Index(primes, pp[1])-1 : pp in NNfact | Index(primes,pp[1]) gt 0];'
                )))
        AL_eigsin = [hecke_eigenvalues[c] for c in ALind]
        try:
            assert all([abs(int(c)) <= 1 for c in AL_eigsin])
        except:
            ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        AL_eigenvalues = []
        ees = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))
        for i in range(len(AL_eigsin)):
            if ees[i] >= 2:
                if hecke_eigenvalues[ALind[i]] != 0:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
            else:
                try:
                    if abs(int(AL_eigsin[i])) != 1:
                        ferrors.write(
                            str(n) + '/' + str(d) + ' ' + label + '\n')
                    else:
                        AL_eigenvalues.append(
                            [primes_str[ALind[i]], -AL_eigsin[i]])
                except TypeError:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        assert magma(
            '[Valuation(NN, ideal<ZF | {F!x : x in a}>) gt 0 : a in [' +
            str(''.join([a[0]
                         for a in AL_eigenvalues])).replace('][', '], [') +
            ']];')

        # 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:
            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,
            "dimension": hecke_polynomial.degree(),
            "hecke_polynomial": str(hecke_polynomial),
            "hecke_eigenvalues": hecke_eigenvalues,
            "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:
            hmf_forms.insert(info)
        else:
            existing_form = existing_forms.next()
            assert info['hecke_eigenvalues'] == existing_form[
                'hecke_eigenvalues']
            print "...duplicate"
def spanning_maass_products(ring,
                            weight,
                            subspaces=None,
                            lazy_rank_check=False):
    r"""This function is intended to quickly find good generators. This may be
    calculated in low precision and then ported to higher.
    
    the maass spaces of all subspaces are expected to be ordered the same way as
    ``ring.type()._maass_generator_preimages``.
    """
    ## Outline :
    ##  - get the basis as polynomials form the ring
    ##  - get the maass products, convert them to polynomials and try to
    ##  construct a basis
    ##  - get the maass space associated to this weight and get its coordinates
    ##  - return a basis containing of maass forms and the products

    if subspaces is None:
        subspaces = dict()

    assert ring.base_ring() is QQ or ring.base_ring() is ZZ, \
           "%s doesn't have rational base field" % ring

    dim = ring.graded_submodule(weight).dimension()
    precision = ring.fourier_expansion_precision()
    maass_lift_func = ring.type()._maass_generators
    maass_lift_preimage_func = ring.type()._maass_generator_preimages

    lift_polys = []
    preims = []
    monomials = set()

    for k in xrange(min((weight // 2) - ((weight // 2) % 2), weight - 4), 3,
                    -2):
        if k not in subspaces:
            subspaces[k] = ring.graded_submodule(k)
        if weight - k not in subspaces:
            subspaces[weight - k] = ring.graded_submodule(weight - k)

        try:
            kgs = zip(subspaces[k].maass_space()._provided_basis(),
                      maass_lift_preimage_func(k))
            ogs = zip(subspaces[weight - k].maass_space()._provided_basis(),
                      maass_lift_preimage_func(weight - k))
        except ValueError:
            continue

        for f_coords, f_pre in kgs:
            for g_coords, g_pre in ogs:
                assert f_coords.parent().graded_ambient() is ring
                assert g_coords.parent().graded_ambient() is ring

                f = ring(f_coords)
                g = ring(g_coords)

                fg_poly = (f * g)._reduce_polynomial()
                monomials = monomials.union(set(fg_poly.monomials()))

                M = matrix(QQ,
                           len(lift_polys) + 1, [
                               poly.monomial_coefficient(m)
                               for poly in lift_polys + [fg_poly]
                               for m in monomials
                           ])

                # TODO : Use linbox
                try:
                    if magma(M).Rank() <= len(lift_polys):
                        continue
                except TypeError:
                    for i in xrange(10):
                        Mp = matrix(Qp(random_prime(10**10), 10), M)
                        if Mp.rank() > len(lift_polys): break
                    else:
                        if lazy_rank_check:
                            continue
                        elif M.rank() <= len(lift_polys):
                            continue

                lift_polys.append(fg_poly)
                preims.append([f_pre, g_pre])

                if len(lift_polys) == dim:
                    break

    if len(lift_polys) == dim:
        ss = construct_from_maass_products(ring, weight,
                                           zip(preims,
                                               lift_polys), True, False, True)
        poly_coords = [[0] * i + [1] + (len(lift_polys) - i - 1) * [0]
                       for i in xrange(len(lift_polys))]
    else:
        # The products of two Maass lifts don't span this space
        # we hence have to consider the Maass lifts
        ss = ring.graded_submodule(weight)
        poly_coords = [ss.coordinates(ring(p)) for p in lift_polys]

    maass_lifts = maass_lift_func(weight, precision)
    preims[0:0] = [[p] for p in maass_lift_preimage_func(weight)]
    all_coords = map(ss.coordinates, maass_lifts) + poly_coords

    M = matrix(QQ, all_coords).transpose()

    nmb_mls = len(maass_lifts)
    for i in xrange(10):
        Mp = matrix(Qp(random_prime(10**10), 10), M)
        pvs = Mp.pivots()
        if pvs[:nmb_mls] == list(range(nmb_mls)) and \
           len(pvs) == dim :
            break
    else:
        pvs = M.pivots()

    if len(pvs) == dim:
        return [(preims[i], ring(ss(all_coords[i])).polynomial()) for i in pvs]
    else:
        raise RuntimeError, "The products of at most two Maass lifts don't span " + \
                            "this space"
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:
        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 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({"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 '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.save(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 = data[1]
        weight = [2 for i in range(n)]

        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]

        # Sort out Atkin-Lehner involutions
        magma.eval('NN := ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>;')
        magma.eval('NNfact := Factorization(NN);')
        ALind = eval(
            preparse(magma.eval('[Index(primes, pp[1])-1 : pp in NNfact | Index(primes,pp[1]) gt 0];')))
        AL_eigsin = [hecke_eigenvalues[c] for c in ALind]
        try:
            assert all([abs(int(c)) <= 1 for c in AL_eigsin])
        except:
            ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        AL_eigenvalues = []
        ees = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))
        for i in range(len(AL_eigsin)):
            if ees[i] >= 2:
                if hecke_eigenvalues[ALind[i]] != 0:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
            else:
                try:
                    if abs(int(AL_eigsin[i])) != 1:
                        ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
                    else:
                        AL_eigenvalues.append([primes_str[ALind[i]], -AL_eigsin[i]])
                except TypeError:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        assert magma('[Valuation(NN, ideal<ZF | {F!x : x in a}>) gt 0 : a in [' +
                     str(''.join([a[0] for a in AL_eigenvalues])).replace('][', '], [') + ']];')

        # 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:
            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,
                "dimension": hecke_polynomial.degree(),
                "hecke_polynomial": str(hecke_polynomial),
                "hecke_eigenvalues": hecke_eigenvalues,
                "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(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 import_data_fix_perm_primes(hmf_filename, fileprefix=None, ferrors=None, test=True):
    if fileprefix==None:
        fileprefix="."
    hmff = file(os.path.join(fileprefix,hmf_filename))

    if ferrors==None:
        ferrors = file('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:].split(';')[0])
    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})
    assert F_hmf is not None    # only proceed if field already in database
    print "...found!"

    print "Computing ideals..."
    ideals_str = F_hmf['ideals']
    # ideals = [eval(preparse(c)) for c in ideals_str] # doesn't appear to be used
    # ideals_norms = [c[0] for c in ideals] # doesn't appear to be used
    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]

    if 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] + '}>;')

        # 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]
        print("Compare primes in hmf.fields\n  %s\n with NEW primes\n  %s" % (F_hmf['primes'], primes_str))
        if test:
            print("...Didn't do anything!  Just a test")
        else:
            F_hmf['primes'] = primes_str
            hmf_fields.save(F_hmf)
            print "...saved!"
예제 #18
0
def import_data_fix_perm_primes(hmf_filename,
                                fileprefix=None,
                                ferrors=None,
                                test=True):
    if fileprefix == None:
        fileprefix = "."
    hmff = file(os.path.join(fileprefix, hmf_filename))

    if ferrors == None:
        ferrors = file('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:].split(';')[0])
    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})
    assert F_hmf is not None  # only proceed if field already in database
    print "...found!"

    print "Computing ideals..."
    ideals_str = F_hmf['ideals']
    # ideals = [eval(preparse(c)) for c in ideals_str] # doesn't appear to be used
    # ideals_norms = [c[0] for c in ideals] # doesn't appear to be used
    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]

    if 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] +
                         '}>;')

        # 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]
        print("Compare primes in hmf.fields\n  %s\n with NEW primes\n  %s" %
              (F_hmf['primes'], primes_str))
        if test:
            print("...Didn't do anything!  Just a test")
        else:
            F_hmf['primes'] = primes_str
            hmf_fields.save(F_hmf)
            print "...saved!"
예제 #19
0
def sage_F_ideal_to_magma(F_magma, x, magma):
    Zm = F_magma.MaximalOrder()
    gens = x.gens_reduced()
    return magma.bar_call(Zm, 'ideal', [Zm(magma(o)) for o in gens])