SymbolicUtils.jl
SymbolicUtils.jl copied to clipboard
Orders of subexpressions in expression and `@rule` are not consistent.
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!
We don't really guarantee a specific ordering, currently. The are arguments are sorted in an ordering that has total order over all expressions.