Python – Can sympy simplify block matrix expressions?

Can sympy simplify block matrix expressions?… here is a solution to the problem.

Can sympy simplify block matrix expressions?

Consider the following example: (I tried to calculate the matrix product involving the block matrix).

import sympy as sy

k, n = sy.symbols('k,n')
A = sy. MatrixSymbol("A", n, n)
B = sy. MatrixSymbol("B", n, k)
M = sy. BlockMatrix([[A * B, A]])
A.inverse() * M

Output

A^-1*Matrix([[A*B, A]])

Is there a way to simplify/extend/remove this to a more readable Matrix([[B, I]]) form?

Solution

Yes, use functions block _collapse it evaluates the block matrix expression to the block level.

sy.block_collapse(A.inverse()*M)   

Returns Matrix([[B, I]]).

Related Problems and Solutions