Simplifying Symbolics.jl expression

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

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

Edit: I found a simplified example.

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

Edit 2: a MWE

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.

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.

1 Like

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.

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