Sums, products, limits and extrapolation

The functions listed here permit approximation of infinite sums, products, and other sequence limits. Use mpmath.fsum() and mpmath.fprod() for summation and multiplication of finite sequences.



mpmath.nsum(ctx, f, *intervals, **options)

Computes the sum

\[S = \sum_{k=a}^b f(k)\]

where \((a, b)\) = interval, and where \(a = -\infty\) and/or \(b = \infty\) are allowed, or more generally

\[S = \sum_{k_1=a_1}^{b_1} \cdots \sum_{k_n=a_n}^{b_n} f(k_1,\ldots,k_n)\]

if multiple intervals are given.

Two examples of infinite series that can be summed by nsum(), where the first converges rapidly and the second converges slowly, are:

>>> from mpmath import *
>>> mp.dps = 15; mp.pretty = True
>>> nsum(lambda n: 1/fac(n), [0, inf])
>>> nsum(lambda n: 1/n**2, [1, inf])

When appropriate, nsum() applies convergence acceleration to accurately estimate the sums of slowly convergent series. If the series is finite, nsum() currently does not attempt to perform any extrapolation, and simply calls fsum().

Multidimensional infinite series are reduced to a single-dimensional series over expanding hypercubes; if both infinite and finite dimensions are present, the finite ranges are moved innermost. For more advanced control over the summation order, use nested calls to nsum(), or manually rewrite the sum as a single-dimensional series.


Desired maximum final error. Defaults roughly to the epsilon of the working precision.
Which summation algorithm to use (described below). Default: 'richardson+shanks'.
Cancel after at most this many terms. Default: 10*dps.
An iterable giving the number of terms to add between each extrapolation attempt. The default sequence is [10, 20, 30, 40, ...]. For example, if you know that approximately 100 terms will be required, efficiency might be improved by setting this to [100, 10]. Then the first extrapolation will be performed after 100 terms, the second after 110, etc.
Print details about progress.
If enabled, any term that raises ArithmeticError or ValueError (e.g. through division by zero) is replaced by a zero. This is convenient for lattice sums with a singular term near the origin.


Unfortunately, an algorithm that can efficiently sum any infinite series does not exist. nsum() implements several different algorithms that each work well in different cases. The method keyword argument selects a method.

The default method is 'r+s', i.e. both Richardson extrapolation and Shanks transformation is attempted. A slower method that handles more cases is 'r+s+e'. For very high precision summation, or if the summation needs to be fast (for example if multiple sums need to be evaluated), it is a good idea to investigate which one method works best and only use that.

'richardson' / 'r':
Uses Richardson extrapolation. Provides useful extrapolation when \(f(k) \sim P(k)/Q(k)\) or when \(f(k) \sim (-1)^k P(k)/Q(k)\) for polynomials \(P\) and \(Q\). See richardson() for additional information.
'shanks' / 's':
Uses Shanks transformation. Typically provides useful extrapolation when \(f(k) \sim c^k\) or when successive terms alternate signs. Is able to sum some divergent series. See shanks() for additional information.
'euler-maclaurin' / 'e':
Uses the Euler-Maclaurin summation formula to approximate the remainder sum by an integral. This requires high-order numerical derivatives and numerical integration. The advantage of this algorithm is that it works regardless of the decay rate of \(f\), as long as \(f\) is sufficiently smooth. See sumem() for additional information.
'direct' / 'd':
Does not perform any extrapolation. This can be used (and should only be used for) rapidly convergent series. The summation automatically stops when the terms decrease below the target tolerance.

Basic examples

A finite sum:

>>> nsum(lambda k: 1/k, [1, 6])

Summation of a series going to negative infinity and a doubly infinite series:

>>> nsum(lambda k: 1/k**2, [-inf, -1])
>>> nsum(lambda k: 1/(1+k**2), [-inf, inf])

nsum() handles sums of complex numbers:

>>> nsum(lambda k: (0.5+0.25j)**k, [0, inf])
(1.6 + 0.8j)

