Using the sympy module to compute the matrix multiplication involving symbols

24 views Asked by At

My problem is given as follows:

import sympy as sp

p = sp.symbols('p')
I_p = sp.Identity(p)
C = sp.BlockMatrix([[I_p, I_p], [I_p, -I_p]])
Sigma_1 = sp.MatrixSymbol('Sigma_1', p, p)
Sigma_2 = sp.MatrixSymbol('Sigma_2', p, p)
Sigma = sp.BlockMatrix([[Sigma_1, Sigma_2], [Sigma_2, Sigma_1]])

C_Sigma_C_transpose = C * Sigma * C.T

print(C_Sigma_C_transpose)

## Matrix([
## [I,  I],
## [I, -I]])*Matrix([
## [Sigma_1, Sigma_2],
## [Sigma_2, Sigma_1]])*Matrix([
## [I,  I],
## [I, -I]])

The result does not match the expected output. How can I correct it?

1

There are 1 answers

0
jared On BEST ANSWER

Instead of using BlockMatrix make them regular matrices. Then the operations will be performed.

C = sp.Matrix([[I_p, I_p], [I_p, -I_p]])
Sigma = sp.Matrix([[Sigma_1, Sigma_2], [Sigma_2, Sigma_1]])

# the @ symbol is for matrix multiplication, but * will work
C_Sigma_C_transpose = C@[email protected]

print(C_Sigma_C_transpose )
# Matrix([[2*Sigma_1 + 2*Sigma_2, 0], [0, 2*Sigma_1 - 2*Sigma_2]])

Formatted in LaTeX (from the Jupyter Notebook):

enter image description here