You may be looking for Experimental`OptimizeExpression
. It produces precisely the output you describe.
In[50]:= Experimental`OptimizeExpression[(a + b*t)*t/(b*t + q) + (b*t + q)*t/(a + b*t)]
Out[50]= Experimental`OptimizedExpression[
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[Experimental`OptimizeExpression]
{ExcludedForms -> {}, "ExternalForms" -> {}, "InertForms" -> {},
"OptimizationLevel" -> 1, "OptimizationSymbol" -> Compile`$}
You may want to set "OptimizationSymbol" -> v
.
See also:
Experimental`OptimizeExpression
, 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 ... $\endgroup$