]>
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: M = MatrixSpace(QQ,4,4)
31 sage: (p, p_inv) = isomorphism(M)
32 sage: m = M(range(0,16))
33 sage: p_inv(p(m)) == m
37 from sage
.matrix
.matrix_space
import is_MatrixSpace
38 if not is_MatrixSpace(matrix_space
):
39 raise TypeError('argument must be a matrix space')
41 base_ring
= matrix_space
.base_ring()
42 vector_space
= VectorSpace(base_ring
, matrix_space
.dimension())
45 return vector_space(m
.list())
48 return matrix_space(v
.list())
50 return (phi
, phi_inverse
)
54 def matrix_of_transformation(T
, V
):
56 Compute the matrix of a linear transformation ``T``, `$T : V
57 \rightarrow V$` with domain/range ``V``. This essentially uses the
58 Riesz representation theorem to represent the entries of the matrix
59 of ``T`` in terms of inner products.
63 - ``T`` -- The linear transformation whose matrix we should
64 compute. This should be a callable function that
65 takes as its single argument an element of ``V``.
67 - ``V`` -- The vector or matrix space on which ``T`` is defined.
71 If the dimension of ``V`` is `$n$`, we return an `$n \times n$`
72 matrix that represents ``T`` with respect to the standard basis of
77 The matrix of a transformation on a simple vector space should be
80 sage: V = VectorSpace(QQ, 3)
84 sage: matrix_of_transformation(f, V)
89 A more complicated example confirms that we get a matrix consistent
90 with our ``matrix_to_vector`` function::
92 sage: M = MatrixSpace(QQ,3,3)
93 sage: Q = M([[0,1,0],[1,0,0],[0,0,1]])
95 ....: return Q*x*Q.inverse()
97 sage: F = matrix_of_transformation(f, M)
108 sage: phi, phi_inv = isomorphism(M)
109 sage: X = M([[1,2,3],[4,5,6],[7,8,9]])
111 (5, 4, 6, 2, 1, 3, 8, 7, 9)
113 (5, 4, 6, 2, 1, 3, 8, 7, 9)
114 sage: F*phi(X) == phi(f(X))
121 def inner_product(v
, w
):
122 # An inner product function that works for both matrices and
124 if callable(getattr(v
, 'inner_product', None)):
125 return v
.inner_product(w
)
126 elif callable(getattr(v
, 'matrix_space', None)):
127 # V must be a matrix space?
128 return (v
*w
.transpose()).trace()
130 raise ValueError('inner_product only works on vectors and matrices')
133 # A "call" that works for both matrices and functions.
134 if callable(getattr(L
, 'matrix_space', None)):
135 # L is a matrix, and we need to use "multiply" to call it.
138 # If L isn't a matrix, try this. It works for python
139 # functions at least.
145 entry
= inner_product(apply(T
,B
[i
]), B
[j
])
146 entries
.append(entry
)
148 # Construct the matrix space in which our return value will lie.
149 W
= MatrixSpace(V
.base_ring(), n
, n
)
151 # And make a matrix out of our list of entries.