/
elliptic_curve.py
878 lines (783 loc) · 34.1 KB
/
elliptic_curve.py
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
# -*- coding: utf-8 -*-
import re
from pymongo import ASCENDING, DESCENDING
import lmfdb.base
from lmfdb.base import app
from flask import Flask, session, g, render_template, url_for, request, redirect, make_response
import tempfile
import os
from lmfdb.utils import ajax_more, image_src, web_latex, to_dict, parse_range2, web_latex_split_on_pm, make_logger, comma, clean_input
logger = make_logger("EllipticCurve")
from number_fields.number_field import parse_list
import sage.all
from sage.all import ZZ, QQ, EllipticCurve, latex, matrix, srange
q = ZZ['x'].gen()
#########################
# Utility functions
#########################
ncurves = nclasses = max_N = max_rank = None
rank_counts = sha_counts = max_sha = tor_counts = None
init_ecdb_flag = False
init_ecdb_stats_flag = False
def init_ecdb_count():
global ncurves, nclasses, max_N, max_rank, init_ecdb_flag
if not init_ecdb_flag:
print "Computing elliptic curve counts..."
ecdb = lmfdb.base.getDBConnection().elliptic_curves.curves
ncurves = ecdb.count()
nclasses = ecdb.find({'number': 1}).count()
max_N = ecdb.find().sort('conductor', DESCENDING).limit(1)[0]['conductor']
max_rank = ecdb.find().sort('rank', DESCENDING).limit(1)[0]['rank']
print "... finished computing elliptic curve counts."
init_ecdb_flag = True
def format_percentage(num, denom):
return "%10.2f"%((100.0*num)/denom)
def init_ecdb_stats():
global rank_counts, max_sha, sha_counts, tor_counts, init_ecdb_stats_flag
init_ecdb_count() # sets max_rank
if not init_ecdb_stats_flag:
print "Computing elliptic curve stats..."
ecdb = lmfdb.base.getDBConnection().elliptic_curves.curves
rank_counts = []
for r in range(max_rank+1):
ncu = ecdb.find({'rank': r}).count()
ncl = ecdb.find({'rank': r, 'number': 1}).count()
prop = format_percentage(ncl,nclasses)
rank_counts.append({'r': r, 'ncurves': ncu, 'nclasses': ncl, 'prop': prop})
tor_counts = []
tor_counts2 = []
for t in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 16]:
ncu = ecdb.find({'torsion': t}).count()
if t in [4,8,12]: # two possible structures
ncyc = ecdb.find({'torsion_structure': [str(t)]}).count()
gp = "\(C_{%s}\)"%t
prop = format_percentage(ncyc,ncurves)
tor_counts.append({'t': t, 'gp': gp, 'ncurves': ncyc, 'prop': prop})
nncyc = ncu-ncyc
gp = "\(C_{2}\\times C_{%s}\)"%(t//2)
prop = format_percentage(nncyc,ncurves)
tor_counts2.append({'t': t, 'gp': gp, 'ncurves': nncyc, 'prop': prop})
elif t==16: # all C_2 x C_8
gp = "\(C_{2}\\times C_{8}\)"
prop = format_percentage(ncu,ncurves)
tor_counts2.append({'t': t, 'gp': gp, 'ncurves': ncu, 'prop': prop})
else: # all cyclic
gp = "\(C_{%s}\)"%t
prop = format_percentage(ncu,ncurves)
tor_counts.append({'t': t, 'gp': gp, 'ncurves': ncu, 'prop': prop})
tor_counts = tor_counts+tor_counts2
max_sha = ecdb.find().sort('sha_an', DESCENDING).limit(1)[0]['sha_an']
sha_counts = []
from math import sqrt
for s in range(1,int(sqrt(max_sha))+1):
s2 = s*s
nc = ecdb.find({'sha_an': { '$gt': s2-0.1, '$lt': s2+0.1}}).count()
if nc:
sha_counts.append({'s': s, 'ncurves': nc})
print "... finished computing elliptic curve stats."
init_ecdb_stats_flag = True
cremona_label_regex = re.compile(r'(\d+)([a-z]+)(\d*)')
lmfdb_label_regex = re.compile(r'(\d+)\.([a-z]+)(\d*)')
sw_label_regex = re.compile(r'sw(\d+)(\.)(\d+)(\.*)(\d*)')
LIST_RE = re.compile(r'^(\d+|(\d+-(\d+)?))(,(\d+|(\d+-(\d+)?)))*$')
TORS_RE = re.compile(r'^\[\]|\[\d+(,\d+)*\]$')
QQ_RE = re.compile(r'^-?\d+(/\d+)?$')
def format_ainvs(ainvs):
"""
The a-invariants are stored as a list of strings because mongodb doesn't
have big-ints, and all strings are stored as unicode. However, printing
a list of unicodes looks like [u'0', u'1', ...]
"""
return [ZZ(a) for a in ainvs]
def xintegral_point(s):
"""
parses integral points
"""
return [int(a) for a in eval(s) if a not in ['[', ',', ']']]
def proj_to_aff(s):
r"""
This is used to convert projective coordinates to affine for integral points
"""
fulllist = []
for x in s:
L = []
for y in x:
if y != ':'and len(L) < 2:
L.append(y)
fulllist.append(tuple(L))
return fulllist
def parse_gens(s):
r"""
Converts projective coordinates to affine coordinates for generator
"""
fulllist = []
for g in s:
g1 = g.replace('(', ' ').replace(')', ' ').split(':')
x, y, z = [ZZ(str(c)) for c in g1]
fulllist.append((x / z, y / z))
return fulllist
def cmp_label(lab1, lab2):
from sage.databases.cremona import parse_cremona_label, class_to_int
a, b, c = parse_cremona_label(lab1)
id1 = int(a), class_to_int(b), int(c)
a, b, c = parse_cremona_label(lab2)
id2 = int(a), class_to_int(b), int(c)
return cmp(id1, id2)
#########################
# Top level
#########################
@app.route("/EC")
def EC_redirect():
return redirect(url_for("rational_elliptic_curves", **request.args))
@app.route("/EllipticCurve")
def EC_toplevel():
return redirect(url_for("rational_elliptic_curves", **request.args))
#########################
# Search/navigate
#########################
@app.route("/EllipticCurve/Q")
def rational_elliptic_curves(err_args=None):
if err_args is None:
if len(request.args) != 0:
return elliptic_curve_search(**request.args)
else:
err_args = {}
for field in ['conductor', 'jinv', 'torsion', 'rank', 'sha_an', 'optimal', 'torsion_structure', 'msg']:
err_args[field] = ''
err_args['count'] = '100'
init_ecdb_count()
conductor_list_endpoints = [1, 100, 1000, 10000, 100000, max_N + 1]
conductor_list = ["%s-%s" % (start, end - 1) for start, end in zip(conductor_list_endpoints[:-1],
conductor_list_endpoints[1:])]
info = {
'rank_list': range(max_rank + 1),
'torsion_list': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 16],
'conductor_list': conductor_list,
'ncurves': comma(ncurves),
'max_N': comma(max_N),
'max_rank': max_rank
}
credit = 'John Cremona'
t = 'Elliptic curves/$\Q$'
bread = [('Elliptic Curves', url_for("rational_elliptic_curves")), ('Elliptic curves/$\Q$', ' ')]
return render_template("elliptic_curve/elliptic_curve_Q.html", info=info, credit=credit, title=t, bread=bread, **err_args)
@app.route("/EllipticCurve/Q/stats")
def statistics():
init_ecdb_count()
init_ecdb_stats()
info = {
'ncurves': comma(ncurves),
'nclasses': comma(nclasses),
'max_N': comma(max_N),
'max_rank': max_rank,
'rank_counts': rank_counts,
'tor_counts': tor_counts,
'max_sha': max_sha,
'sha_counts': sha_counts
}
credit = 'John Cremona'
t = 'Elliptic curves/$\Q$: statistics'
bread = [('Elliptic Curves', url_for("rational_elliptic_curves")), ('Elliptic curves/$\Q$: statistics', ' ')]
return render_template("elliptic_curve/statistics.html", info=info, credit=credit, title=t, bread=bread)
@app.route("/EllipticCurve/Q/<int:conductor>")
def by_conductor(conductor):
return elliptic_curve_search(conductor=conductor, **request.args)
def elliptic_curve_jump_error(label, args, wellformed_label=False, cremona_label=False):
err_args = {}
for field in ['conductor', 'torsion', 'rank', 'sha_an', 'optimal', 'torsion_structure']:
err_args[field] = args.get(field, '')
err_args['count'] = args.get('count', '100')
if wellformed_label:
err_args['err_msg'] = "No curve or isogeny class in database with label %s" % label
elif cremona_label:
err_args['err_msg'] = "To search for a Cremona label use 'Cremona:%s'" % label
else:
err_args['err_msg'] = "Could not understand label %s" % label
return rational_elliptic_curves(err_args)
def elliptic_curve_search(**args):
info = to_dict(args)
query = {}
bread = [('Elliptic Curves', url_for("rational_elliptic_curves")),
('Search Results', '.')]
if 'jump' in args:
label = info.get('label', '').replace(" ", "")
m = lmfdb_label_regex.match(label)
if m:
try:
return by_ec_label(label)
except ValueError:
return elliptic_curve_jump_error(label, info, wellformed_label=True)
elif label.startswith("Cremona:"):
label = label[8:]
m = cremona_label_regex.match(label)
if m:
try:
return by_ec_label(label)
except ValueError:
return elliptic_curve_jump_error(label, info, wellformed_label=True)
elif cremona_label_regex.match(label):
return elliptic_curve_jump_error(label, info, cremona_label=True)
elif label:
# Try to parse a string like [1,0,3,2,4]
lab = re.sub(r'\s','',label)
lab = re.sub(r'^\[','',lab)
lab = re.sub(r']$','',lab)
try:
labvec = lab.split(',')
labvec = [QQ(str(z)) for z in labvec] # Rationals allowed
E = EllipticCurve(labvec)
ainvs = [str(c) for c in E.minimal_model().ainvs()]
C = lmfdb.base.getDBConnection()
data = C.elliptic_curves.curves.find_one({'ainvs': ainvs})
if data is None:
return elliptic_curve_jump_error(label, info)
return by_ec_label(data['lmfdb_label'])
except (ValueError, ArithmeticError):
return elliptic_curve_jump_error(label, info)
else:
query['label'] = ''
if info.get('jinv'):
j = clean_input(info['jinv'])
j = j.replace('+', '')
if not QQ_RE.match(j):
info['err'] = 'Error parsing input for the j-invariant. It needs to be a rational number.'
return search_input_error(info, bread)
query['jinv'] = j
for field in ['conductor', 'torsion', 'rank', 'sha_an']:
if info.get(field):
info[field] = clean_input(info[field])
ran = info[field]
ran = ran.replace('..', '-').replace(' ', '')
if not LIST_RE.match(ran):
names = {'conductor': 'conductor', 'torsion': 'torsion order', 'rank':
'rank', 'sha_an': 'analytic order of Ш'}
info['err'] = 'Error parsing input for the %s. It needs to be an integer (such as 5), a range of integers (such as 2-10 or 2..10), or a comma-separated list of these (such as 2,3,8 or 3-5, 7, 8-11).' % names[field]
return search_input_error(info, bread)
# Past input check
tmp = parse_range2(ran, field)
# work around syntax for $or
# we have to foil out multiple or conditions
if tmp[0] == '$or' and '$or' in query:
newors = []
for y in tmp[1]:
oldors = [dict.copy(x) for x in query['$or']]
for x in oldors:
x.update(y)
newors.extend(oldors)
tmp[1] = newors
if field=='sha_an': # database sha_an values are not all exact!
query[tmp[0]] = { '$gt': tmp[1]-0.1, '$lt': tmp[1]+0.1}
print query
else:
query[tmp[0]] = tmp[1]
if 'optimal' in info and info['optimal'] == 'on':
# fails on 990h3
query['number'] = 1
if 'torsion_structure' in info and info['torsion_structure']:
info['torsion_structure'] = clean_input(info['torsion_structure'])
if not TORS_RE.match(info['torsion_structure']):
info['err'] = 'Error parsing input for the torsion structure. It needs to be one or more integers in square brackets, such as [6], [2,2], or [2,4]. Moreover, each integer should be bigger than 1, and each divides the next.'
return search_input_error(info, bread)
query['torsion_structure'] = [str(a) for a in parse_list(info['torsion_structure'])]
info['query'] = query
count_default = 100
if info.get('count'):
try:
count = int(info['count'])
except:
count = count_default
else:
count = count_default
info['count'] = count
start_default = 0
if info.get('start'):
try:
start = int(info['start'])
if(start < 0):
start += (1 - (start + 1) / count) * count
except:
start = start_default
else:
start = start_default
print query
cursor = lmfdb.base.getDBConnection().elliptic_curves.curves.find(query)
nres = cursor.count()
if(start >= nres):
start -= (1 + (start - nres) / count) * count
if(start < 0):
start = 0
res = cursor.sort([('conductor', ASCENDING), ('lmfdb_iso', ASCENDING), ('lmfdb_number', ASCENDING)
]).skip(start).limit(count)
info['curves'] = res
info['format_ainvs'] = format_ainvs
info['number'] = nres
info['start'] = start
if nres == 1:
info['report'] = 'unique match'
else:
if nres > count or start != 0:
info['report'] = 'displaying matches %s-%s of %s' % (start + 1, min(nres, start + count), nres)
else:
info['report'] = 'displaying all %s matches' % nres
credit = 'John Cremona'
t = 'Elliptic Curves'
return render_template("elliptic_curve/elliptic_curve_search.html", info=info, credit=credit, bread=bread, title=t)
def search_input_error(info, bread):
return render_template("elliptic_curve/elliptic_curve_search.html", info=info, title='Elliptic Curve Search Input Error', bread=bread)
##########################
# Specific curve pages
##########################
@app.route("/EllipticCurve/Q/<label>")
def by_ec_label(label):
logger.debug(label)
try:
N, iso, number = lmfdb_label_regex.match(label).groups()
except AttributeError:
try:
N, iso, number = cremona_label_regex.match(label).groups()
except AttributeError:
return elliptic_curve_jump_error(label, {})
C = lmfdb.base.getDBConnection()
# We permanently redirect to the lmfdb label
if number:
data = C.elliptic_curves.curves.find_one({'label': label})
if data is None:
return elliptic_curve_jump_error(label, {})
logger.debug(url_for("by_ec_label", label=data['lmfdb_label']))
return redirect(url_for("by_ec_label", label=data['lmfdb_label']), 301)
else:
data = C.elliptic_curves.curves.find_one({'iso': label})
if data is None:
return elliptic_curve_jump_error(label, {})
logger.debug(url_for("by_ec_label", label=data['lmfdb_label']))
return redirect(url_for("by_ec_label", label=data['lmfdb_iso']), 301)
# N,d1, iso,d2, number = sw_label_regex.match(label).groups()
if number:
return render_curve_webpage_by_label(label=label)
else:
return render_isogeny_class(str(N) + '.' + iso)
@app.route("/EllipticCurve/Q/plot/<label>")
def plot_ec(label):
C = lmfdb.base.getDBConnection()
data = C.elliptic_curves.curves.find_one({'lmfdb_label': label})
if data is None:
return elliptic_curve_jump_error(label, {})
ainvs = [int(a) for a in data['ainvs']]
E = EllipticCurve(ainvs)
P = E.plot()
_, filename = tempfile.mkstemp('.png')
P.save(filename)
data = open(filename).read()
os.unlink(filename)
response = make_response(data)
response.headers['Content-type'] = 'image/png'
return response
@app.route("/EllipticCurve/Q/iso_graph/<label>")
def plot_iso_graph(label):
C = lmfdb.base.getDBConnection()
data = C.elliptic_curves.curves.find_one({'lmfdb_iso': label})
if data is None:
return elliptic_curve_jump_error(label, {})
ainvs = [int(a) for a in data['ainvs']]
E = EllipticCurve(ainvs)
G = E.isogeny_graph()
n = G.num_verts()
G.relabel(range(1, n + 1)) # proper lmfdb labels...
P = G.plot(edge_labels=True, layout='spring')
_, filename = tempfile.mkstemp('.png')
P.save(filename)
data = open(filename).read()
os.unlink(filename)
response = make_response(data)
response.headers['Content-type'] = 'image/png'
return response
def render_isogeny_class(iso_class):
info = {}
credit = 'John Cremona'
lmfdb_iso = iso_class # e.g. '11.a'
N, iso, number = lmfdb_label_regex.match(lmfdb_iso).groups()
CDB = lmfdb.base.getDBConnection().elliptic_curves.curves
E1data = CDB.find_one({'lmfdb_label': lmfdb_iso + '1'})
if E1data is None:
return elliptic_curve_jump_error(lmfdb_iso, {})
cremona_iso = E1data['iso']
ainvs = [int(a) for a in E1data['ainvs']]
E1 = EllipticCurve(ainvs)
curves, mat = E1.isogeny_class()
size = len(curves)
# Create a list of the curves in the class from the database, so
# they are in the correct order!
db_curves = [E1]
optimal_flags = [False] * size
degrees = [0] * size
if 'degree' in E1data:
degrees[0] = E1data['degree']
else:
try:
degrees[0] = E1.modular_degree()
except RuntimeError:
pass
cremona_labels = [E1data['label']] + [0] * (size - 1)
if E1data['number'] == 1:
optimal_flags[0] = True
for i in range(2, size + 1):
Edata = CDB.find_one({'lmfdb_label': lmfdb_iso + str(i)})
E = EllipticCurve([int(a) for a in Edata['ainvs']])
cremona_labels[i - 1] = Edata['label']
if Edata['number'] == 1:
optimal_flags[i - 1] = True
if 'degree' in Edata:
degrees[i - 1] = Edata['degree']
else:
try:
degrees[i - 1] = E.modular_degree()
except RuntimeError:
pass
db_curves.append(E)
if cremona_iso == '990h': # this isogeny class is labeled wrong in Cremona's tables
optimal_flags = [False, False, True, False]
# Now work out the permutation needed to match the two lists of curves:
perm = [db_curves.index(E) for E in curves]
# Apply the same permutation to the isogeny matrix:
mat = [[mat[perm[i], perm[j]] for j in range(size)] for i in range(size)]
info = {'label': lmfdb_iso}
info['optimal_ainvs'] = ainvs
info['rank'] = E1data['rank']
info['isogeny_matrix'] = latex(matrix(mat))
# info['f'] = ajax_more(E.q_eigenform, 10, 20, 50, 100, 250)
info['f'] = web_latex(E.q_eigenform(10))
info['graph_img'] = url_for('plot_iso_graph', label=lmfdb_iso)
info['curves'] = [[lmfdb_iso + str(i + 1), cremona_labels[i], str(
list(c.ainvs())), c.torsion_order(), degrees[i], optimal_flags[i]] for i, c in enumerate(db_curves)]
friends = []
# friends.append(('Quadratic Twist', "/quadratic_twists/%s" % (lmfdb_iso)))
friends.append(('L-function', url_for("l_functions.l_function_ec_page", label=lmfdb_iso)))
friends.append(('Symmetric square L-function', url_for("l_functions.l_function_ec_sym_page",
power='2', label=lmfdb_iso)))
friends.append(('Symmetric 4th power L-function', url_for("l_functions.l_function_ec_sym_page",
power='4', label=lmfdb_iso)))
# render_one_elliptic_modular_form(level,weight,character,label,**kwds)
friends.append(('Modular form ' + lmfdb_iso.replace(
'.', '.2'), url_for("emf.render_elliptic_modular_forms", level=N, weight=2, character=0, label=iso)))
info['friends'] = friends
info['downloads'] = [('Download coeffients of q-expansion', url_for("download_EC_qexp", label=lmfdb_iso, limit=100)),
('Download stored data for curves in this class', url_for("download_EC_all", label=lmfdb_iso))]
if lmfdb_iso == cremona_iso:
t = "Elliptic Curve Isogeny Class %s" % lmfdb_iso
else:
t = "Elliptic Curve Isogeny Class %s (Cremona label %s)" % (lmfdb_iso, cremona_iso)
bread = [('Elliptic Curves ', url_for("rational_elliptic_curves")), ('isogeny class %s' % lmfdb_iso, ' ')]
return render_template("elliptic_curve/iso_class.html", info=info, bread=bread, credit=credit, title=t, friends=info['friends'], downloads=info['downloads'])
@app.route("/EllipticCurve/Q/modular_form_display/<label>/<number>")
def modular_form_display(label, number):
try:
number = int(number)
except:
number = 10
if number < 10:
number = 10
if number > 100000:
number = 20
if number > 50000:
return "OK, I give up."
if number > 20000:
return "This incident will be reported to the appropriate authorities."
if number > 9600:
return "You have been banned from this website."
if number > 4800:
return "Seriously."
if number > 2400:
return "I mean it."
if number > 1200:
return "Please stop poking me."
if number > 1000:
number = 1000
C = lmfdb.base.getDBConnection()
data = C.elliptic_curves.curves.find_one({'lmfdb_label': label})
if data is None:
return elliptic_curve_jump_error(label, {})
ainvs = [int(a) for a in data['ainvs']]
E = EllipticCurve(ainvs)
modform = E.q_eigenform(number)
modform_string = web_latex_split_on_pm(modform)
return modform_string
# url_for_more = url_for('modular_form_coefficients_more', label = label, number = number * 2)
# return """
# <span id='modular_form_more'> %(modform_string)s
# <a onclick="$('modular_form_more').load(
# '%(url_for_more)s', function() {
# MathJax.Hub.Queue(['Typeset',MathJax.Hub,'modular_form_more']);
# });
# return false;" href="#">more</a></span>
#""" % { 'modform_string' : modform_string, 'url_for_more' : url_for_more }
#@app.route("/EllipticCurve/Q/<label>")
# def by_cremona_label(label):
# try:
# N, iso, number = cremona_label_regex.match(label).groups()
# except:
# N, iso, number = sw_label_regex.match(label).groups()
# if number:
# return render_curve_webpage_by_label(str(label))
# else:
# return render_isogeny_class(str(N)+iso)
#@app.route("/EllipticCurve/Q/<int:conductor>/<iso_class>/<int:number>")
# def by_curve(conductor, iso_class, number):
# if conductor <140000:
# return render_curve_webpage_by_label(label="%s%s%s" % (conductor, iso_class, number))
# else:
# return render_curve_webpage_by_label(label="sw%s.%s.%s" % (conductor, iso_class, number))
def render_curve_webpage_by_label(label):
C = lmfdb.base.getDBConnection()
data = C.elliptic_curves.curves.find_one({'lmfdb_label': label})
if data is None:
return elliptic_curve_jump_error(label, {})
info = {}
ainvs = [int(a) for a in data['ainvs']]
E = EllipticCurve(ainvs)
cremona_label = data['label']
lmfdb_label = data['lmfdb_label']
N = ZZ(data['conductor'])
cremona_iso_class = data['iso'] # eg '37a'
lmfdb_iso_class = data['lmfdb_iso'] # eg '37.a'
rank = data['rank']
try:
j_invariant = QQ(str(data['jinv']))
except KeyError:
j_invariant = E.j_invariant()
if j_invariant == 0:
j_inv_factored = latex(0)
else:
j_inv_factored = latex(j_invariant.factor())
jinv = unicode(str(j_invariant))
CMD = 0
CM = "no"
EndE = "\(\Z\)"
if E.has_cm():
CMD = E.cm_discriminant()
CM = "yes (\(%s\))"%CMD
if CMD%4==0:
d4 = ZZ(CMD)//4
# r = d4.squarefree_part()
# f = (d4//r).isqrt()
# f="" if f==1 else str(f)
# EndE = "\(\Z[%s\sqrt{%s}]\)"%(f,r)
EndE = "\(\Z[\sqrt{%s}]\)"%(d4)
else:
EndE = "\(\Z[(1+\sqrt{%s})/2]\)"%CMD
# plot=E.plot()
discriminant = E.discriminant()
xintpoints_projective = [E.lift_x(x) for x in xintegral_point(data['x-coordinates_of_integral_points'])]
xintpoints = proj_to_aff(xintpoints_projective)
if 'degree' in data:
modular_degree = data['degree']
else:
try:
modular_degree = E.modular_degree()
except RuntimeError:
modular_degree = 0 # invalid, will be displayed nicely
G = E.torsion_subgroup().gens()
E_pari = E.pari_curve(prec=200)
from sage.libs.pari.all import PariError
try:
minq = E.minimal_quadratic_twist()[0]
except PariError: # this does occur with 164411a1
print "PariError computing minimal quadratic twist of elliptic curve %s"%lmfdb_label
minq = E
if E == minq:
minq_label = lmfdb_label
else:
minq_ainvs = [str(c) for c in minq.ainvs()]
minq_label = C.elliptic_curves.curves.find_one({'ainvs': minq_ainvs})['lmfdb_label']
# We do not just do the following, as Sage's installed database
# might not have all the curves in the LMFDB database.
# minq_label = E.minimal_quadratic_twist()[0].label()
if 'gens' in data:
generator = parse_gens(data['gens'])
if len(G) == 0:
tor_struct = '\mathrm{Trivial}'
tor_group = '\mathrm{Trivial}'
else:
tor_group = ' \\times '.join(['\Z/{%s}\Z' % a.order() for a in G])
if 'torsion_structure' in data:
info['tor_structure'] = ' \\times '.join(['\Z/{%s}\Z' % int(a) for a in data['torsion_structure']])
else:
info['tor_structure'] = tor_group
info.update(data)
if rank >= 2:
lder_tex = "L%s(E,1)" % ("^{(" + str(rank) + ")}")
elif rank == 1:
lder_tex = "L%s(E,1)" % ("'" * rank)
else:
assert rank == 0
lder_tex = "L(E,1)"
info['Gamma0optimal'] = (
cremona_label[-1] == '1' if cremona_iso_class != '990h' else cremona_label[-1] == '3')
info['modular_degree'] = modular_degree
p_adic_data_exists = (C.elliptic_curves.padic_db.find(
{'lmfdb_iso': lmfdb_iso_class}).count()) > 0 and info['Gamma0optimal']
# Local data
local_data = []
for p in N.prime_factors():
local_info = E.local_data(p, algorithm="generic")
local_data.append({'p': p,
'tamagawa_number': local_info.tamagawa_number(),
'kodaira_symbol': web_latex(local_info.kodaira_symbol()).replace('$', ''),
'reduction_type': local_info.bad_reduction_type()
})
mod_form_iso = lmfdb_label_regex.match(lmfdb_iso_class).groups()[1]
tamagawa_numbers = [E.local_data(p, algorithm="generic").tamagawa_number() for p in N.prime_factors()]
# if we use E.tamagawa_numbers() it calls E.local_data(p) which
# crashes on some curves e.g. 164411a1
info.update({
'conductor': N,
'disc_factor': latex(discriminant.factor()),
'j_invar_factor': j_inv_factored,
'label': lmfdb_label,
'cremona_label': cremona_label,
'iso_class': lmfdb_iso_class,
'cremona_iso_class': cremona_iso_class,
'equation': web_latex(E),
#'f': ajax_more(E.q_eigenform, 10, 20, 50, 100, 250),
'f': web_latex(E.q_eigenform(10)),
'generators': ', '.join(web_latex(g) for g in generator) if 'gens' in data else ' ',
'lder': lder_tex,
'p_adic_primes': [p for p in sage.all.prime_range(5, 100) if E.is_ordinary(p) and not p.divides(N)],
'p_adic_data_exists': p_adic_data_exists,
'ainvs': format_ainvs(data['ainvs']),
'CM': CM,
'CMD': CMD,
'EndE': EndE,
'tamagawa_numbers': r' \cdot '.join(str(sage.all.factor(c)) for c in tamagawa_numbers),
'local_data': local_data,
'cond_factor': latex(N.factor()),
'xintegral_points': ', '.join(web_latex(P) for P in xintpoints),
'tor_gens': ', '.join(web_latex(eval(g)) for g in data['torsion_generators']) if False else ', '.join(web_latex(P.element().xy()) for P in list(G))
})
info['friends'] = [
('Isogeny class ' + lmfdb_iso_class, "/EllipticCurve/Q/%s" % lmfdb_iso_class),
('Minimal quadratic twist ' + minq_label, "/EllipticCurve/Q/%s" % minq_label),
('All twists ', url_for("rational_elliptic_curves", jinv=jinv)),
('L-function', url_for("l_functions.l_function_ec_page", label=lmfdb_label)),
('Symmetric square L-function', url_for("l_functions.l_function_ec_sym_page", power='2',
label=lmfdb_iso_class)),
('Symmetric 4th power L-function', url_for("l_functions.l_function_ec_sym_page", power='4',
label=lmfdb_iso_class))]
info['friends'].append(('Modular form ' + lmfdb_iso_class.replace('.', '.2'), url_for(
"emf.render_elliptic_modular_forms", level=int(N), weight=2, character=0, label=mod_form_iso)))
info['downloads'] = [('Download coeffients of q-expansion', url_for("download_EC_qexp", label=lmfdb_label, limit=100)),
('Download all stored data', url_for("download_EC_all", label=lmfdb_label))]
# info['learnmore'] = [('Elliptic Curves', url_for("not_yet_implemented"))]
# info['plot'] = image_src(plot)
info['plot'] = url_for('plot_ec', label=lmfdb_label)
properties2 = [('Label', '%s' % lmfdb_label),
(None, '<img src="%s" width="200" height="150"/>' % url_for(
'plot_ec', label=lmfdb_label)),
('Conductor', '\(%s\)' % N),
('Discriminant', '\(%s\)' % discriminant),
('j-invariant', '%s' % web_latex(j_invariant)),
('CM', '%s' % CM),
('Rank', '\(%s\)' % rank),
('Torsion Structure', '\(%s\)' % tor_group)
]
# properties.extend([ "prop %s = %s<br/>" % (_,_*1923) for _ in range(12) ])
credit = 'John Cremona'
if info['label'] == info['cremona_label']:
t = "Elliptic Curve %s" % info['label']
else:
t = "Elliptic Curve %s (Cremona label %s)" % (info['label'], info['cremona_label'])
bread = [('Elliptic Curves ', url_for("rational_elliptic_curves")), ('Elliptic curves %s' %
lmfdb_label, ' ')]
return render_template("elliptic_curve/elliptic_curve.html",
properties2=properties2, credit=credit, bread=bread, title=t, info=info, friends=info['friends'], downloads=info['downloads'])
@app.route("/EllipticCurve/Q/padic_data")
def padic_data():
info = {}
label = request.args['label']
p = int(request.args['p'])
info['p'] = p
N, iso, number = lmfdb_label_regex.match(label).groups()
# print N, iso, number
if request.args['rank'] == '0':
info['reg'] = 1
elif number == '1':
C = lmfdb.base.getDBConnection()
data = C.elliptic_curves.curves.find_one({'lmfdb_iso': N + '.' + iso})
data = C.elliptic_curves.padic_db.find_one({'lmfdb_iso': N + '.' + iso, 'p': p})
info['data'] = data
if data is None:
info['reg'] = 'no data'
else:
val = int(data['val'])
aprec = data['prec']
reg = sage.all.Qp(p, aprec)(int(data['unit']), aprec - val) << val
info['reg'] = web_latex(reg)
else:
info['reg'] = "no data"
return render_template("elliptic_curve/elliptic_curve_padic.html", info=info)
@app.route("/EllipticCurve/Q/download_qexp/<label>/<limit>")
def download_EC_qexp(label, limit):
logger.debug(label)
CDB = lmfdb.base.getDBConnection().elliptic_curves.curves
N, iso, number = lmfdb_label_regex.match(label).groups()
if number:
data = CDB.find_one({'lmfdb_label': label})
else:
data = CDB.find_one({'lmfdb_iso': label})
ainvs = data['ainvs']
logger.debug(ainvs)
E = EllipticCurve([int(a) for a in ainvs])
response = make_response(','.join(str(an) for an in E.anlist(int(limit), python_ints=True)))
response.headers['Content-type'] = 'text/plain'
return response
@app.route("/EllipticCurve/Q/download_all/<label>")
def download_EC_all(label):
CDB = lmfdb.base.getDBConnection().elliptic_curves.curves
N, iso, number = lmfdb_label_regex.match(label).groups()
if number:
data = CDB.find_one({'lmfdb_label': label})
if data is None:
return elliptic_curve_jump_error(label, {})
data_list = [data]
else:
data_list = sorted(list(CDB.find({'lmfdb_iso': label})), key=lambda E: E['number'])
if len(data_list) == 0:
return elliptic_curve_jump_error(label, {})
# titles of all entries of curves
dump_data = []
titles = [str(c) for c in data_list[0]]
titles = [t for t in titles if t[0] != '_']
titles.sort()
dump_data.append(titles)
for data in data_list:
data1 = []
for t in titles:
d = data[t]
if t == 'ainvs':
data1.append(format_ainvs(d))
elif t in ['torsion_generators', 'torsion_structure']:
data1.append([eval(g) for g in d])
elif t == 'x-coordinates_of_integral_points':
data1.append(eval(d))
elif t == 'gens':
data1.append(parse_gens(d))
elif t in ['iso', 'label', 'lmfdb_iso', 'lmfdb_label']:
data1.append(str(d))
else:
data1.append(d)
dump_data.append(data1)
response = make_response('\n'.join(str(an) for an in dump_data))
response.headers['Content-type'] = 'text/plain'
return response
#@app.route("/EllipticCurve/Q/download_Rub_data")
# def download_Rub_data():
# import gridfs
# label=(request.args.get('label'))
# type=(request.args.get('type'))
# C = base.getDBConnection()
# fs = gridfs.GridFS(C.elliptic_curves,'isogeny' )
# isogeny=C.ellcurves.isogeny.files
# filename=isogeny.find_one({'label':str(label),'type':str(type)})['filename']
# d= fs.get_last_version(filename)
# response = make_response(d.readline())
# response.headers['Content-type'] = 'text/plain'
# return response