This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
constructor.py
1117 lines (901 loc) · 38.9 KB
/
constructor.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
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
"""
Elliptic curve constructor
AUTHORS:
- William Stein (2005): Initial version
- John Cremona (2008-01): EllipticCurve(j) fixed for all cases
"""
#*****************************************************************************
# Copyright (C) 2005 William Stein <wstein@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
import sage.rings.all as rings
from sage.rings.finite_rings.integer_mod_ring import is_IntegerModRing
from sage.rings.rational_field import is_RationalField
from sage.rings.polynomial.multi_polynomial_ring import is_MPolynomialRing
from sage.rings.finite_rings.constructor import is_FiniteField
from sage.rings.number_field.number_field import is_NumberField
from sage.rings.polynomial.multi_polynomial_element import is_MPolynomial
from sage.rings.ring import is_Ring
from sage.rings.ring_element import is_RingElement
from sage.categories.fields import Fields
_Fields = Fields()
from sage.structure.sequence import Sequence
from sage.structure.element import parent
from sage.symbolic.ring import SR
from sage.symbolic.expression import is_SymbolicEquation
def EllipticCurve(x=None, y=None, j=None, minimal_twist=True):
r"""
Construct an elliptic curve.
In Sage, an elliptic curve is always specified by its a-invariants
.. math::
y^2 + a_1 xy + a_3 y = x^3 + a_2 x^2 + a_4 x + a_6.
INPUT:
There are several ways to construct an elliptic curve:
- ``EllipticCurve([a1,a2,a3,a4,a6])``: Elliptic curve with given
a-invariants. The invariants are coerced into the parent of the
first element. If all are integers, they are coerced into the
rational numbers.
- ``EllipticCurve([a4,a6])``: Same as above, but `a_1=a_2=a_3=0`.
- ``EllipticCurve(label)``: Returns the elliptic curve over Q from
the Cremona database with the given label. The label is a
string, such as ``"11a"`` or ``"37b2"``. The letters in the
label *must* be lower case (Cremona's new labeling).
- ``EllipticCurve(R, [a1,a2,a3,a4,a6])``: Create the elliptic
curve over ``R`` with given a-invariants. Here ``R`` can be an
arbitrary ring. Note that addition need not be defined.
- ``EllipticCurve(j=j0)`` or ``EllipticCurve_from_j(j0)``: Return
an elliptic curve with j-invariant ``j0``.
- ``EllipticCurve(polynomial)``: Read off the a-invariants from
the polynomial coefficients, see
:func:`EllipticCurve_from_Weierstrass_polynomial`.
In each case above where the input is a list of length 2 or 5, one
can instead give a 2 or 5-tuple instead.
EXAMPLES:
We illustrate creating elliptic curves::
sage: EllipticCurve([0,0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
We create a curve from a Cremona label::
sage: EllipticCurve('37b2')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 1873*x - 31833 over Rational Field
sage: EllipticCurve('5077a')
Elliptic Curve defined by y^2 + y = x^3 - 7*x + 6 over Rational Field
sage: EllipticCurve('389a')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field
Old Cremona labels are allowed::
sage: EllipticCurve('2400FF')
Elliptic Curve defined by y^2 = x^3 + x^2 + 2*x + 8 over Rational Field
Unicode labels are allowed::
sage: EllipticCurve(u'389a')
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field
We create curves over a finite field as follows::
sage: EllipticCurve([GF(5)(0),0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 + 4*x over Finite Field of size 5
sage: EllipticCurve(GF(5), [0, 0,1,-1,0])
Elliptic Curve defined by y^2 + y = x^3 + 4*x over Finite Field of size 5
Elliptic curves over `\ZZ/N\ZZ` with `N` prime are of type
"elliptic curve over a finite field"::
sage: F = Zmod(101)
sage: EllipticCurve(F, [2, 3])
Elliptic Curve defined by y^2 = x^3 + 2*x + 3 over Ring of integers modulo 101
sage: E = EllipticCurve([F(2), F(3)])
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_finite_field.EllipticCurve_finite_field_with_category'>
sage: E.category()
Category of schemes over Ring of integers modulo 101
In contrast, elliptic curves over `\ZZ/N\ZZ` with `N` composite
are of type "generic elliptic curve"::
sage: F = Zmod(95)
sage: EllipticCurve(F, [2, 3])
Elliptic Curve defined by y^2 = x^3 + 2*x + 3 over Ring of integers modulo 95
sage: E = EllipticCurve([F(2), F(3)])
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_generic.EllipticCurve_generic_with_category'>
sage: E.category()
Category of schemes over Ring of integers modulo 95
The following is a curve over the complex numbers::
sage: E = EllipticCurve(CC, [0,0,1,-1,0])
sage: E
Elliptic Curve defined by y^2 + 1.00000000000000*y = x^3 + (-1.00000000000000)*x over Complex Field with 53 bits of precision
sage: E.j_invariant()
2988.97297297297
We can also create elliptic curves by giving the Weierstrass equation::
sage: x, y = var('x,y')
sage: EllipticCurve(y^2 + y == x^3 + x - 9)
Elliptic Curve defined by y^2 + y = x^3 + x - 9 over Rational Field
sage: R.<x,y> = GF(5)[]
sage: EllipticCurve(x^3 + x^2 + 2 - y^2 - y*x)
Elliptic Curve defined by y^2 + x*y = x^3 + x^2 + 2 over Finite Field of size 5
We can explicitly specify the `j`-invariant::
sage: E = EllipticCurve(j=1728); E; E.j_invariant(); E.label()
Elliptic Curve defined by y^2 = x^3 - x over Rational Field
1728
'32a2'
sage: E = EllipticCurve(j=GF(5)(2)); E; E.j_invariant()
Elliptic Curve defined by y^2 = x^3 + x + 1 over Finite Field of size 5
2
See :trac:`6657` ::
sage: EllipticCurve(GF(144169),j=1728)
Elliptic Curve defined by y^2 = x^3 + x over Finite Field of size 144169
By default, when a rational value of `j` is given, the constructed
curve is a minimal twist (minimal conductor for curves with that
`j`-invariant). This can be changed by setting the optional
parameter ``minimal_twist``, which is True by default, to False::
sage: EllipticCurve(j=100)
Elliptic Curve defined by y^2 = x^3 + x^2 + 3392*x + 307888 over Rational Field
sage: E =EllipticCurve(j=100); E
Elliptic Curve defined by y^2 = x^3 + x^2 + 3392*x + 307888 over Rational Field
sage: E.conductor()
33129800
sage: E.j_invariant()
100
sage: E =EllipticCurve(j=100, minimal_twist=False); E
Elliptic Curve defined by y^2 = x^3 + 488400*x - 530076800 over Rational Field
sage: E.conductor()
298168200
sage: E.j_invariant()
100
Without this option, constructing the curve could take a long time
since both `j` and `j-1728` have to be factored to compute the
minimal twist (see :trac:`13100`)::
sage: E = EllipticCurve_from_j(2^256+1,minimal_twist=False)
sage: E.j_invariant() == 2^256+1
True
TESTS::
sage: R = ZZ['u', 'v']
sage: EllipticCurve(R, [1,1])
Elliptic Curve defined by y^2 = x^3 + x + 1 over Multivariate Polynomial Ring in u, v
over Integer Ring
We create a curve and a point over QQbar (see #6879)::
sage: E = EllipticCurve(QQbar,[0,1])
sage: E(0)
(0 : 1 : 0)
sage: E.base_field()
Algebraic Field
sage: E = EllipticCurve(RR,[1,2]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 2.00000000000000 over Real Field with 53 bits of precision
Real Field with 53 bits of precision
sage: EllipticCurve(CC,[3,4]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 3.00000000000000*x + 4.00000000000000 over Complex Field with 53 bits of precision
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 2.00000000000000 over Real Field with 53 bits of precision
Real Field with 53 bits of precision
sage: E = EllipticCurve(QQbar,[5,6]); E; E.base_field()
Elliptic Curve defined by y^2 = x^3 + 5*x + 6 over Algebraic Field
Algebraic Field
See :trac:`6657` ::
sage: EllipticCurve(3,j=1728)
Traceback (most recent call last):
...
ValueError: First parameter (if present) must be a ring when j is specified
sage: EllipticCurve(GF(5),j=3/5)
Traceback (most recent call last):
...
ValueError: First parameter must be a ring containing 3/5
If the universe of the coefficients is a general field, the object
constructed has type EllipticCurve_field. Otherwise it is
EllipticCurve_generic. See :trac:`9816` ::
sage: E = EllipticCurve([QQbar(1),3]); E
Elliptic Curve defined by y^2 = x^3 + x + 3 over Algebraic Field
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field_with_category'>
sage: E = EllipticCurve([RR(1),3]); E
Elliptic Curve defined by y^2 = x^3 + 1.00000000000000*x + 3.00000000000000 over Real Field with 53 bits of precision
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field_with_category'>
sage: E = EllipticCurve([i,i]); E
Elliptic Curve defined by y^2 = x^3 + I*x + I over Symbolic Ring
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field_with_category'>
sage: E.category()
Category of schemes over Symbolic Ring
sage: SR in Fields()
True
sage: F = FractionField(PolynomialRing(QQ,'t'))
sage: t = F.gen()
sage: E = EllipticCurve([t,0]); E
Elliptic Curve defined by y^2 = x^3 + t*x over Fraction Field of Univariate Polynomial Ring in t over Rational Field
sage: type(E)
<class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field_with_category'>
sage: E.category()
Category of schemes over Fraction Field of Univariate Polynomial Ring in t over Rational Field
See :trac:`12517`::
sage: E = EllipticCurve([1..5])
sage: EllipticCurve(E.a_invariants())
Elliptic Curve defined by y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 5 over Rational Field
See :trac:`11773`::
sage: E = EllipticCurve()
Traceback (most recent call last):
...
TypeError: invalid input to EllipticCurve constructor
"""
import ell_generic, ell_field, ell_finite_field, ell_number_field, ell_rational_field, ell_padic_field # here to avoid circular includes
if j is not None:
if not x is None:
if is_Ring(x):
try:
j = x(j)
except (ZeroDivisionError, ValueError, TypeError):
raise ValueError, "First parameter must be a ring containing %s"%j
else:
raise ValueError, "First parameter (if present) must be a ring when j is specified"
return EllipticCurve_from_j(j, minimal_twist)
if x is None:
raise TypeError, "invalid input to EllipticCurve constructor"
if is_SymbolicEquation(x):
x = x.lhs() - x.rhs()
if parent(x) is SR:
x = x._polynomial_(rings.QQ['x', 'y'])
if is_MPolynomial(x):
if y is None:
return EllipticCurve_from_Weierstrass_polynomial(x)
else:
return EllipticCurve_from_cubic(x, y, morphism=False)
if is_Ring(x):
if is_RationalField(x):
return ell_rational_field.EllipticCurve_rational_field(x, y)
elif is_FiniteField(x) or (is_IntegerModRing(x) and x.characteristic().is_prime()):
return ell_finite_field.EllipticCurve_finite_field(x, y)
elif rings.is_pAdicField(x):
return ell_padic_field.EllipticCurve_padic_field(x, y)
elif is_NumberField(x):
return ell_number_field.EllipticCurve_number_field(x, y)
elif x in _Fields:
return ell_field.EllipticCurve_field(x, y)
return ell_generic.EllipticCurve_generic(x, y)
if isinstance(x, unicode):
x = str(x)
if isinstance(x, basestring):
return ell_rational_field.EllipticCurve_rational_field(x)
if is_RingElement(x) and y is None:
raise TypeError, "invalid input to EllipticCurve constructor"
if not isinstance(x, (list, tuple)):
raise TypeError, "invalid input to EllipticCurve constructor"
x = Sequence(x)
if not (len(x) in [2,5]):
raise ValueError, "sequence of coefficients must have length 2 or 5"
R = x.universe()
if isinstance(x[0], (rings.Rational, rings.Integer, int, long)):
return ell_rational_field.EllipticCurve_rational_field(x, y)
elif is_NumberField(R):
return ell_number_field.EllipticCurve_number_field(x, y)
elif rings.is_pAdicField(R):
return ell_padic_field.EllipticCurve_padic_field(x, y)
elif is_FiniteField(R) or (is_IntegerModRing(R) and R.characteristic().is_prime()):
return ell_finite_field.EllipticCurve_finite_field(x, y)
elif R in _Fields:
return ell_field.EllipticCurve_field(x, y)
return ell_generic.EllipticCurve_generic(x, y)
def EllipticCurve_from_Weierstrass_polynomial(f):
"""
Return the elliptic curve defined by a cubic in (long) Weierstrass
form.
INPUT:
- ``f`` -- a inhomogeneous cubic polynomial in long Weierstrass
form.
OUTPUT:
The elliptic curve defined by it.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: f = y^2 + 1*x*y + 3*y - (x^3 + 2*x^2 + 4*x + 6)
sage: EllipticCurve(f)
Elliptic Curve defined by y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 6 over Rational Field
sage: EllipticCurve(f).a_invariants()
(1, 2, 3, 4, 6)
The polynomial ring may have extra variables as long as they
do not occur in the polynomial itself::
sage: R.<x,y,z,w> = QQ[]
sage: EllipticCurve(-y^2 + x^3 + 1)
Elliptic Curve defined by y^2 = x^3 + 1 over Rational Field
sage: EllipticCurve(-x^2 + y^3 + 1)
Elliptic Curve defined by y^2 = x^3 + 1 over Rational Field
sage: EllipticCurve(-w^2 + z^3 + 1)
Elliptic Curve defined by y^2 = x^3 + 1 over Rational Field
TESTS::
sage: from sage.schemes.elliptic_curves.constructor import EllipticCurve_from_Weierstrass_polynomial
sage: EllipticCurve_from_Weierstrass_polynomial(-w^2 + z^3 + 1)
Elliptic Curve defined by y^2 = x^3 + 1 over Rational Field
"""
R = f.parent()
cubic_variables = [ x for x in R.gens() if f.degree(x) == 3 ]
quadratic_variables = [ y for y in R.gens() if f.degree(y) == 2 ]
try:
x = cubic_variables[0]
y = quadratic_variables[0]
except IndexError:
raise ValueError('polynomial is not in long Weierstrass form')
a1 = a2 = a3 = a4 = a6 = 0
x3 = y2 = None
for coeff, mon in f:
if mon == x**3:
x3 = coeff
elif mon == x**2:
a2 = coeff
elif mon == x:
a4 = coeff
elif mon == 1:
a6 = coeff
elif mon == y**2:
y2 = -coeff
elif mon == x*y:
a1 = -coeff
elif mon == y:
a3 = -coeff
else:
raise ValueError('polynomial is not in long Weierstrass form')
if x3 != y2:
raise ValueError('the coefficient of x^3 and -y^2 must be the same')
elif x3 != 1:
a1, a2, a3, a4, a6 = a1/x3, a2/x3, a3/x3, a4/x3, a6/x3
return EllipticCurve([a1, a2, a3, a4, a6])
def EllipticCurve_from_c4c6(c4, c6):
"""
Return an elliptic curve with given `c_4` and
`c_6` invariants.
EXAMPLES::
sage: E = EllipticCurve_from_c4c6(17, -2005)
sage: E
Elliptic Curve defined by y^2 = x^3 - 17/48*x + 2005/864 over Rational Field
sage: E.c_invariants()
(17, -2005)
"""
try:
K = c4.parent()
except AttributeError:
K = rings.RationalField()
if K not in _Fields:
K = K.fraction_field()
return EllipticCurve([-K(c4)/K(48), -K(c6)/K(864)])
def EllipticCurve_from_j(j, minimal_twist=True):
"""
Return an elliptic curve with given `j`-invariant.
INPUT:
- ``j`` -- an element of some field.
- ``minimal_twist`` (boolean, default True) -- If True and ``j`` is in `\QQ`, the curve returned is a
minimal twist, i.e. has minimal conductor. If `j` is not in `\QQ` this parameter is ignored.
OUTPUT:
An elliptic curve with `j`-invariant `j`.
EXAMPLES::
sage: E = EllipticCurve_from_j(0); E; E.j_invariant(); E.label()
Elliptic Curve defined by y^2 + y = x^3 over Rational Field
0
'27a3'
sage: E = EllipticCurve_from_j(1728); E; E.j_invariant(); E.label()
Elliptic Curve defined by y^2 = x^3 - x over Rational Field
1728
'32a2'
sage: E = EllipticCurve_from_j(1); E; E.j_invariant()
Elliptic Curve defined by y^2 + x*y = x^3 + 36*x + 3455 over Rational Field
1
The ``minimal_twist`` parameter (ignored except over `\QQ` and
True by default) controls whether or not a minimal twist is
computed::
sage: EllipticCurve_from_j(100)
Elliptic Curve defined by y^2 = x^3 + x^2 + 3392*x + 307888 over Rational Field
sage: _.conductor()
33129800
sage: EllipticCurve_from_j(100, minimal_twist=False)
Elliptic Curve defined by y^2 = x^3 + 488400*x - 530076800 over Rational Field
sage: _.conductor()
298168200
Since computing the minimal twist requires factoring both `j` and
`j-1728` the following example would take a long time without
setting ``minimal_twist`` to False::
sage: E = EllipticCurve_from_j(2^256+1,minimal_twist=False)
sage: E.j_invariant() == 2^256+1
True
"""
try:
K = j.parent()
except AttributeError:
K = rings.RationalField()
if K not in _Fields:
K = K.fraction_field()
char=K.characteristic()
if char==2:
if j == 0:
return EllipticCurve(K, [ 0, 0, 1, 0, 0 ])
else:
return EllipticCurve(K, [ 1, 0, 0, 0, 1/j ])
if char == 3:
if j==0:
return EllipticCurve(K, [ 0, 0, 0, 1, 0 ])
else:
return EllipticCurve(K, [ 0, j, 0, 0, -j**2 ])
if K is rings.RationalField():
# we construct the minimal twist, i.e. the curve with minimal
# conductor with this j_invariant:
if j == 0:
return EllipticCurve(K, [ 0, 0, 1, 0, 0 ]) # 27a3
if j == 1728:
return EllipticCurve(K, [ 0, 0, 0, -1, 0 ]) # 32a2
if not minimal_twist:
k=j-1728
return EllipticCurve(K, [0,0,0,-3*j*k, -2*j*k**2])
n = j.numerator()
m = n-1728*j.denominator()
a4 = -3*n*m
a6 = -2*n*m**2
# Now E=[0,0,0,a4,a6] has j-invariant j=n/d
from sage.sets.set import Set
for p in Set(n.prime_divisors()+m.prime_divisors()):
e = min(a4.valuation(p)//2,a6.valuation(p)//3)
if e>0:
p = p**e
a4 /= p**2
a6 /= p**3
# Now E=[0,0,0,a4,a6] is minimal at all p != 2,3
tw = [-1,2,-2,3,-3,6,-6]
E1 = EllipticCurve([0,0,0,a4,a6])
Elist = [E1] + [E1.quadratic_twist(t) for t in tw]
crv_cmp = lambda E,F: cmp(E.conductor(),F.conductor())
Elist.sort(cmp=crv_cmp)
return Elist[0]
# defaults for all other fields:
if j == 0:
return EllipticCurve(K, [ 0, 0, 0, 0, 1 ])
if j == 1728:
return EllipticCurve(K, [ 0, 0, 0, 1, 0 ])
k=j-1728
return EllipticCurve(K, [0,0,0,-3*j*k, -2*j*k**2])
def EllipticCurve_from_cubic(F, P, morphism=True):
r"""
Construct an elliptic curve from a ternary cubic with a rational point.
If you just want the Weierstrass form and are not interested in
the morphism then it is easier to use
:func:`~sage.schemes.elliptic_curves.jacobian.Jacobian`
instead. This will construct the same elliptic curve but you don't
have to supply the point ``P``.
INPUT:
- ``F`` -- a homogeneous cubic in three variables with rational
coefficients, as a polynomial ring element, defining a smooth
plane cubic curve.
- ``P`` -- a 3-tuple `(x,y,z)` defining a projective point on the
curve `F=0`. Need not be a flex, but see caveat on output.
- ``morphism`` -- boolean (default: ``True``). Whether to return
the morphism or just the elliptic curve.
OUTPUT:
An elliptic curve in long Weierstrass form isomorphic to the curve
`F=0`.
If ``morphism=True`` is passed, then a birational equivalence
between F and the Weierstrass curve is returned. If the point
happens to be a flex, then this is an isomorphism.
EXAMPLES:
First we find that the Fermat cubic is isomorphic to the curve
with Cremona label 27a1::
sage: R.<x,y,z> = QQ[]
sage: cubic = x^3+y^3+z^3
sage: P = [1,-1,0]
sage: E = EllipticCurve_from_cubic(cubic, P, morphism=False); E
Elliptic Curve defined by y^2 + 2*x*y + 1/3*y = x^3 - x^2 - 1/3*x - 1/27 over Rational Field
sage: E.cremona_label()
'27a1'
sage: EllipticCurve_from_cubic(cubic, [0,1,-1], morphism=False).cremona_label()
'27a1'
sage: EllipticCurve_from_cubic(cubic, [1,0,-1], morphism=False).cremona_label()
'27a1'
Next we find the minimal model and conductor of the Jacobian of the
Selmer curve::
sage: R.<a,b,c> = QQ[]
sage: cubic = a^3+b^3+60*c^3
sage: P = [1,-1,0]
sage: E = EllipticCurve_from_cubic(cubic, P, morphism=False); E
Elliptic Curve defined by y^2 + 2*x*y + 20*y = x^3 - x^2 - 20*x - 400/3 over Rational Field
sage: E.minimal_model()
Elliptic Curve defined by y^2 = x^3 - 24300 over Rational Field
sage: E.conductor()
24300
We can also get the birational equivalence to and from the
Weierstrass form. We start with an example where ``P`` is a flex
and the equivalence is an isomorphism::
sage: f = EllipticCurve_from_cubic(cubic, P, morphism=True)
sage: f
Scheme morphism:
From: Closed subscheme of Projective Space of dimension 2 over Rational Field defined by:
a^3 + b^3 + 60*c^3
To: Elliptic Curve defined by y^2 + 2*x*y + 20*y = x^3 - x^2 - 20*x - 400/3
over Rational Field
Defn: Defined on coordinates by sending (a : b : c) to
(-c : -b + c : 1/20*a + 1/20*b)
sage: finv = f.inverse(); finv
Scheme morphism:
From: Elliptic Curve defined by y^2 + 2*x*y + 20*y = x^3 - x^2 - 20*x - 400/3
over Rational Field
To: Closed subscheme of Projective Space of dimension 2 over Rational Field defined by:
a^3 + b^3 + 60*c^3
Defn: Defined on coordinates by sending (x : y : z) to
(x + y + 20*z : -x - y : -x)
We verify that `f` maps the chosen point `P=(1,-1,0)` on the cubic
to the origin of the elliptic curve::
sage: f([1,-1,0])
(0 : 1 : 0)
sage: finv([0,1,0])
(-1 : 1 : 0)
To verify the output, we plug in the polynomials to check that
this indeed transforms the cubic into Weierstrass form::
sage: cubic(finv.defining_polynomials()) * finv.post_rescaling()
-x^3 + x^2*z + 2*x*y*z + y^2*z + 20*x*z^2 + 20*y*z^2 + 400/3*z^3
sage: E.defining_polynomial()(f.defining_polynomials()) * f.post_rescaling()
a^3 + b^3 + 60*c^3
If the point is not a flex then the cubic can not be transformed
to a Weierstrass equation by a linear transformation. The general
birational transformation is quadratic::
sage: cubic = a^3+7*b^3+64*c^3
sage: P = [2,2,-1]
sage: f = EllipticCurve_from_cubic(cubic, P, morphism=True)
sage: E = f.codomain(); E
Elliptic Curve defined by y^2 - 722*x*y - 21870000*y = x^3
+ 23579*x^2 over Rational Field
sage: E.minimal_model()
Elliptic Curve defined by y^2 + y = x^3 - 331 over Rational Field
sage: f
Scheme morphism:
From: Closed subscheme of Projective Space of dimension 2 over Rational Field defined by:
a^3 + 7*b^3 + 64*c^3
To: Elliptic Curve defined by y^2 - 722*x*y - 21870000*y =
x^3 + 23579*x^2 over Rational Field
Defn: Defined on coordinates by sending (a : b : c) to
(-5/112896*a^2 - 17/40320*a*b - 1/1280*b^2 - 29/35280*a*c
- 13/5040*b*c - 4/2205*c^2 :
-4055/112896*a^2 - 4787/40320*a*b - 91/1280*b^2 - 7769/35280*a*c
- 1993/5040*b*c - 724/2205*c^2 :
1/4572288000*a^2 + 1/326592000*a*b + 1/93312000*b^2 + 1/142884000*a*c
+ 1/20412000*b*c + 1/17860500*c^2)
sage: finv = f.inverse(); finv
Scheme morphism:
From: Elliptic Curve defined by y^2 - 722*x*y - 21870000*y =
x^3 + 23579*x^2 over Rational Field
To: Closed subscheme of Projective Space of dimension 2 over Rational Field defined by:
a^3 + 7*b^3 + 64*c^3
Defn: Defined on coordinates by sending (x : y : z) to
(2*x^2 + 227700*x*z - 900*y*z :
2*x^2 - 32940*x*z + 540*y*z :
-x^2 - 56520*x*z - 180*y*z)
sage: cubic(finv.defining_polynomials()) * finv.post_rescaling()
-x^3 - 23579*x^2*z - 722*x*y*z + y^2*z - 21870000*y*z^2
sage: E.defining_polynomial()(f.defining_polynomials()) * f.post_rescaling()
a^3 + 7*b^3 + 64*c^3
TESTS::
sage: R.<x,y,z> = QQ[]
sage: cubic = x^2*y + 4*x*y^2 + x^2*z + 8*x*y*z + 4*y^2*z + 9*x*z^2 + 9*y*z^2
sage: EllipticCurve_from_cubic(cubic, [1,-1,1], morphism=False)
Elliptic Curve defined by y^2 - 882*x*y - 2560000*y = x^3 - 127281*x^2 over Rational Field
"""
import sage.matrix.all as matrix
# check the input
R = F.parent()
if not is_MPolynomialRing(R):
raise TypeError('equation must be a polynomial')
if R.ngens() != 3:
raise TypeError('equation must be a polynomial in three variables')
if not F.is_homogeneous():
raise TypeError('equation must be a homogeneous polynomial')
K = F.parent().base_ring()
try:
P = [K(c) for c in P]
except TypeError:
raise TypeError('cannot convert %s into %s'%(P,K))
if F(P) != 0:
raise ValueError('%s is not a point on %s'%(P,F))
if len(P) != 3:
raise TypeError('%s is not a projective point'%P)
x, y, z = R.gens()
# First case: if P = P2 then P is a flex
P2 = chord_and_tangent(F, P)
if are_projectively_equivalent(P, P2, base_ring=K):
# find the tangent to F in P
dx = K(F.derivative(x)(P))
dy = K(F.derivative(y)(P))
dz = K(F.derivative(z)(P))
# find a second point Q on the tangent line but not on the cubic
for tangent in [[dy, -dx, K.zero()], [dz, K.zero(), -dx], [K.zero(), -dz, dx]]:
tangent = projective_point(tangent)
Q = [tangent[0]+P[0], tangent[1]+P[1], tangent[2]+P[2]]
F_Q = F(Q)
if F_Q != 0: # At most one further point may accidentally be on the cubic
break
assert F_Q != 0
# pick linearly independent third point
for third_point in [(1,0,0), (0,1,0), (0,0,1)]:
M = matrix.matrix(K, [Q, P, third_point]).transpose()
if M.is_invertible():
break
F2 = R(M.act_on_polynomial(F))
# scale and dehomogenise
a = K(F2.coefficient(x**3))
F3 = F2/a
b = K(F3.coefficient(y*y*z))
S = rings.PolynomialRing(K, 'x,y,z')
# elliptic curve coordinates
X, Y, Z = S.gen(0), S.gen(1), S(-1/b)*S.gen(2)
F4 = F3(X, Y, Z)
E = EllipticCurve(F4.subs(z=1))
if not morphism:
return E
inv_defining_poly = [ M[i,0]*X + M[i,1]*Y + M[i,2]*Z for i in range(3) ]
inv_post = -1/a
M = M.inverse()
trans_x, trans_y, trans_z = [ M[i,0]*x + M[i,1]*y + M[i,2]*z for i in range(3) ]
fwd_defining_poly = [trans_x, trans_y, -b*trans_z]
fwd_post = -a
# Second case: P is not a flex, then P, P2, P3 are different
else:
P3 = chord_and_tangent(F, P2)
# send P, P2, P3 to (1:0:0), (0:1:0), (0:0:1) respectively
M = matrix.matrix(K, [P, P2, P3]).transpose()
F2 = M.act_on_polynomial(F)
# substitute x = U^2, y = V*W, z = U*W, and rename (x,y,z)=(U,V,W)
F3 = F2.substitute({x:x**2, y:y*z, z:x*z}) // (x**2*z)
# scale and dehomogenise
a = K(F3.coefficient(x**3))
F4 = F3/a
b = K(F4.coefficient(y*y*z))
# change to a polynomial in only two variables
S = rings.PolynomialRing(K, 'x,y,z')
# elliptic curve coordinates
X, Y, Z = S.gen(0), S.gen(1), S(-1/b)*S.gen(2)
F5 = F4(X, Y, Z)
E = EllipticCurve(F5.subs(z=1))
if not morphism:
return E
inv_defining_poly = [ M[i,0]*X*X + M[i,1]*Y*Z + M[i,2]*X*Z for i in range(3) ]
inv_post = -1/a/(X**2)/Z
M = M.inverse()
trans_x, trans_y, trans_z = [
(M[i,0]*x + M[i,1]*y + M[i,2]*z) for i in range(3) ]
fwd_defining_poly = [ trans_x*trans_z, trans_x*trans_y, -b*trans_z*trans_z ]
fwd_post = -a/(trans_x*trans_z*trans_z)
# Construct the morphism
from sage.schemes.projective.projective_space import ProjectiveSpace
P2 = ProjectiveSpace(2, K, names=map(str, R.gens()))
cubic = P2.subscheme(F)
from sage.schemes.elliptic_curves.weierstrass_transform import \
WeierstrassTransformationWithInverse
return WeierstrassTransformationWithInverse(
cubic, E, fwd_defining_poly, fwd_post, inv_defining_poly, inv_post)
def chord_and_tangent(F, P):
"""
Use the chord and tangent method to get another point on a cubic.
INPUT:
- ``F`` -- a homogeneous cubic in three variables with rational
coefficients, as a polynomial ring element, defining a smooth
plane cubic curve.
- ``P`` -- a 3-tuple `(x,y,z)` defining a projective point on the
curve `F=0`.
OUTPUT:
Another point satisfying the equation ``F``.
EXAMPLES::
sage: R.<x,y,z> = QQ[]
sage: from sage.schemes.elliptic_curves.constructor import chord_and_tangent
sage: F = x^3+y^3+60*z^3
sage: chord_and_tangent(F, [1,-1,0])
[1, -1, 0]
sage: F = x^3+7*y^3+64*z^3
sage: p0 = [2,2,-1]
sage: p1 = chord_and_tangent(F, p0); p1
[-5, 3, -1]
sage: p2 = chord_and_tangent(F, p1); p2
[1265, -183, -314]
TESTS::
sage: F(p2)
0
sage: map(type, p2)
[<type 'sage.rings.rational.Rational'>,
<type 'sage.rings.rational.Rational'>,
<type 'sage.rings.rational.Rational'>]
"""
# check the input
R = F.parent()
if not is_MPolynomialRing(R):
raise TypeError('equation must be a polynomial')
if R.ngens() != 3:
raise TypeError('%s is not a polynomial in three variables'%F)
if not F.is_homogeneous():
raise TypeError('%s is not a homogeneous polynomial'%F)
x, y, z = R.gens()
if len(P) != 3:
raise TypeError('%s is not a projective point'%P)
K = R.base_ring()
try:
P = [K(c) for c in P]
except TypeError:
raise TypeError('cannot coerce %s into %s'%(P,K))
if F(P) != 0:
raise ValueError('%s is not a point on %s'%(P,F))
# find the tangent to F in P
dx = K(F.derivative(x)(P))
dy = K(F.derivative(y)(P))
dz = K(F.derivative(z)(P))
# if dF/dy(P) = 0, change variables so that dF/dy != 0
if dy == 0:
if dx != 0:
g = F.substitute({x:y, y:x})
Q = [P[1], P[0], P[2]]
R = chord_and_tangent(g, Q)
return [R[1], R[0], R[2]]
elif dz != 0:
g = F.substitute({y:z, z:y})
Q = [P[0], P[2], P[1]]
R = chord_and_tangent(g, Q)
return [R[0], R[2], R[1]]
else:
raise ValueError('%s is singular at %s'%(F, P))
# t will be our choice of parmeter of the tangent plane
# dx*(x-P[0]) + dy*(y-P[1]) + dz*(z-P[2])
# through the point P
t = rings.PolynomialRing(K, 't').gen(0)
Ft = F(dy*t+P[0], -dx*t+P[1], P[2])
if Ft == 0: # (dy, -dx, 0) is projectively equivalent to P
# then (0, -dz, dy) is not projectively equivalent to P
g = F.substitute({x:z, z:x})
Q = [P[2], P[1], P[0]]
R = chord_and_tangent(g, Q)
return [R[2], R[1], R[0]]
# Ft has a double zero at t=0 by construction, which we now remove
Ft = Ft // t**2
# first case: the third point is at t=infinity
if Ft.is_constant():
return projective_point([dy, -dx, 0])
# second case: the third point is at finite t
else:
assert Ft.degree() == 1
t0 = Ft.roots()[0][0]
return projective_point([dy*t0+P[0], -dx*t0+P[1], P[2]])
def projective_point(p):
"""
Return equivalent point with denominators removed
INPUT:
- ``P``, ``Q`` -- list/tuple of projective coordinates.
OUTPUT:
List of projective coordinates.
EXAMPLES::
sage: from sage.schemes.elliptic_curves.constructor import projective_point
sage: projective_point([4/5, 6/5, 8/5])
[2, 3, 4]
sage: F = GF(11)
sage: projective_point([F(4), F(8), F(2)])
[4, 8, 2]
"""
from sage.rings.integer import GCD_list, LCM_list
try:
p_gcd = GCD_list([x.numerator() for x in p])
p_lcm = LCM_list([x.denominator() for x in p])
except AttributeError:
return p
scale = p_lcm / p_gcd
return [scale * x for x in p]
def are_projectively_equivalent(P, Q, base_ring):
"""
Test whether ``P`` and ``Q`` are projectively equivalent.
INPUT:
- ``P``, ``Q`` -- list/tuple of projective coordinates.
- ``base_ring`` -- the base ring.
OUTPUT:
Boolean.
EXAMPLES::
sage: from sage.schemes.elliptic_curves.constructor import are_projectively_equivalent
sage: are_projectively_equivalent([0,1,2,3], [0,1,2,2], base_ring=QQ)
False
sage: are_projectively_equivalent([0,1,2,3], [0,2,4,6], base_ring=QQ)
True
"""
from sage.matrix.constructor import matrix
return matrix(base_ring, [P, Q]).rank() < 2
def EllipticCurve_from_plane_curve(C, P):
"""
Deprecated way to construct an elliptic curve.
Use :meth:`~sage.schemes.elliptic_curves.jacobian.Jacobian` instead.
EXAMPLES::
sage: R.<x,y,z> = QQ[]
sage: C = Curve(x^3+y^3+z^3)