2
$\begingroup$

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

Experimental`OptimizeExpression[ expression , OptimizationLevel -> 2]

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

Many thanks in advance

Michael

$\endgroup$
1

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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])
$\endgroup$
4
  • $\begingroup$ thanks to both people, ubpdqn's was exactly the answer I am looking for $\endgroup$ Commented Dec 8, 2013 at 16:58
  • $\begingroup$ I initially had a problem with it but this is because I was using variable names already taken $\endgroup$ Commented Dec 8, 2013 at 16:58
  • $\begingroup$ 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? $\endgroup$
    – ubpdqn
    Commented Dec 8, 2013 at 22:36
  • $\begingroup$ I would vote for it but I can't :o) $\endgroup$ Commented Dec 8, 2013 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.