Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/hmf/hmf-count-aps.sage
1452 views
1
# -*- coding: utf-8 -*-
2
r""" Script written just for use at June 2017 LMFDB workshop at Warwick. Output is text files describing the number of a_p that are in that database. Modify as needed.
3
Initial version Dan Yasaki, modified by Aurel Page (2017)
4
5
"""
6
7
import os
8
sys.path.append("../..")
9
from pymongo.mongo_client import MongoClient
10
from lmfdb.WebNumberField import WebNumberField
11
12
print("getting connection")
13
C = MongoClient(port=int(37010))
14
C['admin'].authenticate('lmfdb','lmfdb')
15
hmfs = C.hmfs
16
forms = hmfs.forms
17
fields = hmfs.fields
18
flabels = fields.distinct('label')
19
20
print("authenticating on the hmfs database")
21
import yaml
22
pw_dict = yaml.load(open(os.path.join(os.getcwd(), os.pardir, os.pardir, "passwords.yaml")))
23
username = pw_dict['data']['username']
24
password = pw_dict['data']['password']
25
C['hmfs'].authenticate(username, password) ## read/write on hmfs
26
27
#Caching WebNumberFields
28
WNFs = {}
29
nf_data = {}
30
31
def get_WNF(label):
32
if not label in WNFs:
33
WNFs[label] = WebNumberField(label)
34
return WNFs[label]
35
36
def get_nf_data(label):
37
"""List of allowed numbers of eigenvalues for an HMF over that field.
38
"""
39
if not label in nf_data:
40
WebF = get_WNF(label)
41
F = WebF.K()
42
Fhmf = fields.find_one({'label':label})
43
primes = Fhmf['primes']
44
N = len(primes)
45
last = primes[N-1]
46
last = last.encode()[1:]
47
last = last.split(",")[0]
48
last = ZZ(last)
49
L = [0]
50
for n in range(last+1):
51
p,f = ZZ(n).is_prime_power(get_data=True)
52
if f > 0:
53
Lp = F.primes_above(p, degree=f)
54
L.append(L[len(L)-1]+len(Lp))
55
L.append(Infinity)
56
nf_data[label] = L
57
return nf_data[label]
58
59
def find_discrepancies():
60
with open('numap2-done-fields.txt','w') as donefile:
61
with open('hmf-numap-Np-missing.txt','w') as datafile:
62
for flabel in flabels:
63
print(flabel)
64
F = fields.find_one({'label':flabel})
65
primes = F['primes']
66
for f in forms.find({'field_label':flabel}):
67
num_ap = len(f['hecke_eigenvalues'])
68
discrepancy = len(primes) - num_ap
69
if discrepancy != 0:
70
print(f['label'])
71
Np = F['primes'][num_ap - 1].split(',')[0][1:] #already a string
72
datafile.write(':'.join([f['label'],str(num_ap),Np,str(discrepancy)])+'\n')
73
donefile.write(flabel+'\n')
74
donefile.flush()
75
76
def binary_search(L,x):
77
N = len(L)
78
#this case happens if the form has all the eigenvalues
79
if x == L[N-2]:
80
return x
81
i = 0
82
j = N-1
83
while j-i > 1:
84
k = (i+j)//2
85
if x < L[k]:
86
j = k
87
else:
88
i = k
89
return L[i]
90
91
#note: could be optimised by precomputing the number of primes up to each norm
92
def truncation_bound(f):
93
"""Given an HMF f or its label, compute a bound X on the norm
94
of the primes at which the list of eigenvalues should be truncated
95
to ensure that the list of primes P for which a_P(f) contains every
96
prime up to X. Return the total number of primes of norm up to X
97
and the number of eigenvalues stored for f.
98
"""
99
if type(f) == str:
100
f = forms.find_one({'label':f})
101
num_ap = len(f['hecke_eigenvalues'])
102
field_label = f['field_label']
103
data = get_nf_data(field_label)
104
nb = binary_search(data,num_ap)
105
assert abs(nb - num_ap) <= int(f['label'].split('.')[0]) - 1
106
return nb,num_ap
107
108
def check_form(f, dofix=False):
109
if type(f) == str:
110
f = forms.find_one({'label':f})
111
form_label = f['label']
112
print("\tchecking form " + form_label)
113
nb, nbap = truncation_bound(f)
114
if nb < nbap:
115
print("\tform needs fixing: %s eigenvalues -> truncate at %s eigenvalues" % (nbap, nb))
116
ev = f['hecke_eigenvalues']
117
ev = ev[0:nb]
118
update = {"$set":{'hecke_eigenvalues':ev}}
119
if dofix:
120
res = forms.update_one({'label':form_label}, update)
121
assert res.acknowledged and res.modified_count == 1
122
print("\tform fixed")
123
return 2
124
else:
125
return 1
126
return 0
127
128
def check_field(field_label, dofix=False):
129
print("\nchecking field " + field_label)
130
nbforms = 0
131
nbtodo = 0
132
nbfixed = 0
133
for f in forms.find({'field_label':field_label}):
134
res = check_form(f, dofix=dofix)
135
nbforms += 1
136
if res>0:
137
nbtodo += 1
138
if res==2:
139
nbfixed += 1
140
print("%s forms, %s forms needed fixing, %s forms fixed" % (nbforms, nbtodo, nbfixed))
141
return nbforms, nbtodo, nbfixed
142
143
def check_all(degree, disc_bound=Infinity, dofix=False):
144
totforms = 0
145
tottodo = 0
146
totfixed = 0
147
query = {}
148
query['degree'] = int(degree)
149
if disc_bound < Infinity:
150
query['discriminant'] = {"$lte":int(disc_bound)}
151
LF = fields.find(query)
152
Llab = [F['label'] for F in LF]
153
for field_label in Llab:
154
nbforms, nbtodo, nbfixed = check_field(field_label, dofix=dofix)
155
print("Total so far: %s forms, %s needed fixing, %s fixed" % (totforms, tottodo, totfixed))
156
157