# Mathematica integration failure - new or old?

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.

• The indefinite integral is evaluated instantaneously and it seems correct. The rest of the time, and possibly the bug, is due to the limits. – b.gates.you.know.what Sep 26 '14 at 15:00
• 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. – Bill Sep 26 '14 at 15:18
• @b.gatessucks If you look at the discussion following Algoghi's answer, you will see that the answer is NOT correct. – Igor Rivin Sep 26 '14 at 15:53
• @Bill Naively, I would have thunk that support would monitor this group... – Igor Rivin Sep 26 '14 at 16:28
• @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 :-) – Albert Retey Sep 27 '14 at 19:52

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 *)


Try this and see if it works.

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


I got the result very fast.

• @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... – Igor Rivin Sep 26 '14 at 15:51
• int[x_] = Integrate[Log[2 Cos[x]^2 + 3 Sin[x]^2], x]; int[2 Pi] - int // N => -8.71034 + 19.7392 I – Junho Lee Sep 26 '14 at 15:59
• NIntegrate[Log[2 Cos[x]^2 + 3 Sin[x]^2], {x, 0, 2 Pi}] => 5.69343 I think this is a bug. – Junho Lee Sep 26 '14 at 16:04