30
30
from sage .arith .all import GCD , LCM
31
31
from sage .rings .all import Ideal , QQ
32
32
from sage .rings .ring import is_Ring , PrincipalIdealDomain
33
- from sage .structure .sage_object import SageObject
34
33
from sage .structure .element import is_Vector
35
34
from sage .rings .polynomial .polynomial_ring_constructor import PolynomialRing
35
+ from sage .rings .polynomial .polynomial_element import is_Polynomial
36
+ from sage .rings .polynomial .multi_polynomial_element import is_MPolynomial
36
37
from sage .modules .free_module_element import vector
37
38
from sage .quadratic_forms .genera .genus import genera
38
39
from sage .quadratic_forms .quadratic_form__evaluate import QFEvaluateVector , QFEvaluateMatrix
39
-
40
+ from sage .structure .sage_object import SageObject
41
+ from sage .combinat .integer_lists .invlex import IntegerListsLex
40
42
41
43
def QuadraticForm__constructor (R , n = None , entries = None ):
42
44
"""
@@ -206,6 +208,10 @@ class QuadraticForm(SageObject):
206
208
in `R` (given lexicographically, or equivalently, by rows of the
207
209
matrix)
208
210
211
+ #. ``QuadraticForm(p)``, where
212
+
213
+ - `p` -- a homogeneous polynomial of degree `2`
214
+
209
215
#. ``QuadraticForm(R, n)``, where
210
216
211
217
- `R` -- a ring
@@ -284,6 +290,16 @@ class QuadraticForm(SageObject):
284
290
[ 1 5 ]
285
291
[ * 4 ]
286
292
293
+ ::
294
+
295
+ sage: P.<x,y,z> = QQ[]
296
+ sage: p = x^2 + 2*x*y + x*z/2 + y^2 + y*z/3
297
+ sage: QuadraticForm(p)
298
+ Quadratic form in 3 variables over Rational Field with coefficients:
299
+ [ 1 2 1/2 ]
300
+ [ * 1 1/3 ]
301
+ [ * * 0 ]
302
+
287
303
::
288
304
289
305
sage: QuadraticForm(ZZ, m + m.transpose())
@@ -499,6 +515,25 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
499
515
sage: s.dim()
500
516
4
501
517
518
+ sage: P.<x,y,z> = QQ[]
519
+ sage: p = x^2 + y^2 + 2*x*z
520
+ sage: QuadraticForm(p)
521
+ Quadratic form in 3 variables over Rational Field with coefficients:
522
+ [ 1 0 2 ]
523
+ [ * 1 0 ]
524
+ [ * * 0 ]
525
+ sage: z = P.zero()
526
+ sage: QuadraticForm(z)
527
+ Quadratic form in 3 variables over Rational Field with coefficients:
528
+ [ 0 0 0 ]
529
+ [ * 0 0 ]
530
+ [ * * 0 ]
531
+ sage: q = x^2 + 3*y - z
532
+ sage: QuadraticForm(q)
533
+ Traceback (most recent call last):
534
+ ...
535
+ ValueError: polynomial is neither zero nor homogeneous of degree 2
536
+
502
537
TESTS::
503
538
504
539
sage: s == loads(dumps(s))
@@ -510,6 +545,10 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
510
545
511
546
sage: x = polygen(ZZ, 'x')
512
547
sage: QuadraticForm(x**2)
548
+ Quadratic form in 1 variables over Integer Ring with coefficients:
549
+ [ 1 ]
550
+
551
+ sage: QuadraticForm(1)
513
552
Traceback (most recent call last):
514
553
....
515
554
TypeError: wrong input for QuadraticForm
@@ -527,20 +566,39 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
527
566
M_ring = R
528
567
matrix_init_flag = True
529
568
530
- elif not is_Matrix (R ):
531
- # first argument, if not a ring, must be a matrix
532
- raise TypeError ('wrong input for QuadraticForm' )
533
- else :
534
- # Deal with: QuadraticForm(matrix)
569
+ elif is_Matrix (R ):
570
+ M = R
571
+
535
572
# Test if R is symmetric and has even diagonal
536
- if not self ._is_even_symmetric_matrix_ (R ):
573
+ if not self ._is_even_symmetric_matrix_ (M ):
537
574
raise TypeError ("the matrix is not a symmetric with even diagonal" )
538
575
539
- # Rename the matrix and ring
540
- M = R
541
- M_ring = R .base_ring ()
576
+ M_ring = M .base_ring ()
542
577
matrix_init_flag = True
543
578
579
+ elif is_Polynomial (R ) or is_MPolynomial (R ):
580
+ p = R
581
+
582
+ if not p .is_zero () and not (p .is_homogeneous () and p .degree () == 2 ):
583
+ raise ValueError ("polynomial is neither zero nor homogeneous of degree 2" )
584
+
585
+ P = p .parent ()
586
+ R , n = P .base_ring (), P .ngens ()
587
+
588
+ # Extract quadratic form coefficients
589
+ entries = []
590
+ if n == 0 :
591
+ exponents = []
592
+ elif n == 1 :
593
+ exponents = [2 ]
594
+ else :
595
+ exponents = IntegerListsLex (2 , length = n )
596
+ for alpha in exponents :
597
+ entries .append (p [alpha ])
598
+
599
+ else :
600
+ raise TypeError ('wrong input for QuadraticForm' )
601
+
544
602
# Perform the quadratic form initialization
545
603
if matrix_init_flag :
546
604
self .__n = ZZ (M .nrows ())
0 commit comments