user357269
user357269

Reputation: 1913

Multiply out block matrices in sympy

n = Symbol('n', integer=True)
f = Symbol('f', integer=True)
A = MatrixSymbol('A', f, n)
B = MatrixSymbol('B', f, f)
C = MatrixSymbol('C', n, f)
D = MatrixSymbol('D', f, f)
expr = (BlockMatrix([[A, B]]) @ BlockMatrix([[C], [D]]))

I can't seem to figure out what function to use to simplify expr into A@C+B@D.

I tried using block_collapse, but that didn't do anything.

Upvotes: 1

Views: 127

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14500

The function is called block_collapse:

In [8]: expr
Out[8]: 
       ⎡C⎤
[A  B]⋅⎢ ⎥
       ⎣D⎦

In [9]: block_collapse(expr)
Out[9]: A⋅C + B⋅D

https://docs.sympy.org/latest/modules/matrices/expressions.html#block-matrices

Upvotes: 1

Related Questions