Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Literal replace" in SymPy

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the xreplace method:

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

Returns:

exp(-t*I)

Here is the documentation for reference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading