13

Consider the following example

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

The example prints out B^-1*(A^-1*B^-1 - A^-1*C*B^-1)^-1*A^-1.

Can SymPy simplify the expression to (I-C)^-1? If not, how about any of the intermediate results, like collecting common factors in M?

sarasvati119
  • 162
  • 8
  • I'm not sure if there's a way to do it. I opened an issue for it at https://github.com/sympy/sympy/issues/15120. Also, I think the answer should be `(I - C)^-1`, no? – asmeurer Aug 21 '18 at 21:27
  • @asmeurer : Right you are. I edited accordingly; also maybe this goes to show that the feature would be useful. – sarasvati119 Aug 22 '18 at 06:33

1 Answers1

1

The work around for this is using string converting on expression:

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)

Output:

 -1  
─────
C - 1
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
  • That's pretty neat. I suspect, however, that SymPy just does a scalar conversion here: if we define **K** and **L** same way as A,B,C, and define `A.inverse()*C*K*B.inverse() - A.inverse()*L*C*B.inverse()`, the result should be `(CK-LC)^-1`, but I get `(C*(K-L))^-1`, i.e. there's a scalar simplification going on. If there's a way to be sure string conversion respects matrix algebra, this is a solution. – sarasvati119 Jul 08 '19 at 11:06