The following sum converges very rapidly, so it is most efficient to sum it by disabling convergence acceleration:

>>> mp.dps = 1000
>>> a = nsum(lambda k: -(-1)**k * k**2 / fac(2*k), [1, inf],
...     method='direct')
>>> b = (cos(1)+sin(1))/4
>>> abs(a-b) < mpf('1e-998')

Examples with Richardson extrapolation

Richardson extrapolation works well for sums over rational functions, as well as their alternating counterparts:

>>> mp.dps = 50
>>> nsum(lambda k: 1 / k**3, [1, inf],
...     method='richardson')
>>> zeta(3)

>>> nsum(lambda n: (n + 3)/(n**3 + n**2), [1, inf],
...     method='richardson')
>>> pi**2/2-2

>>> nsum(lambda k: (-1)**k / k**3, [1, inf],
...     method='richardson')
>>> -3*zeta(3)/4

Examples with Shanks transformation

The Shanks transformation works well for geometric series and typically provides excellent acceleration for Taylor series near the border of their disk of convergence. Here we apply it to a series for \(\log(2)\), which can be seen as the Taylor series for \(\log(1+x)\) with \(x = 1\):

>>> nsum(lambda k: -(-1)**k/k, [1, inf],
...     method='shanks')
>>> log(2)

Here we apply it to a slowly convergent geometric series:

>>> nsum(lambda k: mpf('0.995')**k, [0, inf],
...     method='shanks')

Finally, Shanks’ method works very well for alternating series where \(f(k) = (-1)^k g(k)\), and often does so regardless of the exact decay rate of \(g(k)\):

>>> mp.dps = 15
>>> nsum(lambda k: (-1)**(k+1) / k**1.5, [1, inf],
...     method='shanks')
>>> (2-sqrt(2))*zeta(1.5)/2

The following slowly convergent alternating series has no known closed-form value. Evaluating the sum a second time at higher precision indicates that the value is probably correct:

>>> nsum(lambda k: (-1)**k / log(k), [2, inf],
...     method='shanks')
>>> mp.dps = 30
>>> nsum(lambda k: (-1)**k / log(k), [2, inf],
...     method='shanks')

Examples with Euler-Maclaurin summation

The sum in the following example has the wrong rate of convergence for either Richardson or Shanks to be effective.

>>> f = lambda k: log(k)/k**2.5
>>> mp.dps = 15
>>> nsum(f, [1, inf], method='euler-maclaurin')
>>> -diff(zeta, 2.5)

Increasing steps improves speed at higher precision:

>>> mp.dps = 50
>>> nsum(f, [1, inf], method='euler-maclaurin', steps=[250])
>>> -diff(zeta, 2.5)

Divergent series

The Shanks transformation is able to sum some divergent series. In particular, it is often able to sum Taylor series beyond their radius of convergence (this is due to a relation between the Shanks transformation and Pade approximations; see pade() for an alternative way to evaluate divergent Taylor series).

Here we apply it to \(\log(1+x)\) far outside the region of convergence:

>>> mp.dps = 50
>>> nsum(lambda k: -(-9)**k/k, [1, inf],
...     method='shanks')
>>> log(10)

A particular type of divergent series that can be summed using the Shanks transformation is geometric series. The result is the same as using the closed-form formula for an infinite geometric series:

>>> mp.dps = 15
>>> for n in range(-8, 8):
...     if n == 1:
...         continue
...     print("%s %s %s" % (mpf(n), mpf(1)/(1-n),
...         nsum(lambda k: n**k, [0, inf], method='shanks')))
-8.0 0.111111111111111 0.111111111111111
-7.0 0.125 0.125
-6.0 0.142857142857143 0.142857142857143
-5.0 0.166666666666667 0.166666666666667
-4.0 0.2 0.2
-3.0 0.25 0.25
-2.0 0.333333333333333 0.333333333333333
-1.0 0.5 0.5
0.0 1.0 1.0
2.0 -1.0 -1.0
3.0 -0.5 -0.5
4.0 -0.333333333333333 -0.333333333333333
5.0 -0.25 -0.25
6.0 -0.2 -0.2
7.0 -0.166666666666667 -0.166666666666667

