]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
Add the matrix_vector module with only the isomorphism() function for now.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 3 Nov 2014 03:00:27 +0000 (22:00 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 3 Nov 2014 03:00:27 +0000 (22:00 -0500)
mjo/matrix_vector.py [new file with mode: 0644]

diff --git a/mjo/matrix_vector.py b/mjo/matrix_vector.py
new file mode 100644 (file)
index 0000000..e1d1931
--- /dev/null
@@ -0,0 +1,50 @@
+"""
+There is an explicit isomorphism between all finite-dimensional vector
+spaces. In particular, there is an isomorphism between the m-by-n
+matrices and `$R^(m \times n)$`. Since most vector operations are not
+available on Sage matrices, we have to go back and forth between these
+two vector spaces often.
+"""
+
+from sage.all import *
+
+def isomorphism(matrix_space):
+    """
+    Create isomorphism (i.e. the function) that converts elements
+    of a matrix space into those of the corresponding finite-dimensional
+    vector space.
+
+    INPUT:
+
+    - matrix_space: A finite-dimensional ``MatrixSpace`` object.
+
+    OUTPUT:
+
+    - (phi, phi_inverse): If ``matrix_space`` has dimension m*n, then
+                          ``phi`` will map m-by-n matrices to R^(m*n).
+                          The inverse mapping ``phi_inverse`` will go
+                          the other way.
+
+    EXAMPLES:
+
+        sage: M = MatrixSpace(QQ,4,4)
+        sage: (p, p_inv) = isomorphism(M)
+        sage: m = M(range(0,16))
+        sage: p_inv(p(m)) == m
+        True
+
+    """
+    from sage.matrix.matrix_space import is_MatrixSpace
+    if not is_MatrixSpace(matrix_space):
+        raise TypeError('argument must be a matrix space')
+
+    base_ring = matrix_space.base_ring()
+    vector_space = VectorSpace(base_ring, matrix_space.dimension())
+
+    def phi(m):
+        return vector_space(m.list())
+
+    def phi_inverse(v):
+        return matrix_space(v.list())
+
+    return (phi, phi_inverse)