# Strange behavior of SymPy in VS Code

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451>\
**Category:** General Usage\
**Created:** [February 11, 2023, 2:09am UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451 "2023-02-11T02:09:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 11, 2023, 2:09am UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451/1 "2023-02-11T02:09:19Z")

</div>

In VS Code, I used the following code

```julia
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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 11, 2023, 5:10am UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451/2 "2023-02-11T05:10:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 11, 2023, 1:31pm UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451/3 "2023-02-11T13:31:08Z")

</div>

> [@Firstname\_Lastname](#):
>
> ```julia
> 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 is best to be explicit about what variables you are substituting for, as in:

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

```

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

---

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 11, 2023, 2:56pm UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451/4 "2023-02-11T14:56:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 11, 2023, 3:26pm UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451/5 "2023-02-11T15:26:07Z")

</div>

Thank you @j_verzani .

On the substitution operation, the behavior still seems strange

```julia
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

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

```

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)`.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 11, 2023, 4:09pm UTC](https://discourse.julialang.org/t/strange-behavior-of-sympy-in-vs-code/94451/6 "2023-02-11T16:09:04Z")

</div>

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.
