summaryrefslogtreecommitdiffstats
path: root/src/sage/schemes/elliptic_curves/isogeny_small_degree.py
blob: 63e6ef7a8ffb188598dcad7d7fb30fd17a4a7c3b (plain)
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
r"""
Isogenies of small prime degree.

Functions for the computation of isogenies of small primes
degree. First: `l` = 2, 3, 5, 7, or 13, where the modular curve
`X_0(l)` has genus 0.  Second: `l` = 11, 17, 19, 23, 29, 31, 41, 47,
59, or 71, where `X_0^+(l)` has genus 0 and `X_0(l)` is elliptic or
hyperelliptic.  Also: `l` = 11, 17, 19, 37, 43, 67 or 163 over `\QQ`
(the sporadic cases with only finitely many `j`-invariants each).  All
the above only require factorization of a polynomial of degree `l+1`.
Finally, a generic function which works for arbitrary odd primes `l`
(including the characteristic), but requires factorization of the
`l`-division polynomial, of degree `(l^2-1)/2`.


AUTHORS:

- John Cremona and Jenny Cooley: 2009-07..11: the genus 0 cases the sporadic cases over `\QQ`.

- Kimi Tsukazaki and John Cremona: 2013-07: The 10 (hyper)-elliptic
  cases and the generic algorithm.  See [KT2013]_.

REFERENCES:

.. [CW2005] \J. E. Cremona and M. Watkins. Computing isogenies of elliptic curves. preprint, 2005.
.. [KT2013] \K. Tsukazaki, Explicit Isogenies of Elliptic Curves,
   PhD thesis, University of Warwick, 2013.


"""

#*****************************************************************************
#       Copyright (C) 2012-2013 John Cremona, Jenny Cooley, Kimi Tsukazaki
#
#  Distributed under the terms of the GNU General Public License (GPL)
#  as published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#                  http://www.gnu.org/licenses/
#*****************************************************************************

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.polynomial.polynomial_ring import polygen
from sage.rings.all import ZZ, QQ
from sage.schemes.elliptic_curves.all import EllipticCurve

from sage.misc.cachefunc import cached_function

##########################################################################
# The following section is all about computing l-isogenies, where l is
# a prime.  The genus 0 cases `l` = 2, 3, 5, 7 and 13 are
# implemented over any field of characteristic not 2, 3 or `l`; over
# `\QQ` the "sporadic" cases `l` = 11, 17, 19, 37, 43, 67 or 163 with
# only finitely many `j`-invariants each. are also implemented.
##########################################################################

@cached_function
def Fricke_polynomial(l):
    r"""
    Fricke polynomial for ``l`` =2,3,5,7,13.

    For these primes (and these only) the modular curve `X_0(l)` has
    genus zero, and its field is generated by a single modular
    function called the Fricke module (or Hauptmodul), `t`.  There is
    a classical choice of such a generator `t` in each case, and the
    `j`-function is a rational function of `t` of degree `l+1` of the
    form `P(t)/t` where `P` is a polynomial of degree `l+1`.  Up to
    scaling, `t` is determined by the condition that the ramification
    points above `j=\infty` are `t=0` (with ramification degree `1`)
    and `t=\infty` (with degree `l`).  The ramification above `j=0`
    and `j=1728` may be seen in the factorizations of `j(t)` and
    `k(t)` where `k=j-1728`.

    OUTPUT:

    The polynomial `P(t)` as an element of `\ZZ[t]`.

    TESTS::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import Fricke_polynomial
        sage: Fricke_polynomial(2)
        t^3 + 48*t^2 + 768*t + 4096
        sage: Fricke_polynomial(3)
        t^4 + 36*t^3 + 270*t^2 + 756*t + 729
        sage: Fricke_polynomial(5)
        t^6 + 30*t^5 + 315*t^4 + 1300*t^3 + 1575*t^2 + 750*t + 125
        sage: Fricke_polynomial(7)
        t^8 + 28*t^7 + 322*t^6 + 1904*t^5 + 5915*t^4 + 8624*t^3 + 4018*t^2 + 748*t + 49
        sage: Fricke_polynomial(13)
        t^14 + 26*t^13 + 325*t^12 + 2548*t^11 + 13832*t^10 + 54340*t^9 + 157118*t^8 + 333580*t^7 + 509366*t^6 + 534820*t^5 + 354536*t^4 + 124852*t^3 + 15145*t^2 + 746*t + 13
    """
    Zt = PolynomialRing(ZZ,'t')
    t = Zt.gen()
    if l==2: return (t+16)**3
    elif l==3: return (t+3)**3*(t+27)
    elif l==5: return (t**2+10*t+5)**3
    elif l==7: return (t**2+5*t+1)**3 * (t**2+13*t+49)
    elif l==13: return (t**2+5*t+13)*(t**4+7*t**3+20*t**2+19*t+1)**3
    else:
        raise ValueError("The only genus zero primes are 2, 3, 5, 7 or 13.")

@cached_function
def Fricke_module(l):
    r"""
    Fricke module for ``l`` =2,3,5,7,13.

    For these primes (and these only) the modular curve `X_0(l)` has
    genus zero, and its field is generated by a single modular
    function called the Fricke module (or Hauptmodul), `t`.  There is
    a classical choice of such a generator `t` in each case, and the
    `j`-function is a rational function of `t` of degree `l+1` of the
    form `P(t)/t` where `P` is a polynomial of degree `l+1`.  Up to
    scaling, `t` is determined by the condition that the ramification
    points above `j=\infty` are `t=0` (with ramification degree `1`)
    and `t=\infty` (with degree `l`).  The ramification above `j=0`
    and `j=1728` may be seen in the factorizations of `j(t)` and
    `k(t)` where `k=j-1728`.

    OUTPUT:

    The rational function `P(t)/t`.

    TESTS::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import Fricke_module
        sage: Fricke_module(2)
        (t^3 + 48*t^2 + 768*t + 4096)/t
        sage: Fricke_module(3)
        (t^4 + 36*t^3 + 270*t^2 + 756*t + 729)/t
        sage: Fricke_module(5)
        (t^6 + 30*t^5 + 315*t^4 + 1300*t^3 + 1575*t^2 + 750*t + 125)/t
        sage: Fricke_module(7)
        (t^8 + 28*t^7 + 322*t^6 + 1904*t^5 + 5915*t^4 + 8624*t^3 + 4018*t^2 + 748*t + 49)/t
        sage: Fricke_module(13)
        (t^14 + 26*t^13 + 325*t^12 + 2548*t^11 + 13832*t^10 + 54340*t^9 + 157118*t^8 + 333580*t^7 + 509366*t^6 + 534820*t^5 + 354536*t^4 + 124852*t^3 + 15145*t^2 + 746*t + 13)/t
    """
    try:
        t = PolynomialRing(QQ,'t').gen()
        return Fricke_polynomial(l) / t
    except ValueError:
        raise ValueError("The only genus zero primes are 2, 3, 5, 7 or 13.")

@cached_function
def Psi(l, use_stored=True):
    r"""
    Generic kernel polynomial for genus zero primes.

    For each of the primes `l` for which `X_0(l)` has genus zero
    (namely `l=2,3,5,7,13`), we may define an elliptic curve `E_t`
    over `\QQ(t)`, with coefficients in `\ZZ[t]`, which has good
    reduction except at `t=0` and `t=\infty` (which lie above
    `j=\infty`) and at certain other values of `t` above `j=0` when
    `l=3` (one value) or `l\equiv1\pmod{3}` (two values) and above
    `j=1728` when `l=2` (one value) or `l\equiv1 \pmod{4}` (two
    values).  (These exceptional values correspond to endomorphisms of
    `E_t` of degree `l`.)  The `l`-division polynomial of `E_t` has a
    unique factor of degree `(l-1)/2` (or 1 when `l=2`), with
    coefficients in `\ZZ[t]`, which we call the Generic Kernel
    Polynomial for `l`.  These are used, by specialising `t`, in the
    function :meth:`isogenies_prime_degree_genus_0`, which also has to
    take into account the twisting factor between `E_t` for a specific
    value of `t` and the short Weierstrass form of an elliptic curve
    with `j`-invariant `j(t)`.  This enables the computation of the
    kernel polynomials of isogenies without having to compute and
    factor division polynomials.

    All of this data is quickly computed from the Fricke modules, except
    that for `l=13` the factorization of the Generic Division Polynomial
    takes a long time, so the value have been precomputed and cached; by
    default the cached values are used, but the code here will recompute
    them when ``use_stored`` is ``False``, as in the doctests.

    INPUT:

    - ``l`` -- either 2, 3, 5, 7, or 13.

    - ``use_stored`` (boolean, default True) -- If True, use
      precomputed values, otherwise compute them on the fly.

    .. note:

       This computation takes a negligible time for `l=2,3,5,7`
       but more than 100s for `l=13`.  The reason
       for allowing dynamic computation here instead of just using
       precomputed values is for testing.

    TESTS::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import Fricke_module, Psi
        sage: assert Psi(2, use_stored=True) == Psi(2, use_stored=False)
        sage: assert Psi(3, use_stored=True) == Psi(3, use_stored=False)
        sage: assert Psi(5, use_stored=True) == Psi(5, use_stored=False)
        sage: assert Psi(7, use_stored=True) == Psi(7, use_stored=False)
        sage: assert Psi(13, use_stored=True) == Psi(13, use_stored=False) # not tested (very long time)
    """
    if not l in [2,3,5,7,13]:
        raise ValueError("Genus zero primes are 2, 3, 5, 7 or 13.")

    R = PolynomialRing(ZZ,2,'Xt')
    X,t = R.gens()

    if use_stored:
        if l==2:
            return X + t + 64
        if l==3:
            return X + t + 27
        if l==5:
            return X**2 + 2*X*(t**2 + 22*t + 125)+ (t**2 + 22*t + 89) * (t**2 + 22*t + 125)
        if l==7:
            return (X**3 + 3*(t**2 + 13*t + 49)*X**2
                    + 3*(t**2 + 13*t + 33)*(t**2 + 13*t + 49)*X
                    + (t**2 + 13*t + 49)*(t**4 + 26*t**3 + 219*t**2 + 778*t + 881))
        if l==13:
            return (t**24 + 66*t**23 + 2091*t**22 + 6*X*t**20 + 42582*t**21 + 330*X*t**19 + 627603*t**20 + 8700*X*t**18 + 7134744*t**19 + 15*X**2*t**16 + 146886*X*t**17 + 65042724*t**18 + 660*X**2*t**15 + 1784532*X*t**16 + 487778988*t**17 + 13890*X**2*t**14 + 16594230*X*t**15 + 3061861065*t**16 + 20*X**3*t**12 + 186024*X**2*t**13 + 122552328*X*t**14 + 16280123754*t**15 + 660*X**3*t**11 + 1774887*X**2*t**12 + 735836862*X*t**13 + 73911331425*t**14 + 10380*X**3*t**10 + 12787272*X**2*t**11 + 3646188342*X*t**12 + 287938949178*t**13 + 15*X**4*t**8 + 102576*X**3*t**9 + 71909658*X**2*t**10 + 15047141292*X*t**11 + 964903805434*t**12 + 330*X**4*t**7 + 707604*X**3*t**8 + 321704316*X**2*t**9 + 51955096824*X*t**10 + 2781843718722*t**11 + 3435*X**4*t**6 + 3582876*X**3*t**7 + 1155971196*X**2*t**8 + 150205315932*X*t**9 + 6885805359741*t**10 + 6*X**5*t**4 + 21714*X**4*t**5 + 13632168*X**3*t**6 + 3343499244*X**2*t**7 + 362526695094*X*t**8 + 14569390179114*t**9 + 66*X**5*t**3 + 90660*X**4*t**4 + 39215388*X**3*t**5 + 7747596090*X**2*t**6 + 725403501318*X*t**7 + 26165223178293*t**8 + 336*X**5*t**2 + 255090*X**4*t**3 + 84525732*X**3*t**4 + 14206132008*X**2*t**5 + 1189398495432*X*t**6 + 39474479008356*t**7 + X**6 + 858*X**5*t + 472143*X**4*t**2 + 132886992*X**3*t**3 + 20157510639*X**2*t**4 + 1569568001646*X*t**5 + 49303015587132*t**6 + 1014*X**5 + 525954*X**4*t + 144222780*X**3*t**2 + 21320908440*X**2*t**3 + 1622460290100*X*t**4 + 49941619724976*t**5 + 272259*X**4 + 96482100*X**3*t + 15765293778*X**2*t**2 + 1260038295438*X*t**3 + 39836631701295*t**4 + 29641924*X**3 + 7210949460*X**2*t + 686651250012*X*t**2 + 23947528862166*t**3 + 1506392823*X**2 + 231462513906*X*t + 10114876838391*t**2 + 35655266790*X + 2644809206442*t + 317295487717)