Multidimensional sums

Any combination of finite and infinite ranges is allowed for the summation indices:

>>> mp.dps = 15
>>> nsum(lambda x,y: x+y, [2,3], [4,5])
>>> nsum(lambda x,y: x/2**y, [1,3], [1,inf])
>>> nsum(lambda x,y: y/2**x, [1,inf], [1,3])
>>> nsum(lambda x,y,z: z/(2**x*2**y), [1,inf], [1,inf], [3,4])
>>> nsum(lambda x,y,z: y/(2**x*2**z), [1,inf], [3,4], [1,inf])
>>> nsum(lambda x,y,z: x/(2**z*2**y), [3,4], [1,inf], [1,inf])

Some nice examples of double series with analytic solutions or reductions to single-dimensional series (see [1]):

>>> nsum(lambda m, n: 1/2**(m*n), [1,inf], [1,inf])
>>> nsum(lambda n: 1/(2**n-1), [1,inf])

>>> nsum(lambda i,j: (-1)**(i+j)/(i**2+j**2), [1,inf], [1,inf])
>>> pi*(pi-3*ln2)/12

>>> nsum(lambda i,j: (-1)**(i+j)/(i+j)**2, [1,inf], [1,inf])
>>> altzeta(2) - altzeta(1)

>>> nsum(lambda i,j: (-1)**(i+j)/(i+j)**3, [1,inf], [1,inf])
>>> altzeta(3) - altzeta(2)

>>> nsum(lambda m,n: m**2*n/(3**m*(n*3**m+m*3**n)),
...     [1,inf], [1,inf])
>>> mpf(9)/32

>>> nsum(lambda i,j: fac(i-1)*fac(j-1)/fac(i+j),
...     [1,inf], [1,inf], workprec=400)
>>> zeta(2)

A hard example of a multidimensional sum is the Madelung constant in three dimensions (see [2]). The defining sum converges very slowly and only conditionally, so nsum() is lucky to obtain an accurate value through convergence acceleration. The second evaluation below uses a much more efficient, rapidly convergent 2D sum:

>>> nsum(lambda x,y,z: (-1)**(x+y+z)/(x*x+y*y+z*z)**0.5,
...     [-inf,inf], [-inf,inf], [-inf,inf], ignore=True)
>>> nsum(lambda x,y: -12*pi*sech(0.5*pi * \
...     sqrt((2*x+1)**2+(2*y+1)**2))**2, [0,inf], [0,inf])

Another example of a lattice sum in 2D:

>>> nsum(lambda x,y: (-1)**(x+y) / (x**2+y**2), [-inf,inf],
...     [-inf,inf], ignore=True)
>>> -pi*ln2

An example of an Eisenstein series:

>>> nsum(lambda m,n: (m+n*1j)**(-4), [-inf,inf], [-inf,inf],
...     ignore=True)
(3.1512120021539 + 0.0j)


  1. [Weisstein] http://mathworld.wolfram.com/DoubleSeries.html,
  2. [Weisstein] http://mathworld.wolfram.com/MadelungConstants.html


mpmath.sumem(ctx, f, interval, tol=None, reject=10, integral=None, adiffs=None, bdiffs=None, verbose=False, error=False, _fast_abort=False)

Uses the Euler-Maclaurin formula to compute an approximation accurate to within tol (which defaults to the present epsilon) of the sum

\[S = \sum_{k=a}^b f(k)\]

where \((a,b)\) are given by interval and \(a\) or \(b\) may be infinite. The approximation is

\[S \sim \int_a^b f(x) \,dx + \frac{f(a)+f(b)}{2} + \sum_{k=1}^{\infty} \frac{B_{2k}}{(2k)!} \left(f^{(2k-1)}(b)-f^{(2k-1)}(a)\right).\]

