SymbolicUtils.jl icon indicating copy to clipboard operation
SymbolicUtils.jl copied to clipboard

Orders of subexpressions in expression and `@rule` are not consistent.

Open frankwswang opened this issue 4 years ago • 1 comments

I notice that there's a default reordering operation for the input expression, so I can use one rule for slightly different expressions.

julia> @syms a b c
(a, b, c)
julia> expr1 = (a-b)^-1*(2a-2b)
((a - b)^-1)*(2a - (2b))

julia> expr2 = (2a-2b)*(a-b)^-1
((a - b)^-1)*(2a - (2b))

julia> myRule = @rule ((~x+(-1)*~y)^-1)*(2*~x + (-2)*~y) => 2
(~x + -1 * ~y) ^ -1 * (2 * ~x + -2 * ~y) => 2

julia> myRule(expr1)
2

julia> myRule(expr2)
2

However, in the case of a no-reordering case, I try to switch the position of 2 subexpressions, and expect making a new rule by doing the same switch when defining it, should make the new rule work for the new expression:

julia> expr3 = (2c-2b)*(c-b)^-1
(2c - (2b))*((c - b)^-1)

julia> myRule2 = @rule (2*~x + (-2)*~y)*((~x+(-1)*~y)^-1) => 2
(2 * ~x + -2 * ~y) * (~x + -1 * ~y) ^ -1 => 2

julia> @show myRule2(expr3)
myRule2(expr3) = nothing

However, it doesn't work. I'm not sure but I think the way symbols fused together has changed after I switch the position of subexpressions. However, the () shown in info printing are in the same ways for each subexpression, which I assume is worked as an indicator for how the symbols are fused. I'm not sure if this is a bug or a consequence of a certain design choice. I have noticed there are similar issues about the fusing between symbols and symbols/variables (https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/208), but so far

Thank you!

frankwswang avatar May 22 '21 02:05 frankwswang

We don't really guarantee a specific ordering, currently. The are arguments are sorted in an ordering that has total order over all expressions.

YingboMa avatar May 22 '21 17:05 YingboMa