Reputation: 21
Simple example:
from sympy import *
n = symbols('n',integer=True)
x,y = symbols('x,y')
expression = exp(I*n*x)
expression.subs(exp(I*n),y)
#>> exp(I*n*x)
Why doesn't SymPy replace the exp(I*x)
to give y**n
? It works perfectly fine the other way around (replacing y
in y**n
with exp(I*x)
to give exp(I*n*x)
). Is there a hack to get this done?
Upvotes: 2
Views: 92
Reputation: 91500
subs
might not work in general if the expression you are replacing doesn't appear exactly in the expression.
One workaround is to use replace
with pattern matching to replace a more general pattern
>>> a = Wild('a')
>>> expression.replace(exp(I*n*a), y**a)
y**x
Upvotes: 2