# Pulling out common terms when simplifying complicated equations

I have a complicated expression where common terms are apparent but Simplify[] and FullSimplify[] don't appear able, even with plenty of assumptions added, to reduce down to a simpler form with common terms recognised so that I can give them a symbol and simplify the expressions acordingly. A simpler example which demonstrates is

h (140 + Current -
lastU1 + (lastV1 +
1/2 h (140 + Current - lastU1 + (5 + 0.04 lastV1) lastV1)) (5 +
0.04 (lastV1 +
1/2 h (140 + Current - lastU1 + (5 + 0.04 lastV1) lastV1))))


with obvious common terms being

140 + Current - lastU1   -> Alpha
(5 + 0.04 lastV1) lastV1 -> Beta


With these and one further common term which contains them both recognised

(lastV1 + 1/2 h (Alpha + Beta)) -> Gamma


this expression can be reduced to

h (Alpha + Gamma (5 + 0.04 Gamma))


I have played with Factor[], Collect[] and many others but nothing seems to do what I need. I am sure that Mathematica is capable of doing what I am looking for, so I am asking the experts here for tips for how best to go about it.

As a matter of interest, these expressions are going to be compiled into C eventually and I have found this to produce output of interest in terms of structuring the algebraic reduction

ExperimentalOptimizeExpression[ expression , OptimizationLevel -> 2]


but I would still like more control over the manipulation before I get to this stage.

Michael

-
– Sjoerd C. de Vries Dec 7 '13 at 13:01

While this won't work in general with very complicated expressions (see the other post for more general approaches), you could try simple replacement rules to get a reasonable degree of simplification. If you had only linear relations, that would work for sure. In the case of your 'toy' example

expr = h (140 + Current - lastU1 + (lastV1 + 1/2 h (140 + Current -
lastU1 + (5 + 0.04
lastV1) lastV1)) (5 + 0.04 (lastV1 + 1/2 h (140 +
Current - lastU1 + (5 + 0.04 lastV1) lastV1))))


the second rule below works only because it had the terms to be replaced in the very same form throughout the whole expression.

expr /. {lastU1 -> 140 + Current - Alpha, (5 + 0.04 lastV1) -> Beta/lastV1}


then you can apply the last rule

% /. lastV1 -> Gamma - 1/2 h (Alpha + Beta)


(Alpha + (0.04 Gamma + 5) Gamma) h

Often, after applying the rules, it is a good idea to feed the resulting expression to Simplify.

-

I am avoiding Beta and Gamma but using symbols as the former are special symbols in Mathematica.

The rules can be applied for simplification:

exp = h (140 + Current -
lastU1 + (lastV1 +
1/2 h (140 + Current -
lastU1 + (5 + 0.04 lastV1) lastV1)) (5 +
0.04 (lastV1 +
1/2 h (140 + Current - lastU1 + (5 + 0.04 lastV1) lastV1))))


Then replacing all:

(exp //. {140 + Current - lastU1 -> \[Alpha],
(5 + 0.04 lastV1) lastV1 -> \[Beta]}) //. (lastV1 +
1/2 h (\[Alpha] + \[Beta])) -> \[Gamma]


yields:

h (\[Alpha] + (5 + 0.04 \[Gamma]) \[Gamma])

-
thanks to both people, ubpdqn's was exactly the answer I am looking for – Michael Hopkins Dec 8 '13 at 16:58
I initially had a problem with it but this is because I was using variable names already taken – Michael Hopkins Dec 8 '13 at 16:58
Thank you. Is there a reason this does not merit a vote. Accepting both answers merely use replacement rules I point out the potential harmful unintentional use of special symbols which were actually verbatim used in other answer? – ubpdqn Dec 8 '13 at 22:36
I would vote for it but I can't :o) – Michael Hopkins Dec 8 '13 at 23:33
Ok. Thank you for confirming acceptable answer. – ubpdqn Dec 8 '13 at 23:47