Is there a straight-forward way to create a matrix from blocks?

The Matrix.diag function is nice for creating a block diagonal matrix:

In [1]: Matrix.diag(2*eye(2),3*eye(3))
Out[1]:
⎡2  0  0  0  0⎤
⎢             ⎥
⎢0  2  0  0  0⎥
⎢             ⎥
⎢0  0  3  0  0⎥
⎢             ⎥
⎢0  0  0  3  0⎥
⎢             ⎥
⎣0  0  0  0  3⎦

I'd like to do something similar but for a general block matrix. I
imagine something like:

>>> Matrix.fromblocks([[eye(2),2*eye(2)],[3*eye(2),4*eye(2)]])

The expected output would be:

[1 0 2 0]
[0 1 0 2]
[3 0 4 0]
[0 3 0 4]

This almost works if I pass a list of matrices to the Matrix constructor:

In [5]: Matrix([eye(2), eye(2)])
Out[5]:
⎡1  0⎤
⎢    ⎥
⎢0  1⎥
⎢    ⎥
⎢1  0⎥
⎢    ⎥
⎣0  1⎦

It doesn't work if I try to concatenate horizontally though:

In [7]: Matrix([[eye(2), eye(2)]])
Out[7]:
⎡⎡1  0⎤  ⎡1  0⎤⎤
⎢⎢    ⎥  ⎢    ⎥⎥
⎣⎣0  1⎦  ⎣0  1⎦⎦

Same happens if I try to do both:

In [8]: Matrix([[eye(2), eye(2)], [eye(2), eye(2)]])
Out[8]:
⎡⎡1  0⎤  ⎡1  0⎤⎤
⎢⎢    ⎥  ⎢    ⎥⎥
⎢⎣0  1⎦  ⎣0  1⎦⎥
⎢              ⎥
⎢⎡1  0⎤  ⎡1  0⎤⎥
⎢⎢    ⎥  ⎢    ⎥⎥
⎣⎣0  1⎦  ⎣0  1⎦⎦

I see that there is a BlockMatrix class but I don't want a block
matrix. I want to construct a normal matrix from blocks (similar to
diag). There is a deblock function but it doesn't do what I hoped:

In [13]: from sympy.matrices.expressions.blockmatrix import deblock

In [14]: deblock(Matrix([[eye(2), eye(2)], [eye(2), eye(2)]]))
Out[14]:
⎡⎡1  0⎤  ⎡1  0⎤⎤
⎢⎢    ⎥  ⎢    ⎥⎥
⎢⎣0  1⎦  ⎣0  1⎦⎥
⎢              ⎥
⎢⎡1  0⎤  ⎡1  0⎤⎥
⎢⎢    ⎥  ⎢    ⎥⎥
⎣⎣0  1⎦  ⎣0  1⎦

In [15]: deblock(BlockMatrix([[eye(2), eye(2)], [eye(2), eye(2)]]))
Out[15]:
⎡⎡1  0⎤  ⎡1  0⎤⎤
⎢⎢    ⎥  ⎢    ⎥⎥
⎢⎣0  1⎦  ⎣0  1⎦⎥
⎢              ⎥
⎢⎡1  0⎤  ⎡1  0⎤⎥
⎢⎢    ⎥  ⎢    ⎥⎥
⎣⎣0  1⎦  ⎣0  1⎦⎦

Am I missing a similar way to do this?

--
Oscar

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxTUe8Qtu1YGzcKWtR7Q_SXfOrenxqYORE_i6hCS8VnMuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to