The last sum in the Euler-Maclaurin formula is not generally convergent (a notable exception is if \(f\) is a polynomial, in which case Euler-Maclaurin actually gives an exact result).

The summation is stopped as soon as the quotient between two consecutive terms falls below reject. That is, by default (reject = 10), the summation is continued as long as each term adds at least one decimal.

Although not convergent, convergence to a given tolerance can often be “forced” if \(b = \infty\) by summing up to \(a+N\) and then applying the Euler-Maclaurin formula to the sum over the range \((a+N+1, \ldots, \infty)\). This procedure is implemented by nsum().

By default numerical quadrature and differentiation is used. If the symbolic values of the integral and endpoint derivatives are known, it is more efficient to pass the value of the integral explicitly as integral and the derivatives explicitly as adiffs and bdiffs. The derivatives should be given as iterables that yield \(f(a), f'(a), f''(a), \ldots\) (and the equivalent for \(b\)).


Summation of an infinite series, with automatic and symbolic integral and derivative values (the second should be much faster):

>>> from mpmath import *
>>> mp.dps = 50; mp.pretty = True
>>> sumem(lambda n: 1/n**2, [32, inf])
>>> I = mpf(1)/32
>>> D = adiffs=((-1)**n*fac(n+1)*32**(-2-n) for n in range(999))
>>> sumem(lambda n: 1/n**2, [32, inf], integral=I, adiffs=D)

An exact evaluation of a finite polynomial sum:

>>> sumem(lambda n: n**5-12*n**2+3*n, [-100000, 200000])
>>> print(sum(n**5-12*n**2+3*n for n in range(-100000, 200001)))


mpmath.sumap(ctx, f, interval, integral=None, error=False)

Evaluates an infinite series of an analytic summand f using the Abel-Plana formula

\[\sum_{k=0}^{\infty} f(k) = \int_0^{\infty} f(t) dt + \frac{1}{2} f(0) + i \int_0^{\infty} \frac{f(it)-f(-it)}{e^{2\pi t}-1} dt.\]

Unlike the Euler-Maclaurin formula (see sumem()), the Abel-Plana formula does not require derivatives. However, it only works when \(|f(it)-f(-it)|\) does not increase too rapidly with \(t\).


The Abel-Plana formula is particularly useful when the summand decreases like a power of \(k\); for example when the sum is a pure zeta function:

>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> sumap(lambda k: 1/k**2.5, [1,inf])
>>> zeta(2.5)
>>> sumap(lambda k: 1/(k+1j)**(2.5+2.5j), [1,inf])
(-3.385361068546473342286084 - 0.7432082105196321803869551j)
>>> zeta(2.5+2.5j, 1+1j)
(-3.385361068546473342286084 - 0.7432082105196321803869551j)

If the series is alternating, numerical quadrature along the real line is likely to give poor results, so it is better to evaluate the first term symbolically whenever possible:

>>> n=3; z=-0.75
>>> I = expint(n,-log(z))
>>> chop(sumap(lambda k: z**k / k**n, [1,inf], integral=I))
>>> polylog(n,z)



mpmath.nprod(ctx, f, interval, nsum=False, **kwargs)

Computes the product

\[P = \prod_{k=a}^b f(k)\]

where \((a, b)\) = interval, and where \(a = -\infty\) and/or \(b = \infty\) are allowed.

By default, nprod() uses the same extrapolation methods as nsum(), except applied to the partial products rather than partial sums, and the same keyword options as for nsum() are supported. If nsum=True, the product is instead computed via nsum() as

\[P = \exp\left( \sum_{k=a}^b \log(f(k)) \right).\]

This is slower, but can sometimes yield better results. It is also required (and used automatically) when Euler-Maclaurin summation is requested.


A simple finite product:

>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> nprod(lambda k: k, [1, 4])

A large number of infinite products have known exact values, and can therefore be used as a reference. Most of the following examples are taken from MathWorld [1].

A few infinite products with simple values are:

>>> 2*nprod(lambda k: (4*k**2)/(4*k**2-1), [1, inf])
>>> nprod(lambda k: (1+1/k)**2/(1+2/k), [1, inf])
>>> nprod(lambda k: (k**3-1)/(k**3+1), [2, inf])
>>> nprod(lambda k: (1-1/k**2), [2, inf])

Next, several more infinite products with more complicated values:

>>> nprod(lambda k: exp(1/k**2), [1, inf]); exp(pi**2/6)

>>> nprod(lambda k: (k**2-1)/(k**2+1), [2, inf]); pi*csch(pi)

>>> nprod(lambda k: (k**4-1)/(k**4+1), [2, inf])
>>> pi*sinh(pi)/(cosh(sqrt(2)*pi)-cos(sqrt(2)*pi))

>>> nprod(lambda k: (1+1/k+1/k**2)**2/(1+2/k+3/k**2), [1, inf])
>>> 3*sqrt(2)*cosh(pi*sqrt(3)/2)**2*csch(pi*sqrt(2))/pi

>>> nprod(lambda k: (1-1/k**4), [2, inf]); sinh(pi)/(4*pi)

>>> nprod(lambda k: (1-1/k**6), [2, inf])
>>> (1+cosh(pi*sqrt(3)))/(12*pi**2)

>>> nprod(lambda k: (1+1/k**2), [2, inf]); sinh(pi)/(2*pi)

>>> nprod(lambda n: (1+1/n)**n * exp(1/(2*n)-1), [1, inf])
>>> exp(1+euler/2)/sqrt(2*pi)

The following two products are equivalent and can be evaluated in terms of a Jacobi theta function. Pi can be replaced by any value (as long as convergence is preserved):

>>> nprod(lambda k: (1-pi**-k)/(1+pi**-k), [1, inf])
>>> nprod(lambda k: tanh(k*log(pi)/2), [1, inf])
>>> jtheta(4,0,1/pi)

This product does not have a known closed form value:

>>> nprod(lambda k: (1-1/2**k), [1, inf])

A product taken from \(-\infty\):

>>> nprod(lambda k: 1-k**(-3), [-inf,-2])
>>> cosh(pi*sqrt(3)/2)/(3*pi)

A doubly infinite product:

>>> nprod(lambda k: exp(1/(1+k**2)), [-inf, inf])
>>> exp(pi/tanh(pi))

A product requiring the use of Euler-Maclaurin summation to compute an accurate value:

>>> nprod(lambda k: (1-1/k**2.5), [2, inf], method='e')


  1. [Weisstein] http://mathworld.wolfram.com/InfiniteProduct.html

Limits (limit)


mpmath.limit(ctx, f, x, direction=1, exp=False, **kwargs)

Computes an estimate of the limit

\[\lim_{t \to x} f(t)\]

where \(x\) may be finite or infinite.

For finite \(x\), limit() evaluates \(f(x + d/n)\) for consecutive integer values of \(n\), where the approach direction \(d\) may be specified using the direction keyword argument. For infinite \(x\), limit() evaluates values of \(f(\mathrm{sign}(x) \cdot n)\).

If the approach to the limit is not sufficiently fast to give an accurate estimate directly, limit() attempts to find the limit using Richardson extrapolation or the Shanks transformation. You can select between these methods using the method keyword (see documentation of nsum() for more information).


The following options are available with essentially the same meaning as for nsum(): tol, method, maxterms, steps, verbose.

If the option exp=True is set, \(f\) will be sampled at exponentially spaced points \(n = 2^1, 2^2, 2^3, \ldots\) instead of the linearly spaced points \(n = 1, 2, 3, \ldots\). This can sometimes improve the rate of convergence so that limit() may return a more accurate answer (and faster). However, do note that this can only be used if \(f\) supports fast and accurate evaluation for arguments that are extremely close to the limit point (or if infinite, very large arguments).


A basic evaluation of a removable singularity:

>>> from mpmath import *
>>> mp.dps = 30; mp.pretty = True
>>> limit(lambda x: (x-sin(x))/x**3, 0)

Computing the exponential function using its limit definition:

>>> limit(lambda n: (1+3/n)**n, inf)
>>> exp(3)

A limit for \(\pi\):

>>> f = lambda n: 2**(4*n+1)*fac(n)**4/(2*n+1)/fac(2*n)**2
>>> limit(f, inf)

Calculating the coefficient in Stirling’s formula:

>>> limit(lambda n: fac(n) / (sqrt(n)*(n/e)**n), inf)
>>> sqrt(2*pi)

Evaluating Euler’s constant \(\gamma\) using the limit representation

\[\gamma = \lim_{n \rightarrow \infty } \left[ \left( \sum_{k=1}^n \frac{1}{k} \right) - \log(n) \right]\]

(which converges notoriously slowly):

>>> f = lambda n: sum([mpf(1)/k for k in range(1,int(n)+1)]) - log(n)
>>> limit(f, inf)
>>> +euler

With default settings, the following limit converges too slowly to be evaluated accurately. Changing to exponential sampling however gives a perfect result:

>>> f = lambda x: sqrt(x**3+x**2)/(sqrt(x**3)+x)
>>> limit(f, inf)
>>> limit(f, inf, exp=True)


The following functions provide a direct interface to extrapolation algorithms. nsum() and limit() essentially work by calling the following functions with an increasing number of terms until the extrapolated limit is accurate enough.

The following functions may be useful to call directly if the precise number of terms needed to achieve a desired accuracy is known in advance, or if one wishes to study the convergence properties of the algorithms.


mpmath.richardson(ctx, seq)

Given a list seq of the first \(N\) elements of a slowly convergent infinite sequence, richardson() computes the \(N\)-term Richardson extrapolate for the limit.

richardson() returns \((v, c)\) where \(v\) is the estimated limit and \(c\) is the magnitude of the largest weight used during the computation. The weight provides an estimate of the precision lost to cancellation. Due to cancellation effects, the sequence must be typically be computed at a much higher precision than the target accuracy of the extrapolation.

Applicability and issues

The \(N\)-step Richardson extrapolation algorithm used by richardson() is described in [1].

Richardson extrapolation only works for a specific type of sequence, namely one converging like partial sums of \(P(1)/Q(1) + P(2)/Q(2) + \ldots\) where \(P\) and \(Q\) are polynomials. When the sequence does not convergence at such a rate richardson() generally produces garbage.

Richardson extrapolation has the advantage of being fast: the \(N\)-term extrapolate requires only \(O(N)\) arithmetic operations, and usually produces an estimate that is accurate to \(O(N)\) digits. Contrast with the Shanks transformation (see shanks()), which requires \(O(N^2)\) operations.

richardson() is unable to produce an estimate for the approximation error. One way to estimate the error is to perform two extrapolations with slightly different \(N\) and comparing the results.

Richardson extrapolation does not work for oscillating sequences. As a simple workaround, richardson() detects if the last three elements do not differ monotonically, and in that case applies extrapolation only to the even-index elements.


Applying Richardson extrapolation to the Leibniz series for \(\pi\):

>>> from mpmath import *
>>> mp.dps = 30; mp.pretty = True
>>> S = [4*sum(mpf(-1)**n/(2*n+1) for n in range(m))
...     for m in range(1,30)]
>>> v, c = richardson(S[:10])
>>> v
>>> nprint([v-pi, c])
[0.0711058, 2.0]

>>> v, c = richardson(S[:30])
>>> v
>>> nprint([v-pi, c])
[1.09645e-9, 20833.3]


  1. [BenderOrszag] pp. 375-376


mpmath.shanks(ctx, seq, table=None, randomized=False)

Given a list seq of the first \(N\) elements of a slowly convergent infinite sequence \((A_k)\), shanks() computes the iterated Shanks transformation \(S(A), S(S(A)), \ldots, S^{N/2}(A)\). The Shanks transformation often provides strong convergence acceleration, especially if the sequence is oscillating.

The iterated Shanks transformation is computed using the Wynn epsilon algorithm (see [1]). shanks() returns the full epsilon table generated by Wynn’s algorithm, which can be read off as follows:

  • The table is a list of lists forming a lower triangular matrix, where higher row and column indices correspond to more accurate values.
  • The columns with even index hold dummy entries (required for the computation) and the columns with odd index hold the actual extrapolates.
  • The last element in the last row is typically the most accurate estimate of the limit.
  • The difference to the third last element in the last row provides an estimate of the approximation error.
  • The magnitude of the second last element provides an estimate of the numerical accuracy lost to cancellation.

For convenience, so the extrapolation is stopped at an odd index so that shanks(seq)[-1][-1] always gives an estimate of the limit.

Optionally, an existing table can be passed to shanks(). This can be used to efficiently extend a previous computation after new elements have been appended to the sequence. The table will then be updated in-place.

The Shanks transformation

The Shanks transformation is defined as follows (see [2]): given the input sequence \((A_0, A_1, \ldots)\), the transformed sequence is given by

\[S(A_k) = \frac{A_{k+1}A_{k-1}-A_k^2}{A_{k+1}+A_{k-1}-2 A_k}\]

The Shanks transformation gives the exact limit \(A_{\infty}\) in a single step if \(A_k = A + a q^k\). Note in particular that it extrapolates the exact sum of a geometric series in a single step.

Applying the Shanks transformation once often improves convergence substantially for an arbitrary sequence, but the optimal effect is obtained by applying it iteratively: \(S(S(A_k)), S(S(S(A_k))), \ldots\).

Wynn’s epsilon algorithm provides an efficient way to generate the table of iterated Shanks transformations. It reduces the computation of each element to essentially a single division, at the cost of requiring dummy elements in the table. See [1] for details.

Precision issues

Due to cancellation effects, the sequence must be typically be computed at a much higher precision than the target accuracy of the extrapolation.

If the Shanks transformation converges to the exact limit (such as if the sequence is a geometric series), then a division by zero occurs. By default, shanks() handles this case by terminating the iteration and returning the table it has generated so far. With randomized=True, it will instead replace the zero by a pseudorandom number close to zero. (TODO: find a better solution to this problem.)


We illustrate by applying Shanks transformation to the Leibniz series for \(\pi\):

>>> from mpmath import *
>>> mp.dps = 50
>>> S = [4*sum(mpf(-1)**n/(2*n+1) for n in range(m))
...     for m in range(1,30)]
>>> T = shanks(S[:7])
>>> for row in T:
...     nprint(row)
[1.25, 3.16667]
[-1.75, 3.13333, -28.75]
[2.25, 3.14524, 82.25, 3.14234]
[-2.75, 3.13968, -177.75, 3.14139, -969.937]
[3.25, 3.14271, 327.25, 3.14166, 3515.06, 3.14161]

The extrapolated accuracy is about 4 digits, and about 4 digits may have been lost due to cancellation:

>>> L = T[-1]
>>> nprint([abs(L[-1] - pi), abs(L[-1] - L[-3]), abs(L[-2])])
[2.22532e-5, 4.78309e-5, 3515.06]

Now we extend the computation:

>>> T = shanks(S[:25], T)
>>> L = T[-1]
>>> nprint([abs(L[-1] - pi), abs(L[-1] - L[-3]), abs(L[-2])])
[3.75527e-19, 1.48478e-19, 2.96014e+17]

The value for pi is now accurate to 18 digits. About 18 digits may also have been lost to cancellation.

Here is an example with a geometric series, where the convergence is immediate (the sum is exactly 1):

>>> mp.dps = 15
>>> for row in shanks([0.5, 0.75, 0.875, 0.9375, 0.96875]):
...     nprint(row)
[8.0, 1.0]


  1. [GravesMorris]
  2. [BenderOrszag] pp. 368-375

Table Of Contents

Previous topic

Root-finding and optimization

Next topic


This Page