JuliaSymbolics / SymbolicUtils.jl

Symbolic expressions, rewriting and simplification
https://docs.sciml.ai/SymbolicUtils/stable/
Other
523 stars 99 forks source link

`@acrule` only uses associativity-commutativity on the top-level operand #586

Open Alseidon opened 3 months ago

Alseidon commented 3 months ago

Prompted by this discussion

When using @acrule, I can't "propagate" AC to operands other than the toplevel one. In the example below, a correct factorisation rewriter would be Rewriters.Chain([factor, factor2, factor3]) where I would expect to only have to write the first rule. Maybe there is a way to circumvent this though?

using SymbolicUtils

@syms a b c
expr = a*b + b*c

factor = @acrule +(~x*~y, ~x*~z) => *(~x, ~y+~z)
factor2 = @acrule +(~y*~x, ~z*~x) => *(~x, ~y+~z)

factor(expr) # nothing
factor2(expr) # nothing

factor3 = @acrule +(~x*~y, ~z*~x) => *(~x, ~y+~z)
factor3(expr) # b*(a+c)