# Why does Mathematica give the wrong answer when integrating?

Bug introduced in 8.0 or earlier and fixed in 9.0.0

I integrate

Integrate[Exp[I Cos[b - c]] Cos[b], {b, 0, 2 Pi}]


Mathematica gives:

2 I Pi BesselJ[1, 1]


Which is indepedent on $c$. However, the result is NOT independent on $c$.

The numerical value of what Mathematica gives is 0. + 2.76492 I while

NIntegrate[Exp[I Cos[b - 0.5]] Cos[b], {b, 0, 2 Pi}]


Gives

1.16655*10^-10 + 2.42645 I


Certainly not the same. If we multiply the value Mathematica gives with $\cos(0.5)$ the result is the same. What is wrong?

Looks like a bug that ran off in the development version of Mathematica.

In:= Integrate[Exp[I Cos[b - c]] Cos[b], {b, 0, 2 Pi}]//InputForm

Out//InputForm= (2*I)*Pi*BesselJ[1, 1]*Cos[c]

In:= %/.c->.5

Out= 0. + 2.42645 I

• @JonasTeuwen he's referring to the fact that the bug no longer shows up in the development version, i.e. a version that hasn't been released to the public, yet. Mar 8, 2012 at 19:55
• Numeric checks like the one you did seem like a good way to go about the checking process. Mar 8, 2012 at 20:13
• Right... That implies that I have to check a few hundred of them with many parameters :-). Of course, any program can have a bug, but it still is very annoying. Mar 8, 2012 at 20:17
• @Rolf Would you prefer that we not comment on bugs? That's the only alternative to what I did, as you are well aware. Mar 10, 2012 at 20:43
• @Daniel I find all the feedback about bugs encouraging. Even though your answer doesn't fix the problem here and now, it is good to see that these problems get paid attention to and that fixes are on the way. I'd appreciate if WRI people kept commenting on bugs in public like this. Mar 11, 2012 at 21:08

This bug appears to have been known for over a year now! See the mathGroup archive

Edit

To explain why I consider these bugs to be equivalent, I'll re-write the two integrals using the integration variable x. In the present question, we have $$I_1(c)=\int_0^{2\pi}\exp(i\cos(x-c))\cos x\,dx$$

On the other hand, the MathGroup post I linked here considers the integral $$I_2(c)=\int_{-\pi}^{\pi}\exp(i\cos(x-c))\exp(ix-ic)\,dx$$ where I specialized to the case giving the same Bessel argument in the result. Pulling out the constant factor, using the periodicity of the integrand and making a substitution of variables, one finds $$I_2(c)=e^{-ic}\left[I_1(c)+i\, I_1(c-\frac{\pi}{2})\right]$$

Therefore, if Wolfram fixed the bug in the calculation of $I_2$ but still gets an incorrect result for $I_1$, that fix was itself incorrect. This argument generalizes to integer Bessel indices other than 1 (which is what this question has).

Finally, the question is whether it is a bug fix to return the integrals unevaluated. I would argue that because these are such ubiquitous integrals, Mathematica should definitely know what to do with them. But I may not have all the information as to why this bug is so hard to fix... I just wanted to briefly clarify my thinking in case anyone cares.

• They actually seem to be opposites in some sense, so I doubt it's the same bug. Mar 8, 2012 at 22:08
• No - it's the exact same thing.
– Jens
Mar 8, 2012 at 22:11
• Jonas is getting <constant>*<BesselJ> instead of an expected <constant>*<BesselJ><f[otherVariable]>. The MathGroup poster is getting <constant>*<BesselJ><f[otherVariable]> instead of <constant>*<BesselJ>. They may be related, but that's enough to classify them as separate bugs in my book. Mar 8, 2012 at 22:21
• The fix to one was to disable it from giving any result. This may be why the other will in a future release give the correct result. But its not as simple one change causing BOTH to give a correct result. Mar 9, 2012 at 16:01
• @DanielLichtblau Not sure if you noticed the edit here. Could you comment on this? Mar 12, 2012 at 0:10