mulllhausen
mulllhausen

Reputation: 4435

sympy.sympify() does not perform substitutions

this works as expected:

>>> from sympy import *
>>> (x, y, z) = symbols("x y z")
>>> y = x
>>> z = y
>>> z
x

however sympify() does not perform the substitution:

>>> from sympy import *
>>> y = sympify('x')
>>> z = sympify('y')
>>> z
y

z should be set to x.

are there any flags i can pass to sympify() to get it to substitute? i'm using sympy version 0.7.1.rc1 and python 2.7.3

Upvotes: 0

Views: 225

Answers (1)

tzaman
tzaman

Reputation: 47780

You're misunderstanding the difference between sympy symbols and Python names.

>>> y = sympify('x')

Here you've created a symbol x referred to by a name y.

>>> z = sympify('y')

Now you create a symbol y referred to by a name z. Note that the symbol y and the local name y have NOTHING to do with each other. Sympy does not care that you have a variable named y when you say sympify('y') -- it's not inspecting your local namespace.

What you probably want is:

>>> z = sympify(y)

i.e. assigning z to the symbol pointed to by y; this gets you what you expect:

>>> z
x 

Also note that the sympify call is entirely redundant in this case, you really should just be doing:

>>> z = y

Upvotes: 1

Related Questions