# Simplifying Symbolics.jl expression

**URL:** <https://discourse.julialang.org/t/simplifying-symbolics-jl-expression/118935>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [September 2, 2024, 9:36am UTC](https://discourse.julialang.org/t/simplifying-symbolics-jl-expression/118935 "2024-09-02T09:36:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [September 2, 2024, 9:36am UTC](https://discourse.julialang.org/t/simplifying-symbolics-jl-expression/118935/1 "2024-09-02T09:36:09Z")

</div>

I’m adding to the list of posts about simplifying a `Symbolics.jl` expression, but I wanted to check if I did something wrong or if it is an internal limitation. I want to check if two (lengthy) expressions are identical, and am trying to simplify their difference. However, `simplify(expr)` doesn’t simplify things such as this:  
 ![Result of simplify(expr) |> simplify: -2 * G1 * cos(w1°w2) * G2 * rt2 * r2 + 2 * G1 * cos(w1°w2) * G2 * rt2 * r2](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e5d8a9feee6abffd223493fa9c70924e12484a6.png)

(I ran `simplify` twice, and a third pass didn’t change anything).

Edit: I found a simplified example.

 ![image of b1 coming from substition computations, and b2 defined by hand. b1 doesn't simplify to 0, but b2 does.](https://global.discourse-cdn.com/julialang/original/3X/0/8/08a6f09867cf67875dcb91b4ea9265fde65ff11a.png)

Maybe the fact that the computed expression’s (`b1`) terms are not reordered is the problem ?

Edit 2: a MWE

```julia
julia> using Symbolics
[Info: Precompiling Symbolics [0c5d862f-8b57-4792-8d23-62f2024744c7]

julia> @variables a b c d e f
6-element Vector{Num}:
 a
 b
 c
 d
 e
 f

julia> (a^2 + b^2) * cos(c + d) * e + e * cos(c + d) * (-a^2 - b^2)
(a^2 + b^2)*e*cos(c + d) + (-(a^2) - (b^2))*e*cos(c + d)

julia> (a^2 + b^2) * cos(c + d) * e + e * cos(c + d) * (-a^2 - b^2) |> simplify
0

julia> (a^2 + b^2) * cos(c + d) * e * f + e * cos(c + d) * (-a^2 - b^2) * f |> simplify
(a^2 + b^2)*e*f*cos(c + d) + (-(a^2) - (b^2))*e*f*cos(c + d)

```

I suspect the problem here is the terms in the computed expression (`b1`) are not reordered.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 2, 2024, 10:39pm UTC](https://discourse.julialang.org/t/simplifying-symbolics-jl-expression/118935/2 "2024-09-02T22:39:33Z")

</div>

> [@Alseidon](#):
>
> ```julia
> julia> (a^2 + b^2) * cos(c + d) * e * f + e * cos(c + d) * (-a^2 - b^2) * f |> simplify
> (a^2 + b^2)*e*f*cos(c + d) + (-(a^2) - (b^2))*e*f*cos(c + d)
> 
> ```

Just change `simplify` to `expand`, and the output becomes 0.  
It’ll be great to have a smarter `simplify`, but in this case, expansion is clearly the solution.

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [September 3, 2024, 2:12pm UTC](https://discourse.julialang.org/t/simplifying-symbolics-jl-expression/118935/3 "2024-09-03T14:12:28Z")

</div>

> [@greatpet](#):
>
> Just change `simplify` to `expand`, and the output becomes 0.

This was indeed the solution for the two last examples, thank you!

For the first one (quoted below), it didn’t work: differences of identical terms do not cancel out.

> [@Alseidon](#):
>
> However, `simplify(expr)` doesn’t simplify things such as this:  
> ![Result of simplify(expr) |> simplify: -2 * G1 * cos(w1°w2) * G2 * rt2 * r2 + 2 * G1 * cos(w1°w2) * G2 * rt2 * r2](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e5d8a9feee6abffd223493fa9c70924e12484a6.png)

I got it working with the same expression on another Pluto notebook though, it might just be an error I didn’t spot.
