ModelingToolkit.jl: '===' comparison on Operations(/Expressions)

Hi,


Since this is my first post, I want to thank everyone involved in the creation of the Julia language.

I would like to compare Operation/Expression objects, my initial goal being to replace sub-(Operation/Expression) by another one. I’m having issues understanding the behavior of ‘===’ operator on them :

using ModelingToolkit

@variables      x t
@derivatives    D'~t

julia> a=D(x)
derivative(x, t)

julia> b=a         # Copy b to a
derivative(x, t)

julia> a==b                 # '==' operator builds a Symbolic Expression, does not compare Operations.
derivative(x, t) == derivative(x, t)

julia> a===b              # Using '===' operator on a and b gives expected result
true

julia> D(x)===D(x)    # This does NOT give the expected result
false
1 Like

Check here:
https://stackoverflow.com/questions/38601141/what-is-the-difference-between-and-comparison-operators-in-julia

Or here: Overload === for custom type

By executing D(x) two times you are creating two objects stored at different memory locations. So that behavior is expected.

Since == is overloaded, you can use isequal instead:

julia> isequal(D(x), D(x))
true
5 Likes

I now remember stumbling into isequal() some time ago looking for something else and wrongly though that it was a local package function, not such a fundamental one.

One of the sub-links in the StackOverflow post makes a good summary of it :
When should i use == vs === vs isequal

Thank you very much. :+1: