考虑下面的例子

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中收集公共(public)因子呢?

最佳答案

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

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

关于python - 如何在SymPy中简化矩阵表达式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51943198/

10-09 22:41