Skip to content

Commit 87c8658

Browse files
committed
construct a QuadraticForm from a given multivariate polynomial
1 parent bb7ee85 commit 87c8658

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/sage/quadratic_forms/quadratic_form.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,46 @@ def polynomial(self,names='x'):
13001300
P = (V*M).dot_product(V)
13011301
return P
13021302

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)
13031343

13041344

13051345

0 commit comments

Comments
 (0)