Path: blob/master/sage/quadratic_forms/constructions.py
4045 views
"""1Some Extras2"""3##4## Some extra routines to make the QuadraticForm class more useful.5##67from sage.rings.all import ZZ8from sage.rings.polynomial.polynomial_element import is_Polynomial9from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing10from sage.quadratic_forms.quadratic_form import QuadraticForm11121314def BezoutianQuadraticForm(f, g):15r"""16Compute the Bezoutian of two polynomials defined over a common base ring. This is defined by1718.. math::1920{\rm Bez}(f, g) := \frac{f(x) g(y) - f(y) g(x)}{y - x}2122and has size defined by the maximum of the degrees of `f` and `g`.2324INPUT:2526- `f`, `g` -- polynomials in `R[x]`, for some ring `R`2728OUTPUT:2930a quadratic form over `R`3132EXAMPLES::3334sage: R = PolynomialRing(ZZ, 'x')35sage: f = R([1,2,3])36sage: g = R([2,5])37sage: Q = BezoutianQuadraticForm(f, g) ; Q38Quadratic form in 2 variables over Integer Ring with coefficients:39[ 1 -12 ]40[ * -15 ]4142AUTHORS:4344- Fernando Rodriguez-Villegas, Jonathan Hanke -- added on 11/9/20084546"""47## Check that f and g are polynomials with a common base ring48if not is_Polynomial(f) or not is_Polynomial(g):49raise TypeError, "Oops! One of your inputs is not a polynomial. =("50if f.base_ring() != g.base_ring(): ## TO DO: Change this to allow coercion!51raise TypeError, "Oops! These polynomials are not defined over the same coefficient ring."5253## Initialize the quadratic form54R = f.base_ring()55P = PolynomialRing(R, ['x','y'])56a, b = P.gens()57n = max(f.degree(), g.degree())58Q = QuadraticForm(R, n)5960## Set the coefficients of Bezoutian61bez_poly = (f(a) * g(b) - f(b) * g(a)) // (b - a) ## Truncated (exact) division here62for i in range(n):63for j in range(i, n):64if i == j:65Q[i,j] = bez_poly.coefficient({a:i,b:j})66else:67Q[i,j] = bez_poly.coefficient({a:i,b:j}) * 26869return Q707172def HyperbolicPlane_quadratic_form(R, r=1):73"""74Constructs the direct sum of `r` copies of the quadratic form `xy`75representing a hyperbolic plane defined over the base ring `R`.7677INPUT:7879- `R`: a ring80- `n` (integer, default 1) number of copies8182EXAMPLES::8384sage: HyperbolicPlane_quadratic_form(ZZ)85Quadratic form in 2 variables over Integer Ring with coefficients:86[ 0 1 ]87[ * 0 ]8889"""90r = ZZ(r)91## Check that the multiplicity is a natural number92if r < 1:93raise TypeError, "The multiplicity r must be a natural number."9495H = QuadraticForm(R, 2, [0, 1, 0])96return sum([H for i in range(r-1)], H)979899