Pro Q
Pro Q

Reputation: 5016

How do you convert a string to a Sympy expression without simplifying?

I would like to use a function like sympify in order to convert the string "c+1+1" into a Sympy expression. However, I don't want it to simplify.

Right now, I do

from sympy import sympify
print(sympify("c+1+1")) # >> c + 2

The docs mentioned something about a kernS function that might simplify less, but it too does some simplification during the conversion:

from sympy.core.sympify import kernS
print(kernS("c+1+1")) # >> c + 2

What I would like to see back is c+1+1.

How do I turn a string into a Sympy expression without any simplifications?

Upvotes: 1

Views: 1125

Answers (1)

Pro Q
Pro Q

Reputation: 5016

Looks like I didn't read far enough in the docs.

The simplification can be stopped by setting the evaluate parameter to False, like so:

from sympy import sympify
print(sympify("c+1+1", evaluate=False)) # >> c + 1 + 1

Upvotes: 3

Related Questions