File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -1300,6 +1300,46 @@ def polynomial(self,names='x'):
1300
1300
P = (V * M ).dot_product (V )
1301
1301
return P
1302
1302
1303
+ @staticmethod
1304
+ def from_polynomial (poly ):
1305
+ r"""
1306
+ Construct a :class:`QuadraticForm` from a multivariate
1307
+ polynomial. Converse of :meth:`polynomial`.
1308
+
1309
+ EXAMPLES::
1310
+
1311
+ sage: R.<x,y,z> = ZZ[]
1312
+ sage: f = 5*x^2 - x*z - 3*y*z - 2*y^2 + 9*z^2
1313
+ sage: Q = QuadraticForm.from_polynomial(f); Q
1314
+ Quadratic form in 3 variables over Integer Ring with coefficients:
1315
+ [ 5 0 -1 ]
1316
+ [ * -2 -3 ]
1317
+ [ * * 9 ]
1318
+ sage: Q.polynomial()
1319
+ 5*x0^2 - 2*x1^2 - x0*x2 - 3*x1*x2 + 9*x2^2
1320
+ sage: Q.polynomial()(R.gens()) == f
1321
+ True
1322
+
1323
+ The method fails if the given polynomial is not a quadratic form::
1324
+
1325
+ sage: QuadraticForm.from_polynomial(x^3 + x*z + 5*y^2)
1326
+ Traceback (most recent call last):
1327
+ ...
1328
+ ValueError: polynomial has monomials of degree != 2
1329
+ """
1330
+ R = poly .parent ()
1331
+ from sage .rings .polynomial .multi_polynomial_ring_base import MPolynomialRing_base
1332
+ if not isinstance (R , MPolynomialRing_base ):
1333
+ raise TypeError (f'not a multivariate polynomial ring: { R } ' )
1334
+ if not all (mon .degree () == 2 for mon in poly .monomials ()):
1335
+ raise ValueError (f'polynomial has monomials of degree != 2' )
1336
+ base = R .base_ring ()
1337
+ vs = R .gens ()
1338
+ coeffs = []
1339
+ for i ,v in enumerate (vs ):
1340
+ for w in vs [i :]:
1341
+ coeffs .append (poly .monomial_coefficient (v * w ))
1342
+ return QuadraticForm (base , len (vs ), coeffs )
1303
1343
1304
1344
1305
1345
You can’t perform that action at this time.
0 commit comments