forked from tdupu/root-unitary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_maker.sage
389 lines (369 loc) · 15.3 KB
/
simple_maker.sage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
##############################################################
##################### Data specification #####################
##############################################################
# 0 label string
# 1 g int
# 2 q int
# 3 polynomial list of ints
# 4 angle_numbers list of floats
# 5 angle_rank int ("" if unfilled)
# 6 p_rank int
# 7 slopes list of strings
# 8 A_counts list of ints
# 9 C_counts list of ints
# 10 known_jacobian int
# 11 principally_polarizable int
# 12 decomposition list of pairs [string, int]
# 13 brauer_invariants list of strings
# 14 places list of lists of lists of strings
# 15 primitive_models list of strings ("" if unfilled)
# 16 number_field string ("" if unknown/non-simple)
# 17 galois_n int ("" if non-simple)
# 18 galois_t int ("" if unknown/non-simple)
load("table_functions.sage")
load("prescribed_roots.sage")
from collections import namedtuple
import json
NonsimplePolyData = namedtuple("NonsimplePolyData","g q p r label angle_numbers p_rank slopes jac decomposition invs places polyRingF")
SimplePolyData = namedtuple("SimplePolyData","g q p r label polyRingF")
from lmfdb.WebNumberField import WebNumberField
def check_dataspec(filename):
bad = {}
with open(filename) as F:
for line in F.readlines():
if not line.strip():
bad[100] = 'empty lines'
continue
data = json.loads(line.strip())
if len(data) != 19:
print "%s: %s has length %s"%(filename, data[0], len(data))
break
if not (isinstance(data[14], list) and len(data[14]) > 0 and
isinstance(data[14][0], list) and len(data[14][0]) > 0 and
isinstance(data[14][0][0], basestring)):
bad[14] = data[0]
else:
if bad:
for key in sorted(bad.keys()):
print "%s: %s"%(filename, bad[key])
else:
print filename, "ok"
def check_all(rootdir=None):
rootdir = rootdir or os.path.abspath(os.curdir)
for filename in os.listdir(rootdir):
if simplematcher.match(filename):
check_dataspec(filename)
def know_simple_pp(g, q, coeffs):
if g == 1:
return 1r
if g == 2:
# P-poly: x^4 = ax^3 + bx^2 + aqx + q^2
# Howe, Maisner, Nart, Ritzenthaler
# "Principally polarizable isogeny classes of abelian surfaces over finite fields"
a = ZZ(coeffs[1])
b = ZZ(coeffs[2])
if (a^2 - b == q and b < 0 and all(p % 3 == 1 for p in b.prime_divisors())):
return -1r
else:
return 1r
return 0r
def know_nonsimple_pp(g, q, coeffs):
if g == 2:
return know_simple_pp(g, q, coeffs)
return 0r
def know_simple_jac(g, q, p, r, coeffs, p_rank):
if g == 1:
return 1r
if g == 2:
# P-poly: x^4 + ax^3 + bx^2 + aqx + q^2
# Howe, Nart, Ritzenthaler
# "Jacobians in isogeny classes of abelian surfaces over finite fields"
a = ZZ(coeffs[1])
b = ZZ(coeffs[2])
if (a^2 - b == q and b < 0 and all(p % 3 == 1 for p in b.prime_divisors())
or p_rank == 2 and a == 0 and (b == 1-2*q or p > 2 and b == 2-2*q)
or p_rank == 0 and a == 0 and b == -q and (p % 12 == 11 and r % 2 == 0 or
p == 3 and r % 2 == 0 or
p == 2 and r % 2 == 1)
or p_rank == 0 and a == 0 and b == -2*q and (q == 2 or q == 3)):
return -1r
else:
return 1r
return 0r
def know_nonsimple_jac(g, q, p, r, factors, p_rank):
if g == 2:
# P-poly: (x^2 - sx + q)(x^2 - tx + q) with |s| >= |t|
# Howe, Nart, Ritzenthaler
# "Jacobians in isogeny classes of abelian surfaces over finite fields"
s = -ZZ(factors[0][1])
t = -ZZ(factors[1][1])
if abs(t) > abs(s):
s, t = t, s
if (abs(s - t) == 1
or p_rank == 2 and (s == t and t^2 - 4*q in [-3, -4, -7] or
q == 2 and abs(s) == abs(t) == 1 and s != t)
or p_rank == 1 and r % 2 == 0 and s^2 == 4*q and (s-t).is_squarefree()
or p_rank == 0 and (p > 3 and abs(s) != abs(t) or
p == 3 and r % 2 == 1 and s^2 == t^2 == 3*q or
p == 3 and r % 2 == 0 and (s - t) % (3*p^(r//2)) != 0 or
p == 2 and (s^2 - t^2) % (2*q) != 0 or
q in [2,3] and s == t or
q in [4,9] and s^2 == t^2 == 4*q)):
return -1r
else:
return 1r
return 0r
def create_line(Lpoly, polydata, simple = True):
Ppoly = Lpoly.reverse()
coeffs = Lpoly.coefficients(sparse=False)
Ppolyt = polydata.polyRingF(Ppoly) #p-adic (automatically changes variable from x to t)
if simple:
factors = Ppoly.factor()
if len(factors) != 1:
return ""
factor, power = factors[0]
invs, places, newton_slopes = find_invs_places_and_slopes(polydata.p, polydata.r, factor)
e = lcm([a.denominator() for a in invs])
# When q is not a square, the case 1-qx^2 must be handled separately.
if (not is_square(polydata.q)) and factor.degree() == 2 and factor[1] == 0 and factor[0] < 0:
e = 2
if e != power:
return ""
angle_numbers = map(float, sorted(angles(Lpoly)))
slopes, p_rank = newton_and_prank(polydata.p, polydata.r, Ppolyt)
slopes = [str(s) for s in sorted(slopes)]
jac = know_simple_jac(polydata.g, polydata.q, polydata.p, polydata.r, coeffs, p_rank)
pp = know_simple_pp(polydata.g, polydata.q, coeffs)
decomposition = [[polydata.label, 1r]]
invs = [str(inv) for inv in invs]
places = [places]
galois_n = int(factor.degree())
nf = WebNumberField.from_polynomial(factor)
if nf.label == 'a':
nf_label = galois_t = ""
else:
nf_label = nf.label
galois_t = nf.galois_t()
else:
angle_numbers = polydata.angle_numbers
p_rank = polydata.p_rank
slopes = polydata.slopes
jac = polydata.jac
pp = know_nonsimple_pp(polydata.g, polydata.q, coeffs)
decomposition = polydata.decomposition
invs = polydata.invs
places = polydata.places
nf_label = galois_t = galois_n = ""
C_counts = map(int, curve_counts(polydata.g, polydata.q, Lpoly))
if any(c < 0r for c in C_counts):
if jac == 1r:
raise RuntimeError("Incorrect value for Known Jacobian")
jac = -1r
line = [polydata.label, # label
int(polydata.g), # dim
int(polydata.q), # q
map(int, coeffs), # polynomial coefficients
angle_numbers, # angle numbers
"", # angle ranks
int(p_rank), # p-rank
slopes, # slopes
map(int, abelian_counts(polydata.g, polydata.p, polydata.r, Lpoly)), # A_counts
C_counts, # C_counts
jac, # known jacobian
pp, # principally polarizable
decomposition, # decomposition
invs, # brauer invariants
places, # corresponding ideals
"", # primitive models
nf_label, # number field
galois_n, galois_t] # galois group
return json.dumps(line) + "\n"
def make_simples(g, q):
print "starting g=%s, q=%s..." % (g,q)
p,r = q.is_prime_power(get_data=True)
#open a file for writing
filename = "weil-simple-g%s-q%s.txt"%(g, q)
polyRing.<x> = PolynomialRing(ZZ)
polyRingF.<t> = PolynomialRing(Qp(p)) #p-adic version
Lpolys,_ = roots_on_unit_circle( 1+ (q*x^2)^g )
npolys = len(Lpolys)
with open(filename, 'w') as target:
for polycount,Lpoly in enumerate(Lpolys):
if polycount % 500 == 0 and polycount != 0:
print "%s / %s polynomials complete" % (polycount, npolys)
label = make_label(g, q, Lpoly)
polydata = SimplePolyData(g, q, p, r, label, polyRingF)
target.write(create_line(Lpoly, polydata))
def regen(g, q, rootdir):
print "Regenerating g=%s q=%s"%(g, q)
rootdir = rootdir or os.path.abspath(os.curdir)
filename = os.path.join(rootdir, "weil-simple-g%s-q%s.txt"%(g, q))
p,r = Integer(q).is_prime_power(get_data=True)
polyRing.<x> = PolynomialRing(ZZ)
polyRingF.<t> = PolynomialRing(Qp(p)) #p-adic version
s = ""
counter = 0
with open(filename) as F:
for line in F.readlines():
data = json.loads(line.strip())
label = data[0]
if counter % 100 == 0 and counter != 0:
print "%s (%s)"%(label, counter)
counter += 1
Lpoly = polyRing(data[3])
polydata = SimplePolyData(g, q, p, r, label, polyRingF)
s += create_line(Lpoly, polydata)
with open(filename, 'w') as F:
F.write(s)
def regen_qrange(qmin, qmax, rootdir=None):
rootdir = rootdir or os.path.abspath(os.curdir)
for filename in os.listdir(rootdir):
match = simplematcher.match(filename)
if match:
gf, qf = map(int, match.groups())
if qf >= qmin and qf <= qmax:
regen(gf, qf, rootdir)
def regen_all(rootdir=None):
rootdir = rootdir or os.path.abspath(os.curdir)
for filename in os.listdir(rootdir):
match = simplematcher.match(filename)
if match:
gf, qf = map(int, match.groups())
if gf == 5 and qf == 3:
continue
regen(gf, qf, rootdir)
# def revise_file(g, q):
# print "revising %s, %s"%(g, q)
# p,r = ZZ(q).is_prime_power(get_data=True)
# oldfilename = "weil-%s-%s.txt"%(g, q)
# newfilename = "weil-simple-g%s-q%s.txt"%(g, q)
# s = ""
# polyRing.<x> = PolynomialRing(ZZ)
# polyRingF.<t> = PolynomialRing(Qp(p)) #p-adic version
# with open(oldfilename) as F:
# for line in F.readlines():
# try:
# data = json.loads(line.strip())
# label = data[0]
# data[12] = [[label, 1r]] # decomposition
# s += json.dumps(data) + "\n"
# except ValueError:
# Lpoly = polyRing(json.loads(line[line.find('[',1):line.find(']')+1]))
# label = make_label(g, q, Lpoly)
# polydata = SimplePolyData(g, q, p, r, label, polyRingF)
# s += create_line(Lpoly, polydata)
# with open(newfilename,'w') as F:
# F.write(s)
# def revise_files(rootdir=None):
# rootdir = rootdir or os.path.abspath(os.curdir)
# for filename in os.listdir(rootdir):
# match = oldmatcher.match(filename)
# if match:
# gf, qf = map(int,match.groups())
# revise_file(gf, qf)
# def fill_nf_data(rootdir=None):
# rootdir = rootdir or os.path.abspath(os.curdir)
# R = PolynomialRing(QQ,'x')
# def insert_data(data, newdata):
# data[10] = newdata[0]
# data[13:14] = newdata[1:3]
# data.extend(newdata[3:])
# for filename in os.listdir(rootdir):
# match = simplematcher.match(filename)
# if match:
# g, q = map(int,match.groups())
# if g == 6 and q == 2:
# continue
# print filename
# p,r = Integer(q).is_prime_power(get_data=True)
# D = {}
# s = ""
# done_already = False
# with open(filename) as F:
# for line in F.readlines():
# data = json.loads(line.strip())
# if len(data) == 19:
# done_already = True
# print "Skipping"
# break
# label = data[0]
# print label
# Ppoly = R(data[3]).reverse()
# C_counts = data[9]
# if any(c < 0 for c in C_counts):
# jac = -1r
# else:
# jac = 0r
# factor, power = Ppoly.factor()[0]
# galois_n = int(factor.degree())
# nf = WebNumberField.from_polynomial(factor)
# if nf.label == 'a':
# nf_label = galois_t = ""
# else:
# nf_label = nf.label
# galois_t = int(nf.galois_t())
# invs, places, slopes = find_invs_places_and_slopes(p, r, factor)
# newdata = (jac, [str(inv) for inv in invs], [places], nf_label, galois_n, galois_t)
# D[label] = newdata
# insert_data(data, newdata)
# s += json.dumps(data) + '\n'
# if done_already:
# continue
# with open(filename, 'w') as F:
# F.write(s)
# s = ""
# filename = "weil-all-g%s-q%s.txt"%(g, q)
# print filename
# with open(filename) as F:
# for line in F.readlines():
# data = json.loads(line.strip())
# label = data[0]
# print label
# if label in D:
# insert_data(data, D[label])
# else:
# C_counts = data[9]
# if any(c < 0 for c in C_counts):
# jac = -1r
# else:
# jac = 0r
# insert_data(data, (jac, "", "", "", "", ""))
# s += json.dumps(data) + '\n'
# with open(filename, 'w') as F:
# F.write(s)
def clean(qmin, qmax, rootdir=None):
rootdir = rootdir or os.path.abspath(os.curdir)
qs = set()
for q in srange(qmin, qmax+1):
p, r = q.is_prime_power(get_data=True)
if r != 0: # prime power
revise_file(1,q)
for a in range(1,r+1):
qs.add(p^a)
qs = {1r: sorted(list(qs))}
models = _make_models_dict(qs, rootdir)
_fill_primitive_models(models, qs, rootdir)
def fix_places_and_jac_pp(rootdir=None):
rootdir = rootdir or os.path.abspath(os.curdir)
for filename in os.listdir(rootdir):
match = simplematcher.match(filename)
if match:
print filename
g, q = map(Integer, match.groups())
p, r = q.is_prime_power(get_data=True)
# if g = 2
# q power of 3 or q=r^2 where r=2 (mod 3)
# then pp is True
s = ""
with open(filename) as F:
for line in F.readlines():
data = json.loads(line.strip())
assert len(data) == 19
coeffs = data[3]
p_rank = data[6]
data[10] = know_simple_jac(g, q, p, r, coeffs, p_rank)
data[11] = know_simple_pp(g, q, coeffs)
data[14] = [data[14]]
s += json.dumps(data) + '\n'
with open(filename, 'w') as F:
F.write(s)