Simplify appears to give different results

I have an issue with Simplify (and FullSimplify) giving a different numerical expression to the pre-Simplified expression. The function is an alternating sum of trigonometric quantities:

f[n_, x_] := Sum[Sum[(-1)^j Sin[(j-1)(n-2)Pi/n], {j, x, i-1}] *
Sum[(-1)^j Sin[(j-1)(n-2)Pi/n], {j, 2, i}], {i, 2, n-1}]

If I evaluate this at $$(n, x) = (6, 3)$$ for instance, my answer is 2.25. But if I first pass this expression to Simplify, e.g.

g[n_, x_] := Simplify[f[n, x]]

then evaluate, my answer is 1.5.

Which answer should I trust and what's the source of the issue? I should add that I'm using an older version of Mathematica (10.3).

I don't think that what you're seeing is a bug. The problem is that Sum implicitly assumes that the summation bounds are ordered and differ by an integer amount (mentioned here). The following example illustrates how this might cause issues:

(* evaluate sum symbolically *)
Sum[i, {i, a, b}]
(* -(1/2) (-1 + a - b) (a + b) *)

(* insert problematic values with a>b into symbolic result *)
Sum[i, {i, a, b}] /. {a -> 4, b -> 2}
(* -3 *)

(* insert values before evaluating *)
Unevaluated@Sum[i, {i, a, b}] /. {a -> 4, b -> 2}
(* 0 *)

(* generate conditions to verify input *)
Sum[i, {i, a, b}, GenerateConditions -> True] /. {a -> 4,
b -> 2}
(* Undefined *)

As you can see (and as noted in the documentation page linked above), GenerateConditions can be used to protect against this problem. Note however that the result will simply be Undefined in those cases instead of 0.

Now, how does this apply to the example in the question? The issue lies in the first inner sum:

Sum[(-1)^j Sin[(j-1)(n-2)Pi/n], {j, x, i-1}]

For $$i=6,x=3$$, we are in the case where the lower bound is larger than the upper bound, so symbolic evaluation will produce incorrect results. Adding GenerateConditions->True tells us that something is indeed wrong:

f[n_, x_] = Sum[
Sum[
(-1)^j Sin[(j - 1) (n - 2) Pi/n],
{j, x, i - 1},
GenerateConditions -> True
]*Sum[
(-1)^j Sin[(j - 1) (n - 2) Pi/n],
{j, 2, i},
GenerateConditions -> True
],
{i, 2, n - 1},
GenerateConditions -> True
];

f[6, 3]
(* Undefined *)

In summary, be careful with sums with symbolic bounds, and check whether the lower bound is less than the upper one. Unless there really is a bug somewhere, the correct result should be 9/4, since there, the sums are done with the explicit values instead of symbolically.

• Thanks - do you happen to know if Mathematica is able to handle (in Simplify) empty summations, for instance through another function? Or should I always ensure that the sum is never empty to avoid such problems? – Riley Sep 9 at 10:56

I can't reproduce the first part you say. On V 12 on windows 10, I get 2.25 in both cases (it has to be, as simplifying 2.25 to 1.5 would be a really bad bug)

But what I see, is that when changing the definition from delayed := to =, then now Mathematica gives 1.5

And this I think must be a bug.

Code

ClearAll[n, x, j, i, f, g];
f[n_, x_] :=
Sum[Sum[(-1)^j Sin[(j - 1) (n - 2) Pi/n], {j, x, i - 1}]*
Sum[(-1)^j Sin[(j - 1) (n - 2) Pi/n], {j, 2, i}], {i, 2, n - 1}];
f[6, 3]
(* 9/4 *)

And now (this will take few seconds, since it is immediate evaluation)

ClearAll[n, x, j, i, f, g];
f[n_, x_] =
Sum[Sum[(-1)^j Sin[(j - 1) (n - 2) Pi/n], {j, x, i - 1}]*
Sum[(-1)^j Sin[(j - 1) (n - 2) Pi/n], {j, 2, i}], {i, 2, n - 1}];
f[6, 3]
(* 3/2 *)

btw, the same issue shows up on 11.3 as well.

• Thanks for your help - do you know which expression is correct / more reliable? I suppose I should probably do a computation myself but I don't really trust myself... in any event, I should add that following your tests, I get a different answer if I do: g[n_, x_] := Simplify[f[n,x]] which gives me g[6, 3] = 9/4, but if I do Simplify[f[n,x]] then copy the input from the console and paste it into g[n_, x_] := (Ctrl+V), then I get 3/2. Is this also a bug I'm guessing? – Riley Sep 9 at 8:01
• In Maple sum gives 3/2 and add gives 9/4. – Alx Sep 9 at 8:37