Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/quadratic_forms/quadratic_form__evaluate.pyx
4079 views
1
2
3
def QFEvaluateVector(Q, v):
4
"""
5
Evaluate this quadratic form Q on a vector or matrix of elements
6
coercible to the base ring of the quadratic form. If a vector
7
is given then the output will be the ring element Q(v), but if a
8
matrix is given then the output will be the quadratic form Q'
9
which in matrix notation is given by:
10
11
Q' = v^t * Q * v.
12
13
Note: This is a Python wrapper for the fast evaluation routine
14
QFEvaluateVector_cdef(). This routine is for internal use and is
15
called more conveniently as Q(M).
16
17
INPUT:
18
Q -- QuadraticForm over a base ring R
19
v -- a tuple or list (or column matrix) of Q.dim() elements of R
20
21
OUTPUT:
22
an element of R
23
24
EXAMPLES:
25
sage: from sage.quadratic_forms.quadratic_form__evaluate import QFEvaluateVector
26
sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
27
Quadratic form in 4 variables over Integer Ring with coefficients:
28
[ 0 1 2 3 ]
29
[ * 4 5 6 ]
30
[ * * 7 8 ]
31
[ * * * 9 ]
32
sage: QFEvaluateVector(Q, (1,0,0,0))
33
0
34
sage: QFEvaluateVector(Q, (1,0,1,0))
35
9
36
37
"""
38
return QFEvaluateVector_cdef(Q, v)
39
40
41
42
cdef QFEvaluateVector_cdef(Q, v):
43
"""
44
Routine to quickly evaluate a quadratic form Q on a vector v. See
45
the Python wrapper function QFEvaluate() above for details.
46
47
"""
48
## If we are passed a matrix A, return the quadratic form Q(A(x))
49
## (In matrix notation: A^t * Q * A)
50
n = Q.dim()
51
52
tmp_val = Q.base_ring()(0)
53
for i from 0 <= i < n:
54
for j from i <= j < n:
55
tmp_val += Q[i,j] * v[i] * v[j]
56
57
## Return the value (over R)
58
return Q.base_ring()._coerce_(tmp_val)
59
60
61
62
def QFEvaluateMatrix(Q, M, Q2):
63
"""
64
Evaluate this quadratic form Q on a matrix M of elements coercible
65
to the base ring of the quadratic form, which in matrix notation
66
is given by:
67
68
Q2 = M^t * Q * M.
69
70
Note: This is a Python wrapper for the fast evaluation routine
71
QFEvaluateMatrix_cdef(). This routine is for internal use and is
72
called more conveniently as Q(M). The inclusion of Q2 as an
73
argument is to avoid having to create a QuadraticForm here, which
74
for now creates circular imports.
75
76
INPUT:
77
Q -- QuadraticForm over a base ring R
78
M -- a Q.dim() x Q2.dim() matrix of elements of R
79
80
OUTPUT:
81
Q2 -- a QuadraticForm over R
82
83
EXAMPLES:
84
sage: from sage.quadratic_forms.quadratic_form__evaluate import QFEvaluateMatrix
85
sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
86
Quadratic form in 4 variables over Integer Ring with coefficients:
87
[ 0 1 2 3 ]
88
[ * 4 5 6 ]
89
[ * * 7 8 ]
90
[ * * * 9 ]
91
sage: Q2 = QuadraticForm(ZZ, 2)
92
sage: M = Matrix(ZZ, 4, 2, [1,0,0,0, 0,1,0,0]); M
93
[1 0]
94
[0 0]
95
[0 1]
96
[0 0]
97
sage: QFEvaluateMatrix(Q, M, Q2)
98
Quadratic form in 2 variables over Integer Ring with coefficients:
99
[ 0 2 ]
100
[ * 7 ]
101
102
"""
103
return QFEvaluateMatrix_cdef(Q, M, Q2)
104
105
106
107
cdef QFEvaluateMatrix_cdef(Q, M, Q2):
108
"""
109
Routine to quickly evaluate a quadratic form Q on a matrix M. See
110
the Python wrapper function QFEvaluateMatrix() above for details.
111
112
"""
113
## Create the new quadratic form
114
n = Q.dim()
115
m = Q2.dim()
116
117
## TO DO: Check the dimensions of M are compatible with those of Q and Q2
118
119
## Evaluate Q(M) into Q2
120
for k from 0 <= k < m:
121
for l from k <= l < m:
122
tmp_sum = Q2.base_ring()(0)
123
for i from 0 <= i < n:
124
for j from i <= j < n:
125
if (k == l):
126
tmp_sum += Q[i,j] * (M[i,k] * M[j,l])
127
else:
128
tmp_sum += Q[i,j] * (M[i,k] * M[j,l] + M[i,l] * M[j,k])
129
Q2[k,l] = tmp_sum
130
return Q2
131
132