Create a Matrix from blocks

35 vues
Accéder directement au premier message non lu

Oscar Benjamin

non lue,
7 nov. 2018, 18:56:1207/11/2018
à sy...@googlegroups.com
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

Aaron Meurer

non lue,
8 nov. 2018, 02:40:4908/11/2018
à sy...@googlegroups.com
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
> --
> 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.
> To post to this group, send email to sy...@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.

Chris Smith

non lue,
26 mars 2019, 18:32:3626/03/2019
à sympy
```
>>> 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
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message