Struggling with defining a good factorization rule in SymbolicUtils.jl

Try defining two rules.

factor1 = @rule (~x * ~y + ~x * ~z) => ~x * (~y + ~z)
factor2 = @rule (~x * ~z + ~y * ~z) => (~x + ~y) * ~z
factor1(a*b + a*c) # a*(b + c)
factor2(a*b + a*c) # nothing
factor1(sin(a)*b + sin(a)*c) # nothing
factor2(sin(a)*b + sin(a)*c) # (b + c)*sin(a)

I think the root problem is that SymbolicsUtils.jl is not aware of the inherent commutativity of the multiplication operator *. The @acrule macro only imposes associativity-commutativity for the top-level operator, which is the + operator for the rule with LHS (~x * ~y + ~x * ~z). This seems to be a serious limitation though not strictly speaking a bug. It’ll be great if Symbolics devs can offer some insights here. Should I file a GitHub issue?

The example factor(2a*b + 2a*c) will not work because 2*a*b cannot be matched by ~x*~y, a well documented behavior (or perhaps limitation) of SymbolicsUtils.

1 Like