JuliaPy / SymPy.jl

Julia interface to SymPy via PyCall
http://juliapy.github.io/SymPy.jl/
MIT License
268 stars 62 forks source link

`subs` change the value of an expression #479

Closed newptcai closed 1 year ago

newptcai commented 1 year ago

The following code in REPL is bit strange

julia> n = symbols("n")
n

julia> ans = 4^n-3^n-n*3^(n-1)
   n    n - 1      n
- 3  - 3     ⋅n + 4 

julia> println(subs(ans, (n,4)))
67

julia> println(ans)
nothing

How can substitute n by 4 turns ans into nothing?

newptcai commented 1 year ago

This is different from this behaviour

julia> ex = (x-y)*(x+2y)
(x - y)⋅(x + 2⋅y)

julia> println(subs(ex, (x, 3)))
(3 - y)*(2*y + 3)

julia> println(subs(ex, (y, 3)))
(x - 3)*(x + 6)

julia> println(ex)
(x - y)*(x + 2*y)
mzaffalon commented 1 year ago

println returns nothing and the variable ans is bound to the last result printed to the REPL. You should remove the println statement to get what you want, or even better, assign the result to a variable with a different name.

newptcai commented 1 year ago

println returns nothing and the variable ans is bound to the last result printed to the REPL. You should remove the println statement to get what you want, or even better, assign the result to a variable with a different name.

:astonished: Oh the ans is a special variable! I didn't know that.