# The coefficients for l=13 are:
# X**6: 1
# X**5: (6) * (t**2 + 5*t + 13) * (t**2 + 6*t + 13)
# X**4: (3) * (t**2 + 5*t + 13) * (t**2 + 6*t + 13) * (5*t**4 + 55*t**3 + 260*t**2 + 583*t + 537)
# X**3: (4) * (t**2 + 5*t + 13) * (t**2 + 6*t + 13)**2 * (5*t**6 + 80*t**5 + 560*t**4 + 2214*t**3 + 5128*t**2 + 6568*t + 3373)
# X**2: (3) * (t**2 + 5*t + 13)**2 * (t**2 + 6*t + 13)**2 * (5*t**8 + 110*t**7 + 1045*t**6 + 5798*t**5 + 20508*t**4 + 47134*t**3 + 67685*t**2 + 54406*t + 17581)
# X**1: (6) * (t**2 + 5*t + 13)**2 * (t**2 + 6*t + 13)**3 * (t**10 + 27*t**9 + 316*t**8 + 2225*t**7 + 10463*t**6 + 34232*t**5 + 78299*t**4 + 122305*t**3 + 122892*t**2 + 69427*t + 16005)
# X**0: (t**2 + 5*t + 13)**2 * (t**2 + 6*t + 13)**3 * (t**14 + 38*t**13 + 649*t**12 + 6844*t**11 + 50216*t**10 + 271612*t**9 + 1115174*t**8 + 3520132*t**7 + 8549270*t**6 + 15812476*t**5 + 21764840*t**4 + 21384124*t**3 + 13952929*t**2 + 5282630*t + 854569)
#

    # Here the generic kernel polynomials are actually calculated:
    j = Fricke_module(l)
    k = j-1728
    from sage.misc.all import prod
    f = prod( [p for p,e in j.factor() if e==3]
             +[p for p,e in k.factor() if e==2])
    A4 = -3*t**2*j*k // f**2
    A6 = -2*t**3*j*k**2 // f**3
    E = EllipticCurve([0,0,0,A4,A6])
    assert E.j_invariant() == j
    return E.division_polynomial(l,X).factor()[0][0]


def isogenies_prime_degree_genus_0(E, l=None):
    """
    Returns list of ``l`` -isogenies with domain ``E``.

    INPUT:

    - ``E`` -- an elliptic curve.

    - ``l`` -- either None or 2, 3, 5, 7, or 13.

    OUTPUT:

    (list) When ``l`` is None a list of all isogenies of degree 2, 3,
    5, 7 and 13, otherwise a list of isogenies of the given degree.

    .. note::

       This function would normally be invoked indirectly via
       ``E.isogenies_prime_degree(l)``, which automatically calls the
       appropriate function.

    ALGORITHM:

    Cremona and Watkins [CW2005]_. See also [KT2013]_, Chapter 4.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_prime_degree_genus_0
        sage: E = EllipticCurve([0,12])
        sage: isogenies_prime_degree_genus_0(E, 5)
        []

        sage: E = EllipticCurve('1450c1')
        sage: isogenies_prime_degree_genus_0(E)
        [Isogeny of degree 3 from Elliptic Curve defined by y^2 + x*y = x^3 + x^2 + 300*x - 1000 over Rational Field to Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 5950*x - 182250 over Rational Field]

        sage: E = EllipticCurve('50a1')
        sage: isogenies_prime_degree_genus_0(E)
        [Isogeny of degree 3 from Elliptic Curve defined by y^2 + x*y + y = x^3 - x - 2 over Rational Field to Elliptic Curve defined by y^2 + x*y + y = x^3 - 126*x - 552 over Rational Field,
        Isogeny of degree 5 from Elliptic Curve defined by y^2 + x*y + y = x^3 - x - 2 over Rational Field to Elliptic Curve defined by y^2 + x*y + y = x^3 - 76*x + 298 over Rational Field]
    """
    if not l in [2, 3, 5, 7, 13, None]:
        raise ValueError("%s is not a genus 0 prime."%l)
    F = E.base_ring()
    j = E.j_invariant()
    if F.characteristic() in [2, 3, l]:
        raise NotImplementedError("2, 3, 5, 7 and 13-isogenies are not yet implemented in characteristic 2 and 3, and when the characteristic is the same as the degree of the isogeny.")
    if l==2:
        return isogenies_2(E)
    if l==3:
        return isogenies_3(E)
    if j==F(0):
        if l==5:
            return isogenies_5_0(E)
        if l==7:
            return isogenies_7_0(E)
        if l==13:
            return isogenies_13_0(E)
    if j==F(1728):
        if l==5:
            return isogenies_5_1728(E)
        if l==7:
            return isogenies_7_1728(E)
        if l==13:
            return isogenies_13_1728(E)

    if l is not None:
        R = PolynomialRing(F,'t')
        t = R.gen()
        f = R(Fricke_polynomial(l))
        t_list = sorted((f-j*t).roots(multiplicities=False))
        # The generic kernel polynomial applies to a standard curve
        # E_t with the correct j-invariant; we must compute the
        # appropriate twising factor to scale X by:
        c4, c6 = E.c_invariants()
        T = c4/(3*c6)
        jt = Fricke_module(l)
        kt = jt-1728
        from sage.misc.all import prod
        psi = Psi(l)
        X = t
        f = R(prod( [p for p,e in jt.factor() if e==3]
                 +[p for p,e in kt.factor() if e==2]))
        kernels = [R(psi(X*T*(j-1728)*t0/f(t0),t0)) for t0 in t_list]
        kernels = [ker.monic() for ker in kernels]
        E1 = EllipticCurve([-27*c4,-54*c6])
        w = E.isomorphism_to(E1)
        from sage.rings.number_field.number_field_base import is_NumberField
        model = "minimal" if is_NumberField(F) else None
        isogs = [E1.isogeny(kernel=ker, model=model) for ker in kernels]
        [isog.set_pre_isomorphism(w) for isog in isogs]
        return isogs

    if l is None:
        return sum([isogenies_prime_degree_genus_0(E, l) for l in [2,3,5,7,13]],[])


# The following code computes data to be used in
# isogenies_sporadic_Q. Over Q there are only finitely many
# j-invariants of curves with l-isogenies where l is not equal to 2,
# 3, 5, 7 or 13. In these cases l is equal to 11, 17, 19, 37, 43, 67
# or 163. We refer to these l as "sporadic".

# sporadic_j is a dictionary holding for each possible sporadic
# j-invariant, the unique l such that an l-isogeny exists.
sporadic_j = {
    QQ(-121)                : 11,
    QQ(-32768)              : 11,
    QQ(-24729001)           : 11,
    QQ(-297756989)/2        : 17,
    QQ(-882216989)/131072   : 17,
    QQ(-884736)             : 19,
    QQ(-9317)               : 37,
    QQ(-162677523113838677) : 37,
    QQ(-884736000)          : 43,
    QQ(-147197952000)       : 67,
    QQ(-262537412640768000) : 163
    }

