Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/quadratic_forms/constructions.py
4045 views
1
"""
2
Some Extras
3
"""
4
##
5
## Some extra routines to make the QuadraticForm class more useful.
6
##
7
8
from sage.rings.all import ZZ
9
from sage.rings.polynomial.polynomial_element import is_Polynomial
10
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
11
from sage.quadratic_forms.quadratic_form import QuadraticForm
12
13
14
15
def BezoutianQuadraticForm(f, g):
16
r"""
17
Compute the Bezoutian of two polynomials defined over a common base ring. This is defined by
18
19
.. math::
20
21
{\rm Bez}(f, g) := \frac{f(x) g(y) - f(y) g(x)}{y - x}
22
23
and has size defined by the maximum of the degrees of `f` and `g`.
24
25
INPUT:
26
27
- `f`, `g` -- polynomials in `R[x]`, for some ring `R`
28
29
OUTPUT:
30
31
a quadratic form over `R`
32
33
EXAMPLES::
34
35
sage: R = PolynomialRing(ZZ, 'x')
36
sage: f = R([1,2,3])
37
sage: g = R([2,5])
38
sage: Q = BezoutianQuadraticForm(f, g) ; Q
39
Quadratic form in 2 variables over Integer Ring with coefficients:
40
[ 1 -12 ]
41
[ * -15 ]
42
43
AUTHORS:
44
45
- Fernando Rodriguez-Villegas, Jonathan Hanke -- added on 11/9/2008
46
47
"""
48
## Check that f and g are polynomials with a common base ring
49
if not is_Polynomial(f) or not is_Polynomial(g):
50
raise TypeError, "Oops! One of your inputs is not a polynomial. =("
51
if f.base_ring() != g.base_ring(): ## TO DO: Change this to allow coercion!
52
raise TypeError, "Oops! These polynomials are not defined over the same coefficient ring."
53
54
## Initialize the quadratic form
55
R = f.base_ring()
56
P = PolynomialRing(R, ['x','y'])
57
a, b = P.gens()
58
n = max(f.degree(), g.degree())
59
Q = QuadraticForm(R, n)
60
61
## Set the coefficients of Bezoutian
62
bez_poly = (f(a) * g(b) - f(b) * g(a)) // (b - a) ## Truncated (exact) division here
63
for i in range(n):
64
for j in range(i, n):
65
if i == j:
66
Q[i,j] = bez_poly.coefficient({a:i,b:j})
67
else:
68
Q[i,j] = bez_poly.coefficient({a:i,b:j}) * 2
69
70
return Q
71
72
73
def HyperbolicPlane_quadratic_form(R, r=1):
74
"""
75
Constructs the direct sum of `r` copies of the quadratic form `xy`
76
representing a hyperbolic plane defined over the base ring `R`.
77
78
INPUT:
79
80
- `R`: a ring
81
- `n` (integer, default 1) number of copies
82
83
EXAMPLES::
84
85
sage: HyperbolicPlane_quadratic_form(ZZ)
86
Quadratic form in 2 variables over Integer Ring with coefficients:
87
[ 0 1 ]
88
[ * 0 ]
89
90
"""
91
r = ZZ(r)
92
## Check that the multiplicity is a natural number
93
if r < 1:
94
raise TypeError, "The multiplicity r must be a natural number."
95
96
H = QuadraticForm(R, 2, [0, 1, 0])
97
return sum([H for i in range(r-1)], H)
98
99