Ask Your Question

Generate >2D Matrix

asked 2020-02-03 14:27:19 +0100

mattb gravatar image

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 flag offensive close merge delete


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

John Palmieri gravatar imageJohn Palmieri ( 2020-02-03 20:34:10 +0100 )edit

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

John Palmieri gravatar imageJohn Palmieri ( 2020-02-03 20:37:19 +0100 )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?

mattb gravatar imagemattb ( 2020-02-03 23:55:44 +0100 )edit

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

John Palmieri gravatar imageJohn Palmieri ( 2020-02-04 01:37:33 +0100 )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.

mattb gravatar imagemattb ( 2020-02-04 03:24:20 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-02-04 01:37:00 +0100

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()
sage: v.ncols()
sage: v.base_ring()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-02-03 14:27:19 +0100

Seen: 601 times

Last updated: Feb 04 '20