"Literal replace" in SymPy

Question:

Is there a way to perform a "literal replacing" in SymPy, which matches exactly the pattern?
For example, for the expression exp(I*theta)+exp(-I*theta), I would like to replace only exp(It) to zero without replacing exp(-It)
A minimal example would be

from sympy import *
f=exp(I*t)+exp(-I*t)

Clearly, subs will not work

f.subs((exp(I*t),0))

because it also replaces exp(-I*t) with infinity.

Asked By: Jake Pan

||

Answers:

you can use the replace method —

t = 2
f = exp(I * t) + exp(-I * t)
f = f.replace(exp(I * t), 0)
print(f)

outputs:

exp(-2*I)

hope this helps!

Answered By: harriet

You can use the xreplace method:

f.xreplace({exp(I*t):0})

Returns:

exp(-t*I)

Here is the documentation for reference.

Answered By: Marcelo Paco
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.