# 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.

edit retag close merge delete

Is a list of matrices, or a tuple of matrices, insufficient? If so, why?

( 2020-02-03 13:34:10 -0600 )edit

You can also make a 1 x n matrix whose entries are matrices.

( 2020-02-03 13:37:19 -0600 )edit

A 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?

( 2020-02-03 16:55:44 -0600 )edit

I agree that vectors of matrices don't work, but matrices of matrices do.

( 2020-02-03 18:37:33 -0600 )edit

OK, 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.

( 2020-02-03 20:24:20 -0600 )edit

Sort by » oldest newest most voted

To 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

more