# SymEngine Not Calculating Function Correctly

**URL:** <https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532>\
**Category:** New to Julia\
**Created:** [July 4, 2020, 5:38pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532 "2020-07-04T17:38:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vf30kf](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vf30kf](https://discourse.julialang.org/u/vf30kf)\
**Post date:** [July 4, 2020, 5:38pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/1 "2020-07-04T17:38:02Z")

</div>

See the code from the REPL below. What gives?

```julia
julia> using SymEngine

julia> @vars x1, x2, x3
(x1, x2, x3)

julia> f = sin(x1*x2) + exp(x2 + x3) - x3
-x3 + exp(x2 + x3) + sin(x2*x1)

julia> gf_x1 = diff(f,x1)
x2*cos(x2*x1)

julia> gf_x2 = diff(f,x2)
x1*cos(x2*x1) + exp(x2 + x3)

julia> gf_x3 = diff(f,x3)
-1 + exp(x2 + x3)

julia> gf_x1(1.,2.,3.)
-0.416146836547142

julia> 2.0*cos(2.0)
-0.8322936730942848

julia> gf_x2(1.,2.,3.)
53.76585636005

julia> cos(2.0)+exp(5.0)
147.99701226602946

julia> gf_x3(1.,2.,3)
19.0855369231877

julia> -1+exp(5.)
147.4131591025766

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 4, 2020, 6:29pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/2 "2020-07-04T18:29:43Z")

</div>

Now I’m not sure how `SymEngine` determines the argument order, but

> [@vf30kf](#):
>
> ```julia
> x2*cos(x2*x1)
> 
> ```

It seems that you are evaluating `x1 = 2; x2 = 1`, which would agree with the order these variables appears in the expression.

---

<div class="post-metadata">

**Author:** ![vf30kf](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vf30kf](https://discourse.julialang.org/u/vf30kf)\
**Post date:** [July 4, 2020, 7:39pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/3 "2020-07-04T19:39:29Z")

</div>

Thank you for your reply. From that perspective, means that x2 and x1 have “swapped” their respective positions. Interesting. Explicitly:

From what I see:  
gf\_x1 =\> cos(2.0)  
gf\_x2 =\> 2.0\*cos(2.0) + exp(4.0)  
gf\_x3 =\> -1 + exp(3.)

```julia
julia> cos(2.0)
-0.4161468365471424

julia> 2.0*cos(2.0) + exp(4.0)
53.76585636004995

julia> -1 + exp(3.)
19.085536923187668

```

I wonder if there is a way to have SymEngine hold respect to the variables as they are originally defined in the parent function f? Must be some trick. Maybe someone out there knows.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 4, 2020, 8:10pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/4 "2020-07-04T20:10:33Z")

</div>

As @yuyichao said, the heuristic SymEngine is using here to decide on the order of positional arguments is a bit mysterious. I’d say you shouldn’t use the positional arguments like that anyways and instead use the `Pair` methods:

```julia
julia> using SymEngine

julia> @vars x1, x2, x3
(x1, x2, x3)

julia> f = sin(x1*x2) + exp(x2 + x3) - x3; gf1 = diff(f, x1)
x2*cos(x2*x1)

julia> gf_x1(x1 => 1., x2 => 2., x3 => 3.)
-0.832293673094285

julia> 2 * cos(2)
-0.8322936730942848

```

as this is _much_ clearer what is going on.

* * *

Also, I should mention that your title is a little misleading. It’s the SymEngine.jl external package that’s causing the mysterious behaviour. This isn’t about julia itself doing something wrong.

---

<div class="post-metadata">

**Author:** ![vf30kf](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vf30kf](https://discourse.julialang.org/u/vf30kf)\
**Post date:** [July 4, 2020, 8:24pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/5 "2020-07-04T20:24:20Z")

</div>

Thank you very much for your response also. Sorry about the misleading title. At the time I posted, I wasn’t aware that this was a SymEngine related error until @yuyichao replied . Let me see if there is a way to change it. Sorry but I’m obviously brand new to Julia.

And thank you a second time for the Pair example of using this properly.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 4, 2020, 8:27pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/6 "2020-07-04T20:27:28Z")

</div>

No need to apologize, and I’m happy I was able to help.

Welcome to the community!

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [July 4, 2020, 9:21pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/7 "2020-07-04T21:21:59Z")

</div>

@yuyichao is right. Just try @vars a,b,c followed by f(a,b,c)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 4, 2020, 9:23pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/8 "2020-07-04T21:23:40Z")

</div>

I’ve taken the liberty to change the thread title to be more descriptive of the actual problem 🙂

---

<div class="post-metadata">

**Author:** ![vf30kf](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@vf30kf](https://discourse.julialang.org/u/vf30kf)\
**Post date:** [July 4, 2020, 9:26pm UTC](https://discourse.julialang.org/t/symengine-not-calculating-function-correctly/42532/9 "2020-07-04T21:26:28Z")

</div>

Thank you,@Sukera!
