# How to apply Simplify only to elements of list that cause specific Warning

I have a set of replacement rules I use to analytically compute nested integrals of very long symbolic expressions since Integrate literally takes forever. To do that I use List@@ to transform a sum into a list of summands and apply my rules to each element of the list list. An example that causes problems at later evaluation is e.g.

list = {Cos[x], Sin[x], -( Cos[t3 (d1 - d2)]/(1728 tg^3 (d1 - d2)^2)) + Cos[t3 (d1 - d2)]/(864 tg^3 (d1 - d2) (-d1 + d2)) +  Cos[t3 (d1 - d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) - 2 (-d1 + d2))) -  Cos[t3 (d1 - d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) + 2 (-d1 + d2))) + Cos[3 t3 (d1 - d2)]/(1728 tg^3 (d1- d2)^2) - Cos[t3 (d1 - d2) - 2 t3 (-d1 + d2)]/(864 tg^3 (d1 - d2) (2 (d1- d2) - 2 (-d1 + d2))) - Cos[t3 (d1 - d2) + 2 t3 (-d1 + d2)]/(864 tg^3 (d1 - d2) (-d1 + d2)) +  Cos[3 t3 (d1 - d2) + 2 t3 (-d1 + d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) + 2 (-d1 + d2))) + (t3 Sin[t3 (d1 - d2)])/(432 tg^3 (d1 - d2))};


For later computation I do for instance need list[] /. {d1 -> 0.045, d2 -> -0.35} which will evaluate to Indeterminate since there is a 1/0 component inside due to the way the previous replacement rules work. But observe that

(Simplify@list[]) /. {d1 -> 0.045, d2 -> -0.35}


works fine and yields the correct result (compared to Integrate result for that specific element), since the terms causing ComplexInfinity in fact cancel out. Note that I cannot include Simplify in my replacement rules because there are only few terms having that particular problem, simplifying the rest is not necessary and takes forever as well.

My goal is to perform a check, e.g. apply the replacement {d1 -> 0.045, d2 -> -0.35} to my list list, look for Indeterminate warning and only apply Simplify to those terms that cause the warning. The simplified terms should still depend on d1,d2 so the replacement of those should only be used to find critical list elements.

How can this be achieved at all? In principle, I would prefer a solution where you would directly apply Simplify if the warning is recognized to avoid multiple calls of the list. However, since I am clueless on how to do it at all I will be happy with any solution, anyway.

Edit To avoid misunderstandings: For this example, Simplify yields a result almost instantly. That is not the case for my full set of lists/terms. There seems to be no chance to Simplify the list - even with appropriate assumptions - in reasonable timescales.

Edit 2 As pointed out in a comment below, Szabolcs' suggestion to Map a function like checkInt[expr_, rule_] := Quiet[Check[expr /. rule, Simplify[expr]]]; over list did indeed work (i) at all and (ii) in reasonable time. Compared to trying to simplify the whole list - which has not even finished after multiple days - I was now able to achieve what I want in roughly 35 minutes.

• Map this? f[expr_] := Module[{result}, result = expr /. rules; If[result == Indeterminate, result = Simplify[expr] /. rules]; result]. Or this? Check[# /. rules, Simplify[#] /. rules] &. Check can check for specific messages only and Quiet can silence them. – Szabolcs Jul 27 '15 at 7:18
• You may be interested in this rule based integrator too. – Szabolcs Jul 27 '15 at 7:18
• @Szabolcs Thanks for your fast reply. In fact, Map[f,list] does not work since it seems that the Indeterminate case is not recognized properly. However, your second suggestion using Check works perfectly fine! If you post it as an answer, I can accept. Also, many thanks for pointing to RUBI which may definitely be of interest for me :) – Lukas Jul 27 '15 at 7:31

## 1 Answer

It is not exactly what you asked about, but did you try this:

  list = {Cos[x],
Sin[x], -(Cos[t3 (d1 - d2)]/(1728 tg^3 (d1 - d2)^2)) +
Cos[t3 (d1 - d2)]/(864 tg^3 (d1 - d2) (-d1 + d2)) +
Cos[t3 (d1 - d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) -
2 (-d1 + d2))) -
Cos[t3 (d1 - d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) +
2 (-d1 + d2))) +
Cos[3 t3 (d1 - d2)]/(1728 tg^3 (d1 - d2)^2) -
Cos[t3 (d1 - d2) -
2 t3 (-d1 + d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) -
2 (-d1 + d2))) -
Cos[t3 (d1 - d2) +
2 t3 (-d1 + d2)]/(864 tg^3 (d1 - d2) (-d1 + d2)) +
Cos[3 t3 (d1 - d2) +
2 t3 (-d1 + d2)]/(864 tg^3 (d1 - d2) (2 (d1 - d2) +
2 (-d1 + d2))) + (t3 Sin[
t3 (d1 - d2)])/(432 tg^3 (d1 - d2))};


Then

 AbsoluteTiming[(Simplify /@ list)]

(*   {0., {Cos[x],
Sin[x], -((
Sin[(d1 - d2) t3] (4 (-d1 + d2) t3 + Sin[2 (d1 - d2) t3]))/(
1728 (d1 - d2)^2 tg^3))}}   *)


As we see, it takes three times nothing, at list within this example. And this:

  AbsoluteTiming[(Simplify /@ list) /. {d1 -> 0.045, d2 -> -0.35}]

(*  {0., {Cos[x],
Sin[x], -((0.00370904 Sin[0.395 t3] (-1.58 t3 + Sin[0.79 t3]))/
tg^3)}}   *)


takes four times nothing.

Have fun!

• In fact, for this particular example Simplify yields a result instantly. However, the actual terms I deal with are way more complicated and the lists have around 400.000 elements where each element is way more comlicated than what is shown in the question. I have been running Simplify naively with appropriate assumptions for 4 days now, without end in sight. But anyways, thanks for your contribution :) – Lukas Jul 27 '15 at 7:36
• If you map Simplify  onto your list and it takes much time, than the same is to be expected, if you map the combination Simplify[ReplaceAll[#, {d1 -> 0.045, d2 -> -0.35}]]& . Clearly that not the terms like Sin[x] or Cos[x] give rise to such a timing. This may be the indication that there are terms (those creating your problems) that are much too complex themselves. – Alexei Boulbitch Jul 27 '15 at 7:57
• Indeed. And this is why I only want to Simplify those few terms that cause trouble if you map ReplaceAll[#,{d1->0.045,d2->-0.35}] or any other values of d1,d2. Purely replacing does not take that much time, so first checking for warning and only simplifying if a warning occurs (few cases) should not take that much time, I hope. Edit: Basically, there are only Cos,Sin terms involved with lots of different arguments and (longish) prefactors depending on more symbols than shown here. That is why Simplify probably takes so long... Just my guess – Lukas Jul 27 '15 at 8:01