@@ -495,6 +495,48 @@ def polynomial(self):
495
495
self ._poly = self (ZZ ['x, y' ].gens ())
496
496
return self ._poly
497
497
498
+ @staticmethod
499
+ def from_polynomial (poly ):
500
+ r"""
501
+ Construct a :class:`BinaryQF` from a bivariate polynomial
502
+ with integer coefficients. Converse of :meth:`polynomial`.
503
+
504
+ EXAMPLES::
505
+
506
+ sage: R.<u,v> = ZZ[]
507
+ sage: f = u^2 + 419*v^2
508
+ sage: Q = BinaryQF.from_polynomial(f); Q
509
+ x^2 + 419*y^2
510
+ sage: Q.polynomial()
511
+ x^2 + 419*y^2
512
+ sage: Q.polynomial()(R.gens()) == f
513
+ True
514
+
515
+ The method fails if the given polynomial is not a quadratic form::
516
+
517
+ sage: BinaryQF.from_polynomial(u^3 - 5*v)
518
+ Traceback (most recent call last):
519
+ ...
520
+ ValueError: polynomial has monomials of degree != 2
521
+
522
+ ...or if the coefficients aren't integers::
523
+
524
+ sage: BinaryQF.from_polynomial(u^2/7 + v^2)
525
+ Traceback (most recent call last):
526
+ ...
527
+ TypeError: no conversion of this rational to integer
528
+ """
529
+ R = poly .parent ()
530
+ from sage .rings .polynomial .multi_polynomial_ring_base import MPolynomialRing_base
531
+ if not isinstance (R , MPolynomialRing_base ) or R .ngens () != 2 :
532
+ raise TypeError (f'not a bivariate polynomial ring: { R } ' )
533
+ if not all (mon .degree () == 2 for mon in poly .monomials ()):
534
+ raise ValueError (f'polynomial has monomials of degree != 2' )
535
+ x ,y = R .gens ()
536
+ coeffs = (poly .monomial_coefficient (mon ) for mon in (x ** 2 , x * y , y ** 2 ))
537
+ a ,b ,c = map (ZZ , coeffs )
538
+ return BinaryQF (a , b , c )
539
+
498
540
@cached_method
499
541
def discriminant (self ):
500
542
"""
0 commit comments