17
$\begingroup$

Bug introduced in 9.0, persisting through 12.3.1


I was trying to evaluate the following sum.

$$ \frac{2}{m}\sum_{\substack{\text{odd }k\\1\leq k\leq m-1}} f\left(\frac{m+2+\sqrt{m^2-4k+4}}{2}\right)+f\left(\frac{m+2-\sqrt{m^2-4k+4}}{2}\right). $$

And I wrote the following Mathematica code.

2/m Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])] +
        f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}]

Then I tested it with $f(x)=x^2$ and $m=60$. I defined

f[x_] = x^2

and evaluated

2/m Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])] +
     f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}] /.
  m -> 60 // N

which gave

(* 3664. *)

However, if I split the sum into two parts, it ends with a very different value:

 2/m Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}] +
 2/m Sum[f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}] /. m->60 //N

gives

(* 4.6048 + 1.12029*10^-13 I *)

What is the problem here? There seems to be no problem if $f(x)=x^p$ and $p$ is fractional. I tried $p=1.1$, $1.5$, $2.1$, etc., and both answers agree. Looks like the problem occurs when $p$ is an integer...

$\endgroup$
4
  • $\begingroup$ I still get the discrepancy on 11.0.0 (Windows). You see the bug fixed on your side? $\endgroup$
    – user31159
    Commented Sep 11, 2016 at 9:55
  • $\begingroup$ @Xavier Sorry, the bug of the main thread post still exists... however I cannot reproduce the bug of the last piece of code in the last reply... $\endgroup$
    – user58955
    Commented Sep 13, 2016 at 0:40
  • 1
    $\begingroup$ The bug is introduced in v9: i.sstatic.net/18iZW.png $\endgroup$
    – xzczd
    Commented Apr 12, 2020 at 2:13
  • $\begingroup$ In fact the bug is gone when x^2 becomes x^2.000001 (wait for it). $\endgroup$ Commented Jul 2, 2022 at 22:20

2 Answers 2

16
$\begingroup$

There is a symbolic calculation bug in there:

Let's define:

plus[k_, m_]  := f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])];
minus[k_, m_] := f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])];

While

(With[{m = 60}, Sum[minus[k, m], {k, 1, -1 + m, 2}]] // N) ==
               (Sum[minus[k, m], {k, 1, -1 + m, 2}] /. m -> 60 // N)

(* True *)

on the other hand:

(With[{m = 60}, Sum[plus[k, m], {k, 1, -1 + m, 2}]] // N) ==
               (Sum[plus[k, m], {k, 1, -1 + m, 2}] /. m -> 60 // N)

(* False *)

So the term with the HurwitzZeta[ ..] functions is in error.

$\endgroup$
4
  • $\begingroup$ This example has been fixed in MMA 11.2.0. I got True for the 'on the other hand' expression. But the problem in the original problem still exists $\endgroup$
    – user58955
    Commented Sep 19, 2017 at 14:50
  • 1
    $\begingroup$ With v12.1 on a Mac I get True for both tests. $\endgroup$
    – Bob Hanlon
    Commented Apr 11, 2020 at 18:02
  • $\begingroup$ @BobHanlon Something strange happens here. If I start Mathematica, with a clean kernel, I run this code, I get True for both. However, if I run the code in my original post first, then I run the code in this reply, I get True for the first one and False for the second one. $\endgroup$
    – user58955
    Commented Apr 12, 2020 at 0:12
  • 2
    $\begingroup$ @user58955 - On checking, it depends on whether f is defined. Just running code above (f not defined) gives True for both. Including f[x_] = x^2; gives True and False respectively. $\endgroup$
    – Bob Hanlon
    Commented Apr 12, 2020 at 1:45
10
$\begingroup$

This is interesting! Here's a partial answer (so more of a long comment):

Clear[f]
f[x_] = x^2;
Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}]

and the output is:

enter image description here

whereas

Sum[f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}]

doesn't evaluate (i.e. it returns itself), and

Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])] + f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}]
(* -(1 + Floor[1/2 (-2 + m)]) (-2 - 2 m - m^2 + 2 Floor[1/2 (-2 + m)]) *)

You will note that if you set the terms with the HurwitzZeta function to zero, then these two expressions are identical up to a factor of 1/2. The HurwitzZeta functions would cancel each other out if the arguments for two of those functions didn't have the Floor[m/2] pieces in them.


Now, if we evaluate the sums symbolically first (the 2/m doesn't make a difference), we get

Clear[f, f1]
f1 = Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])] + 
         f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}];
f1 = f1 /. m -> 60 // Expand;
f[x_] = x^2;
f1 // N
(* 109920. *)

and

Clear[f]
f1 = Sum[f[1/2 (2 + m + Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}] + 
  Sum[f[1/2 (2 + m - Sqrt[4 - 4 k + m^2])], {k, 1, -1 + m, 2}];
f1 = f1 /. m -> 60 // Expand;
f[x_] = x^2;
f1 // N
(* 109920. *)

which indicates to me that they should be equal.


On the other hand,

Sum[(1/2 (2 + 60 + Sqrt[4 - 4 k + 60^2]))^2 + (1/2 (2 + 60 - Sqrt[4 - 4 k + 60^2]))^2, {k, 1, -1 + 60, 2}] // N
Sum[(1/2 (2 + 60 - Sqrt[4 - 4 k + 60^2]))^2, {k, 1, -1 + 60, 2}] +
   Sum[(1/2 (2 + 60 - Sqrt[4 - 4 k + 60^2]))^2, {k, 1, -1 + 60, 2}] // N
(* 109920 *)
(* 71.3744 *)

I perhaps suspect a bug. However, I also suspect that if the second sum evaluated, it would have terms that would cancel the HurwitzZeta functions in the first term, so perhaps it's instead some sort of problem there.

$\endgroup$
3
  • $\begingroup$ The bug in the 'on the other hand' part seems fixed in MMA 11.0.0 $\endgroup$
    – user58955
    Commented Sep 13, 2016 at 0:42
  • $\begingroup$ In MMA 11.2.0, the second expression in the 'on the other hand' part evaluates to 138.144061345985... $\endgroup$
    – user58955
    Commented Sep 19, 2017 at 14:52
  • $\begingroup$ @user58955. Actually, it does in my copy of V10.0.1, too, so I'm not sure what version I used in this answer. Probably V8. $\endgroup$
    – march
    Commented Sep 19, 2017 at 15:36

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