```
>>> BlockMatrix([[eye(2),2*eye(2)],[3*eye(2),4*eye(2)]]).as_explicit()
Matrix([
[1, 0, 2, 0],
[0, 1, 0, 2],
[3, 0, 4, 0],
[0, 3, 0, 4]])
```
See also #16454 <https://github.com/sympy/sympy/pull/16454>

On Thursday, November 8, 2018 at 1:40:49 AM UTC-6, Aaron Meurer wrote:
>
> I think a fromblocks classmethod would be useful. 
>
> Also I don't see why BlockMatrix shouldn't automatically flatten when 
> all the entries are explicit matrices. Or at the least have a method 
> that does so. 
>
> As for Matrix([[eye(2), eye(2)], [eye(2), eye(2)]]), it seems it 
> should either auto-flatten, or create a BlockMatrix. 
>
> Aaron Meurer 
> On Wed, Nov 7, 2018 at 4:56 PM Oscar Benjamin 
> <oscar.j....@gmail.com <javascript:>> wrote: 
> > 
> > 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+un...@googlegroups.com <javascript:>. 
> > To post to this group, send email to sy...@googlegroups.com 
> <javascript:>. 
> > 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. 
>

-- 
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/5fd0d4ec-3843-400e-92ca-a840bb283ffb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to