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


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

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

Check here:

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

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: