Possible bug in Mathematica?

I'm running Mathematica 7.0.0 and came across this weird calculation (lines 1-14).

f[x_, r_] := x*(r - x)*Exp[-Pi*I/(2 r)*x^2];
g[x_, r_] := Exp[-Pi*I/(2 r)*x^2]*(r - Pi*I*x^2 - 2*x + Pi*I/r*x^3);
int[r_] :=
FullSimplify[
Integrate[f[x, r], {x, 0, r}] +
Integrate[-Sum[Sin[2*Pi*n*x]/(n*Pi), {n, 1, Infinity}]*
g[x, r], {x, 0, r}], r > 0 && Element[r, Integers]];
int[r] // N
int[12] // N

Output:
0.
2.64102 - 39.4256 I


So I've got two functions, $f$ and $g = f'$, and I'm interested in a certain expression (as shown). However, the general expression that Mathematica 7 gives for positive integers $r$ is $0$, whereas it gives something non-zero for particular integers $r$ (as it should).

Can somebody with Mathematica 8 verify this or tell me why I shouldn't be surprised? Thanks.

-
Well, I get the same results with 8.0.1.0 on a Mac. –  castal Nov 17 '11 at 16:48
@Sasha: I'm not quite sure what you mean; the expression is a sum of two integrals, neither of which are constant in $r$. –  fuglede Nov 17 '11 at 17:09
@SørenFugledeJørgensen Yes, you are right, of course. The value of the integral depends on r, my bad. As castal said, v 8.0.1, but also v 8.0.4 produce the same result as v7. –  Sasha Nov 17 '11 at 17:17
Thanks to both of you; I'll report it. –  fuglede Nov 17 '11 at 17:22
Also, Integrate doesn't do anything with an integer assumption (see the comments), so you have to watch out for them yourself. –  rcollyer Nov 17 '11 at 18:48
show 6 more comments

1 Answer

First note that $h(x) = \sum_{n=1}^\infty \frac{\sin(2\pi n x)}{ n \pi} = \frac{1}{2} - \{ x \}$.

Second, the first integral is easy to compute integrating by parts: $$\mathcal{I}_f = \int_0^r f(x,r) \mathrm{d} x = \int_0^r x (r-x) \exp\left(-i \frac{\pi}{2} \frac{x^2}{r}\right) \mathrm{d} x = i \frac{r}{\pi } \left( \sqrt{r} C\left(\sqrt{r}\right)-i \sqrt{r} S\left(\sqrt{r}\right)- r\right)$$

The second integral will have to be split: $$\begin{eqnarray} \mathcal{I}_g &=& \int_0^r h(x) g(x,r) \mathrm{d} x = \sum_{k=0}^{r-1} \int_0^1 h(k+x) g(k+x,r) \mathrm{d} x = \sum_{k=0}^{r-1} \int_0^1 \left( \frac{1}{2} -x \right) g(k+x,r) \mathrm{d} x \\ &=& \phantom{+} \sum_{k=0}^{r-1} \frac{1}{2\pi} (k-r+1)(\pi(k+1)-2 i r) \exp\left( -i \frac{\pi}{2} \frac{(k+1)^2}{r} \right) \\ &\phantom{=}& + \sum_{k=0}^{r-1} \frac{1}{2\pi} (k-r)( \pi k + 2i r) \exp\left( -i \frac{\pi}{2} \frac{k^2}{r} \right) \\ &\phantom{=}& + \sum_{k=0}^{r-1} \frac{1+i}{2 \pi} r^{3/2} \left( \operatorname{erf}\left( \frac{1+i}{2} \sqrt{\frac{\pi}{r}} k \right) - \operatorname{erf}\left( \frac{1+i}{2} \sqrt{\frac{\pi}{r}} (k+1) \right) \right) \end{eqnarray}$$

Now, with explicit evaluations of the integrals for explicit $r$ neither Fresnel integrals, nor error functions do appear, so they must be cancelling. One can probably show they do using integral representations. Anyway, assuming they do we have a "closed-form" expression for the expression you were computing:

$$\mathcal{I}_f - \mathcal{I}_g = \sum_{k=1}^r k (r-k) \exp\left( -i \frac{\pi}{2} \frac{k^2}{r} \right)$$

You can verify with explicit computations that the formula is correct:

In[94]:= FullSimplify[
Table[int[r] - Sum[Exp[-((I*k^2*Pi)/(2*r))]*(r - k)*k, {k, 1, r}],
{r, 1, 8}]]

Out[94]= {0, 0, 0, 0, 0, 0, 0, 0}

-
Thanks. Actually, the sum in the end was the one I was trying to approximate, going the other way around, as the integrals would hopefully be easier to manage. But what must be cancelling? We know that the integrals will not. I find it somewhat peculiar that Mathematica acknowledges that the sum of the integrals equals the sum, given that it also believes that int[$r$] = 0 for all $r$. –  fuglede Nov 17 '11 at 21:25
add comment