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.Wed, 02 May 2012 04:42:57 +0200Is it possible to join matrices in to a bigger matrix?https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/
I have two symbolic $2 \times 2$ matrices $A$ and $B$. I want to join them into a $4 \times 4$ matrix of the form
$\begin{pmatrix}A & B \cr B & A\end{pmatrix}$.
The problem is that when I give
C=matrix(SR,4,4)
C=matrix([[A,B],[B,A]])
I get a three dimensional object, not a four by four matrix.
The actual matrices I am working with are bigger than this, so I don't want to do it by hand. Is there a way of doing this without a bunch of for loops?Tue, 01 May 2012 18:58:57 +0200https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/Answer by ndomes for <p>I have two symbolic $2 \times 2$ matrices $A$ and $B$. I want to join them into a $4 \times 4$ matrix of the form
$\begin{pmatrix}A & B \cr B & A\end{pmatrix}$.</p>
<p>The problem is that when I give </p>
<pre><code>C=matrix(SR,4,4)
C=matrix([[A,B],[B,A]])
</code></pre>
<p>I get a three dimensional object, not a four by four matrix.
The actual matrices I am working with are bigger than this, so I don't want to do it by hand. Is there a way of doing this without a bunch of for loops?</p>
https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/?answer=13526#post-id-13526 M = matrix(4,4,0)
A = matrix(2,2,[1,2,3,4])
M.set_block(0,0,A)
MTue, 01 May 2012 19:27:03 +0200https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/?answer=13526#post-id-13526Answer by achrzesz for <p>I have two symbolic $2 \times 2$ matrices $A$ and $B$. I want to join them into a $4 \times 4$ matrix of the form
$\begin{pmatrix}A & B \cr B & A\end{pmatrix}$.</p>
<p>The problem is that when I give </p>
<pre><code>C=matrix(SR,4,4)
C=matrix([[A,B],[B,A]])
</code></pre>
<p>I get a three dimensional object, not a four by four matrix.
The actual matrices I am working with are bigger than this, so I don't want to do it by hand. Is there a way of doing this without a bunch of for loops?</p>
https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/?answer=13527#post-id-13527For example:
A=matrix(2,2,range(1,5))
B=A*10
AA=A.block_sum(A)
AA.set_block(0,2,B)
AA.set_block(2,0,B)Tue, 01 May 2012 19:44:07 +0200https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/?answer=13527#post-id-13527Answer by Jason Grout for <p>I have two symbolic $2 \times 2$ matrices $A$ and $B$. I want to join them into a $4 \times 4$ matrix of the form
$\begin{pmatrix}A & B \cr B & A\end{pmatrix}$.</p>
<p>The problem is that when I give </p>
<pre><code>C=matrix(SR,4,4)
C=matrix([[A,B],[B,A]])
</code></pre>
<p>I get a three dimensional object, not a four by four matrix.
The actual matrices I am working with are bigger than this, so I don't want to do it by hand. Is there a way of doing this without a bunch of for loops?</p>
https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/?answer=13531#post-id-13531Use the [`block_matrix`](http://www.sagemath.org/doc/reference/sage/matrix/constructor.html#sage.matrix.constructor.block_matrix) command instead of the `matrix` command:
var('x,y')
A=matrix([[x,y],[x^2,y^2]])
B=matrix([[2*x,3*y],[4*x,5*y]])
C=block_matrix(SR,[[A,B],[B,A]])
C
http://aleph.sagemath.org/?q=789a5a21-2cad-487d-a04b-1c01e53ba214
Wed, 02 May 2012 04:42:57 +0200https://ask.sagemath.org/question/8948/is-it-possible-to-join-matrices-in-to-a-bigger-matrix/?answer=13531#post-id-13531