I use SymPy to compute exp(U)
, where U
is a matrix of symbols.
Then, I want to substitute the symbols in U
for values, but SymPy gives me the wrong result.
Below is a minimal code example:
from sympy import *
var("u0, u1, u2, u3", complex=True)
U = Matrix([[u0, u1], [u2, u3]])
x1 = exp(U.subs({u0: 1, u1: 0, u2: 0, u3: -1}))
x2 = exp(U).subs({u0: 1, u1: 0, u2: 0, u3: -1})
display(x1) # [[e, 0], [0, exp(-1)]]
display(x2) # [[0, 0], [0, exp(-1)]]
assert x1.equals(x2)
This example should compute
however, when I substitute U
after computing exp(U)
I get the wrong result.
Does anybody know a trick to make my example work?
I’d like to substitute U
after computing exp(U)
.
I’ve seen many posts about SymPy’s subs
function not working as expected, but didn’t find a solution for my case.