]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/matrix_vector.py
2 There is an explicit isomorphism between all finite-dimensional vector
3 spaces. In particular, there is an isomorphism between the m-by-n
4 matrices and `$R^(m \times n)$`. Since most vector operations are not
5 available on Sage matrices, we have to go back and forth between these
6 two vector spaces often.
11 def isomorphism(matrix_space
):
13 Create isomorphism (i.e. the function) that converts elements
14 of a matrix space into those of the corresponding finite-dimensional
19 - matrix_space: A finite-dimensional ``MatrixSpace`` object.
23 - (phi, phi_inverse): If ``matrix_space`` has dimension m*n, then
24 ``phi`` will map m-by-n matrices to R^(m*n).
25 The inverse mapping ``phi_inverse`` will go
30 sage: from mjo.matrix_vector import isomorphism
34 sage: M = MatrixSpace(QQ,4,4)
35 sage: (p, p_inv) = isomorphism(M)
36 sage: m = M(xrange(16))
37 sage: p_inv(p(m)) == m
41 from sage
.matrix
.matrix_space
import is_MatrixSpace
42 if not is_MatrixSpace(matrix_space
):
43 raise TypeError('argument must be a matrix space')
45 base_ring
= matrix_space
.base_ring()
46 vector_space
= VectorSpace(base_ring
, matrix_space
.dimension())
49 return vector_space(m
.list())
52 return matrix_space(v
.list())
54 return (phi
, phi_inverse
)
58 def matrix_of_transformation(T
, V
):
60 Compute the matrix of a linear transformation ``T``, `$T : V
61 \rightarrow V$` with domain/range ``V``. This essentially uses the
62 Riesz representation theorem to represent the entries of the matrix
63 of ``T`` in terms of inner products.
67 - ``T`` -- The linear transformation whose matrix we should
68 compute. This should be a callable function that
69 takes as its single argument an element of ``V``.
71 - ``V`` -- The vector or matrix space on which ``T`` is defined.
75 If the dimension of ``V`` is `$n$`, we return an `$n \times n$`
76 matrix that represents ``T`` with respect to the standard basis of
81 sage: from mjo.matrix_vector import isomorphism, matrix_of_transformation
85 The matrix of a transformation on a simple vector space should be
88 sage: V = VectorSpace(QQ, 3)
92 sage: matrix_of_transformation(f, V)
97 A more complicated example confirms that we get a matrix consistent
98 with our ``matrix_to_vector`` function::
100 sage: M = MatrixSpace(QQ,3,3)
101 sage: Q = M([[0,1,0],[1,0,0],[0,0,1]])
103 ....: return Q*x*Q.inverse()
105 sage: F = matrix_of_transformation(f, M)
116 sage: phi, phi_inv = isomorphism(M)
117 sage: X = M([[1,2,3],[4,5,6],[7,8,9]])
119 (5, 4, 6, 2, 1, 3, 8, 7, 9)
121 (5, 4, 6, 2, 1, 3, 8, 7, 9)
122 sage: F*phi(X) == phi(f(X))
129 def inner_product(v
, w
):
130 # An inner product function that works for both matrices and
132 if callable(getattr(v
, 'inner_product', None)):
133 return v
.inner_product(w
)
134 elif callable(getattr(v
, 'matrix_space', None)):
135 # V must be a matrix space?
136 return (v
*w
.transpose()).trace()
138 raise ValueError('inner_product only works on vectors and matrices')
141 # A "call" that works for both matrices and functions.
142 if callable(getattr(L
, 'matrix_space', None)):
143 # L is a matrix, and we need to use "multiply" to call it.
146 # If L isn't a matrix, try this. It works for python
147 # functions at least.
153 entry
= inner_product(apply(T
,B
[i
]), B
[j
])
154 entries
.append(entry
)
156 # Construct the matrix space in which our return value will lie.
157 W
= MatrixSpace(V
.base_ring(), n
, n
)
159 # And make a matrix out of our list of entries.