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.Sun, 10 May 2015 07:45:28 +0200How to build a matrix thought of as an array of smaller matrices?https://ask.sagemath.org/question/26764/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]
Fri, 08 May 2015 19:38:32 +0200https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/Answer by Francis Clarke for <p>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,<em>) tuple occurs then (2,1,</em>) tuple will not occur) </p>
<p>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$. </p>
<ul>
<li><p>How can one create such a X on SAGE?</p>
<p>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. </p></li>
</ul>
<p>[I showed this above example with just $4$ tuples. I want to eventually do it with much larger data sets] </p>
https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?answer=26776#post-id-26776The `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
Sat, 09 May 2015 14:14:59 +0200https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?answer=26776#post-id-26776Comment by phoenix for <p>The <code>block_matrix</code> construction will do what you want. An example follows.</p>
<p>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 )</p>
<pre><code>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]])
</code></pre>
<p>Then I defined a 4 by 4 array of zero matrices and put the data matrices in the appropriate positions</p>
<pre><code>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()
</code></pre>
<p>Now</p>
<pre><code>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]
</code></pre>
<p>All the usual matrix methods are available, e.g.,</p>
<pre><code>sage: block_matrix(m).rank()
6
</code></pre>
https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26777#post-id-26777(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?Sat, 09 May 2015 20:04:06 +0200https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26777#post-id-26777Comment by phoenix for <p>The <code>block_matrix</code> construction will do what you want. An example follows.</p>
<p>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 )</p>
<pre><code>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]])
</code></pre>
<p>Then I defined a 4 by 4 array of zero matrices and put the data matrices in the appropriate positions</p>
<pre><code>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()
</code></pre>
<p>Now</p>
<pre><code>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]
</code></pre>
<p>All the usual matrix methods are available, e.g.,</p>
<pre><code>sage: block_matrix(m).rank()
6
</code></pre>
https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26778#post-id-26778(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?Sat, 09 May 2015 20:19:49 +0200https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26778#post-id-26778Comment by phoenix for <p>The <code>block_matrix</code> construction will do what you want. An example follows.</p>
<p>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 )</p>
<pre><code>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]])
</code></pre>
<p>Then I defined a 4 by 4 array of zero matrices and put the data matrices in the appropriate positions</p>
<pre><code>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()
</code></pre>
<p>Now</p>
<pre><code>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]
</code></pre>
<p>All the usual matrix methods are available, e.g.,</p>
<pre><code>sage: block_matrix(m).rank()
6
</code></pre>
https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26779#post-id-26779(3) And why is "d={}" different from starting as "d=[]" ?Sat, 09 May 2015 20:25:54 +0200https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26779#post-id-26779Comment by Francis Clarke for <p>The <code>block_matrix</code> construction will do what you want. An example follows.</p>
<p>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 )</p>
<pre><code>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]])
</code></pre>
<p>Then I defined a 4 by 4 array of zero matrices and put the data matrices in the appropriate positions</p>
<pre><code>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()
</code></pre>
<p>Now</p>
<pre><code>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]
</code></pre>
<p>All the usual matrix methods are available, e.g.,</p>
<pre><code>sage: block_matrix(m).rank()
6
</code></pre>
https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26784#post-id-26784This 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.Sun, 10 May 2015 07:45:28 +0200https://ask.sagemath.org/question/26764/how-to-build-a-matrix-thought-of-as-an-array-of-smaller-matrices/?comment=26784#post-id-26784