Assigning a variable directly does not modify expressions that used the variable retroactively.
>>> from sympy import Symbol
>>> x = Symbol('x')
>>> y = Symbol('y')
>>> f = x + y
>>> x = 0
>>> f
x + y
Assigning a variable directly does not modify expressions that used the variable retroactively.
>>> from sympy import Symbol
>>> x = Symbol('x')
>>> y = Symbol('y')
>>> f = x + y
>>> x = 0
>>> f
x + y
To substitute several values:
Actually sympy is designed not to substitute values until you really want to substitute them with
subs
(see http://docs.sympy.org/latest/tutorial/basic_operations.html)Try
instead of
The command
x = Symbol('x')
stores Sympy'sSymbol('x')
into Python's variablex
. The Sympy expressionf
that you create afterwards does containSymbol('x')
, not the Python variablex
.When you reassign
x = 0
, the Python variablex
is set to zero, and is no longer related toSymbol('x')
. This has no effect on the Sympy expression, which still containsSymbol('x')
.This is best explained in this page of the Sympy documentation: http://docs.sympy.org/latest/gotchas.html#variables
What you want to do is
f.subs(x,0)
, as said in other answers.