0
$\begingroup$

Below is a simple example to illustrate the problem

test = Compile[{{n, _Integer}}, n]

Now, Table works fine

Table[test[n], {n, 1, 10}]

But NSum

NSum[test[n], {n, 1, 10}]

gives errors as

CompiledFunction::cfsa: Argument n at position 1 should be a machine-size integer. >>

What does it mean?


Update

Simon Woods suggested that we could simply ignore this warning. But I don't think so. See the timing below.

In[2]:= NSum[test[n], {n, 1, 100000}] // AbsoluteTiming


During evaluation of In[2]:= CompiledFunction::cfsa: Argument n at position 1 should be a machine-size integer. >>

During evaluation of In[2]:= CompiledFunction::cfsa: Argument n at position 1 should be a machine-size integer. >>

Out[2]= {0.270994, 5.00005*10^9}

In[4]:= Sum[test[n], {n, 1, 100000}] // AbsoluteTiming

Out[4]= {0.114388, 5000050000}

NSum is significantly slower than Sum

$\endgroup$
6
  • $\begingroup$ While it's a different warning the cause is the same as in the duplicate question - symbolic evaluation of the summand. You can ignore the warning, but if it bothers you use NSum[Hold@test[n],{n,1,10}] or create a wrapper function for test which only lets through the correct type of argument e.g. test2[n_Integer]:=test[n] $\endgroup$ Commented Dec 22, 2015 at 12:48
  • $\begingroup$ I don't reproduce your results on the timing - I get a much smaller time from Quiet@NSum[test[n], {n, 1, 100000}] // RepeatedTiming than I do from Quiet@Sum[test[n], {n, 1, 100000}] // RepeatedTiming $\endgroup$
    – Jason B.
    Commented Dec 22, 2015 at 15:33
  • $\begingroup$ I get the same as Jason - NSum is about 20 times faster than Sum $\endgroup$ Commented Dec 22, 2015 at 16:05
  • $\begingroup$ @SimonWoods I found my timing is somewhat wrong. I updated my post. But still NSum is slower. I think RepeatedTiming is lying, maybe it is using caching? Using AbsoluteTiming Nsum is slower on my computer $\endgroup$
    – matheorem
    Commented Dec 22, 2015 at 16:17
  • $\begingroup$ Processing and issuing the warning message has a cost, of course. With Quiet as shown by Jason the message is suppressed and NSum is much faster. When I said you could ignore the warning, I meant that the result would be correct. If it's important to save that fraction of a second then don't ignore it. $\endgroup$ Commented Dec 22, 2015 at 16:38

0

Browse other questions tagged or ask your own question.