6

Let's define

sum1N[m_] := Sum[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}] // Simplify

When m=4, we get

(3 NN)/8

However, if we compute directly with x=1, NN=4,

Cos[1.0 - 2 Pi/4]^4 + Cos[1.0 - 2*2 Pi/4]^4 + Cos[1.0 - 2*3 Pi/4]^4 + Cos[1.0 - 2*4 Pi/4]^4

we get result of

1.17318

which does not agree with

In[1]:= (3 NN)/8 /. NN -> 4.0

Out[1]= 1.5

Can someone explain this?

Ping Chia
  • 63
  • 4
  • Sum[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}] and Sum[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}, Assumptions -> m \[Element] PositiveIntegers] return the inputs in 13.3.1 on Windows 10. – user64494 Nov 15 '23 at 19:13

4 Answers4

6

An extended comment, not a definitive answer...

Treating this sum as just a mathematical concept rather than WolframLanguage code,

Sum[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}]

I suspect that there are certain values of NN for which the sum is undeterminable. But there may be a general solution, equivalent to (3 NN)/8 that works for "almost all" integers. I further suspect that WolframLanguage implementation of Sum applies some heuristics to arrive at that general solution. And so, I'm not convinced that we're seeing an actual bug so much as a good faith attempt to produce the "best" answer.

As for the expression being WolframLanguage code, your expectation is simply wrong. Starting with

sum1N[m_] := Sum[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}]

notice that x, n, and NN are unbound. That is, sum1N[4] will be evaluated without substituting any values in for those variables. One might expect that this simply wouldn't evaluate at all, but again, I suspect that Sum is implemented with rules that happen to handle this case for most inputs.

So, when you later substitute in values for x and NN and show that it should be

Cos[1.0 - 2 Pi/4]^4 + Cos[1.0 - 2*2 Pi/4]^4 + Cos[1.0 - 2*3 Pi/4]^4 + Cos[1.0 - 2*4 Pi/4]^4

that is a completely different calculation. If you had defined it this way,

sum1N[m_] := Sum[Cos[1 - 2 n Pi/4]^m, {n, 1, 4}]

then you would get exactly what you'd expect. In this definition, Sum doesn't need to apply any heuristics, it can just do the four iterations directly.

Furthermore, going back to

sum1N[m_] := Sum[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}]

you should realize that this is a very strange formulation. NN is the max value for the iterator n, but it also shows up in the body of the sum where it cannot possibly be bound to anything. Sum has the same structure as Table so replace Sum with Table to see what I mean:

sum1N[m_] := Table[Cos[x - 2 n Pi/NN]^m, {n, 1, NN}]

Now, if you try to evaluate sum1N[4], you'll get an immediate error message and the expression will fail to evaluate. Again, one might reasonably have expected Sum to do the same thing, but Sum was trying to be "nice" by applying its extensive set of heuristics to give you something rather than nothing. But it's up to you to make sure that you provide meaningful inputs. It's the whole "garbage in, garbage out" thing.

lericr
  • 27,668
  • 1
  • 18
  • 64
5

GenerateConditions option could give a hint:

sum1N[m_] := 
 Sum[Cos[x - 2  n  Pi/NN]^m, {n, 1, NN}, GenerateConditions -> True] //
   Simplify

sum1N[4]

ConditionalExpression[(3 NN)/8, NN [Element] Integers && 2/NN [NotElement] Integers && 4/NN [NotElement] Integers && NN >= 1]

halmir
  • 15,082
  • 37
  • 53
  • 1
    The problem with this definition is that you get Undefined for NN=1,2,4. But the sum is well defined at such cases. – azerbajdzan Nov 15 '23 at 16:41
2

You can avoid this confusion by defining

Clear @ sum1N

sum1N[m_, nn_, x_] := Sum[Cos[x - 2 n Pi/nn]^m, {n, 1, nn}] // Simplify

Now you can evaluate symbolically

sum1N[4, 4, x]

(3 + Cos[4*x])/2

sum1N[m, 4, x]

(-Cos[x])^m + Cos[x]^m + (-Sin[x])^m + Sin[x]^m

or numerically

sum1N[4, 4, 1.]

1.17318

or map over several values

sum1N[4, 4, #] & /@ {1., 1.5, 2.}

{1.17318, 1.98009, 1.42725}

eldo
  • 67,911
  • 5
  • 60
  • 168
  • 1
    Instead of "You can avoid this confusion by defining..." I would say "You can avoid this bug by defining". Definitely it is not a confusion but a bug. – azerbajdzan Nov 15 '23 at 12:02
2

It is a bug. For some NN it may be true but not for all.

Sum[Cos[x - 2 n Pi/NN]^4, {n, 1, NN}] // Simplify
(* (3 NN)/8 *)

Table[{NN, Sum[Cos[x - 2 n Pi/NN]^4, {n, 1, NN}] == 3 NN/8}, {NN, 0, 
   10}] // FullSimplify

({ {{0, True}}, {{1, 8 Cos[x]^4 == 3}}, {{2, 8 Cos[x]^4 == 3}}, {{3, True}}, {{4, Cos[4 x] == 0}}, {{5, True}}, {{6, True}}, {{7, True}}, {{8, True}}, {{9, True}}, {{10, True}} })

As can be seen for NN being one of 1,2,4 the identity does not hold.

azerbajdzan
  • 15,863
  • 1
  • 16
  • 48
  • Given @halmir's answer with GenerateConditions, I don't think you can assert so definitively that this is a bug with the data we have so far. – lericr Nov 15 '23 at 16:40
  • @lericr: See my comment to his answer. – azerbajdzan Nov 15 '23 at 16:42
  • 2
    Results that are generically correct are not regarded as bugs. Hence the implicit advice in the response from @halmir" to use GenerateConditions->True when such anomalies arise. Also it is fair to expect results with jump discontinuities when the summation endpoint is also used as a parameter in the summand. Stated differently, if you separate into parameter nn1 and summation bound nn2, then the analytic result for the purely symbolic summation has a point of indeterminacy at {4,4}. – Daniel Lichtblau Nov 15 '23 at 16:50