使用sympy模块计算涉及符号的矩阵乘法

问题描述 投票:0回答:1

我的问题如下:

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]])

结果与预期输出不符。我该如何纠正?

python matrix sympy matrix-multiplication
1个回答
0
投票

不要使用

BlockMatrix
使它们成为正则矩阵。然后将执行操作。

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]])

以 LaTeX 格式(来自 Jupyter Notebook):

© www.soinside.com 2019 - 2024. All rights reserved.