12
$\begingroup$

Bug introduced in 7.0.1 or earlier, persists through 10.1


Consider the following integral:

Integrate[Log[a Cos[x]^2 + b Sin[x]^2], {x, 0, 2Pi}]

This takes a LONG time (this is Mathematica 10.0.1 on a Mac), and sometimes crashes the kernel, and sometimes returns:

$$ \frac{\left(4 i \pi \sqrt{\frac{a}{b}-1} \sqrt{-\frac{b}{a}} \sqrt{a b}+a \sqrt{-\frac{b}{a}} \sqrt{1-\frac{b}{a}} \log \left(-\frac{2 \left(\sqrt{a b}+b\right)}{a-b}\right)-a \log (2) \sqrt{-\frac{b}{a-b}}+\sqrt{-\frac{b}{a-b}} (b-a) \log \left(\frac{\sqrt{a b}-b}{a-b}\right)+b \log (2) \sqrt{\frac{b}{b-a}}\right) \sin ^{-1}\left(\frac{\sqrt{b}}{\sqrt{b-a}}\right)+\sqrt{b} \sqrt{b-a} \left(\left(\log \left(-\frac{\sqrt{a b}+b}{a-b}\right)-\log \left(\frac{\sqrt{a b}-b}{a-b}\right)\right) \sin ^{-1}\left(\sqrt{-\frac{b}{a-b}}\right)+2 \pi \left(-\log \left(\frac{\sqrt{a b}-b}{a-b}\right)-\log \left(-\frac{\sqrt{a b}+b}{a-b}\right)+\log (b)+2 i \pi -\log (4)\right)\right)}{\sqrt{b} \sqrt{b-a}} $$

Which is clearly wrong for, e.g., $a=2, b=3.$ (by the way, doing the integral with $2$ in place of $a$ and $3$ in place of $b$ does return the correct answer, albeit after much thought). On the other hand, the obviously equivalent:

Integrate[Log[1+a Cos[x]^2], {x, 0, 2Pi}] 

returns relatively quickly with a seemingly correct answer.

The question is whether this is a new (10.0.1) bug, or a perma-bug? I don't have access to older mathematicae to check.

$\endgroup$
8
  • $\begingroup$ The indefinite integral is evaluated instantaneously and it seems correct. The rest of the time, and possibly the bug, is due to the limits. $\endgroup$ Sep 26, 2014 at 15:00
  • $\begingroup$ Killed the kernel on the first try V9.0 Win64. You have missed the window for quick fixes in 10.01 so it will probably be another 18 months before a fix is released, but please report this to support. Maybe this will expose other related problems and they can all get fixed next time. $\endgroup$
    – Bill
    Sep 26, 2014 at 15:18
  • $\begingroup$ @b.gatessucks If you look at the discussion following Algoghi's answer, you will see that the answer is NOT correct. $\endgroup$
    – Igor Rivin
    Sep 26, 2014 at 15:53
  • 3
    $\begingroup$ @Bill Naively, I would have thunk that support would monitor this group... $\endgroup$
    – Igor Rivin
    Sep 26, 2014 at 16:28
  • 3
    $\begingroup$ @IgorRivin: there is WRI personal participating on this site, but I don't think they officially speak/act for WRI here and there most probably is not a well defined automatic process to create a bug report from errors reported to this site. So it does always make sense to report the bug to their support. It might even help to increase priority if it is sent more than once from different sources :-) $\endgroup$ Sep 27, 2014 at 19:52

2 Answers 2

7
$\begingroup$

I believe the main problem with original integration is due to Mathematica try to integrate with a and b being complex numbers. I have some doubts that it's even possible to analytically integrate with complex constants.

Integrate[Log[a Cos[x]^2 + b Sin[x]^2], {x, 0, 2 Pi}, Assumptions -> a > 0 && b > 0]
(* π (Log[(a b)/16] + 2 Log[(1 + Sqrt[a/b]) (1 + Sqrt[b/a])]) *)

Which is the correct, comparing to some NIntegrate.

NIntegrate[Log[Cos[x]^2 + 2 Sin[x]^2], {x, 0, 2 Pi}]
(* 5.69343 *)

π (Log[(a b)/16] + 2 Log[(1 + Sqrt[a/b]) (1 + Sqrt[b/a])]) /. {a -> 2, b -> 3} // N
(* 5.69343 *)
$\endgroup$
1
  • $\begingroup$ This can still be simplified to 4*Pi*Log[(Sqrt[a] + Sqrt[b])/2] $\endgroup$
    – Andreas
    Jan 23 at 21:08
2
$\begingroup$

Try this and see if it works.

int[x_] = Integrate[Log[a Cos[x]^2 + b Sin[x]^2], x];
int[2 Pi] - int[0]

I got the result very fast.

$\endgroup$
3
  • $\begingroup$ @JunhoLee Right, numerically this is the same result (I think) as what the definite integral returns, and, of course, just as wrong (since, in particular, integrating a real-valued function over a subset of the real line should return a real answer... $\endgroup$
    – Igor Rivin
    Sep 26, 2014 at 15:51
  • 2
    $\begingroup$ int[x_] = Integrate[Log[2 Cos[x]^2 + 3 Sin[x]^2], x]; int[2 Pi] - int[0] // N => -8.71034 + 19.7392 I $\endgroup$
    – Junho Lee
    Sep 26, 2014 at 15:59
  • 3
    $\begingroup$ NIntegrate[Log[2 Cos[x]^2 + 3 Sin[x]^2], {x, 0, 2 Pi}] => 5.69343 I think this is a bug. $\endgroup$
    – Junho Lee
    Sep 26, 2014 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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