Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/hmf/import_hmf_data_fix_permuted_primes.py
1455 views
1
# -*- coding: utf-8 -*-
2
3
import os
4
import sage.repl.preparse
5
from sage.repl.preparse import preparse
6
from sage.interfaces.magma import magma
7
8
from sage.all import ZZ
9
10
from lmfdb.base import getDBConnection
11
print("getting connection")
12
C= getDBConnection()
13
C['admin'].authenticate('lmfdb', 'lmfdb') # read-only
14
15
#import yaml
16
#pw_dict = yaml.load(open(os.path.join(os.getcwd(), os.extsep, os.extsep, os.extsep, "passwords.yaml")))
17
#username = pw_dict['data']['username']
18
#password = pw_dict['data']['password']
19
#C['hmfs'].authenticate(username, password)
20
hmf_forms = C.hmfs.forms
21
hmf_fields = C.hmfs.fields
22
fields = C.numberfields.fields
23
24
magma.eval('nice_idealstr := function(F : Bound := 10000); idealsstr := []; ideals := IdealsUpTo(Bound, F); for I in ideals do bl, g := IsPrincipal(I); if bl then s := Sprintf("[%o, %o, %o]", Norm(I), Minimum(I), F!g); else zs := Generators(I); z := zs[#zs]; m := Minimum(I); z := F![(Integers()!c) mod m : c in Eltseq(F!z)]; assert ideal<Integers(F) | [m, z]> eq I; s := Sprintf("[%o, %o, %o]", Norm(I), m, z); end if; Append(~idealsstr, s); end for; return idealsstr; end function;')
25
26
from lmfdb.number_fields.number_field import make_disc_key
27
28
P = sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing(sage.rings.rational_field.RationalField(), 3, ['w', 'e', 'x'])
29
w, e, x = P.gens()
30
31
def import_all_data_fix_perm_primes(n, fileprefix=None, ferrors=None, test=True):
32
nstr = str(n)
33
34
if fileprefix is None:
35
fileprefix = "/home/jvoight/Elements/ModFrmHilDatav1/Data/" + nstr
36
ff = open(fileprefix + "/dir.tmp", 'r')
37
files = ff.readlines()
38
files = [f[:-1] for f in files]
39
# subprocess.call("rm dir.tmp", shell=True)
40
41
files = [f for f in files if f.find('_old') == -1]
42
for file_name in files:
43
print("About to import data from file %s" % file_name)
44
import_data_fix_perm_primes(file_name, fileprefix=fileprefix, ferrors=ferrors, test=test)
45
46
47
def import_data_fix_perm_primes(hmf_filename, fileprefix=None, ferrors=None, test=True):
48
if fileprefix is None:
49
fileprefix="."
50
hmff = open(os.path.join(fileprefix,hmf_filename))
51
52
if ferrors is None:
53
ferrors = open('/home/jvoight/lmfdb/backups/import_data.err', 'a')
54
55
# Parse field data
56
v = hmff.readline()
57
assert v[:9] == 'COEFFS :='
58
coeffs = eval(v[10:].split(';')[0])
59
v = hmff.readline()
60
assert v[:4] == 'n :='
61
n = int(v[4:][:-2])
62
v = hmff.readline()
63
assert v[:4] == 'd :='
64
d = int(v[4:][:-2])
65
66
magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
67
magma.eval('ZF := Integers(F);')
68
69
# Find the corresponding field in the database of number fields
70
dkey = make_disc_key(ZZ(d))[1]
71
sig = "%s,%s" % (n,0)
72
print("Finding field with signature %s and disc_key %s ..." % (sig,dkey))
73
fields_matching = fields.find({"disc_abs_key": dkey, "signature": sig})
74
cnt = fields_matching.count()
75
print("Found %s fields" % cnt)
76
assert cnt >= 1
77
field_label = None
78
co = str(coeffs)[1:-1].replace(" ","")
79
for i in range(cnt):
80
nf = next(fields_matching)
81
print("Comparing coeffs %s with %s" % (nf['coeffs'], co))
82
if nf['coeffs'] == co:
83
field_label = nf['label']
84
assert field_label is not None
85
print("...found!")
86
87
# Find the field in the HMF database
88
print("Finding field %s in HMF..." % field_label)
89
F_hmf = hmf_fields.find_one({"label": field_label})
90
assert F_hmf is not None # only proceed if field already in database
91
print("...found!")
92
93
print("Computing ideals...")
94
ideals_str = F_hmf['ideals']
95
# ideals = [eval(preparse(c)) for c in ideals_str] # doesn't appear to be used
96
# ideals_norms = [c[0] for c in ideals] # doesn't appear to be used
97
magma.eval('ideals_str := [' + ''.join(F_hmf['ideals']).replace('][', '], [') + ']')
98
magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')
99
100
# Add the list of primes
101
print("Computing primes...")
102
v = hmff.readline() # Skip line
103
v = hmff.readline()
104
assert v[:9] == 'PRIMES :='
105
primes_str = v[10:][:-2]
106
primes_array = [str(t) for t in eval(preparse(primes_str))]
107
magma.eval('primes_array := ' + primes_str)
108
magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_array];')
109
magma.eval('primes_indices := [Index(ideals, pp) : pp in primes];')
110
try:
111
assert magma('&and[primes_indices[j] gt primes_indices[j-1] : j in [2..#primes_indices]]')
112
resort = False
113
except AssertionError:
114
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Primes reordered!")
115
resort = True
116
magma.eval('_, sigma := Sort(primes_indices, func<x,y | x-y>);')
117
magma.eval('perm := [[xx : xx in x] : x in CycleDecomposition(sigma) | #x gt 1]')
118
# Check at least they have the same norm
119
magma.eval('for tau in perm do assert #{Norm(ideals[primes_indices[t]]) : t in tau} eq 1; end for;')
120
primes_resort = eval(magma.eval('Eltseq(sigma)'))
121
primes_resort = [c - 1 for c in primes_resort]
122
123
if resort:
124
primes_indices = eval(magma.eval('primes_indices'))
125
primes_str = [ideals_str[j - 1] for j in primes_indices]
126
assert len(primes_array) == len(primes_str)
127
print("...comparing...")
128
for i in range(len(primes_array)):
129
assert magma('ideal<ZF | {F!x : x in ' + primes_array[i] + '}> eq '
130
+ 'ideal<ZF | {F!x : x in ' + primes_str[i] + '}>;')
131
132
# Important also to resort the list of primes themselves!
133
# not just the a_pp's
134
primes_str = [primes_str[i] for i in primes_resort]
135
print("Compare primes in hmf.fields\n %s\n with NEW primes\n %s" % (F_hmf['primes'], primes_str))
136
if test:
137
print("...Didn't do anything! Just a test")
138
else:
139
F_hmf['primes'] = primes_str
140
hmf_fields.save(F_hmf)
141
print("...saved!")
142
143