# How to reformulate an expression by grouping similar terms into new variables [duplicate]

I am new to Mathematica, and am having trouble achieving something very simple to explain. I would like Mathematica to reformulate my input, replacing similar terms in a formula with variables, so that no computation appears twice in the formula. Something like this:

In[1] = XXXXXXX[(a+b*t)*t/(b*t+q) + (b*t+q)*t/(a+b*t)]
Out[1] = v1*t/v2 + v2*t/v1  with {v0 = b*t, v1 = (a+v0), v2=(v0+q)}


Thanks for your help

• I don't think there's a general way, but there are several questions here applying to specific situations: mathematica.stackexchange.com/q/894/12 mathematica.stackexchange.com/q/28773/12 mathematica.stackexchange.com/q/11178/12 and many more. Typical useful functions: Eliminate, GroebnerBasis, PolynomialReduce. Nov 23 '16 at 13:12
• There's ExperimentalOptimizeExpression, which does something like what you are asking for, but it's really meant for minimizing the numberr of arithmetic operations needed for calculating something (performance optimization), and in practice it doesn't turn out to be useful for symbolic computation tasks like yours. It's better to avoid substituting in too many terms from the beginning ... Nov 23 '16 at 13:13
• mathematica.stackexchange.com/questions/26527/… Nov 23 '16 at 13:30

## 1 Answer

You may be looking for ExperimentalOptimizeExpression. It produces precisely the output you describe.

In[50]:= ExperimentalOptimizeExpression[(a + b*t)*t/(b*t + q) + (b*t + q)*t/(a + b*t)]

Out[50]= ExperimentalOptimizedExpression[
Block[{Compile$1, Compile$2, Compile$3}, Compile$1 = b t;
Compile$2 = a + Compile$1;
Compile$3 = q + Compile$1;
(t Compile$2)/Compile$3 + (t Compile$3)/Compile$2]]


This function is meant for minimizing the number of arithmetic operations to compute an expression. The goal is performance optimization. Compile uses it internally.

I don't find it useful for symbolic computation, but since its output is exactly what you were asking for, I thought it should be in an answer.

Also check its options:

Options[ExperimentalOptimizeExpression]

{ExcludedForms -> {}, "ExternalForms" -> {}, "InertForms" -> {},
"OptimizationLevel" -> 1, "OptimizationSymbol" -> Compile\$}


You may want to set "OptimizationSymbol" -> v.

See also: