# Using list specification in NSum

The below gives the right answer for summing values from a list (as opposed to a mathematical function) but throws out a warning stating that "k cannot be used for list specification".

list = {1, 2, 3, 4, 5};
NSum[list[[k]], {k, 1, 4}]

Why does it work at all then, and what's the right way to do it?

• Use Sum rather than NSum: Sum[list[[k]], {k, 1, 4}]; or list // Most // Total Dec 3, 2017 at 22:00
• Ah I knew I should have specified more ... I need NSum because I want to use WynnExtrapolation as Method, which Sum does not support.
– MvP
Dec 3, 2017 at 22:17
• Please provide a minimal example of where WynnExtrapolation is required. Dec 3, 2017 at 22:21
• I think the precise question is: is there no way to get NSum to work on a list? The MWE list of coefficients I have in mind is list = Table[ SeriesCoefficient[Series[BesselJ[0, t]^2, {t, 0, 10}], k], {k, 0, 10, 2}]
– MvP
Dec 3, 2017 at 22:45
• NSum worked with the List, it just gave a warning (which can be suppressed by Quiet or turning Off the specific message). Dec 3, 2017 at 23:20

Adapted from the docs for NSum:

f[n_] := Exp[-1/(n + 1)];
list = Table[If[n == 1, f[1], f[n] - f[n - 1]], {n, 1., 10^5}];
ff[n_Integer] := list[[n]];

NSum[ff[n], {n, 1, ∞}, Method -> "WynnEpsilon"]
(*  1.00008  *)
• Note: The OP's NSum[list[[n]], {n, 1, \[Infinity]}, Method -> "WynnEpsilon"] works, but slowly, because, I suspect, the list is copied multiple times. There is no such problem with ff[n]. Dec 4, 2017 at 0:25

From the "Details" section of NSum:

NSum first localizes the values of all variables, then evaluates f with the variables being symbolic, and then repeatedly evaluates the result numerically.

So basically (if I'm understanding this correctly), NSum is trying to evaluate list[[k]] (with k as a symbol) at some point, which is why the message appears.

A better way to do it is as @Bob Hanlon commented:

Sum[list[[k]], {k, 1, 4}];

or:

list // Most // Total

The interpretation given by Anne is correct, you can reproduce the error message simply by evaluating

list[[k]]

During evaluation of In[5]:= Part::pkspec1: The expression k cannot be used as a part specification.

{1, 2, 3, 4, 5}[[k]]

One workaround is to use Indexed instead of Part:

NSum[Indexed[list, k], {k, 1, 4}]
10.