NSum¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
NSum[f, {i, imin, imax}]
gives a numerical approximation to the sum of f for i from imin to imax.
NSum[f, {i, imin, imax, di}] uses step di. imax may be Infinity. NSum[f, {i, ...}, {j, ...}, ...] evaluates a multidimensional sum (an inner bound may depend on an outer index). The index is localised (HoldAll). Method -> Automatic picks Euler-Maclaurin for a monotone series whose summand extends off the integers, the Cohen-Villegas-Zagier method for alternating series, and Wynn's epsilon (partial-sum acceleration) otherwise, with Levin's u-transform as a last resort; an integer-only summand such as 1/Prime[n] cannot use Euler-Maclaurin (it has no continuous tail integral) and is extrapolated instead. Large finite sums use the difference of two infinite tails. Method -> "Levin" forces Levin's transformation ("LevinU" | "LevinT" | "LevinV" select the u/t/v variant). Machine or arbitrary precision via WorkingPrecision.
Notes
Options: Method (Automatic | EulerMaclaurin | AlternatingSigns | WynnEpsilon | "Levin"), WorkingPrecision (default MachinePrecision), NSumTerms (head terms summed explicitly, default 15), NSumExtraTerms, WynnDegree, VerifyConvergence (default True; a divergent sum gives ComplexInfinity), AccuracyGoal, PrecisionGoal.Examples (15)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (6)¶
In[1]:= NSum[1/i^2, {i, 1, Infinity}] - Pi^2/6 // N
Out[1]= 2.22045e-16
In[2]:= NSum[1/2^i, {i, 0, Infinity, 2}]
Out[2]= 1.33333
In[3]:= NSum[Log[x]/x^(2 + 2 I), {x, 1, Infinity}]
Out[3]= -0.182175 - 0.136618*I
In[4]:= NSum[1/i^2, {i, 100, 10^6}]
Out[4]= 0.0100492
In[5]:= NSum[(-1)^n (2/n)^k/k^2, {n, 2, Infinity}, {k, 1, n}]
Out[5]= 0.770188
In[6]:= NSum[2^i, {i, 0, Infinity}] NSum::div: the sum does not appear to converge
Out[6]= Optional[ComplexInfinity NSum::div, appear converge does not sum the to]
Options (4)¶
In[7]:= NSum[(-5)^i/i!, {i, 0, Infinity}, NSumTerms -> 25] - Exp[-5]
Out[7]= -2.4182e-15
In[8]:= NSum[1/n^(11/10), {n, 1, Infinity}, WorkingPrecision -> 40] - Zeta[11/10]
Out[8]= 4.40810381558357815488276201458342129182e-39
In[9]:= NSum[(-1)^x/(1 + (x - 12)^2), {x, 0, Infinity}, Method -> "AlternatingSigns", WorkingPrecision -> 30]
Out[9]= 0.2751938594139530395689715615907
In[10]:= NSum[1/n^2, {n, 1, Infinity}, Method -> "Levin", WorkingPrecision -> 30]
Out[10]= 1.644934066848226436472415166646
Applications (5)¶
In[11]:= NSum[1/n^2, {n, 1, Infinity}]
Out[11]= 1.64493
In[12]:= NSum[(-1)^(n+1)/n, {n, 1, Infinity}]
Out[12]= 0.693147
In[13]:= NSum[1/n^2, {n, 1, Infinity}, WorkingPrecision -> 30]
Out[13]= 1.644934066848226436472415166649
In[14]:= NSum[1/n^4, {n, 1, Infinity}, WorkingPrecision -> 30]
Out[14]= 1.082323233711138191516003696543
In[15]:= NSum[1/n^2, {n, 1, Infinity}, Method -> "Levin"]
Out[15]= 1.64493
Algorithm¶
Strategy -------- NSum holds its arguments, evaluates the iterator bounds, then Block-localises
k = 0, 1, 2, … with the actual index value x_k = imin + k·di, so a step di is handled uniformly and multidimensional sums fall out by making the summand of the outer sum an inner NSum[...] (HoldAll + localisation lets a dependent inner bound such as {k,1,n} see the bound outer index).
Methods are layered: this file currently provides Direct (small finite sums) and WynnEpsilon (partial-sum extrapolation, shared seqaccel kernels), machine
added on top of the same term machinery.
Memory: receives res owned by the evaluator; returns a fresh Expr* on
binding is removed on all return paths.
Implementation notes¶
Attributes: HoldAll, Protected.
References¶
See also: Block, Chop, Integrate, D, BernoulliB, NLimit, AccuracyGoal, PrecisionGoal
- Source:
src/info.c - Specification:
docs/spec/builtins/numerical-calculus.md - Tests:
tests/test_accuracygoal.c - Tests:
tests/test_autocompile.c - Tests:
tests/test_nprod.c - Tests:
tests/test_nsum.c
Notes & additional examples¶
Notes¶
NSum[f, {i, imin, imax}] numerically sums a series, with imax allowed to be
Infinity. The first two cases recover the Basel sum Pi^2/6 = 1.64493... and
the alternating harmonic sum Log[2] = 0.693147.... With WorkingPrecision -> 30
the Basel sum is computed to 30 digits, and Sum[1/n^4] returns
Pi^4/90 = 1.082323233711.... Method -> Automatic chooses Euler–Maclaurin for
monotone series, the Cohen–Villegas–Zagier method for alternating series, and
Wynn's epsilon otherwise, with Levin's u-transform as a last resort. Any
accelerator can be forced: Method -> "Levin" ("LevinU"/"LevinT"/"LevinV")
selects Levin's transformation, which reaches full WorkingPrecision on smooth
series. With VerifyConvergence -> True (default) a divergent sum gives
ComplexInfinity.