Strange behavior of SymPy in VS Code

In VS Code, I used the following code

using SymPy
@sysm x y
f(x,y) = sin(y-x)
df = sympy.diff(f(x,y),x)   # it outputs `- cos(x-y)`: correct
df(PI/2,y)   # it output `-sin(y)`: correct
df(x,PI/2)   # it outputs `-1`: wrong; expected to be `-sin(x)`

I also tried the same code above in Julia running from cmd (in Windows), the output of the code is correct as expected.

Please show me the way I could run my code in VS Code to have correct answers.

Thank you.

you might just have a corrupted workspace in VS Code, try restarting Julia or something

1 Like

It is best to be explicit about what variables you are substituting for, as in:

 df(x=>x, y=>PI/2)

That being said, this should have worked as written. I’ll create an issue.

1 Like

Thank you @jling but restarting Julia does not resolve the situation.

Thank you @j_verzani .

On the substitution operation, the behavior still seems strange

using SymPy
@syms x y
f(x,y) = sin(y-x)
df = sympy.diff(f(x,y),x)
df(x=>PI/2,y=>y) # OK
df(x=>PI/2,y=>x) # it results to `-sin(x)`, meaning that it does not substitute `x=>PI/2` in the result `-sin(x)`, it does not use the fact `y=x` neither (because I substitute `y` by `x`).

But if I tried


Then it seems to evaluate -cos(x-x) to have -1 using the fact y=x. The cause is due to the dependence on the sequence, I think.

How can we just substitute the arguments separately before evaluating the formula (i.e., in -cos(x-y), the first argument x is changed to PI/2, the second argument y is changed to x, and now we have -cos(PI/2-x) which is then simplified to -sin(x).

Yes, the arguments are processed one by one, so that behaviour is expected. You are free to do df(x => Pi/2)(y=>x) to stage the substitutions a different way.

1 Like