Makes Julia reason with equations. General purpose metaprogramming, symbolic computation and algebraic equational reasoning library for the Julia programming language: E-Graphs & equality saturation, term rewriting and more.
I have a use case where I have long algebraic nonlinear expressions (type Expr) to be fed to an optimization model in JuMP, and I wish to simplify the expressions before feeding them to the model to reduce the solver running time and readability. I think Metatheory.jl is the right solution and have tried to use the code from test/integration/cas.jl to simplify my expressions. They were partially simplified, but not as much as SymbolicUtils.jl or MATLAB would do, as in the following example:
Original expression: expr = :((CB-T)-(((((((CA*T+(CA*CA-(((T+(CA-CB))-(((CA+CB)+(((((CA-(((Ti-((Ti-Ti)+CA))-((CB*CB-Ti)-(Ti-CB*CA)))+CB))-T)+((CA+CA)+(CA*T-(Ti+T))))-Ti*CB)+CA*CA))+(T+T)))+0)))+(Ti+(((((CA-((CA+(CB-T))+T))-T)+((CA+CA)+(CA*T-(Ti+CB))))-Ti*CB)+((CB+CA)-(CB-CB)))))+(((((CA+Ti)-T)+(Ti+Ti))-Ti*CB)+CA*Ti))+(((((CA-(((Ti-(CA+CB))-(((((CA-T)-T)+((CA+Ti*CA)+(CA*T-T*CA)))-Ti)-CA*T))+T))-T)+(((CA+CB)+CA)+(CA*T-(Ti+T))))-Ti*CB)+CB))-Ti)-Ti)-((Ti+Ti)-(CA-CB))))
Simplified with test/integration/cas.jl: expr_Meta = :((CB - T) - (CA + CB + (CA - CB) + ((((CA - (CA + T + (CB - T))) - T) + 2CA + (CA * T - (CB + Ti))) - CB * Ti) + (CA * (T + Ti) + ((((CA + Ti) - T) + 2Ti) - CB * Ti) + (CA ^ 2 - ((T + (CA - CB)) - (CA + CB + (((((CA - (CB + ((Ti - CA) - ((CB ^ 2 - Ti) - (Ti - CA * CB))))) - T) + 2CA + (CA * T - (T + Ti))) - CB * Ti) + CA ^ 2) + 2T)))) + (CB + ((CB + ((CA - (T + ((Ti - (CA + CB)) - (((2 * (CA - T) + CA * Ti) - Ti) - CA * T))))-T)+(CA*T-(T+Ti)) + 2CA) CB * Ti)) + -3Ti))
I would really appreciate it if you can give some guidances on how should I improve the code from test/integration/cas.jl to reach simp_expr. Although SymbolicUtils.jl is able to simplify the expressions completely, Metatheory.jl is better for my use case due to its ability to transform Expr from JuMP directly. Thank you.
Hi,
I have a use case where I have long algebraic nonlinear expressions (type
Expr
) to be fed to an optimization model in JuMP, and I wish to simplify the expressions before feeding them to the model to reduce the solver running time and readability. I think Metatheory.jl is the right solution and have tried to use the code fromtest/integration/cas.jl
to simplify my expressions. They were partially simplified, but not as much as SymbolicUtils.jl or MATLAB would do, as in the following example:expr = :((CB-T)-(((((((CA*T+(CA*CA-(((T+(CA-CB))-(((CA+CB)+(((((CA-(((Ti-((Ti-Ti)+CA))-((CB*CB-Ti)-(Ti-CB*CA)))+CB))-T)+((CA+CA)+(CA*T-(Ti+T))))-Ti*CB)+CA*CA))+(T+T)))+0)))+(Ti+(((((CA-((CA+(CB-T))+T))-T)+((CA+CA)+(CA*T-(Ti+CB))))-Ti*CB)+((CB+CA)-(CB-CB)))))+(((((CA+Ti)-T)+(Ti+Ti))-Ti*CB)+CA*Ti))+(((((CA-(((Ti-(CA+CB))-(((((CA-T)-T)+((CA+Ti*CA)+(CA*T-T*CA)))-Ti)-CA*T))+T))-T)+(((CA+CB)+CA)+(CA*T-(Ti+T))))-Ti*CB)+CB))-Ti)-Ti)-((Ti+Ti)-(CA-CB))))
test/integration/cas.jl
:expr_Meta = :((CB - T) - (CA + CB + (CA - CB) + ((((CA - (CA + T + (CB - T))) - T) + 2CA + (CA * T - (CB + Ti))) - CB * Ti) + (CA * (T + Ti) + ((((CA + Ti) - T) + 2Ti) - CB * Ti) + (CA ^ 2 - ((T + (CA - CB)) - (CA + CB + (((((CA - (CB + ((Ti - CA) - ((CB ^ 2 - Ti) - (Ti - CA * CB))))) - T) + 2CA + (CA * T - (T + Ti))) - CB * Ti) + CA ^ 2) + 2T)))) + (CB + ((CB + ((CA - (T + ((Ti - (CA + CB)) - (((2 * (CA - T) + CA * Ti) - Ti) - CA * T))))-T)+(CA*T-(T+Ti)) + 2CA) CB * Ti)) + -3Ti))
simp_expr = 7*T - CB - 15*CA + 8*Ti - 2*CA^2 - CB^2 - CA*CB - 3*CA*T - 2*CA*Ti + 4*CB*Ti
I would really appreciate it if you can give some guidances on how should I improve the code from
test/integration/cas.jl
to reachsimp_expr
. Although SymbolicUtils.jl is able to simplify the expressions completely, Metatheory.jl is better for my use case due to its ability to transformExpr
from JuMP directly. Thank you.