Message Boards Message Boards

0
|
4519 Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

Can you understand this speed up to NSum?

Here is a strange way to increase the speed of NSum. A dummy second summation (below, {n, 1, 1}) nearly halves the timing!! Can anyone figure out why it works?

Can anyone at Wolfram Research figure out why?

 g[x_] := (x^(1/x) - 1);

m = NSum[(-1)^x (g[x]), {x, 1, Infinity}, 
   Method -> "AlternatingSigns", WorkingPrecision -> 10000];


  N[Timing[m - 
   NSum[(-1)^x (g[x]), {x, 1, Infinity}, Method -> "AlternatingSigns",
     WorkingPrecision -> 10000, NSumTerms -> 100]], 50]

 (*{88.75, 0.*10^-9998}*)


  (* In[197]:=*) N[
   Timing[m - 
     NSum[(-1)^x (g[x]), {x, 1, Infinity}, {n, 1}, 
      Method -> "AlternatingSigns", WorkingPrecision -> 10000, 
      NSumTerms -> 100]], 50]

 (* Out[197]= {47.7969, 0.*10^-9998}*)
POSTED BY: Marvin Ray Burns

It's a bit tricky and I'm not certain I have all the issues correct. Has to do with when and where the approximate numbers are computed. The first one substituted bignums of high precision into the formula for g[x]. The second, because it has that extra layer, fools NSum into believing it must treat the function as a "black box", and it substitutes the exact integers before numericizing. For reasons not clear to me, it is faster to do this and numericize the resulting expression after the fact (even with evaluating the exact roots). Also there could be differences in how many terms get used based on the use, or not, of the explicit form of the function.

I think this next example more or less captures what is happening.

g1[x_] := N[(x^(1/x) - 1), 10000];
g2[x_] := With[{nx = N[x, 10000]}, (nx^(1/nx) - 1)]

m = 1000;
vals = Range[m];

ClearSystemCache["Numeric"];
Timing[res1 = Map[g1, vals];]
ClearSystemCache["Numeric"];
Timing[res2 = Map[g2, vals];]

(* Out[363]= {2.019356, Null}

Out[365]= {5.185261, Null} *)
POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract