Cos[ Cos[\[Theta]]] Cosh[ Sin[\[Theta]]] slow in Integrate[]

Why does the function in the title take over 2 minutes to Integrate?

AbsoluteTiming[
Integrate[
Cos[ Cos[[Theta]]] Cosh[ Sin[[Theta]]], {[Theta], 0, 2 Pi}]]

Mathematica 10, iMac w. OS 10.9.3

How long did it take you to do it without Mathematica?

I see what you’re saying. Maple 18 did it in 265 msec.

Hi,

I know that this is not nice and defeats the purpose, but because numerical integration is quite fast:

AbsoluteTiming[NIntegrate[Cos[Cos[Theta]] Cosh[Sin[Theta]], {Theta, 0, 2 Pi}]]

finishes in 0.006338 secs. The problem is that it does not give the symbolic solution. In this case, because the result is simple this dirty trick works:

AbsoluteTiming@WolframAlpha[ToString[NIntegrate[Cos[Cos[Theta]] Cosh[Sin[Theta]], {Theta, 0, 2 Pi}]], {{"PossibleClosedForm", 1}, "ComputableData"}]

runs for less than 5 seconds including the request to WolframAlpha. As I said, this does not give the proper symbolic solution and does not generalise to other problems.

Cheers,

M.

PS: Ups, I just noticed a better solution:

AbsoluteTiming[Integrate[TrigToExp[Cos[Cos[Theta]] Cosh[Sin[Theta]]], {Theta, 0, 2 Pi}]]

gives the symbolic result and runs slightly longer than a second.

All very instructive. Thanks.

Still a bit faster:

AbsoluteTiming[Integrate[Simplify@TrigToExp[Cos[Cos[Theta]] Cosh[Sin[Theta]]], {Theta, 0, 2 Pi}, PrincipalValue -> True]]

takes about a third of a second.

Cheers,

M.

And if we finally use all of the additional tweaks that Integrate offers:

AbsoluteTiming[Integrate[Simplify@TrigToExp[Cos[Cos[Theta]] Cosh[Sin[Theta]]], {Theta, 0, 2 Pi}, PrincipalValue -> True, GenerateConditions -> False]]

we get down to a quarter of a second - on my current machine that is nearly 700 times faster than the “naive integration”.

Cheers,

M.

GenerateConditions->False is the setting that matters in this instance.

I don’t understand why but even without GenerateConditions->False and just the PrincipalValue it gains considerably. Adding both has a positive effect on the calculation time on my machine.

Cheers,

M.

PS: Why is it that TrigToExp speeds the process up so much? I understand that the Exp function might speed it up, but I would have assumed that MMA does that automatically. Is there an algorithmic advantage of not doing this?

Oh, I think I see what you say. The TrigToExp has no effect if the GenerateConditions->False is set. In fact not using TrigToExp is not faster:

AbsoluteTiming[Integrate[Cos[Cos[Theta]] Cosh[Sin[Theta]], {Theta, 0, 2 Pi}, PrincipalValue -> True, GenerateConditions -> False]]

is on average about 0.22 s on my machine rather than 0.27 with the TrigToExp.

Cheers,

M.

One caveat: some combinations of tweaks might give a wrong result. The variant below claims pi instead of 2pi.

In[714]:= AbsoluteTiming[
 Integrate[
  Simplify@TrigToExp[Cos[Cos[Theta]] Cosh[Sin[Theta]]], {Theta, 0, 
   2 Pi}, GenerateConditions -> False]]

(* Out[714]= {0.527774, \[Pi]} *)

Also, some of the timing discrepancies can have to do caching of intermediate results.

Dear Daniel,

why exactly is that? Giving Pi instead of 2 Pi is kind of worse than having to wait for 3 minutes for the result…

Cheers,

M.

PS: Also, the function seems to be quite well behaved:

Plot[Simplify@TrigToExp[Cos[Cos[Theta]] Cosh[Sin[Theta]]], {Theta, 0, 2 Pi}]

GenerateConditions->False (an overloaded option if ever there was one) also in effect turns off or at least weakens checking for path singularities. The antiderivative has a couple of them.

Dear Daniel,

ok, I understand the problem, I think.

Thanks,

Marco

This thread has been fun to follow. Why is it that the following produces Missing[“NotAvailable”], and that only after over 3 minutes? I removed the “N” from NIntegrate[] in the second panel of Marco’s first post.

AbsoluteTiming@WolframAlpha[ToString[Integrate[Cos[Cos[Theta]] Cosh[Sin[Theta]], {Theta, 0, 2 Pi}]], {{“PossibleClosedForm”, 1}, “ComputableData”}]

The idea of the command is to do the numerical integration, which is fast, and the ask Wolfram Alpha to guess what the closed form of the solution might be. The N from NIntegrate is therefore quite important as it instructs Mathematica to use numerical integration instead of symbolic integration. If you remove the N, Mathematica tries to symbolically integrate, which - without any additional options such as GenerateConditions->False - takes quite some time as you report in your original post.

I guess that the WolframAlpha request times out after three minutes.

Cheers,

M.