JuliaSymbolics / SymbolicUtils.jl

Symbolic expressions, rewriting and simplification

Home Page:https://docs.sciml.ai/SymbolicUtils/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve `simplify` rules for factorization

bowenszhu opened this issue · comments

using SymbolicUtils
@syms x y z
expr = x * y + x * z
res = simplify(expr) # return x*(y + z). Good!
using SymbolicUtils
@syms a b c d
expr = a * b * c + c * a * d
res = simplify(expr) # return a*b*c + a*c*d. Bad!
(@v1.8) pkg> st -m SymbolicUtils
Status `~/.julia/environments/v1.8/Manifest.toml`
  [d1185830] SymbolicUtils v0.19.11

This looks simple, but is fundamentally important for generating computationally inexpensive functions.

I would be curious to know how it can be implemented properly.

One can craft the answer with a custom rule

r3 = @rule (~x)*(~y)*(~z) + (~x)*(~a)*(~b) => (~x)*((~y)*(~z)+(~a)*(~b))
#
@syms a b c d
expr = a * b * c + c * a * d
res = simplify(expr, RuleSet([r3])) 

but it is super specific and only works is the common term is the first in both.