# How to build a matrix thought of as an array of smaller matrices?

Say I am given a data set which looks like $[ (0,2,A), (0,3,B), (1,2,C), (1,4,D) ]$ where $A,B,C,D$ are matrices all of the same dimension say $k$. (the data set will always have unique pairs of integers - as in if (1,2,) tuple occurs then (2,1,) tuple will not occur)

Now I want to create a 4x4 matrix say X of dimension $4k$ thought of as a 4x4 array of k-dimensional matrices. The arrays in $X$ are to be defined as $X(0,2) = A, X(2,0) = A^{-1}, X(0,3) = B, X(3,0) = B^{-1}, X(1,2) = C, X(2,1) = C^{-1}, X(1,4) = D, X(4,1) = D^{-1}$ and all other array positions in $X$ are to be filled in with $0$ matrices of dimension $k$.

• How can one create such a X on SAGE?

X is a matrix of matrices and I am not sure how one can define this on SAGE. Like saying "X(0,3) = B" is not going to make any obvious sense to SAGE. I necessarily need X to be a matrix so that i can later say calculate its characteristic polynomial.

[I showed this above example with just $4$ tuples. I want to eventually do it with much larger data sets]

edit retag close merge delete

Sort by » oldest newest most voted

The block_matrix construction will do what you want. An example follows.

I first set up a dictionary containing data of the kind you discussed (I'm assuming that the final entry in your data set should be $(1,3,D)$ if you want a $4k$ by $4k$ matrix )

sage: d = {}
sage: d[0, 2] = matrix([[5, 11], [1, 2]])
sage: d[0, 3] = matrix([[2, 3], [1, 1]])
sage: d[1, 2] = matrix([[-1, 3], [0, -1]])
sage: d[1, 3] = matrix([[4, 9], [-1, -2]])


Then I defined a 4 by 4 array of zero matrices and put the data matrices in the appropriate positions

sage: m = [[matrix(2, 2, 0)]*4 for _ in range(4)]
sage: for i in range(4):
....:     for j in range(4):
....:         if (i, j) in d:
....:             m[i][j] = d[i, j]
....:         elif (j, i) in d:
....:             m[i][j] = d[j, i].inverse()


Now

sage: block_matrix(m)
[ 0  0| 0  0| 5 11| 2  3]
[ 0  0| 0  0| 1  2| 1  1]
[-----+-----+-----+-----]
[ 0  0| 0  0|-1  3| 4  9]
[ 0  0| 0  0| 0 -1|-1 -2]
[-----+-----+-----+-----]
[-2 11|-1 -3| 0  0| 0  0]
[ 1 -5| 0 -1| 0  0| 0  0]
[-----+-----+-----+-----]
[-1  3|-2 -9| 0  0| 0  0]
[ 1 -2| 1  4| 0  0| 0  0]


All the usual matrix methods are available, e.g.,

sage: block_matrix(m).rank()
6

more

(1) Why does $d[0,2]$ make sense? $d$ is not a matrix but just an empty list. If assigning matrices to $d$'s tuple coordinates make sense then why not just read the data and list and assign the appropriate matrices to d's corresponding positions?

(2) Can you explain this " m = [[matrix(2, 2, 0)]*4 for _ in range(4)]" ? What exactly is this doing? Is m not defined as a matrix at this step?

(3) And why is "d={}" different from starting as "d=[]" ?

This is mostly fairly basic python syntax:

(1) d is not an empty list; it is a dictionary.

(2) m is a list of four lists of four 2 by 2 zero matrices. It is not a matrix, but (after it is modified) it is used to create a (block) matrix. The command could have been written as m = [[matrix(2, 2, 0) for j in range(4)] for i in range(4)]

(3) d = {} creates an empty dictionary; d = [] creates an empty list.