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
/
invariant_theory.py
4685 lines (3746 loc) · 174 KB
/
invariant_theory.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
r"""
Classical Invariant Theory
This module lists classical invariants and covariants of homogeneous
polynomials (also called algebraic forms) under the action of the
special linear group. That is, we are dealing with polynomials of
degree `d` in `n` variables. The special linear group `SL(n,\CC)` acts
on the variables `(x_1,\dots, x_n)` linearly,
. MATH::
(x_1,\dots, x_n)^t \to A (x_1,\dots, x_n)^t
,\qquad
A \in SL(n,\CC)
The linear action on the variables transforms a polynomial `p`
generally into a different polynomial `gp`. We can think of it as an
action on the space of coefficients in `p`. An invariant is a
polynomial in the coefficients that is invariant under this action. A
covariant is a polynomial in the coefficients and the variables
`(x_1,\dots, x_n)` that is invariant under the combined action.
For example, the binary quadratic `p(x,y) = a x^2 + b x y + c y^2`
has as its invariant the discriminant `\mathop{disc}(p) = b^2 - 4 a
c`. This means that for any `SL(2,\CC)` coordinate change
.. MATH::
\begin{pmatrix} x' \\ y' \end{pmatrix}
=
\begin{pmatrix} \alpha & \beta \\ \gamma & \delta \end{pmatrix}
\begin{pmatrix} x \\ y \end{pmatrix}
\qquad
\alpha\delta-\beta\gamma=1
the discriminant is invariant, `\mathop{disc}\big(p(x',y')\big) =
\mathop{disc}\big(p(x,y)\big)`.
To use this module, you should use the factory object
:class:`invariant_theory <InvariantTheoryFactory>`. For example, take
the quartic::
sage: R.<x,y> = QQ[]
sage: q = x^4 + y^4
sage: quartic = invariant_theory.binary_quartic(q); quartic
Binary quartic with coefficients (1, 0, 0, 0, 1)
One invariant of a quartic is known as the Eisenstein
D-invariant. Since it is an invariant, it is a polynomial in the
coefficients (which are integers in this example)::
sage: quartic.EisensteinD()
1
One example of a covariant of a quartic is the so-called g-covariant
(actually, the Hessian). As with all covariants, it is a polynomial in
`x`, `y` and the coefficients::
sage: quartic.g_covariant()
-x^2*y^2
As usual, use tab completion and the online help to discover the
implemented invariants and covariants.
In general, the variables of the defining polynomial cannot be
guessed. For example, the zero polynomial can be thought of as a
homogeneous polynomial of any degree. Also, since we also want to
allow polynomial coefficients we cannot just take all variables of the
polynomial ring as the variables of the form. This is why you will
have to specify the variables explicitly if there is any potential
ambiguity. For example::
sage: invariant_theory.binary_quartic(R.zero(), [x,y])
Binary quartic with coefficients (0, 0, 0, 0, 0)
sage: invariant_theory.binary_quartic(x^4, [x,y])
Binary quartic with coefficients (0, 0, 0, 0, 1)
sage: R.<x,y,t> = QQ[]
sage: invariant_theory.binary_quartic(x^4 + y^4 + t*x^2*y^2, [x,y])
Binary quartic with coefficients (1, 0, t, 0, 1)
Finally, it is often convenient to use inhomogeneous polynomials where
it is understood that one wants to homogenize them. This is also
supported, just define the form with an inhomogeneous polynomial and
specify one less variable::
sage: R.<x,t> = QQ[]
sage: invariant_theory.binary_quartic(x^4 + 1 + t*x^2, [x])
Binary quartic with coefficients (1, 0, t, 0, 1)
REFERENCES:
- :wikipedia:`Glossary_of_invariant_theory`
AUTHORS:
- Volker Braun (2013-01-24): initial version
- Jesper Noordsij (2018-05-18): support for binary quintics added
"""
# ****************************************************************************
# Copyright (C) 2012 Volker Braun <vbraun.name@gmail.com>
#
# 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.
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.matrix.constructor import matrix
from sage.structure.sage_object import SageObject
from sage.structure.richcmp import richcmp_method, richcmp
from sage.misc.cachefunc import cached_method
import sage.rings.invariants.reconstruction as reconstruction
######################################################################
def _guess_variables(polynomial, *args):
"""
Return the polynomial variables.
INPUT:
- ``polynomial`` -- a polynomial, or a list/tuple of polynomials
in the same polynomial ring.
- ``*args`` -- the variables. If none are specified, all variables
in ``polynomial`` are returned. If a list or tuple is passed,
the content is returned. If multiple arguments are passed, they
are returned.
OUTPUT:
A tuple of variables in the parent ring of the polynomial(s).
EXAMPLES::
sage: from sage.rings.invariants.invariant_theory import _guess_variables
sage: R.<x,y> = QQ[]
sage: _guess_variables(x^2+y^2)
(x, y)
sage: _guess_variables([x^2, y^2])
(x, y)
sage: _guess_variables(x^2+y^2, x)
(x,)
sage: _guess_variables(x^2+y^2, x,y)
(x, y)
sage: _guess_variables(x^2+y^2, [x,y])
(x, y)
"""
if isinstance(polynomial, (list, tuple)):
R = polynomial[0].parent()
if not all(p.parent() is R for p in polynomial):
raise ValueError('all input polynomials must be in the same ring')
if not args or (len(args) == 1 and args[0] is None):
if isinstance(polynomial, (list, tuple)):
variables = tuple()
for p in polynomial:
for var in p.variables():
if var not in variables:
variables += (var,)
return variables
else:
return polynomial.variables()
elif len(args) == 1 and isinstance(args[0], (tuple, list)):
return tuple(args[0])
else:
return tuple(args)
def transvectant(f, g, h=1, scale='default'):
r"""
Return the h-th transvectant of f and g.
INPUT:
- ``f,g`` -- two homogeneous binary forms in the same polynomial ring.
- ``h`` -- the order of the transvectant. If it is not specified,
the first transvectant is returned.
- ``scale`` -- the scaling factor applied to the result. Possible values
are ``'default'`` and ``'none'``. The ``'default'`` scaling factor is
the one that appears in the output statement below, if the scaling
factor is ``'none'`` the quotient of factorials is left out.
OUTPUT:
The h-th transvectant of the listed forms `f` and `g`:
.. MATH::
(f,g)_h = \frac{(d_f-h)! \cdot (d_g-h)!}{d_f! \cdot d_g!}\left(
\left(\frac{\partial}{\partial x}\frac{\partial}{\partial z'}
- \frac{\partial}{\partial x'}\frac{\partial}{\partial z}
\right)^h \left(f(x,z) \cdot g(x',z')\right)
\right)_{(x',z')=(x,z)}
EXAMPLES::
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm, transvectant
sage: R.<x,y> = QQ[]
sage: f = AlgebraicForm(2, 5, x^5 + 5*x^4*y + 5*x*y^4 + y^5)
sage: transvectant(f, f, 4)
Binary quadratic given by 2*x^2 - 4*x*y + 2*y^2
sage: transvectant(f, f, 8)
Binary form of degree -6 given by 0
The default scaling will yield an error for fields of positive
characteristic below `d_f!` or `d_g!` as the denominator of the scaling
factor will not be invertible in that case. The scale argument ``'none'``
can be used to compute the transvectant in this case::
sage: R.<a0,a1,a2,a3,a4,a5,x0,x1> = GF(5)[]
sage: p = a0*x1^5 + a1*x1^4*x0 + a2*x1^3*x0^2 + a3*x1^2*x0^3 + a4*x1*x0^4 + a5*x0^5
sage: f = AlgebraicForm(2, 5, p, x0, x1)
sage: transvectant(f, f, 4)
Traceback (most recent call last):
...
ZeroDivisionError
sage: transvectant(f, f, 4, scale='none')
Binary quadratic given by -a3^2*x0^2 + a2*a4*x0^2 + a2*a3*x0*x1
- a1*a4*x0*x1 - a2^2*x1^2 + a1*a3*x1^2
The additional factors that appear when ``scale='none'`` is used can be
seen if we consider the same transvectant over the rationals and compare
it to the scaled version::
sage: R.<a0,a1,a2,a3,a4,a5,x0,x1> = QQ[]
sage: p = a0*x1^5 + a1*x1^4*x0 + a2*x1^3*x0^2 + a3*x1^2*x0^3 + a4*x1*x0^4 + a5*x0^5
sage: f = AlgebraicForm(2, 5, p, x0, x1)
sage: transvectant(f, f, 4)
Binary quadratic given by 3/50*a3^2*x0^2 - 4/25*a2*a4*x0^2
+ 2/5*a1*a5*x0^2 + 1/25*a2*a3*x0*x1 - 6/25*a1*a4*x0*x1 + 2*a0*a5*x0*x1
+ 3/50*a2^2*x1^2 - 4/25*a1*a3*x1^2 + 2/5*a0*a4*x1^2
sage: transvectant(f, f, 4, scale='none')
Binary quadratic given by 864*a3^2*x0^2 - 2304*a2*a4*x0^2
+ 5760*a1*a5*x0^2 + 576*a2*a3*x0*x1 - 3456*a1*a4*x0*x1
+ 28800*a0*a5*x0*x1 + 864*a2^2*x1^2 - 2304*a1*a3*x1^2 + 5760*a0*a4*x1^2
If the forms are given as inhomogeneous polynomials, the homogenisation
might fail if the polynomial ring has multiple variables. You can
circumvent this by making sure the base ring of the polynomial has only
one variable::
sage: R.<x,y> = QQ[]
sage: quintic = invariant_theory.binary_quintic(x^5+x^3+2*x^2+y^5, x)
sage: transvectant(quintic, quintic, 2)
Traceback (most recent call last):
...
ValueError: polynomial is not homogeneous
sage: R.<y> = QQ[]
sage: S.<x> = R[]
sage: quintic = invariant_theory.binary_quintic(x^5+x^3+2*x^2+y^5, x)
sage: transvectant(quintic, quintic, 2)
Binary sextic given by 1/5*x^6 + 6/5*x^5*h + (-3/25)*x^4*h^2
+ (2*y^5 - 8/25)*x^3*h^3 + (-12/25)*x^2*h^4 + 3/5*y^5*x*h^5
+ 2/5*y^5*h^6
"""
f = f.homogenized()
g = g.homogenized()
R = f._ring
if g._ring is not R:
raise ValueError('all input forms must be in the same polynomial ring')
x = f._variables[0]
y = f._variables[1]
degree = f._d + g._d - 2*h
if h > f._d or h > g._d:
tv = R(0)
else:
from sage.functions.other import binomial, factorial
if scale == 'default':
scalar = factorial(f._d-h) * factorial(g._d-h) \
* R(factorial(f._d)*factorial(g._d))**(-1)
elif scale == 'none':
scalar = 1
else:
raise ValueError('unknown scale type: %s' %scale)
def diff(j):
df = f.form().derivative(x,j).derivative(y,h-j)
dg = g.form().derivative(x,h-j).derivative(y,j)
return (-1)**j * binomial(h,j) * df * dg
tv = scalar * sum([diff(j) for j in range(h+1)])
if not tv.parent() is R:
S = tv.parent()
x = S(x)
y = S(y)
return AlgebraicForm(2, degree, tv, x, y)
######################################################################
@richcmp_method
class FormsBase(SageObject):
"""
The common base class of :class:`AlgebraicForm` and
:class:`SeveralAlgebraicForms`.
This is an abstract base class to provide common methods. It does
not make much sense to instantiate it.
TESTS::
sage: from sage.rings.invariants.invariant_theory import FormsBase
sage: FormsBase(None, None, None, None)
<sage.rings.invariants.invariant_theory.FormsBase object at ...>
"""
def __init__(self, n, homogeneous, ring, variables):
"""
The Python constructor.
TESTS::
sage: from sage.rings.invariants.invariant_theory import FormsBase
sage: FormsBase(None, None, None, None)
<sage.rings.invariants.invariant_theory.FormsBase object at ...>
"""
self._n = n
self._homogeneous = homogeneous
self._ring = ring
self._variables = variables
def _jacobian_determinant(self, *args):
"""
Return the Jacobian determinant.
INPUT:
- ``*args`` -- list of pairs of a polynomial and its
homogeneous degree. Must be a covariant, that is, polynomial
in the given :meth:`variables`
OUTPUT:
The Jacobian determinant with respect to the variables.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: from sage.rings.invariants.invariant_theory import FormsBase
sage: f = FormsBase(2, True, R, (x, y))
sage: f._jacobian_determinant((x^2+y^2, 2), (x*y, 2))
2*x^2 - 2*y^2
sage: f = FormsBase(2, False, R, (x, y))
sage: f._jacobian_determinant((x^2+1, 2), (x, 2))
2*x^2 - 2
sage: R.<x,y> = QQ[]
sage: cubic = invariant_theory.ternary_cubic(x^3+y^3+1)
sage: cubic.J_covariant()
x^6*y^3 - x^3*y^6 - x^6 + y^6 + x^3 - y^3
sage: 1 / 9 * cubic._jacobian_determinant(
....: [cubic.form(), 3], [cubic.Hessian(), 3], [cubic.Theta_covariant(), 6])
x^6*y^3 - x^3*y^6 - x^6 + y^6 + x^3 - y^3
"""
if self._homogeneous:
def diff(p, d):
return [p.derivative(x) for x in self._variables]
else:
def diff(p, d):
variables = self._variables[0:-1]
grad = [p.derivative(x) for x in variables]
dp_dz = d*p - sum(x*dp_dx for x, dp_dx in zip(variables, grad))
grad.append(dp_dz)
return grad
jac = [diff(p,d) for p,d in args]
return matrix(self._ring, jac).det()
def ring(self):
"""
Return the polynomial ring.
OUTPUT:
A polynomial ring. This is where the defining polynomial(s)
live. Note that the polynomials may be homogeneous or
inhomogeneous, depending on how the user constructed the
object.
EXAMPLES::
sage: R.<x,y,t> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+y^4+t*x^2*y^2, [x,y])
sage: quartic.ring()
Multivariate Polynomial Ring in x, y, t over Rational Field
sage: R.<x,y,t> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+1+t*x^2, [x])
sage: quartic.ring()
Multivariate Polynomial Ring in x, y, t over Rational Field
"""
return self._ring
def variables(self):
"""
Return the variables of the form.
OUTPUT:
A tuple of variables. If inhomogeneous notation is used for the
defining polynomial then the last entry will be ``None``.
EXAMPLES::
sage: R.<x,y,t> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+y^4+t*x^2*y^2, [x,y])
sage: quartic.variables()
(x, y)
sage: R.<x,y,t> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+1+t*x^2, [x])
sage: quartic.variables()
(x, None)
"""
return self._variables
def is_homogeneous(self):
"""
Return whether the forms were defined by homogeneous polynomials.
OUTPUT:
Boolean. Whether the user originally defined the form via
homogeneous variables.
EXAMPLES::
sage: R.<x,y,t> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+y^4+t*x^2*y^2, [x,y])
sage: quartic.is_homogeneous()
True
sage: quartic.form()
x^2*y^2*t + x^4 + y^4
sage: R.<x,y,t> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+1+t*x^2, [x])
sage: quartic.is_homogeneous()
False
sage: quartic.form()
x^4 + x^2*t + 1
"""
return self._homogeneous
######################################################################
class AlgebraicForm(FormsBase):
"""
The base class of algebraic forms (i.e. homogeneous polynomials).
You should only instantiate the derived classes of this base
class.
Derived classes must implement ``coeffs()`` and
``scaled_coeffs()``
INPUT:
- ``n`` -- The number of variables.
- ``d`` -- The degree of the polynomial.
- ``polynomial`` -- The polynomial.
- ``*args`` -- The variables, as a single list/tuple, multiple
arguments, or ``None`` to use all variables of the polynomial.
Derived classes must implement the same arguments for the
constructor.
EXAMPLES::
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm
sage: R.<x,y> = QQ[]
sage: p = x^2 + y^2
sage: AlgebraicForm(2, 2, p).variables()
(x, y)
sage: AlgebraicForm(2, 2, p, None).variables()
(x, y)
sage: AlgebraicForm(3, 2, p).variables()
(x, y, None)
sage: AlgebraicForm(3, 2, p, None).variables()
(x, y, None)
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm
sage: R.<x,y,s,t> = QQ[]
sage: p = s*x^2 + t*y^2
sage: AlgebraicForm(2, 2, p, [x,y]).variables()
(x, y)
sage: AlgebraicForm(2, 2, p, x,y).variables()
(x, y)
sage: AlgebraicForm(3, 2, p, [x,y,None]).variables()
(x, y, None)
sage: AlgebraicForm(3, 2, p, x,y,None).variables()
(x, y, None)
sage: AlgebraicForm(2, 1, p, [x,y]).variables()
Traceback (most recent call last):
...
ValueError: polynomial is of the wrong degree
sage: AlgebraicForm(2, 2, x^2+y, [x,y]).variables()
Traceback (most recent call last):
...
ValueError: polynomial is not homogeneous
"""
def __init__(self, n, d, polynomial, *args, **kwds):
"""
The Python constructor.
INPUT:
See the class documentation.
TESTS::
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm
sage: R.<x,y> = QQ[]
sage: form = AlgebraicForm(2, 2, x^2 + y^2)
"""
self._d = d
self._polynomial = polynomial
variables = _guess_variables(polynomial, *args)
if len(variables) == n:
pass
elif len(variables) == n-1:
variables = variables + (None,)
else:
raise ValueError('need '+str(n)+' or '+
str(n-1)+' variables, got '+str(variables))
ring = polynomial.parent()
homogeneous = variables[-1] is not None
super(AlgebraicForm, self).__init__(n, homogeneous, ring, variables)
self._check()
def _check(self):
"""
Check that the input is of the correct degree and number of
variables.
EXAMPLES::
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm
sage: R.<x,y,t> = QQ[]
sage: p = x^2 + y^2
sage: inv = AlgebraicForm(3, 2, p, [x,y,None])
sage: inv._check()
"""
degrees = set()
R = self._ring
if R.ngens() == 1:
degrees.update(self._polynomial.exponents())
else:
for e in self._polynomial.exponents():
deg = sum([ e[R.gens().index(x)]
for x in self._variables if x is not None ])
degrees.add(deg)
if self._homogeneous and len(degrees)>1:
raise ValueError('polynomial is not homogeneous')
if degrees == set() or \
(self._homogeneous and degrees == set([self._d])) or \
(not self._homogeneous and max(degrees) <= self._d):
return
else:
raise ValueError('polynomial is of the wrong degree')
def _check_covariant(self, method_name, g=None, invariant=False):
r"""
Test whether ``method_name`` actually returns a covariant.
INPUT:
- ``method_name`` -- string. The name of the method that
returns the invariant / covariant to test.
- ``g`` -- an `SL(n,\CC)` matrix or ``None`` (default). The
test will be to check that the covariant transforms
correctly under this special linear group element acting on
the homogeneous variables. If ``None``, a random matrix will
be picked.
- ``invariant`` -- boolean. Whether to additionaly test that
it is an invariant.
EXAMPLES::
sage: R.<a0, a1, a2, a3, a4, x0, x1> = QQ[]
sage: p = a0*x1^4 + a1*x1^3*x0 + a2*x1^2*x0^2 + a3*x1*x0^3 + a4*x0^4
sage: quartic = invariant_theory.binary_quartic(p, x0, x1)
sage: quartic._check_covariant('EisensteinE', invariant=True)
sage: quartic._check_covariant('h_covariant')
sage: quartic._check_covariant('h_covariant', invariant=True)
Traceback (most recent call last):
...
AssertionError: not invariant
"""
assert self._homogeneous
from sage.matrix.constructor import vector, random_matrix
if g is None:
F = self._ring.base_ring()
g = random_matrix(F, self._n, algorithm='unimodular')
v = vector(self.variables())
g_v = g * v
transform = dict( (v[i], g_v[i]) for i in range(self._n) )
# The covariant of the transformed polynomial
g_self = self.__class__(self._n, self._d, self.form().subs(transform), self.variables())
cov_g = getattr(g_self, method_name)()
# The transform of the covariant
g_cov = getattr(self, method_name)().subs(transform)
# they must be the same
assert (g_cov - cov_g).is_zero(), 'not covariant'
if invariant:
cov = getattr(self, method_name)()
assert (cov - cov_g).is_zero(), 'not invariant'
def __richcmp__(self, other, op):
"""
Compare ``self`` with ``other``.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+y^4)
sage: quartic == 'foo'
False
sage: quartic == quartic
True
"""
if type(self) != type(other):
return NotImplemented
return richcmp(self.coeffs(), other.coeffs(), op)
def _repr_(self):
"""
Return a string representation.
OUTPUT:
String.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+y^4)
sage: quartic._repr_()
'Binary quartic with coefficients (1, 0, 0, 0, 1)'
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm
sage: form = AlgebraicForm(2, 5, x^5 + y^5)
sage: form._repr_()
'Binary quintic given by x^5 + y^5'
"""
s = ''
ary = ['Unary', 'Binary', 'Ternary', 'Quaternary', 'Quinary',
'Senary', 'Septenary', 'Octonary', 'Nonary', 'Denary']
try:
s += ary[self._n-1]
except IndexError:
s += 'Algebraic'
ic = ['constant form', 'monic', 'quadratic', 'cubic', 'quartic', 'quintic',
'sextic', 'septimic', 'octavic', 'nonic', 'decimic',
'undecimic', 'duodecimic']
s += ' '
if self._d < 0:
s += 'form of degree {}'.format(self._d)
else:
try:
s += ic[self._d]
except IndexError:
s += 'form'
try:
s += ' with coefficients ' + str(self.coeffs())
except AttributeError:
s += ' given by ' + str(self.form())
return s
def form(self):
"""
Return the defining polynomial.
OUTPUT:
The polynomial used to define the algebraic form.
EXAMPLES::
sage: R.<x,y> = QQ[]
sage: quartic = invariant_theory.binary_quartic(x^4+y^4)
sage: quartic.form()
x^4 + y^4
sage: quartic.polynomial()
x^4 + y^4
"""
return self._polynomial
polynomial = form
def homogenized(self, var='h'):
"""
Return form as defined by a homogeneous polynomial.
INPUT:
- ``var`` -- either a variable name, variable index or a
variable (default: ``'h'``).
OUTPUT:
The same algebraic form, but defined by a homogeneous
polynomial.
EXAMPLES::
sage: T.<t> = QQ[]
sage: quadratic = invariant_theory.binary_quadratic(t^2 + 2*t + 3)
sage: quadratic
Binary quadratic with coefficients (1, 3, 2)
sage: quadratic.homogenized()
Binary quadratic with coefficients (1, 3, 2)
sage: quadratic == quadratic.homogenized()
True
sage: quadratic.form()
t^2 + 2*t + 3
sage: quadratic.homogenized().form()
t^2 + 2*t*h + 3*h^2
sage: R.<x,y,z> = QQ[]
sage: quadratic = invariant_theory.ternary_quadratic(x^2 + 1, [x,y])
sage: quadratic.homogenized().form()
x^2 + h^2
sage: R.<x> = QQ[]
sage: quintic = invariant_theory.binary_quintic(x^4 + 1, x)
sage: quintic.homogenized().form()
x^4*h + h^5
"""
if self._homogeneous:
return self
try:
polynomial = self._polynomial.homogenize(var)
R = polynomial.parent()
variables = [R(_) for _ in self._variables[0:-1]] + [R(var)]
except AttributeError:
from sage.rings.all import PolynomialRing
R = PolynomialRing(self._ring.base_ring(), [str(self._ring.gen(0)), str(var)])
polynomial = R(self._polynomial).homogenize(var)
variables = R.gens()
if polynomial.total_degree() < self._d:
k = self._d - polynomial.total_degree()
polynomial = polynomial * R(var)**k
return self.__class__(self._n, self._d, polynomial, variables)
def _extract_coefficients(self, monomials):
"""
Return the coefficients of ``monomials``.
INPUT:
- ``polynomial`` -- the input polynomial
- ``monomials`` -- a list of all the monomials in the polynomial
ring. If less monomials are passed, an exception is thrown.
OUTPUT:
A tuple containing the coefficients of the monomials in the given
polynomial.
EXAMPLES::
sage: from sage.rings.invariants.invariant_theory import AlgebraicForm
sage: R.<x,y,z,a30,a21,a12,a03,a20,a11,a02,a10,a01,a00> = QQ[]
sage: p = ( a30*x^3 + a21*x^2*y + a12*x*y^2 + a03*y^3 + a20*x^2*z +
....: a11*x*y*z + a02*y^2*z + a10*x*z^2 + a01*y*z^2 + a00*z^3 )
sage: base = AlgebraicForm(3, 3, p, [x,y,z])
sage: m = [x^3, y^3, z^3, x^2*y, x^2*z, x*y^2, y^2*z, x*z^2, y*z^2, x*y*z]
sage: base._extract_coefficients(m)
(a30, a03, a00, a21, a20, a12, a02, a10, a01, a11)
sage: base = AlgebraicForm(3, 3, p.subs(z=1), [x,y])
sage: m = [x^3, y^3, 1, x^2*y, x^2, x*y^2, y^2, x, y, x*y]
sage: base._extract_coefficients(m)
(a30, a03, a00, a21, a20, a12, a02, a10, a01, a11)
sage: T.<t> = QQ[]
sage: univariate = AlgebraicForm(2, 3, t^3+2*t^2+3*t+4)
sage: m = [t^3, 1, t, t^2]
sage: univariate._extract_coefficients(m)
(1, 4, 3, 2)
sage: univariate._extract_coefficients(m[1:])
Traceback (most recent call last):
...
ValueError: less monomials were passed than the form actually has
"""
R = self._ring
Rgens = R.gens()
BR = R.base_ring()
if self._homogeneous:
variables = self._variables
else:
variables = self._variables[:-1]
indices = [Rgens.index(x) for x in variables]
if len(indices) == len(Rgens):
coeff_ring = BR
else:
coeff_ring = R
coeffs = {}
if len(Rgens) == 1:
# Univariate polynomials
def mono_to_tuple(mono):
return (R(mono).exponents()[0],)
def coeff_tuple_iter():
for i, c in enumerate(self._polynomial):
yield (c, (i,))
else:
# Multivariate polynomials, mixing variables and coefficients !
def mono_to_tuple(mono):
# mono is any monomial in the ring R
# keep only the exponents of true variables
mono = R(mono).exponents()[0]
return tuple(mono[i] for i in indices)
def mono_to_tuple_and_coeff(mono):
# mono is any monomial in the ring R
# separate the exponents of true variables
# and one coefficient monomial
mono = mono.exponents()[0]
true_mono = tuple(mono[i] for i in indices)
coeff_mono = list(mono)
for i in indices:
coeff_mono[i] = 0
return true_mono, R.monomial(*coeff_mono)
def coeff_tuple_iter():
for c, m in self._polynomial:
mono, coeff = mono_to_tuple_and_coeff(m)
yield coeff_ring(c * coeff), mono
for c, i in coeff_tuple_iter():
coeffs[i] = c + coeffs.pop(i, coeff_ring.zero())
result = tuple(coeffs.pop(mono_to_tuple(m), coeff_ring.zero()) for m in monomials)
if coeffs:
raise ValueError('less monomials were passed than the form actually has')
return result
def coefficients(self):
"""
Alias for ``coeffs()``.
See the documentation for ``coeffs()`` for details.
EXAMPLES::
sage: R.<a,b,c,d,e,f,g, x,y,z> = QQ[]
sage: p = a*x^2 + b*y^2 + c*z^2 + d*x*y + e*x*z + f*y*z
sage: q = invariant_theory.quadratic_form(p, x,y,z)
sage: q.coefficients()
(a, b, c, d, e, f)
sage: q.coeffs()
(a, b, c, d, e, f)
"""
return self.coeffs()
def transformed(self, g):
r"""
Return the image under a linear transformation of the variables.
INPUT:
- ``g`` -- a `GL(n,\CC)` matrix or a dictionary with the
variables as keys. A matrix is used to define the linear
transformation of homogeneous variables, a dictionary acts
by substitution of the variables.
OUTPUT:
A new instance of a subclass of :class:`AlgebraicForm`
obtained by replacing the variables of the homogeneous
polynomial by their image under ``g``.
EXAMPLES::
sage: R.<x,y,z> = QQ[]
sage: cubic = invariant_theory.ternary_cubic(x^3 + 2*y^3 + 3*z^3 + 4*x*y*z)
sage: cubic.transformed({x:y, y:z, z:x}).form()
3*x^3 + y^3 + 4*x*y*z + 2*z^3
sage: cyc = matrix([[0,1,0],[0,0,1],[1,0,0]])
sage: cubic.transformed(cyc) == cubic.transformed({x:y, y:z, z:x})
True
sage: g = matrix(QQ, [[1, 0, 0], [-1, 1, -3], [-5, -5, 16]])
sage: cubic.transformed(g)
Ternary cubic with coefficients (-356, -373, 12234, -1119, 3578, -1151,
3582, -11766, -11466, 7360)
sage: cubic.transformed(g).transformed(g.inverse()) == cubic
True
"""
if isinstance(g, dict):
transform = g
else:
from sage.modules.all import vector
v = vector(self._ring, self._variables)
g_v = vector(self._ring, g*v)
transform = dict( (v[i], g_v[i]) for i in range(self._n) )
# The covariant of the transformed polynomial
return self.__class__(self._n, self._d,
self.form().subs(transform), self.variables())
######################################################################
class QuadraticForm(AlgebraicForm):
"""
Invariant theory of a multivariate quadratic form.
You should use the :class:`invariant_theory
<InvariantTheoryFactory>` factory object to construct instances
of this class. See :meth:`~InvariantTheoryFactory.quadratic_form`
for details.
TESTS::
sage: R.<a,b,c,d,e,f,g, x,y,z> = QQ[]
sage: p = a*x^2 + b*y^2 + c*z^2 + d*x*y + e*x*z + f*y*z
sage: invariant_theory.quadratic_form(p, x,y,z)
Ternary quadratic with coefficients (a, b, c, d, e, f)
sage: type(_)
<class 'sage.rings.invariants.invariant_theory.TernaryQuadratic'>
sage: R.<a,b,c,d,e,f,g, x,y,z> = QQ[]
sage: p = a*x^2 + b*y^2 + c*z^2 + d*x*y + e*x*z + f*y*z
sage: invariant_theory.quadratic_form(p, x,y,z)
Ternary quadratic with coefficients (a, b, c, d, e, f)
sage: type(_)
<class 'sage.rings.invariants.invariant_theory.TernaryQuadratic'>
Since we cannot always decide whether the form is homogeneous or
not based on the number of variables, you need to explicitly
specify it if you want the variables to be treated as
inhomogeneous::
sage: invariant_theory.inhomogeneous_quadratic_form(p.subs(z=1), x,y)
Ternary quadratic with coefficients (a, b, c, d, e, f)
"""
def __init__(self, n, d, polynomial, *args):
"""
The Python constructor.
TESTS::
sage: R.<x,y> = QQ[]
sage: from sage.rings.invariants.invariant_theory import QuadraticForm
sage: form = QuadraticForm(2, 2, x^2+2*y^2+3*x*y)
sage: form
Binary quadratic with coefficients (1, 2, 3)
sage: form._check_covariant('discriminant', invariant=True)
sage: QuadraticForm(3, 2, x^2+y^2)
Ternary quadratic with coefficients (1, 1, 0, 0, 0, 0)
"""
assert d == 2
super(QuadraticForm, self).__init__(n, 2, polynomial, *args)
@classmethod
def from_invariants(cls, discriminant, x, z, *args, **kwargs):
"""
Construct a binary quadratic from its discriminant.
This function constructs a binary quadratic whose discriminant equal
the one provided as argument up to scaling.
INPUT:
- ``discriminant`` -- Value of the discriminant used to reconstruct
the binary quadratic.
OUTPUT:
A QuadraticForm with 2 variables.