Reputation: 2976
In Sympy, when a symbol is real (real=True
), it is not substituted using subs()
.
Even when real
is false
, it doesn't work.
Whenever real
is specified in symbols()
call, the substitution subs()
does not work.
Otherwise, it works:
import sympy
X = sympy.symbols("X")
print('i', X.subs( {'X': 1.01} )) # works
X = sympy.symbols("X", real=True) # `real=` Seems to be the cause of the problem
print('ii', X.subs( {'X': 2.02} ))
X = sympy.symbols("X", real=False) # even this
print('iii', X.subs( {'X': 3.03} ))
output:
i 1.01000000000000
ii X
iii X
For some reason, I need to specify the assumption real=True
.
My sympy.__version__
is : 1.10.1
Upvotes: 1
Views: 504
Reputation: 13150
That behavior is to be expected: when you call .subs({'X': 3.03})
, the 'X'
is going to be "simpified" ie, it is going to be converted to a generic new symbol (with no assumptions).
In your case:
X = sympy.symbols("X", real=False) # even this
print('iii', X.subs( {'X': 3.03} ))
# iii X
that's because X
is a symbol with assumptions, and 'X'
will be converted to a symbol with no assumptions: they are different symbols, even though they share the name!
To understand it better, you can do something like this:
x1 = sympy.symbols("x")
x2 = sympy.symbols("x", real=True)
print(x1.equals(x2))
# out: False
print(x1 == x2)
# out: False
When you call subs
you should be as specific as possible:
X = sympy.symbols("X", real=False) # even this
print('iii', X.subs( {X: 3.03} ))
# out: iii 3.03000000000000
Keep in mind that subs
is meant to work on larger expressions, composed by one or multiple symbols.
Upvotes: 4