Path: blob/master/sage/quadratic_forms/quadratic_form__evaluate.pyx
4079 views
12def QFEvaluateVector(Q, v):3"""4Evaluate this quadratic form Q on a vector or matrix of elements5coercible to the base ring of the quadratic form. If a vector6is given then the output will be the ring element Q(v), but if a7matrix is given then the output will be the quadratic form Q'8which in matrix notation is given by:910Q' = v^t * Q * v.1112Note: This is a Python wrapper for the fast evaluation routine13QFEvaluateVector_cdef(). This routine is for internal use and is14called more conveniently as Q(M).1516INPUT:17Q -- QuadraticForm over a base ring R18v -- a tuple or list (or column matrix) of Q.dim() elements of R1920OUTPUT:21an element of R2223EXAMPLES:24sage: from sage.quadratic_forms.quadratic_form__evaluate import QFEvaluateVector25sage: Q = QuadraticForm(ZZ, 4, range(10)); Q26Quadratic form in 4 variables over Integer Ring with coefficients:27[ 0 1 2 3 ]28[ * 4 5 6 ]29[ * * 7 8 ]30[ * * * 9 ]31sage: QFEvaluateVector(Q, (1,0,0,0))32033sage: QFEvaluateVector(Q, (1,0,1,0))3493536"""37return QFEvaluateVector_cdef(Q, v)38394041cdef QFEvaluateVector_cdef(Q, v):42"""43Routine to quickly evaluate a quadratic form Q on a vector v. See44the Python wrapper function QFEvaluate() above for details.4546"""47## If we are passed a matrix A, return the quadratic form Q(A(x))48## (In matrix notation: A^t * Q * A)49n = Q.dim()5051tmp_val = Q.base_ring()(0)52for i from 0 <= i < n:53for j from i <= j < n:54tmp_val += Q[i,j] * v[i] * v[j]5556## Return the value (over R)57return Q.base_ring()._coerce_(tmp_val)58596061def QFEvaluateMatrix(Q, M, Q2):62"""63Evaluate this quadratic form Q on a matrix M of elements coercible64to the base ring of the quadratic form, which in matrix notation65is given by:6667Q2 = M^t * Q * M.6869Note: This is a Python wrapper for the fast evaluation routine70QFEvaluateMatrix_cdef(). This routine is for internal use and is71called more conveniently as Q(M). The inclusion of Q2 as an72argument is to avoid having to create a QuadraticForm here, which73for now creates circular imports.7475INPUT:76Q -- QuadraticForm over a base ring R77M -- a Q.dim() x Q2.dim() matrix of elements of R7879OUTPUT:80Q2 -- a QuadraticForm over R8182EXAMPLES:83sage: from sage.quadratic_forms.quadratic_form__evaluate import QFEvaluateMatrix84sage: Q = QuadraticForm(ZZ, 4, range(10)); Q85Quadratic form in 4 variables over Integer Ring with coefficients:86[ 0 1 2 3 ]87[ * 4 5 6 ]88[ * * 7 8 ]89[ * * * 9 ]90sage: Q2 = QuadraticForm(ZZ, 2)91sage: M = Matrix(ZZ, 4, 2, [1,0,0,0, 0,1,0,0]); M92[1 0]93[0 0]94[0 1]95[0 0]96sage: QFEvaluateMatrix(Q, M, Q2)97Quadratic form in 2 variables over Integer Ring with coefficients:98[ 0 2 ]99[ * 7 ]100101"""102return QFEvaluateMatrix_cdef(Q, M, Q2)103104105106cdef QFEvaluateMatrix_cdef(Q, M, Q2):107"""108Routine to quickly evaluate a quadratic form Q on a matrix M. See109the Python wrapper function QFEvaluateMatrix() above for details.110111"""112## Create the new quadratic form113n = Q.dim()114m = Q2.dim()115116## TO DO: Check the dimensions of M are compatible with those of Q and Q2117118## Evaluate Q(M) into Q2119for k from 0 <= k < m:120for l from k <= l < m:121tmp_sum = Q2.base_ring()(0)122for i from 0 <= i < n:123for j from i <= j < n:124if (k == l):125tmp_sum += Q[i,j] * (M[i,k] * M[j,l])126else:127tmp_sum += Q[i,j] * (M[i,k] * M[j,l] + M[i,l] * M[j,k])128Q2[k,l] = tmp_sum129return Q2130131132