Function approximation¶
Taylor series (taylor
)¶
- mpmath.taylor(f, x, n, **options)¶
Produces a degree-\(n\) Taylor polynomial around the point \(x\) of the given function \(f\). The coefficients are returned as a list.
>>> from mpmath import * >>> mp.dps = 15; mp.pretty = True >>> nprint(chop(taylor(sin, 0, 5))) [0.0, 1.0, 0.0, -0.166667, 0.0, 0.00833333]
The coefficients are computed using high-order numerical differentiation. The function must be possible to evaluate to arbitrary precision. See
diff()
for additional details and supported keyword options.Note that to evaluate the Taylor polynomial as an approximation of \(f\), e.g. with
polyval()
, the coefficients must be reversed, and the point of the Taylor expansion must be subtracted from the argument:>>> p = taylor(exp, 2.0, 10) >>> polyval(p[::-1], 2.5 - 2.0) 12.1824939606092 >>> exp(2.5) 12.1824939607035
Pade approximation (pade
)¶
- mpmath.pade(a, L, M)¶
Computes a Pade approximation of degree \((L, M)\) to a function. Given at least \(L+M+1\) Taylor coefficients \(a\) approximating a function \(A(x)\),
pade()
returns coefficients of polynomials \(P, Q\) satisfying\[ \begin{align}\begin{aligned}P = \sum_{k=0}^L p_k x^k\\Q = \sum_{k=0}^M q_k x^k\\Q_0 = 1\\A(x) Q(x) = P(x) + O(x^{L+M+1})\end{aligned}\end{align} \]\(P(x)/Q(x)\) can provide a good approximation to an analytic function beyond the radius of convergence of its Taylor series (example from G.A. Baker ‘Essentials of Pade Approximants’ Academic Press, Ch.1A):
>>> from mpmath import * >>> mp.dps = 15; mp.pretty = True >>> one = mpf(1) >>> def f(x): ... return sqrt((one + 2*x)/(one + x)) ... >>> a = taylor(f, 0, 6) >>> p, q = pade(a, 3, 3) >>> x = 10 >>> polyval(p[::-1], x)/polyval(q[::-1], x) 1.38169105566806 >>> f(x) 1.38169855941551
Chebyshev approximation (chebyfit
)¶
- mpmath.chebyfit(f, interval, N, error=False)¶
Computes a polynomial of degree \(N-1\) that approximates the given function \(f\) on the interval \([a, b]\). With
error=True
,chebyfit()
also returns an accurate estimate of the maximum absolute error; that is, the maximum value of \(|f(x) - P(x)|\) for \(x \in [a, b]\).chebyfit()
uses the Chebyshev approximation formula, which gives a nearly optimal solution: that is, the maximum error of the approximating polynomial is very close to the smallest possible for any polynomial of the same degree.Chebyshev approximation is very useful if one needs repeated evaluation of an expensive function, such as function defined implicitly by an integral or a differential equation. (For example, it could be used to turn a slow mpmath function into a fast machine-precision version of the same.)
Examples
Here we use
chebyfit()
to generate a low-degree approximation of \(f(x) = \cos(x)\), valid on the interval \([1, 2]\):>>> from mpmath import * >>> mp.dps = 15; mp.pretty = True >>> poly, err = chebyfit(cos, [1, 2], 5, error=True) >>> nprint(poly) [0.00291682, 0.146166, -0.732491, 0.174141, 0.949553] >>> nprint(err, 12) 1.61351758081e-5
The polynomial can be evaluated using
polyval
:>>> nprint(polyval(poly, 1.6), 12) -0.0291858904138 >>> nprint(cos(1.6), 12) -0.0291995223013
Sampling the true error at 1000 points shows that the error estimate generated by
chebyfit
is remarkably good:>>> error = lambda x: abs(cos(x) - polyval(poly, x)) >>> nprint(max([error(1+n/1000.) for n in range(1000)]), 12) 1.61349954245e-5
Choice of degree
The degree \(N\) can be set arbitrarily high, to obtain an arbitrarily good approximation. As a rule of thumb, an \(N\)-term Chebyshev approximation is good to \(N/(b-a)\) decimal places on a unit interval (although this depends on how well-behaved \(f\) is). The cost grows accordingly:
chebyfit
evaluates the function \((N^2)/2\) times to compute the coefficients and an additional \(N\) times to estimate the error.Possible issues
One should be careful to use a sufficiently high working precision both when calling
chebyfit
and when evaluating the resulting polynomial, as the polynomial is sometimes ill-conditioned. It is for example difficult to reach 15-digit accuracy when evaluating the polynomial using machine precision floats, no matter the theoretical accuracy of the polynomial. (The option to return the coefficients in Chebyshev form should be made available in the future.)It is important to note the Chebyshev approximation works poorly if \(f\) is not smooth. A function containing singularities, rapid oscillation, etc can be approximated more effectively by multiplying it by a weight function that cancels out the nonsmooth features, or by dividing the interval into several segments.
Fourier series (fourier
, fourierval
)¶
- mpmath.fourier(f, interval, N)¶
Computes the Fourier series of degree \(N\) of the given function on the interval \([a, b]\). More precisely,
fourier()
returns two lists \((c, s)\) of coefficients (the cosine series and sine series, respectively), such that\[f(x) \sim \sum_{k=0}^N c_k \cos(k m x) + s_k \sin(k m x)\]where \(m = 2 \pi / (b-a)\).
Note that many texts define the first coefficient as \(2 c_0\) instead of \(c_0\). The easiest way to evaluate the computed series correctly is to pass it to
fourierval()
.Examples
The function \(f(x) = x\) has a simple Fourier series on the standard interval \([-\pi, \pi]\). The cosine coefficients are all zero (because the function has odd symmetry), and the sine coefficients are rational numbers:
>>> from mpmath import * >>> mp.dps = 15; mp.pretty = True >>> c, s = fourier(lambda x: x, [-pi, pi], 5) >>> nprint(c) [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] >>> nprint(s) [0.0, 2.0, -1.0, 0.666667, -0.5, 0.4]
This computes a Fourier series of a nonsymmetric function on a nonstandard interval:
>>> I = [-1, 1.5] >>> f = lambda x: x**2 - 4*x + 1 >>> cs = fourier(f, I, 4) >>> nprint(cs[0]) [0.583333, 1.12479, -1.27552, 0.904708, -0.441296] >>> nprint(cs[1]) [0.0, -2.6255, 0.580905, 0.219974, -0.540057]
It is instructive to plot a function along with its truncated Fourier series:
>>> plot([f, lambda x: fourierval(cs, I, x)], I)
Fourier series generally converge slowly (and may not converge pointwise). For example, if \(f(x) = \cosh(x)\), a 10-term Fourier series gives an \(L^2\) error corresponding to 2-digit accuracy:
>>> I = [-1, 1] >>> cs = fourier(cosh, I, 9) >>> g = lambda x: (cosh(x) - fourierval(cs, I, x))**2 >>> nprint(sqrt(quad(g, I))) 0.00467963
fourier()
uses numerical quadrature. For nonsmooth functions, the accuracy (and speed) can be improved by including all singular points in the interval specification:>>> nprint(fourier(abs, [-1, 1], 0), 10) ([0.5000441648], [0.0]) >>> nprint(fourier(abs, [-1, 0, 1], 0), 10) ([0.5], [0.0])