@cached_function
def _sporadic_Q_data(j):
    """
    Returns technical data used in computing sporadic isogenies over `\QQ`.

    INPUT:

    - ``j`` -- The `j`-invariant of a sporadic curve, i.e. one of the
      keys of ``sporadic_j``.

    OUTPUT:

    ``([a4,a6],coeffs)`` where ``[a4,a6]`` are the coefficients of a
    short Weierstrass equation of an elliptic curve E with j(E)=``j``,
    and ``coeffs`` is a list of coefficients of a polynomial defining
    the kernel of an l-isogeny from E.

    Whenever we have a curve of j-invariant ``j``, we can compute the
    corresponding l-isogeny by just scaling ``coeffs`` by the right
    twisting factor and using the result as a kernel-polynomial.

    ALGORITHM:

    For small l it works fine to factor the l-division polynomial, but
    this takes a long time for the larger l and is a very bad idea for
    l=163; hence we use floating point arithmetic with a precision
    which is known to work.  This idea was suggested by Samir Siksek.

    TESTS::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import sporadic_j, _sporadic_Q_data
        sage: [_sporadic_Q_data(j) for j in sorted(sporadic_j.keys()) if j != -262537412640768000]
        [([-269675595, -1704553285050],
          [-855506888466179262477032094260950275409164148942611063430052125977143159,
           -1469048260972089939455942042937882262144594798448952781325533511718750,
           -1171741935131505774747142644126089902595908234671576131857702734375,
           -574934780393177024547076427530739751753985644656221274606250000,
           -193516922725803688001809624711400287605136013195315374687500,
           -47085563820928456130325308223963045033502182349693125000,
           -8472233937388712980597845725196873697064639957437500,
           -1124815211213953261752081095348112305023653750000,
           -105684015609077608033913080859605951322531250,
           -5911406027236569746089675554748135312500,
           22343907270397352965399097794968750,
           43602171843758666292581116410000,
           5054350766002463251474186500,
           350135768194635636171000,
           16633063574896677300,
           549939627039600,
           12182993865,
           163170,
           1]),
         ([-117920, 15585808],
          [426552448394636714720553816389274308035895411389805883034985546818882031845376,
           -55876556222880738651382959148329502876096075327084935039031884373558741172224,
           3393295715290183821010552313572221545212247684503012173117764703828786020352,
           -125729166452196578653551230178028570067747190427221869867485520072257044480,
           3121342502030777257351089270834971957072933779704445667351054593298530304,
           -52544031605544530265465344472543470442324636919759253720520768014516224,
           532110915869155495738137756847596184665209453108323879594125221167104,
           -399031158106622651277981701966309467713625045637309782055519780864,
           -101914346170769215732007802723651742508893380955930030421292613632,
           2296526155500449624398016447877283594461904009374321659789443072,
           -31950871094301541469458501953701002806003991982768349794795520,
           329792235011603804948028315065667439678526339671142107709440,
           -2655636715955021784085217734679612378726442691190553837568,
           16825164648840434987220620681420687654501026066872664064,
           -81705027839007003131400500185224450729843244954288128,
           273656504606483403474090105104132405333665144373248,
           -320807702482945680116212224172370503903312084992,
           -3166683390779345463318656135338172047199043584,
           27871349428383710305216046431806697565585408,
           -132774697798318602604125735604528772808704,
           436096215568182871014215818309741314048,
           -964687143341252402362763535357837312,
           942144169187362941776488535425024,
           2794850106281773765892648206336,
           -17236916236678037389276086272,
           50979778712911923486851072,
           -105035658611718440992768,
           161833913559276412928,
           -188675698610077696,
           163929317513984,
           -102098677888,
           42387952,
           -10184,
           1]),
         ([-13760, 621264],
          [-1961864562041980324821547425314935668736,
           784270445793223959453256359333693751296,
           -120528107728500223255333768387027271680,
           10335626145581464192664472924270362624,
           -568426570575654606865505142156820480,
           21261993723422650574629752537088000,
           -544630471727787626557612832587776,
           8870521306520473088172555763712,
           -54993059067301585878494740480,
           -1434261324709904840432549888,
           50978938193065926383894528,
           -845761855773797582372864,
           8627493611216601088000,
           -48299605284169187328,
           -32782260293713920,
           3415534989828096,
           -34580115625984,
           199359712512,
           -730488128,
           1658080,
           -2064,
           1]),
         ([-3940515, 3010787550],
          [-6458213126940667330314375,
           34699336325466068070000,
           -72461450055340471500,
           68342601718080000,
           -15140380554450,
           -25802960400,
           23981220,
           -8160,
           1]),
         ([-38907, -2953962], [-20349931239, -424530315, -134838, 53658, 429, 1]),
         ([-608, 5776],
          [-34162868224,
           -8540717056,
           6405537792,
           -1123778560,
           84283392,
           -2033152,
           -92416,
           6992,
           -152,
           1]),
         ([-9504, 365904], [1294672896, -92835072, 1463616, 7920, -264, 1]),
         ([-10395, 444150],
          [-38324677699334121599624973029296875,
           -17868327793500376961572310472656250,
           2569568362004197901139023084765625,
           -95128267987528547588017818750000,
           -822168183291347061312510937500,
           134395594560592096297190625000,
           -2881389756919344324888937500,
           -2503855007083401977250000,
           922779077075655997443750,
           -11503912310262102937500,
           -18237870962450291250,
           1457822151548910000,
           -10087015556047500,
           -13677678063000,
           490243338900,
           -2461460400,
           5198445,
           -4410,
           1]),
         ([-856035, -341748450],
          [103687510635057329105625,
           961598491955315190000,
           1054634146768300500,
           -6553122389064000,
           -14554350284850,
           -2046589200,
           13185540,
           8160,
           1]),
         ([-3267, -280962], [1480352841, -56169531, -2829222, 10890, 429, 1])]
    """
    from sage.rings.all import RealField
    from sage.misc.all import prod
    ell = sporadic_j[j]
    E = EllipticCurve(j=j).short_weierstrass_model()
    a4a6 = list(E.ainvs())[3:]
    L = E.period_lattice()
    pr = 100
    if ell==163:
        pr=1000
    elif ell>30:
        pr=300
    w1, w2 = L.basis(prec=pr)
    X = polygen(RealField(pr),'X')
    w = w1 # real period
    if j in [-121, -24729001, -162677523113838677, QQ(-882216989)/131072]:
        w = 2*w2-w1 # imaginary period
    kerpol = prod(([X-L.elliptic_exponential(n*w/ell)[0] for n in range(1,(ell+1)//2)]))
    kerpolcoeffs = [c.real().round() for c in list(kerpol)]
    return (a4a6,kerpolcoeffs)

def isogenies_sporadic_Q(E, l=None):
    """
    Returns list of ``l`` -isogenies with domain ``E`` (defined over `\QQ`).

    Returns a list of sporadic l-isogenies from E (l = 11, 17, 19, 37,
    43, 67 or 163). Only for elliptic curves over `\QQ`.

    INPUT:

    - ``E`` -- an elliptic curve defined over `\QQ`.

    - ``l`` -- either None or a prime number.

    OUTPUT:

    (list) If ``l`` is None, a list of all isogenies with domain ``E``
    and of degree 11, 17, 19, 37, 43, 67 or 163; otherwise a list of
    isogenies of the given degree.

    .. note::

       This function would normally be invoked indirectly via
       ``E.isogenies_prime_degree(l)``, which automatically calls the appropriate
       function.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_sporadic_Q
        sage: E = EllipticCurve('121a1')
        sage: isogenies_sporadic_Q(E, 11)
        [Isogeny of degree 11 from Elliptic Curve defined by y^2 + x*y + y = x^3 + x^2 - 30*x - 76 over Rational Field to Elliptic Curve defined by y^2 + x*y + y = x^3 + x^2 - 305*x + 7888 over Rational Field]
        sage: isogenies_sporadic_Q(E, 13)
        []
        sage: isogenies_sporadic_Q(E, 17)
        []
        sage: isogenies_sporadic_Q(E)
        [Isogeny of degree 11 from Elliptic Curve defined by y^2 + x*y + y = x^3 + x^2 - 30*x - 76 over Rational Field to Elliptic Curve defined by y^2 + x*y + y = x^3 + x^2 - 305*x + 7888 over Rational Field]

        sage: E = EllipticCurve([1, 1, 0, -660, -7600])
        sage: isogenies_sporadic_Q(E, 17)
        [Isogeny of degree 17 from Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 660*x - 7600 over Rational Field to Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 878710*x + 316677750 over Rational Field]
        sage: isogenies_sporadic_Q(E)
        [Isogeny of degree 17 from Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 660*x - 7600 over Rational Field to Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 878710*x + 316677750 over Rational Field]
        sage: isogenies_sporadic_Q(E, 11)
        []

        sage: E = EllipticCurve([0, 0, 1, -1862, -30956])
        sage: isogenies_sporadic_Q(E, 11)
        []
        sage: isogenies_sporadic_Q(E, 19)
        [Isogeny of degree 19 from Elliptic Curve defined by y^2 + y = x^3 - 1862*x - 30956 over Rational Field to Elliptic Curve defined by y^2 + y = x^3 - 672182*x + 212325489 over Rational Field]
        sage: isogenies_sporadic_Q(E)
        [Isogeny of degree 19 from Elliptic Curve defined by y^2 + y = x^3 - 1862*x - 30956 over Rational Field to Elliptic Curve defined by y^2 + y = x^3 - 672182*x + 212325489 over Rational Field]

        sage: E = EllipticCurve([0, -1, 0, -6288, 211072])
        sage: E.conductor()
        19600
        sage: isogenies_sporadic_Q(E,37)
        [Isogeny of degree 37 from Elliptic Curve defined by y^2 = x^3 - x^2 - 6288*x + 211072 over Rational Field to Elliptic Curve defined by y^2 = x^3 - x^2 - 163137088*x - 801950801728 over Rational Field]

        sage: E = EllipticCurve([1, 1, 0, -25178045, 48616918750])
        sage: E.conductor()
        148225
        sage: isogenies_sporadic_Q(E,37)
        [Isogeny of degree 37 from Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 25178045*x + 48616918750 over Rational Field to Elliptic Curve defined by y^2 + x*y = x^3 + x^2 - 970*x - 13075 over Rational Field]

        sage: E = EllipticCurve([-3440, 77658])
        sage: E.conductor()
        118336
        sage: isogenies_sporadic_Q(E,43)
        [Isogeny of degree 43 from Elliptic Curve defined by y^2 = x^3 - 3440*x + 77658 over Rational Field to Elliptic Curve defined by y^2 = x^3 - 6360560*x - 6174354606 over Rational Field]

        sage: E = EllipticCurve([-29480, -1948226])
        sage: E.conductor()
        287296
        sage: isogenies_sporadic_Q(E,67)
        [Isogeny of degree 67 from Elliptic Curve defined by y^2 = x^3 - 29480*x - 1948226 over Rational Field to Elliptic Curve defined by y^2 = x^3 - 132335720*x + 585954296438 over Rational Field]

        sage: E = EllipticCurve([-34790720, -78984748304])
        sage: E.conductor()
        425104
        sage: isogenies_sporadic_Q(E,163)
        [Isogeny of degree 163 from Elliptic Curve defined by y^2 = x^3 - 34790720*x - 78984748304 over Rational Field to Elliptic Curve defined by y^2 = x^3 - 924354639680*x + 342062961763303088 over Rational Field]
    """
    F = E.base_field()
    j = E.j_invariant()
    j = QQ(j)
    if (j not in sporadic_j
        or (l is not None and sporadic_j[j] != l)):
        return []

    data = _sporadic_Q_data(j)
    Ew = E.short_weierstrass_model()
    E_to_Ew = E.isomorphism_to(Ew)
    c4, c6 = Ew.c_invariants()
    (a4,a6), f = data
    d = (c6*a4)/(18*c4*a6) # twisting factor
    R = PolynomialRing(F,'X')
    n = len(f)
    ker = R([d**(n-i-1) * f[i] for i in range(n)])
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None
    isog = Ew.isogeny(kernel=ker, degree=l, model=model, check=False)
    isog.set_pre_isomorphism(E_to_Ew)
    return [isog]


def isogenies_2(E):
    """Returns a list of all 2-isogenies with domain ``E``.

    INPUT:

    - ``E`` -- an elliptic curve.

    OUTPUT:

    (list) 2-isogenies with domain ``E``.  In general these are
    normalised, but over `\QQ` and other number fields, the codomain
    is a minimal model where possible.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_2
        sage: E = EllipticCurve('14a1'); E
        Elliptic Curve defined by y^2 + x*y + y = x^3 + 4*x - 6 over Rational Field
        sage: [phi.codomain().ainvs() for phi in isogenies_2(E)]
        [(1, 0, 1, -36, -70)]

        sage: E = EllipticCurve([1,2,3,4,5]); E
        Elliptic Curve defined by y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 5 over Rational Field
        sage: [phi.codomain().ainvs() for phi in isogenies_2(E)]
        []
        sage: E = EllipticCurve(QQbar, [9,8]); E
        Elliptic Curve defined by y^2 = x^3 + 9*x + 8 over Algebraic Field
        sage: isogenies_2(E) # not implemented

    """
    f2 = E.division_polynomial(2)
    x2 = sorted(f2.roots(multiplicities=False))
    x = f2.parent().gen()
    ff = [x-x2i for x2i in x2]
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(E.base_field()) else None
    isogs = [E.isogeny(f, model=model) for f in ff]
    return isogs

def isogenies_3(E):
    """Returns a list of all 3-isogenies with domain ``E``.

    INPUT:

    - ``E`` -- an elliptic curve.

    OUTPUT:

    (list) 3-isogenies with domain ``E``.  In general these are
    normalised, but over `\QQ` or a number field, the codomain is a
    global minimal model where possible.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_3
        sage: E = EllipticCurve(GF(17), [1,1])
        sage: [phi.codomain().ainvs() for phi in isogenies_3(E)]
        [(0, 0, 0, 9, 7), (0, 0, 0, 0, 1)]

        sage: E = EllipticCurve(GF(17^2,'a'), [1,1])
        sage: [phi.codomain().ainvs() for phi in isogenies_3(E)]
        [(0, 0, 0, 9, 7), (0, 0, 0, 0, 1), (0, 0, 0, 5*a + 1, a + 13), (0, 0, 0, 12*a + 6, 16*a + 14)]

        sage: E = EllipticCurve('19a1')
        sage: [phi.codomain().ainvs() for phi in isogenies_3(E)]
        [(0, 1, 1, 1, 0), (0, 1, 1, -769, -8470)]

        sage: E = EllipticCurve([1,1])
        sage: [phi.codomain().ainvs() for phi in isogenies_3(E)]
        []

    """
    f3 = E.division_polynomial(3)
    x3 = sorted(f3.roots(multiplicities=False))
    x = f3.parent().gen()
    ff = [x-x3i for x3i in x3]
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(E.base_field()) else None
    isogs = [E.isogeny(f, model=model) for f in ff]
    return isogs

# 6 special cases: `l` = 5, 7, 13 and `j` = 0, 1728.

def isogenies_5_0(E):
    """Returns a list of all the 5-isogenies  with domain ``E`` when the
    j-invariant is 0.

    OUTPUT:

    (list) 5-isogenies with codomain E.  In general these are
    normalised, but over `\QQ` or a number field, the codomain is a
    global minimal model where possible.

    .. note::

       This implementation requires that the characteristic is not 2,
       3 or 5.

    .. note::

       This function would normally be invoked indirectly via ``E.isogenies_prime_degree(5)``.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_5_0
        sage: E = EllipticCurve([0,12])
        sage: isogenies_5_0(E)
        []

        sage: E = EllipticCurve(GF(13^2,'a'),[0,-3])
        sage: isogenies_5_0(E)
        [Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + (4*a+6)*x + (2*a+10) over Finite Field in a of size 13^2, Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + (12*a+5)*x + (2*a+10) over Finite Field in a of size 13^2, Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + (10*a+2)*x + (2*a+10) over Finite Field in a of size 13^2, Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + (3*a+12)*x + (11*a+12) over Finite Field in a of size 13^2, Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + (a+4)*x + (11*a+12) over Finite Field in a of size 13^2, Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + (9*a+10)*x + (11*a+12) over Finite Field in a of size 13^2]

        sage: K.<a> = NumberField(x**6-320*x**3-320)
        sage: E = EllipticCurve(K,[0,0,1,0,0])
        sage: isogenies_5_0(E)
        [Isogeny of degree 5 from Elliptic Curve defined by y^2 + y = x^3 over Number Field in a with defining polynomial x^6 - 320*x^3 - 320 to Elliptic Curve defined by y^2 + y = x^3 + (643/8*a^5-15779/48*a^4-32939/24*a^3-71989/2*a^2+214321/6*a-112115/3)*x + (2901961/96*a^5+4045805/48*a^4+12594215/18*a^3-30029635/6*a^2+15341626/3*a-38944312/9) over Number Field in a with defining polynomial x^6 - 320*x^3 - 320,
        Isogeny of degree 5 from Elliptic Curve defined by y^2 + y = x^3 over Number Field in a with defining polynomial x^6 - 320*x^3 - 320 to Elliptic Curve defined by y^2 + y = x^3 + (-1109/8*a^5-53873/48*a^4-180281/24*a^3-14491/2*a^2+35899/6*a-43745/3)*x + (-17790679/96*a^5-60439571/48*a^4-77680504/9*a^3+1286245/6*a^2-4961854/3*a-73854632/9) over Number Field in a with defining polynomial x^6 - 320*x^3 - 320]
    """
    F = E.base_field()
    if E.j_invariant() != 0:
        raise ValueError("j-invariant must be 0.")
    if F.characteristic() in [2,3,5]:
        raise NotImplementedError("Not implemented in characteristic 2, 3 or 5.")
    if not F(5).is_square():
        return []
    Ew = E.short_weierstrass_model()
    a = Ew.a6()
    x = polygen(F)
    betas = sorted((x**6-160*a*x**3-80*a**2).roots(multiplicities=False))
    if len(betas)==0:
        return []
    gammas = [(beta**2 *(beta**3-140*a))/(120*a) for beta in betas]
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None
    isogs = [Ew.isogeny(x**2+beta*x+gamma, model=model) for beta,gamma in zip(betas,gammas)]
    iso = E.isomorphism_to(Ew)
    [isog.set_pre_isomorphism(iso) for isog in isogs]
    return isogs

def isogenies_5_1728(E):
    """Returns a list of 5-isogenies with domain ``E`` when the j-invariant is
    1728.

    OUTPUT:

    (list) 5-isogenies with codomain E.  In general these are
    normalised; but if `-1` is a square then there are two
    endomorphisms of degree `5`, for which the codomain is the same as
    the domain curve; and over `\QQ` or a number field, the codomain
    is a global minimal model where possible.

    .. note::

       This implementation requires that the characteristic is not 2,
       3 or 5.

    .. note::

       This function would normally be invoked indirectly via ``E.isogenies_prime_degree(5)``.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_5_1728
        sage: E = EllipticCurve([7,0])
        sage: isogenies_5_1728(E)
        []

        sage: E = EllipticCurve(GF(13),[11,0])
        sage: isogenies_5_1728(E)
        [Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 11*x over Finite Field of size 13 to Elliptic Curve defined by y^2 = x^3 + 11*x over Finite Field of size 13,
        Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + 11*x over Finite Field of size 13 to Elliptic Curve defined by y^2 = x^3 + 11*x over Finite Field of size 13]

    An example of endomorphisms of degree 5::

        sage: K.<i> = QuadraticField(-1)
        sage: E = EllipticCurve(K,[0,0,0,1,0])
        sage: isogenies_5_1728(E)
        [Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + x over Number Field in i with defining polynomial x^2 + 1 to Elliptic Curve defined by y^2 = x^3 + x over Number Field in i with defining polynomial x^2 + 1,
        Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + x over Number Field in i with defining polynomial x^2 + 1 to Elliptic Curve defined by y^2 = x^3 + x over Number Field in i with defining polynomial x^2 + 1]
        sage: _[0].rational_maps()
        (((4/25*i + 3/25)*x^5 + (4/5*i - 2/5)*x^3 - x)/(x^4 + (-4/5*i + 2/5)*x^2 + (-4/25*i - 3/25)),
         ((11/125*i + 2/125)*x^6*y + (-23/125*i + 64/125)*x^4*y + (141/125*i + 162/125)*x^2*y + (3/25*i - 4/25)*y)/(x^6 + (-6/5*i + 3/5)*x^4 + (-12/25*i - 9/25)*x^2 + (2/125*i - 11/125)))

    An example of 5-isogenies over a number field::

        sage: K.<a> = NumberField(x**4+20*x**2-80)
        sage: K(5).is_square() #necessary but not sufficient!
        True
        sage: E = EllipticCurve(K,[0,0,0,1,0])
        sage: isogenies_5_1728(E)
        [Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + x over Number Field in a with defining polynomial x^4 + 20*x^2 - 80 to Elliptic Curve defined by y^2 = x^3 + (-753/4*a^2-4399)*x + (2779*a^3+65072*a) over Number Field in a with defining polynomial x^4 + 20*x^2 - 80,
        Isogeny of degree 5 from Elliptic Curve defined by y^2 = x^3 + x over Number Field in a with defining polynomial x^4 + 20*x^2 - 80 to Elliptic Curve defined by y^2 = x^3 + (-753/4*a^2-4399)*x + (-2779*a^3-65072*a) over Number Field in a with defining polynomial x^4 + 20*x^2 - 80]

    See :trac:`19840`::

        sage: K.<a> = NumberField(x^4 - 5*x^2 + 5)
        sage: E = EllipticCurve([a^2 + a + 1, a^3 + a^2 + a + 1, a^2 + a, 17*a^3 + 34*a^2 - 16*a - 37, 54*a^3 + 105*a^2 - 66*a - 135])
        sage: len(E.isogenies_prime_degree(5))
        2
        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_5_1728
        sage: [phi.codomain().j_invariant() for phi in isogenies_5_1728(E)]
        [19691491018752*a^2 - 27212977933632, 19691491018752*a^2 - 27212977933632]
    """
    F = E.base_field()
    if E.j_invariant() != 1728:
        raise ValueError("j-invariant must be 1728.")
    if F.characteristic() in [2,3,5]:
        raise NotImplementedError("Not implemented in characteristic 2, 3 or 5.")
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None
    # quick test for a negative answer (from Fricke module)
    square5 = F(5).is_square()
    square1 = F(-1).is_square()
    if not square5 and not square1:
        return []
    Ew = E.short_weierstrass_model()
    iso = E.isomorphism_to(Ew)
    a = Ew.a4()
    x = polygen(F)
    isogs = []
    # 2 cases
    # Type 1: if -1 is a square we have 2 endomorphisms
    if square1:
        i = F(-1).sqrt()
        isogs = [Ew.isogeny(f) for f in [x**2+a/(1+2*i), x**2+a/(1-2*i)]]
        [isog.set_post_isomorphism(isog.codomain().isomorphism_to(E)) for isog in isogs]
    # Type 2: if 5 is a square we have up to 4 (non-endomorphism) isogenies
    if square5:
        betas = sorted((x**4+20*a*x**2-80*a**2).roots(multiplicities=False))
        gammas = [(beta**2-2*a)/6 for beta in betas]
        isogs += [Ew.isogeny(x**2+beta*x+gamma, model=model) for beta,gamma in zip(betas,gammas)]
    [isog.set_pre_isomorphism(iso) for isog in isogs]
    return isogs

def isogenies_7_0(E):
    """Returns list of all 7-isogenies from E when the j-invariant is 0.

    OUTPUT:

    (list) 7-isogenies with codomain E.  In general these are
    normalised; but if `-3` is a square then there are two
    endomorphisms of degree `7`, for which the codomain is the same as
    the domain; and over `\QQ` or a number field, the codomain is a
    global minimal model where possible.

    .. note::

       This implementation requires that the characteristic is not 2,
       3 or 7.

    .. note::

       This function would normally be invoked indirectly via ``E.isogenies_prime_degree(7)``.

    EXAMPLES:

    First some examples of endomorphisms::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_7_0
        sage: K.<r> = QuadraticField(-3)
        sage: E = EllipticCurve(K, [0,1])
        sage: isogenies_7_0(E)
        [Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + 1 over Number Field in r with defining polynomial x^2 + 3 to Elliptic Curve defined by y^2 = x^3 + 1 over Number Field in r with defining polynomial x^2 + 3,
        Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + 1 over Number Field in r with defining polynomial x^2 + 3 to Elliptic Curve defined by y^2 = x^3 + 1 over Number Field in r with defining polynomial x^2 + 3]

        sage: E = EllipticCurve(GF(13^2,'a'),[0,-3])
        sage: isogenies_7_0(E)
        [Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2, Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2 to Elliptic Curve defined by y^2 = x^3 + 10 over Finite Field in a of size 13^2]

    Now some examples of 7-isogenies which are not endomorphisms::

        sage: K = GF(101)
        sage: E = EllipticCurve(K, [0,1])
        sage: isogenies_7_0(E)
        [Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field of size 101 to Elliptic Curve defined by y^2 = x^3 + 55*x + 100 over Finite Field of size 101, Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field of size 101 to Elliptic Curve defined by y^2 = x^3 + 83*x + 26 over Finite Field of size 101]

    Examples over a number field::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_7_0
        sage: E = EllipticCurve('27a1').change_ring(QuadraticField(-3,'r'))
        sage: isogenies_7_0(E)
        [Isogeny of degree 7 from Elliptic Curve defined by y^2 + y = x^3 + (-7) over Number Field in r with defining polynomial x^2 + 3 to Elliptic Curve defined by y^2 + y = x^3 + (-7) over Number Field in r with defining polynomial x^2 + 3,
        Isogeny of degree 7 from Elliptic Curve defined by y^2 + y = x^3 + (-7) over Number Field in r with defining polynomial x^2 + 3 to Elliptic Curve defined by y^2 + y = x^3 + (-7) over Number Field in r with defining polynomial x^2 + 3]

        sage: K.<a> = NumberField(x^6 + 1512*x^3 - 21168)
        sage: E = EllipticCurve(K, [0,1])
        sage: isogs = isogenies_7_0(E)
        sage: [phi.codomain().a_invariants() for phi in isogs]
        [(0,
          0,
          0,
          -415/98*a^5 - 675/14*a^4 + 2255/7*a^3 - 74700/7*a^2 - 25110*a - 66420,
          -141163/56*a^5 + 1443453/112*a^4 - 374275/2*a^3 - 3500211/2*a^2 - 17871975/4*a - 7710065),
         (0,
          0,
          0,
          -24485/392*a^5 - 1080/7*a^4 - 2255/7*a^3 - 1340865/14*a^2 - 230040*a - 553500,
          1753037/56*a^5 + 8345733/112*a^4 + 374275/2*a^3 + 95377029/2*a^2 + 458385345/4*a + 275241835)]
        sage: [phi.codomain().j_invariant() for phi in isogs]
        [158428486656000/7*a^3 - 313976217600000,
        -158428486656000/7*a^3 - 34534529335296000]
    """
    if E.j_invariant()!=0:
        raise ValueError("j-invariant must be 0.")
    F = E.base_field()
    if F.characteristic() in [2,3,7]:
        raise NotImplementedError("Not implemented when the characteristic of the base field is 2, 3 or 7.")
    x = polygen(F)
    Ew = E.short_weierstrass_model()
    iso = E.isomorphism_to(Ew)
    a = Ew.a6()
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None

    # there will be 2 endomorphisms if -3 is a square:

    ts = sorted((x**2+3).roots(multiplicities=False))
    kers = [7*x-(2+6*t) for t in ts]
    kers = [k(x**3/a).monic() for k in kers]
    isogs = [Ew.isogeny(k,model=model) for k in kers]
    if len(isogs)>0:
        [endo.set_post_isomorphism(endo.codomain().isomorphism_to(E)) for endo in isogs]

    # we may have up to 6 other isogenies:
    ts = (x**2-21).roots(multiplicities=False)
    for t0 in ts:
        s3 = a/(28+6*t0)
        ss = sorted((x**3-s3).roots(multiplicities=False))
        ker = x**3 - 2*t0*x**2 - 4*t0*x + 4*t0 + 28
        kers = [ker(x/s).monic() for s in ss]
        isogs += [Ew.isogeny(k, model=model) for k in kers]

    [isog.set_pre_isomorphism(iso) for isog in isogs]
    return isogs

def isogenies_7_1728(E):
    """Returns list of all 7-isogenies from E when the j-invariant is 1728.

    OUTPUT:

    (list) 7-isogenies with codomain E.  In general these are
    normalised; but over `\QQ` or a number field, the codomain is a
    global minimal model where possible.

    .. note::

       This implementation requires that the characteristic is not 2,
       3, or 7.

    .. note::

       This function would normally be invoked indirectly via ``E.isogenies_prime_degree(7)``.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_7_1728
        sage: E = EllipticCurve(GF(47), [1, 0])
        sage: isogenies_7_1728(E)
        [Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 47 to Elliptic Curve defined by y^2 = x^3 + 26 over Finite Field of size 47,
        Isogeny of degree 7 from Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 47 to Elliptic Curve defined by y^2 = x^3 + 21 over Finite Field of size 47]

    An example in characteristic 53 (for which an earlier implementation did not work)::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_7_1728
        sage: E = EllipticCurve(GF(53), [1, 0])
        sage: isogenies_7_1728(E)
        []
        sage: E = EllipticCurve(GF(53^2,'a'), [1, 0])
        sage: [iso.codomain().ainvs() for iso in isogenies_7_1728(E)]
        [(0, 0, 0, 36, 19*a + 15), (0, 0, 0, 36, 34*a + 38), (0, 0, 0, 33, 39*a + 28), (0, 0, 0, 33, 14*a + 25), (0, 0, 0, 19, 45*a + 16), (0, 0, 0, 19, 8*a + 37), (0, 0, 0, 3, 45*a + 16), (0, 0, 0, 3, 8*a + 37)]

    ::

        sage: K.<a> = NumberField(x^8 + 84*x^6 - 1890*x^4 + 644*x^2 - 567)
        sage: E = EllipticCurve(K, [1, 0])
        sage: isogs = isogenies_7_1728(E)
        sage: [phi.codomain().j_invariant() for phi in isogs]
        [-526110256146528/53*a^6 + 183649373229024*a^4 - 3333881559996576/53*a^2 + 2910267397643616/53,
        -526110256146528/53*a^6 + 183649373229024*a^4 - 3333881559996576/53*a^2 + 2910267397643616/53]
        sage: E1 = isogs[0].codomain()
        sage: E2 = isogs[1].codomain()
        sage: E1.is_isomorphic(E2)
        False
        sage: E1.is_quadratic_twist(E2)
        -1

    """
    if E.j_invariant()!=1728:
        raise ValueError("j_invariant must be 1728 (in base field).")
    F = E.base_field()
    if F.characteristic() in [2,3,7]:
        raise NotImplementedError("Not implemented when the characteristic of the base field is 2, 3 or 7.")
    Ew = E.short_weierstrass_model()
    iso = E.isomorphism_to(Ew)
    a = Ew.a4()

    ts = (Fricke_module(7)-1728).numerator().roots(F,multiplicities=False)
    if len(ts)==0:
        return []
    ts.sort()
    isogs = []
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None
    x = polygen(F)
    for t0 in ts:
        s2 = a/t0
        ss = sorted((x**2-s2).roots(multiplicities=False))
        ker = 9*x**3 + (-3*t0**3 - 36*t0**2 - 123*t0)*x**2 + (-8*t0**3 - 101*t0**2 - 346*t0 + 35)*x - 7*t0**3 - 88*t0**2 - 296*t0 + 28

        kers = [ker(x/s) for s in ss]
        isogs += [Ew.isogeny(k.monic(), model=model) for k in kers]
    [isog.set_pre_isomorphism(iso) for isog in isogs]
    return isogs

def isogenies_13_0(E):
    """
    Returns list of all 13-isogenies from E when the j-invariant is 0.

    OUTPUT:

    (list) 13-isogenies with codomain E.  In general these are
    normalised; but if `-3` is a square then there are two
    endomorphisms of degree `13`, for which the codomain is the same
    as the domain.

    .. note::

       This implementation requires that the characteristic is not 2,
       3 or 13.

    .. note::

       This function would normally be invoked indirectly via ``E.isogenies_prime_degree(13)``.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_13_0

    Endomorphisms of degree 13 will exist when -3 is a square::

        sage: K.<r> = QuadraticField(-3)
        sage: E = EllipticCurve(K, [0, r]); E
        Elliptic Curve defined by y^2 = x^3 + r over Number Field in r with defining polynomial x^2 + 3
        sage: isogenies_13_0(E)
        [Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + r over Number Field in r with defining polynomial x^2 + 3 to Elliptic Curve defined by y^2 = x^3 + r over Number Field in r with defining polynomial x^2 + 3,
        Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + r over Number Field in r with defining polynomial x^2 + 3 to Elliptic Curve defined by y^2 = x^3 + r over Number Field in r with defining polynomial x^2 + 3]
        sage: isogenies_13_0(E)[0].rational_maps()
        (((7/338*r + 23/338)*x^13 + (-164/13*r - 420/13)*x^10 + (720/13*r + 3168/13)*x^7 + (3840/13*r - 576/13)*x^4 + (4608/13*r + 2304/13)*x)/(x^12 + (4*r + 36)*x^9 + (1080/13*r + 3816/13)*x^6 + (2112/13*r - 5184/13)*x^3 + (-17280/169*r - 1152/169)), ((18/2197*r + 35/2197)*x^18*y + (23142/2197*r + 35478/2197)*x^15*y + (-1127520/2197*r - 1559664/2197)*x^12*y + (-87744/2197*r + 5992704/2197)*x^9*y + (-6625152/2197*r - 9085824/2197)*x^6*y + (-28919808/2197*r - 2239488/2197)*x^3*y + (-1990656/2197*r - 3870720/2197)*y)/(x^18 + (6*r + 54)*x^15 + (3024/13*r + 11808/13)*x^12 + (31296/13*r + 51840/13)*x^9 + (487296/169*r - 2070144/169)*x^6 + (-940032/169*r + 248832/169)*x^3 + (1990656/2197*r + 3870720/2197)))

    An example of endomorphisms over a finite field::

        sage: K = GF(19^2,'a')
        sage: E = EllipticCurve(j=K(0)); E
        Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field in a of size 19^2
        sage: isogenies_13_0(E)
        [Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field in a of size 19^2 to Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field in a of size 19^2,
        Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field in a of size 19^2 to Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field in a of size 19^2]
        sage: isogenies_13_0(E)[0].rational_maps()
        ((6*x^13 - 6*x^10 - 3*x^7 + 6*x^4 + x)/(x^12 - 5*x^9 - 9*x^6 - 7*x^3 + 5), (-8*x^18*y - 9*x^15*y + 9*x^12*y - 5*x^9*y + 5*x^6*y - 7*x^3*y + 7*y)/(x^18 + 2*x^15 + 3*x^12 - x^9 + 8*x^6 - 9*x^3 + 7))

    A previous implementation did not work in some characteristics::

        sage: K = GF(29)
        sage: E = EllipticCurve(j=K(0))
        sage: isogenies_13_0(E)
        [Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field of size 29 to Elliptic Curve defined by y^2 = x^3 + 26*x + 12 over Finite Field of size 29, Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + 1 over Finite Field of size 29 to Elliptic Curve defined by y^2 = x^3 + 16*x + 28 over Finite Field of size 29]

    ::

        sage: K = GF(101)
        sage: E = EllipticCurve(j=K(0)); E.ainvs()
        (0, 0, 0, 0, 1)
        sage: [phi.codomain().ainvs() for phi in isogenies_13_0(E)]
        [(0, 0, 0, 64, 36), (0, 0, 0, 42, 66)]

    ::

        sage: x = polygen(QQ)
        sage: f = x^12 + 78624*x^9 - 130308048*x^6 + 2270840832*x^3 - 54500179968
        sage: K.<a> = NumberField(f)
        sage: E = EllipticCurve(j=K(0)); E.ainvs()
        (0, 0, 0, 0, 1)
        sage: [phi.codomain().ainvs() for phi in isogenies_13_0(E)]
        [(0,
          0,
          20360599/165164973653422080*a^11 - 3643073/41291243413355520*a^10 - 101/8789110986240*a^9 + 5557619461/573489491852160*a^8 - 82824971/11947697746920*a^7 - 19487/21127670640*a^6 - 475752603733/29409717530880*a^5 + 87205112531/7352429382720*a^4 + 8349/521670880*a^3 + 5858744881/12764634345*a^2 - 1858703809/2836585410*a + 58759402/48906645,
          -139861295/2650795873449984*a^11 - 3455957/5664093746688*a^10 - 345310571/50976843720192*a^9 - 500530795/118001953056*a^8 - 12860048113/265504394376*a^7 - 25007420461/44250732396*a^6 + 458134176455/1416023436672*a^5 + 16701880631/9077073312*a^4 + 155941666417/9077073312*a^3 + 3499310115/378211388*a^2 - 736774863/94552847*a - 21954102381/94552847,
          579363345221/13763747804451840*a^11 + 371192377511/860234237778240*a^10 + 8855090365657/1146978983704320*a^9 + 5367261541663/1633873196160*a^8 + 614883554332193/15930263662560*a^7 + 30485197378483/68078049840*a^6 - 131000897588387/2450809794240*a^5 - 203628705777949/306351224280*a^4 - 1587619388190379/204234149520*a^3 + 14435069706551/11346341640*a^2 + 7537273048614/472764235*a + 89198980034806/472764235),
         (0,
          0,
          20360599/165164973653422080*a^11 - 3643073/41291243413355520*a^10 - 101/8789110986240*a^9 + 5557619461/573489491852160*a^8 - 82824971/11947697746920*a^7 - 19487/21127670640*a^6 - 475752603733/29409717530880*a^5 + 87205112531/7352429382720*a^4 + 8349/521670880*a^3 + 5858744881/12764634345*a^2 - 1858703809/2836585410*a + 58759402/48906645,
          -6465569317/1325397936724992*a^11 - 112132307/1960647835392*a^10 - 17075412917/25488421860096*a^9 - 207832519229/531008788752*a^8 - 1218275067617/265504394376*a^7 - 9513766502551/177002929584*a^6 + 4297077855437/708011718336*a^5 + 354485975837/4538536656*a^4 + 4199379308059/4538536656*a^3 - 30841577919/189105694*a^2 - 181916484042/94552847*a - 2135779171614/94552847,
          -132601797212627/3440936951112960*a^11 - 6212467020502021/13763747804451840*a^10 - 1515926454902497/286744745926080*a^9 - 15154913741799637/4901619588480*a^8 - 576888119803859263/15930263662560*a^7 - 86626751639648671/204234149520*a^6 + 16436657569218427/306351224280*a^5 + 1540027900265659087/2450809794240*a^4 + 375782662805915809/51058537380*a^3 - 14831920924677883/11346341640*a^2 - 7237947774817724/472764235*a - 84773764066089509/472764235)]
    """
    if E.j_invariant()!=0:
        raise ValueError("j-invariant must be 0.")
    F = E.base_field()
    if F.characteristic() in [2,3,13]:
        raise NotImplementedError("Not implemented when the characteristic of the base field is 2, 3 or 13.")
    Ew = E.short_weierstrass_model()
    iso = E.isomorphism_to(Ew)
    a = Ew.a6()
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None
    x = polygen(F)

    # there will be 2 endomorphisms if -3 is a square:
    ts = sorted((x**2+3).roots(multiplicities=False))
    kers = [13*x**2 + (78*t + 26)*x + 24*t + 40 for t in ts]
    kers = [k(x**3/a).monic() for k in kers]
    isogs = [Ew.isogeny(k,model=model) for k in kers]
    if len(isogs)>0:
        [endo.set_post_isomorphism(endo.codomain().isomorphism_to(E)) for endo in isogs]

    # we may have up to 12 other isogenies:
    ts = sorted((x**4 + 7*x**3 + 20*x**2 + 19*x + 1).roots(multiplicities=False))
    for t0 in ts:
        s3 = a / (6*t0**3 + 32*t0**2 + 68*t0 + 4)
        ss = sorted((x**3-s3).roots(multiplicities=False))
        ker = (x**6 + (20*t0**3 + 106*t0**2 + 218*t0 + 4)*x**5
            + (-826*t0**3 - 4424*t0**2 - 9244*t0 - 494)*x**4
            + (13514*t0**3 + 72416*t0**2 + 151416*t0 + 8238)*x**3
            + (-101948*t0**3 - 546304*t0**2 - 1142288*t0 - 62116)*x**2
            + (354472*t0**3 + 1899488*t0**2 + 3971680*t0 + 215960)*x
            - 459424*t0**3 - 2461888*t0**2 - 5147648*t0 - 279904)
        kers = [ker(x/s).monic() for s in ss]
        isogs += [Ew.isogeny(k, model=model) for k in kers]

    [isog.set_pre_isomorphism(iso) for isog in isogs]

    return isogs

def isogenies_13_1728(E):
    """Returns list of all 13-isogenies from E when the j-invariant is 1728.

    OUTPUT:

    (list) 13-isogenies with codomain E.  In general these are
    normalised; but if `-1` is a square then there are two
    endomorphisms of degree `13`, for which the codomain is the same
    as the domain; and over `\QQ` or a number field, the codomain is a
    global minimal model where possible.

    .. note::

       This implementation requires that the characteristic is not
       2, 3 or 13.

    .. note::

       This function would normally be invoked indirectly via ``E.isogenies_prime_degree(13)``.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import isogenies_13_1728

        sage: K.<i> = QuadraticField(-1)
        sage: E = EllipticCurve([0,0,0,i,0]); E.ainvs()
        (0, 0, 0, i, 0)
        sage: isogenies_13_1728(E)
        [Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + i*x over Number Field in i with defining polynomial x^2 + 1 to Elliptic Curve defined by y^2 = x^3 + i*x over Number Field in i with defining polynomial x^2 + 1,
        Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + i*x over Number Field in i with defining polynomial x^2 + 1 to Elliptic Curve defined by y^2 = x^3 + i*x over Number Field in i with defining polynomial x^2 + 1]

    ::

        sage: K = GF(83)
        sage: E = EllipticCurve(K, [0,0,0,5,0]); E.ainvs()
        (0, 0, 0, 5, 0)
        sage: isogenies_13_1728(E)
        []
        sage: K = GF(89)
        sage: E = EllipticCurve(K, [0,0,0,5,0]); E.ainvs()
        (0, 0, 0, 5, 0)
        sage: isogenies_13_1728(E)
        [Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + 5*x over Finite Field of size 89 to Elliptic Curve defined by y^2 = x^3 + 5*x over Finite Field of size 89,
        Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + 5*x over Finite Field of size 89 to Elliptic Curve defined by y^2 = x^3 + 5*x over Finite Field of size 89]

    ::

        sage: K = GF(23)
        sage: E = EllipticCurve(K, [1,0])
        sage: isogenies_13_1728(E)
        [Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 23 to Elliptic Curve defined by y^2 = x^3 + 16 over Finite Field of size 23, Isogeny of degree 13 from Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 23 to Elliptic Curve defined by y^2 = x^3 + 7 over Finite Field of size 23]

    ::

        sage: x = polygen(QQ)
        sage: f = x^12 + 1092*x^10 - 432432*x^8 + 6641024*x^6 - 282896640*x^4 - 149879808*x^2 - 349360128
        sage: K.<a> = NumberField(f)
        sage: E = EllipticCurve(K, [1,0])
        sage: [phi.codomain().ainvs() for phi in isogenies_13_1728(E)]
        [(0,
        0,
        0,
        -4225010072113/3063768069807341568*a^10 - 24841071989413/15957125363579904*a^8 + 11179537789374271/21276167151439872*a^6 - 407474562289492049/47871376090739712*a^4 + 1608052769560747/4522994717568*a^2 + 7786720245212809/36937790193472,
        -363594277511/574456513088876544*a^11 - 7213386922793/2991961005671232*a^9 - 2810970361185589/1329760446964992*a^7 + 281503836888046601/8975883017013696*a^5 - 1287313166530075/848061509544*a^3 + 9768837984886039/6925835661276*a),
        (0,
        0,
        0,
        -4225010072113/3063768069807341568*a^10 - 24841071989413/15957125363579904*a^8 + 11179537789374271/21276167151439872*a^6 - 407474562289492049/47871376090739712*a^4 + 1608052769560747/4522994717568*a^2 + 7786720245212809/36937790193472,
        363594277511/574456513088876544*a^11 + 7213386922793/2991961005671232*a^9 + 2810970361185589/1329760446964992*a^7 - 281503836888046601/8975883017013696*a^5 + 1287313166530075/848061509544*a^3 - 9768837984886039/6925835661276*a)]
    """
    if E.j_invariant()!=1728:
        raise ValueError("j-invariant must be 1728.")
    F = E.base_field()
    if F.characteristic() in [2, 3, 13]:
        raise NotImplementedError("Not implemented when the characteristic of the base field is 2, 3 or 13.")
    Ew = E.short_weierstrass_model()
    iso = E.isomorphism_to(Ew)
    a = Ew.a4()
    from sage.rings.number_field.number_field_base import is_NumberField
    model = "minimal" if is_NumberField(F) else None
    x = polygen(F)

    # we will have two endomorphisms if -1 is a square:
    ts = sorted((x**2+1).roots(multiplicities=False))
    kers = [13*x**3 + (-26*i - 13)*x**2 + (-52*i - 13)*x - 2*i - 3 for i in ts]
    kers = [k(x**2/a).monic() for k in kers]
    isogs = [Ew.isogeny(k,model=model) for k in kers]
    if len(isogs)>0:
        [endo.set_post_isomorphism(endo.codomain().isomorphism_to(E)) for endo in isogs]

    # we may have up to 12 other isogenies:

    ts = sorted((x**6 + 10*x**5 + 46*x**4 + 108*x**3 + 122*x**2 + 38*x - 1).roots(multiplicities=False))
    for t0 in ts:
        s2 = a/(66*t0**5 + 630*t0**4 + 2750*t0**3 + 5882*t0**2 + 5414*t0 + 162)
        ss = sorted((x**2-s2).roots(multiplicities=False))
        ker = (x**6 + (-66*t0**5 - 630*t0**4 - 2750*t0**3 - 5882*t0**2
              - 5414*t0 - 162)*x**5 + (-21722*t0**5 - 205718*t0**4 -
              890146*t0**3 - 1873338*t0**2 - 1652478*t0 + 61610)*x**4
              + (-3391376*t0**5 - 32162416*t0**4 - 139397232*t0**3 -
              294310576*t0**2 - 261885968*t0 + 6105552)*x**3 +
              (-241695080*t0**5 - 2291695976*t0**4 - 9930313256*t0**3
              - 20956609720*t0**2 - 18625380856*t0 + 469971320)*x**2 +
              (-8085170432*t0**5 - 76663232384*t0**4 -
              332202985024*t0**3 - 701103233152*t0**2 -
              623190845440*t0 + 15598973056)*x - 101980510208*t0**5 -
              966973468160*t0**4 - 4190156868352*t0**3 -
              8843158270336*t0**2 - 7860368751232*t0 + 196854655936)

        kers = [ker(x/s).monic() for s in ss]
        isogs += [Ew.isogeny(k, model=model) for k in kers]

    [isog.set_pre_isomorphism(iso) for isog in isogs]

    return isogs

# List of primes l for which X_0(l) is (hyper)elliptic and X_0^+(l) has genus 0

hyperelliptic_primes = [11, 17, 19, 23, 29, 31, 41, 47, 59, 71]

@cached_function
def _hyperelliptic_isogeny_data(l):
    r"""
    Helper function for elliptic curve isogenies.

    INPUT:

    - ``l`` -- a prime in [11, 17, 19, 23, 29, 31, 41, 47, 59, 71]

    OUTPUT:

    - A dict holding a collection of precomputed data needed for computing `l`-isogenies.

    EXAMPLES::

        sage: from sage.schemes.elliptic_curves.isogeny_small_degree import _hyperelliptic_isogeny_data
        sage: HID = _hyperelliptic_isogeny_data(11)
        sage: HID['A2']
        55*u - 33
        sage: HID['A4']
        -183*u^2 + 738*u - 180*v - 135
        sage: HID['A6']
        1330*u^3 - 11466*u^2 + 1332*u*v + 2646*u - 1836*v + 1890
        sage: HID['alpha']
        u^11 - 55*u^10 + 1188*u^9 - 12716*u^8 + 69630*u^7 - 177408*u^6 + 133056*u^5 + 132066*u^4 - 187407*u^3 + 40095*u^2 + 24300*u - 6750
        sage: HID['beta']
        u^9 - 47*u^8 + 843*u^7 - 7187*u^6 + 29313*u^5 - 48573*u^4 + 10665*u^3 + 27135*u^2 - 12150*u
        sage: HID['hyper_poly']
        u^4 - 16*u^3 + 2*u^2 + 12*u - 7

        sage: _hyperelliptic_isogeny_data(37)
        Traceback (most recent call last):
        ...
        ValueError: 37 must be one of [11, 17, 19, 23, 29, 31, 41, 47, 59, 71].

    """
    if not l in hyperelliptic_primes:
        raise ValueError("%s must be one of %s."%(l,hyperelliptic_primes))
    data = {}
    Zu = PolynomialRing(ZZ,'u')
    Zuv = PolynomialRing(ZZ,['u','v'])
    Zxuv = PolynomialRing(ZZ,['x','u','v'])
    x,u,v = Zxuv.gens()
    if l == 11:
        data['hyper_poly'] = Zu([-7, 12, 2, -16, 1])
        data['A2'] = Zu([-33, 55])
        data['A4'] = Zuv(Zu([-135, 738, -183])+v*Zu([-180]))
        data['A6'] = Zuv(Zu([1890, 2646, -11466, 1330]) + v*Zu([-1836, 1332]))
        data['alpha'] = Zu([-6750, 24300, 40095, -187407, 132066, 133056, -177408, 69630, -12716, 1188, -55, 1])
        data['beta'] = Zu([0, -12150, 27135, 10665, -48573, 29313, -7187, 843, -47, 1])
        #beta factors as (u - 15) * (u - 6) * (u - 3) * (u - 1) * u * (u**2 - 12*u - 9) * (u**2 - 10*u + 5)
        return data
    if l == 17:
        data['hyper_poly'] = Zu([-8, 4, -3, -10, 1])
        data['A2'] = Zu([68, -204, 136])
        data['A4'] = Zuv(Zu([60, 720, -2595, 2250, -435]) + v*Zu([-360, 792, -432]))
        data['A6'] = Zuv(Zu([-8512, 22608, -5064, -57528, 87288, -43704, 4912] ) + v*Zu( [2520, -15372, 28098, -20160, 4914]))
        data['alpha'] = Zu([16000, -67200, 2720, 557600, -1392232, 1073992, 1104830, -3131026, 2450210, 73746, -1454945, 1110355, -424065, 95659, -13243, 1105, -51, 1])
        data['beta'] = Zu([0, 22400, -105920, 146208, 111616, -593800, 680948, -102282, -457950, 468035, -219274, 58549, -9374, 889, -46, 1])
        #beta factors as (u - 10) * (u - 5) * (u - 2) * (u - 1) * u * (u + 1) * (u**2 - 10*u + 7) * (u**2 - 6*u - 4) * (u**2 - 4*u + 2) * (u**3 - 9*u**2 + 8*u - 4)
        data['endo'] = 17*x**8 + 17*(-4*u + 4)*v*x**6 + 17*(4*u + 6)*v**2*x**4 + 17*(4*u + 4)*v**3*x**2 + (-4*u + 1)*v**4
        data['endo_u'] = 1
        return data
    if l == 19:
        data['hyper_poly'] = Zu([-8, 20, -8, -8, 1])
        data['A2'] = Zu([-114, 57, 171])
        data['A4'] = Zuv(Zu([-1020, 444, 2733, 726, -543]) + v*Zu([-180, -720, -540]))
        data['A6'] = Zuv(Zu([-10080, 21816, 54324, -37386, -86742, -20070, 6858]) + v*Zu([-2968, -13748, -11284, 6356, 6860]))
        data['alpha'] = Zu([16000, -22400, -337440, 475456, 1562104, -1988616, -3025294, 3245960, 2833014, -2420087, -1140950, 932406, 129580, -180443, 21090, 11153, -4066, 570, -38, 1])
        data['beta'] = Zu([0, 33600, -8160, -292400, 23472, 791244, 39282, -847909, -47024, 392654, -24046, -82469, 19162, 4833, -2652, 446, -34, 1])
        #beta factors as (u - 7) * (u - 2) * (u - 1) * u * (u + 1) * (u**2 - 8*u - 4) * (u**2 - 6*u - 15) * (u**2 - 5*u - 5) * (u**2 - 5*u + 2) * (u**2 - 2*u - 4) * (u**2 + u - 1)
        data['endo'] = 19*x**9 + 19*(-12*u - 24)*v*x**6 + 19*(-24*u - 24)*v**2*x**3 + (96*u - 224)*v**3
        data['endo_u'] = -1
        return data
    if l == 23:
        data['hyper_poly'] = Zu([-7, 10, -11, 2, 2, -8, 1])
        data['A2'] = Zu([69, -230, 253])
        data['A4'] = Zuv(Zu([405, 180, -930, 2820, -795]) + v*Zu([360, -792]))
        data['A6'] = Zuv(Zu([-15498, 34020, -36918, -8120, 51114, -72492, 12166]) + v*Zu([-1080, 7704, -24840, 12168]))
        data['alpha'] = Zu([-6750, 48600, -83835, -170775, 1115109, -2492280, 2732814, -116403, -4877702, 8362616, -6612454, 302266, 5423124, -6447728, 3209696, 336674, -1470068, 953856, -336927, 74221, -10465, 920, -46, 1])
        data['beta'] = Zu( [0, 12150, -72495, 168588, -144045, -254034, 930982, -1256170, 604358, 693650, -1563176, 1271974, -225188, -444070, 421050, -184350, 47754, -7696, 759, -42, 1])
        #beta factors as (u - 5) * (u - 3) * (u - 2) * (u - 1) * u * (u + 1) * (u**2 - 8*u + 3) * (u**2 - 6*u - 9) * (u**3 - 7*u**2 + 3*u - 5) * (u**3 - 7*u**2 + 7*u - 3) * (u**4 - 4*u**3 - 1)
        return data
    if l == 29:
        data['hyper_poly'] = Zu([-7, 8, 8, 2, -12, -4, 1])
        data['A2'] = Zu([-174, -232, 348, 406])
        data['A4'] = Zuv(Zu([-1215, -3096, 132, 7614, 6504, -360, -1263] ) + v*Zu( [180, -720, -2160, -1260]))
        data['A6'] = Zuv(Zu([-18900, -63504, 24696, 285068, 285264, -185136, -506268, -275520, 504, 24388] ) + v*Zu( [4482, -2448, -59868, -94968, -18144, 48276, 24390]))
        data['alpha'] = Zu([-6750, -12150, 281880, 570024, -1754181, -5229135, 2357613, 19103721, 9708910, -31795426, -38397537, 19207947, 54103270, 9216142, -37142939, -18871083, 14041394, 10954634, -3592085, -3427365, 853818, 622398, -189399, -53679, 26680, -580, -1421, 319, -29, 1])
        data['beta'] = Zu([0, -24300, -57510, 257850, 839187, -373185, -3602119, -2371192, 5865017, 8434433, -2363779, -10263744, -2746015, 5976011, 3151075, -2093854, -1356433, 569525, 299477, -129484, -28279, 19043, -895, -1076, 273, -27, 1])
        #beta factors as (u - 3) * (u - 1) * u * (u + 1) * (u + 2) * (u**2 - 6*u + 2) * (u**2 - 5*u - 5) * (u**2 - 5*u + 3) * (u**2 - 3*u - 9) * (u**2 - u - 3) * (u**2 - u - 1) * (u**2 + u - 1) * (u**3 - 4*u**2 - 6*u - 5) * (u**4 - 2*u**3 - 5*u**2 - 4*u - 1)
        data['endo'] = 29*x**14 + 29*(-14*u + 3)*v*x**12 + 29*(-20*u + 73)*v**2*x**10 + 29*(-58*u + 115)*v**3*x**8 + 29*(-56*u + 59)*v**4*x**6 + 29*(30*u + 1)*v**5*x**4 + 29*(12*u - 5)*v**6*x**2 + (2*u + 5)*v**7
        data['endo_u'] = -1
        return data
    if l == 31:
        data['hyper_poly'] = Zu([-3, -14, -11, 18, 6, -8, 1])
        data['A2'] = Zu([558, 837, -1488, 465])
        data['A4'] = Zuv(Zu([-4140, -12468, 15189, 16956, -27054, 11184, -1443]) + v*Zu([2160, -7560, 6120, -1440]))
        data['A6'] = Zuv(Zu([71280, 592056, -108324, -2609730, 2373048, 1282266, -2793204, 1530882, -356976, 29790]) + v*Zu([-81312, 181664, 294728, -868392, 701400, -238840, 29792]))
        data['alpha'] = Zu([108000, 475200, -7053120, -27353408, 90884374, 303670296, -665806437, -1361301729, 3259359840, 2249261823, -9368721606, 2279583264, 13054272515, -12759480061, -4169029296, 14390047139, -7803693550, -2988803682, 6239473912, -3296588360, 134066754, 908915598, -685615437, 294482733, -87483178, 18983315, -3052818, 361336, -30659, 1767, -62, 1])
        data['beta'] = Zu([0, 712800, 1216080, -18430560, -15262464, 168899202, -12931221, -720077416, 624871714, 1239052988, -2259335558, 68648452, 2679085427, -2318039014, -229246628, 1710545918, -1243026758, 211524870, 296674626, -291810274, 145889932, -48916468, 11793961, -2085662, 269348, -24778, 1540, -58, 1])
        #beta factors as (u - 3) * (u - 2) * (u - 1) * u * (u + 1) * (u**2 - 8*u + 11) * (u**2 - 7*u + 2) * (u**2 - 5*u - 2) * (u**2 - 5*u + 5) * (u**2 - 4*u - 4) * (u**2 - 4*u - 1) * (u**2 - 2*u - 1) * (u**2 - u - 1) * (u**3 - 9*u**2 + 21*u - 15) * (u**4 - 8*u**3 + 8*u**2 + 12*u - 9)
        data['endo'] = 31*x**15 + 31*(-66*u + 86)*v*x**12 + 31*(168*u + 280)*v**2*x**9 + 31*(576*u + 1792)*v**3*x**6 + 31*(384*u + 896)*v**4*x**3 + (-3072*u - 2048)*v**5
        data['endo_u'] = 2
        return data
    if l == 41:
        data['hyper_poly'] = Zu([-8, -20, -15, 8, 20, 10, -8, -4, 1])
        data['A2'] = Zu([328, 656, -656, -1148, 820])
        data['A4'] = Zuv(Zu([-1380, -4008, 1701, 10872, 6144, -18378, -2160, 9732, -2523]) + v*Zu([720, -1440, -2160, 5400, -2520]))
        data['A6'] = Zuv(Zu([4480, 155616, 16080, -550720, -343968, 832680, 938632, -621648, -1468608, 953920, 427632, -413016, 68920]) + v*Zu([-14616, 6804, 96390, -2016, -324324, 184464, 260568, -276192, 68922]))
        data['alpha'] = Zu([16000, 67200, -465760, -2966432, -1742664, 20985112, 46140990, -31732934, -217030548, -147139488, 436080674, 745775322, -271341362, -1542677562, -605560447, 1832223375, 1772593672, -1270633050, -2400692229, 343522723, 2179745361, 282422801, -1503727029, -421357697, 879637411, 261059095, -462271351, -61715127, 193718727, -24135265, -49355103, 20512341, 3613289, -4706595, 1099661, 163057, -162483, 46617, -7544, 738, -41, 1])
        data['beta'] = Zu([0, 44800, 167040, -447040, -2734272, -1104272, 13488360, 21067652, -24681704, -83929974, -8986886, 169059382, 127641266, -196479899, -283039783, 124573790, 366614063, -12946368, -332987597, -58867672, 241909907, 60568430, -155045647, -17919564, 79114945, -12025938, -24060781, 11190142, 1979597, -2931764, 750233, 110144, -122263, 37484, -6439, 666, -39, 1])
        #beta factors as (u - 5) * (u - 2) * (u - 1) * u * (u + 1) * (u**2 - 5*u + 5) * (u**2 - 3*u - 7) * (u**2 - 2*u - 4) * (u**2 - 2*u - 1) * (u**2 - u - 1) * (u**2 - 2) * (u**2 + u - 1) * (u**3 - 3*u**2 - 5*u - 2) * (u**3 - 2*u**2 - 2*u - 1) * (u**4 - 6*u**3 + 5*u**2 + 2*u - 1) * (u**4 - 5*u**3 + u**2 + 4) * (u**4 - 4*u**3 + 2)
        data['endo'] = 41*x**20 + 41*(-12*u - 22)*v*x**18 + 41*(-252*u - 247)*v**2*x**16 + 41*(-176*u - 424)*v**3*x**14 + 41*(464*u - 254)*v**4*x**12 + 41*(1688*u - 868)*v**5*x**10 + 41*(1720*u - 1190)*v**6*x**8 + 41*(528*u - 232)*v**7*x**6 + 41*(16*u + 29)*v**8*x**4 + 41*(20*u + 10)*v**9*x**2 + (4*u + 5)*v**10
        data['endo_u'] = 1
        return data
    if l == 47:
        data['hyper_poly'] = Zu([-11, 28, -38, 30, -13, -16, 19, -24, 11, -6, 1])
        data['A2'] = Zu([376, -1504, 2209, -1598, 1081])
        data['A4'] = Zuv(Zu([2400, -4080, -1440, 18000, -26355, 34740, -22050, 12900, -3315]) + v*Zu([1152, -3384, 3672, -3312]))
        data['A6'] = Zuv(Zu([-119504, 606336, -1505280, 2109392, -1509360, -515808, 2920702, -4614012, 4334322, -3260312, 1571442, -622428, 103822]) + v*Zu([2016, 48384, -235872, 438984, -627480, 503496, -311976, 103824]))
        data['alpha'] = Zu([-65536, 688128, -2502656, -96256, 38598656, -187217920, 508021120, -845669120, 552981696, 1469334304, -5945275904, 11705275552, -14673798654, 9100068184, 8421580132, -34288012648, 56657584158, -60426283952, 36612252089, 9942017442, -60791892299, 93046207239, -92028642340, 59196883097, -10454018992, -33364599371