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, 15 Oct 2023 01:45:07 +0200coercion of matrix of matrices to single matrixhttps://ask.sagemath.org/question/73889/coercion-of-matrix-of-matrices-to-single-matrix/ I have a need to construct matrices like the following:
A simple 2x2 matrix
m1 = matrix([[a,b],[c,d]])
Then I need to make larger matrices out of them, as in
M1 = matrix([[m1,0],[0,1]])
Big M1 has parent: mat2x2(mat2x2(SR)). However, to manipulate the matrix and do matrix x matrix multiplication I need this to be mat4x4(SR).
How can I do this?Sun, 15 Oct 2023 00:17:34 +0200https://ask.sagemath.org/question/73889/coercion-of-matrix-of-matrices-to-single-matrix/Answer by John Palmieri for <p>I have a need to construct matrices like the following:</p>
<p>A simple 2x2 matrix
m1 = matrix([[a,b],[c,d]])</p>
<p>Then I need to make larger matrices out of them, as in
M1 = matrix([[m1,0],[0,1]])</p>
<p>Big M1 has parent: mat2x2(mat2x2(SR)). However, to manipulate the matrix and do matrix x matrix multiplication I need this to be mat4x4(SR).</p>
<p>How can I do this?</p>
https://ask.sagemath.org/question/73889/coercion-of-matrix-of-matrices-to-single-matrix/?answer=73891#post-id-73891You can use the `block_matrix` function:
sage: M = block_matrix([[m1,0],[zero_matrix(2), identity_matrix(2)]])
sage: M
[a b|0 0]
[c d|0 0]
[---+---]
[0 0|1 0]
[0 0|0 1]
sage: type(M)
<class 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>
sage: parent(M)
Full MatrixSpace of 4 by 4 dense matrices over Symbolic Ring
For this particular example, `block_diagonal_matrix` would also work. With either function, you can omit the subdivisions by passing `subdivide=False` when calling the function:
sage: M = block_diagonal_matrix(m1, identity_matrix(2), subdivide=False)
sage: M
[a b 0 0]
[c d 0 0]
[0 0 1 0]
[0 0 0 1]Sun, 15 Oct 2023 01:45:07 +0200https://ask.sagemath.org/question/73889/coercion-of-matrix-of-matrices-to-single-matrix/?answer=73891#post-id-73891