ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 16 May 2013 07:31:03 +0200How to get a vector space generated by a list of matrices?https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/Sage is very nice.
I want to get a matrix space.
For example,
- F=GF(p)
- V= " vector space over F generated by [M1, M2, ... ,Mn] ,which Mi are n by n matrices with its coefficients in F "
V consist of matrices.
In general, how can I get such V?
Many thanks~Wed, 15 May 2013 04:22:33 +0200https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/Answer by tmonteil for <p>Sage is very nice.</p>
<p>I want to get a matrix space.
For example, </p>
<ul>
<li>F=GF(p)</li>
<li>V= " vector space over F generated by [M1, M2, ... ,Mn] ,which Mi are n by n matrices with its coefficients in F " </li>
</ul>
<p>V consist of matrices.
In general, how can I get such V?</p>
<p>Many thanks~</p>
https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?answer=14929#post-id-14929@Semin : Actually, you can use `gap` if you want, since it is included in Sage:
sage: p = 5
sage: F = GF(p)
sage: MM = MatrixSpace(F,2,3) ; MM
Full MatrixSpace of 2 by 3 dense matrices over Finite Field of size 5
sage: M1 = MM.random_element() ; M1
[3 2 1]
[3 1 2]
sage: M2 = MM.random_element() ; M2
[2 0 4]
[0 3 4]
sage: sp = gap.VectorSpace(F,[M1,M2]) ; sp
VectorSpace( GF(5), [ [ [ Z(5)^3, Z(5), Z(5)^0 ], [ Z(5)^3, Z(5)^0, Z(5) ] ],
[ [ Z(5), 0*Z(5), Z(5)^2 ], [ 0*Z(5), Z(5)^3, Z(5)^2 ] ] ] )
sage: M1 + M2 in sp
True
sage: A = sp.Random() ; A
[ [ Z(5)^3, 0*Z(5), Z(5)^0 ], [ 0*Z(5), Z(5), Z(5)^0 ] ]
But then, how to get the matrix `A` back as a Sage object ?
Wed, 15 May 2013 12:07:27 +0200https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?answer=14929#post-id-14929Comment by vdelecroix for <p><a href="/users/1682/semin/">@Semin</a> : Actually, you can use <code>gap</code> if you want, since it is included in Sage:</p>
<pre><code>sage: p = 5
sage: F = GF(p)
sage: MM = MatrixSpace(F,2,3) ; MM
Full MatrixSpace of 2 by 3 dense matrices over Finite Field of size 5
sage: M1 = MM.random_element() ; M1
[3 2 1]
[3 1 2]
sage: M2 = MM.random_element() ; M2
[2 0 4]
[0 3 4]
sage: sp = gap.VectorSpace(F,[M1,M2]) ; sp
VectorSpace( GF(5), [ [ [ Z(5)^3, Z(5), Z(5)^0 ], [ Z(5)^3, Z(5)^0, Z(5) ] ],
[ [ Z(5), 0*Z(5), Z(5)^2 ], [ 0*Z(5), Z(5)^3, Z(5)^2 ] ] ] )
sage: M1 + M2 in sp
True
sage: A = sp.Random() ; A
[ [ Z(5)^3, 0*Z(5), Z(5)^0 ], [ 0*Z(5), Z(5), Z(5)^0 ] ]
</code></pre>
<p>But then, how to get the matrix <code>A</code> back as a Sage object ?</p>
https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?comment=17687#post-id-17687The following works for me to get back a Sage matrix: "MM(list(A))"Wed, 15 May 2013 13:49:02 +0200https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?comment=17687#post-id-17687Answer by tmonteil for <p>Sage is very nice.</p>
<p>I want to get a matrix space.
For example, </p>
<ul>
<li>F=GF(p)</li>
<li>V= " vector space over F generated by [M1, M2, ... ,Mn] ,which Mi are n by n matrices with its coefficients in F " </li>
</ul>
<p>V consist of matrices.
In general, how can I get such V?</p>
<p>Many thanks~</p>
https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?answer=14925#post-id-14925Unfortunately, `VectorSpace` seems only to consider row vectors, not matrices, as elements. So i do not know whether it is possible out of the box. A possible workaround is to "unfold" your matrices to vectors, do your computations in the span, and then refold result vector to get a matrix back. Folding and unfolding being isomorphisms of vector spaces, you should be safe with your computations.
Here is an example:
sage: p = 5
sage: F = GF(p)
sage: MM = MatrixSpace(F,2,3) ; MM
Full MatrixSpace of 2 by 3 dense matrices over Finite Field of size 5
sage: M1 = MM.random_element() ; M1
[2 0 0]
[1 4 3]
sage: M2 = MM.random_element() ; M2
[2 3 4]
[4 0 3]
sage: V = VectorSpace(F, MM.dimension()) ; V
Vector space of dimension 6 over Finite Field of size 5
sage: def unfold(M):
....: return V(M.list())
sage: def refold(UM):
....: return MM(UM.list())
sage: UM1 = unfold(M1)
sage: UM2 = unfold(M2)
sage: refold(UM1) == M1
True
sage: sp = V.span([UM1, UM2]) ; sp
Vector space of degree 6 and dimension 2 over Finite Field of size 5
Basis matrix:
[1 0 0 3 2 4]
[0 1 3 1 2 0]
sage: UM1 + UM2 in sp
True
sage: UM3 = sp.random_element()
sage: M3 = refold(UM3) ; M3
[2 4 2]
[0 2 3]
sage: M3 in MM
True
Wed, 15 May 2013 06:18:52 +0200https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?answer=14925#post-id-14925Comment by Jason Grout for <p>Unfortunately, <code>VectorSpace</code> seems only to consider row vectors, not matrices, as elements. So i do not know whether it is possible out of the box. A possible workaround is to "unfold" your matrices to vectors, do your computations in the span, and then refold result vector to get a matrix back. Folding and unfolding being isomorphisms of vector spaces, you should be safe with your computations.</p>
<p>Here is an example:</p>
<pre><code>sage: p = 5
sage: F = GF(p)
sage: MM = MatrixSpace(F,2,3) ; MM
Full MatrixSpace of 2 by 3 dense matrices over Finite Field of size 5
sage: M1 = MM.random_element() ; M1
[2 0 0]
[1 4 3]
sage: M2 = MM.random_element() ; M2
[2 3 4]
[4 0 3]
sage: V = VectorSpace(F, MM.dimension()) ; V
Vector space of dimension 6 over Finite Field of size 5
sage: def unfold(M):
....: return V(M.list())
sage: def refold(UM):
....: return MM(UM.list())
sage: UM1 = unfold(M1)
sage: UM2 = unfold(M2)
sage: refold(UM1) == M1
True
sage: sp = V.span([UM1, UM2]) ; sp
Vector space of degree 6 and dimension 2 over Finite Field of size 5
Basis matrix:
[1 0 0 3 2 4]
[0 1 3 1 2 0]
sage: UM1 + UM2 in sp
True
sage: UM3 = sp.random_element()
sage: M3 = refold(UM3) ; M3
[2 4 2]
[0 2 3]
sage: M3 in MM
True
</code></pre>
https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?comment=17685#post-id-17685So effectively you are using coordinate vectors for your matrices (relative to a standard basis for the vector space of matrices), and then doing all of the computations in F^n. That sounds like a reasonable thing to do for vector spaces other than F^n.Thu, 16 May 2013 07:31:03 +0200https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?comment=17685#post-id-17685Comment by Semin for <p>Unfortunately, <code>VectorSpace</code> seems only to consider row vectors, not matrices, as elements. So i do not know whether it is possible out of the box. A possible workaround is to "unfold" your matrices to vectors, do your computations in the span, and then refold result vector to get a matrix back. Folding and unfolding being isomorphisms of vector spaces, you should be safe with your computations.</p>
<p>Here is an example:</p>
<pre><code>sage: p = 5
sage: F = GF(p)
sage: MM = MatrixSpace(F,2,3) ; MM
Full MatrixSpace of 2 by 3 dense matrices over Finite Field of size 5
sage: M1 = MM.random_element() ; M1
[2 0 0]
[1 4 3]
sage: M2 = MM.random_element() ; M2
[2 3 4]
[4 0 3]
sage: V = VectorSpace(F, MM.dimension()) ; V
Vector space of dimension 6 over Finite Field of size 5
sage: def unfold(M):
....: return V(M.list())
sage: def refold(UM):
....: return MM(UM.list())
sage: UM1 = unfold(M1)
sage: UM2 = unfold(M2)
sage: refold(UM1) == M1
True
sage: sp = V.span([UM1, UM2]) ; sp
Vector space of degree 6 and dimension 2 over Finite Field of size 5
Basis matrix:
[1 0 0 3 2 4]
[0 1 3 1 2 0]
sage: UM1 + UM2 in sp
True
sage: UM3 = sp.random_element()
sage: M3 = refold(UM3) ; M3
[2 4 2]
[0 2 3]
sage: M3 in MM
True
</code></pre>
https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?comment=17691#post-id-17691Um~ I got it. Very thanks. Since GAP has the commands "VectorSpace( field , [generators] )" that can make a vector space generated by a list of matrices, I think Sage also have an easy method. But I couldn't find it not yet. :-( Your advice is very helpful. :-)Wed, 15 May 2013 10:38:15 +0200https://ask.sagemath.org/question/10124/how-to-get-a-vector-space-generated-by-a-list-of-matrices/?comment=17691#post-id-17691