Message Boards Message Boards

0
|
10848 Views
|
15 Replies
|
9 Total Likes
View groups...
Share
Share this post:

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

Posted 9 years ago

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

POSTED BY: Gary Palmer
15 Replies

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

POSTED BY: Frank Kampas

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

POSTED BY: Frank Kampas

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.

POSTED BY: Marco Thiel
Posted 9 years ago

All very instructive. Thanks.

POSTED BY: Gary Palmer

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.

POSTED BY: Marco Thiel

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.

POSTED BY: Marco Thiel

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

POSTED BY: Daniel Lichtblau

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?

POSTED BY: Marco Thiel

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.

POSTED BY: Marco Thiel

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.

POSTED BY: Daniel Lichtblau

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}]

enter image description here

POSTED BY: Marco Thiel

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.

POSTED BY: Daniel Lichtblau

Dear Daniel,

ok, I understand the problem, I think.

Thanks,

Marco

POSTED BY: Marco Thiel
Posted 9 years ago
POSTED BY: Gary Palmer

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.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract