如何在SymPy中简化矩阵表达式?

时间:2018-08-21 06:57:39

标签: python sympy symbolic-math

考虑以下示例

import sympy as sy
n = sy.symbols('n')
A = sy.MatrixSymbol("A",n,n)
B = sy.MatrixSymbol("B",n,n)
C = sy.MatrixSymbol("C",n,n)
M = A.inverse()*B.inverse() - A.inverse()*C*B.inverse()
B.inverse()*M.inverse()*A.inverse()

该示例打印出B^-1*(A^-1*B^-1 - A^-1*C*B^-1)^-1*A^-1

SymPy可以将表达式简化为(I-C)^-1吗?如果不是,那么任何中间结果如何,例如在M中收集公共因子呢?

1 个答案:

答案 0 :(得分:1)

解决方法是对表达式使用字符串转换:

from sympy import *

n = symbols('n')
A = MatrixSymbol("A",n,n)
B = MatrixSymbol("B",n,n)
C = MatrixSymbol("C",n,n)
M = A.inverse()*B.inverse() - A.inverse()*C*B.inverse()
expression = B.inverse()*M.inverse()*A.inverse()

# convert expression to string then simplify
simplify_expression = simplify(str(expression))

pprint(simplify_expression)

输出:

 -1  
─────
C - 1