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.Tue, 04 Feb 2020 03:24:20 +0100Generate >2D Matrixhttps://ask.sagemath.org/question/49762/generate-2d-matrix/I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?
Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.Mon, 03 Feb 2020 14:27:19 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/Comment by John Palmieri for <p>I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?</p>
<p>Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.</p>
https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49771#post-id-49771Is a list of matrices, or a tuple of matrices, insufficient? If so, why?Mon, 03 Feb 2020 20:34:10 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49771#post-id-49771Comment by John Palmieri for <p>I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?</p>
<p>Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.</p>
https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49772#post-id-49772You can also make a `1 x n` matrix whose entries are matrices.Mon, 03 Feb 2020 20:37:19 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49772#post-id-49772Comment by mattb for <p>I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?</p>
<p>Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.</p>
https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49773#post-id-49773A list of matrices would work. I was just hoping there would be something more intact, so that you don't have to make function calls through a for loop or list comprehension. Also, nesting lists, etc. can be a lot more confusing to decipher, since you have multiple sets of brackets. It seems like this aspect would actually slow things down. Maybe there's an even easier way to handle it if it is a list matrices without the for loop/list comprehension?
For a vector/matrix of matrices. I tried this. I got an error from Sage. Seems that Sage requires the elements of the matrix to be within some namespace. Maybe this is just due to an issue with declaration?Mon, 03 Feb 2020 23:55:44 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49773#post-id-49773Comment by John Palmieri for <p>I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?</p>
<p>Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.</p>
https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49775#post-id-49775I agree that vectors of matrices don't work, but matrices of matrices do.Tue, 04 Feb 2020 01:37:33 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49775#post-id-49775Comment by mattb for <p>I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?</p>
<p>Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.</p>
https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49776#post-id-49776OK, it did work. It required the 2-dimensions. But that is even more work than just using a Python List, since you still need to two sets of brackets to specify an element. It looks like maybe a list of matrices might be my best bet.
Alternatively, I think I saw that Maxima supports arrays. Since I'm doing most of the computation there, I may look into just storing the data there as well.Tue, 04 Feb 2020 03:24:20 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/?comment=49776#post-id-49776Answer by John Palmieri for <p>I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?</p>
<p>Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.</p>
https://ask.sagemath.org/question/49762/generate-2d-matrix/?answer=49774#post-id-49774To construct a matrix of matrices:
sage: a = random_matrix(QQ, 2, 2)
sage: b = random_matrix(QQ, 2, 2)
sage: c = random_matrix(QQ, 2, 2)
sage: d = random_matrix(QQ, 2, 2)
sage: v = matrix([[a,b,c,d]])
It prints horribly (but `view(v)` looks good).
sage: a
[ 2 0]
[ 1 1/2]
sage: b
[ 0 -1]
[1/2 2]
sage: c
[ 2 1]
[ 2 -2]
sage: d
[ -1 1/2]
[1/2 1]
sage: v
[[ 2 0]
[ 1 1/2] [ 0 -1]
[1/2 2] [ 2 1]
[ 2 -2] [ -1 1/2]
[1/2 1]]
sage: v.nrows()
1
sage: v.ncols()
4
sage: v.base_ring()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
Tue, 04 Feb 2020 01:37:00 +0100https://ask.sagemath.org/question/49762/generate-2d-matrix/?answer=49774#post-id-49774