Skip to article frontmatterSkip to article content

Barycentric form

from pylab import *

The Lagrange interpolation polynomial of degree NN is

p(x)=j=0Nfjj(x)wherej(x)=i=0,ijN(xxi)i=0,ijN(xjxi)p(x) = \sum_{j=0}^N f_j \ell_j(x) \qquad \textrm{where} \qquad \ell_j(x) = \frac{\prod\limits_{i=0,i\ne j}^N (x-x_i)}{\prod\limits_{i=0,i \ne j}^N(x_j - x_i)}

The denominators in j\ell_j can be pre-computed and stored. Then for every xx, the evaluation of p(x)p(x) requires O(N2)O(N^2) flops. Ideally, we desire a method which takes O(N)O(N) flops. Also, if we add a new data pair (xN+1,fN+1)(x_{N+1},f_{N+1}), then this requires a new computation from scratch. Moreover, the numerical evaluation is also unstable to round-off errors (See page 51 in Powell, Approximation theory and methods, 1981.)

1Improved Lagrange formula

Define

(x)=(xx0)(xx1)(xxN)\ell(x) = (x-x_0)(x-x_1) \ldots (x-x_N)
wj=1k=0,kjN(xjxk)=1(xj),j=0,1,,Nw_j = \frac{1}{\prod\limits_{k=0, k \ne j}^N (x_j - x_k)} = \frac{1}{\ell'(x_j)}, \qquad j=0,1,\ldots,N

The Lagrange polynomials can be written as

j(x)=(x)wjxxj\ell_j(x) = \ell(x) \frac{w_j}{x-x_j}

and the interpolant is given by

p(x)=(x)j=0Nwjxxjfjp(x) = \ell(x) \sum_{j=0}^N \frac{w_j}{x-x_j} f_j

This is called the first form of barycentric interpolation formula and is due to Rutishauser. For each xx

  • calculation of wjw_j requires O(N2)O(N^2) flops, and these are independent of xx

  • calculation of (x)\ell(x) requires O(N)O(N) flops

  • calculation of the other term requires O(N)O(N) flops

so the complexity of this formula is O(N)O(N). Once the weights wjw_j are known, this permits interpolation of as many functions as desired in O(N)O(N) operations. If a new data point (xN+1,fN+1)(x_{N+1},f_{N+1}) is added

  • Divide each wjw_j by (xjxN+1)(x_j - x_{N+1}) which needs O(N+1)O(N+1) flops

  • compute wN+1w_{N+1} at O(N+1)O(N+1) flops

The total cost of updating the formula is O(N)O(N) flops.

2Barycentric formula

Since j\ell_j form a partition of unity (Assignment, See Atkinson, page 186, problem 2)

1=j=0Nj(x)=(x)j=0Nwjxxj1 = \sum_{j=0}^N \ell_j(x) = \ell(x) \sum_{j=0}^N \frac{w_j}{x-x_j}

the interpolant can be written as

p(x)=j=0Nwjxxjfjj=0Nwjxxjp(x) = \frac{ \sum_{j=0}^N \frac{w_j}{x-x_j} f_j }{ \sum_{j=0}^N \frac{w_j}{x- x_j} }

which is called the second form of barycentric formula. This has same complexity as the previous form. Higham has shown that barycentric formula is numerically stable. This code is general since it takes any node sets and computes the weights. For special node distributions, we can simplify the weight calculation as in next two sections.

2.1Equispaced points

If we have equispaced data points in [1,+1][-1,+1], with spacing h=2Nh = \frac{2}{N}, the weights are given by

wj=(1)Nj(Nj)1hnn!w_j = (-1)^{N-j} \binom{N}{j} \frac{1}{h^n n!}

Since the weights appear both in numerator and denominator, we can remove any factor independent of jj and use

wj=(1)j(Nj)w_j = (-1)^{j} \binom{N}{j}

If the interval is [a,b][a,b], we have to multiply the above wjw_j by 2N(ba)N2^N (b-a)^{-N}, but this factor can also be dropped since it is common to numerator and denominator. The weights wjw_j change by exponentially large factors, of order approximately 2N2^N. This leads to ill-conditioning and Runge phenomenon. This is not a consequence of the barycentric foruma, but is intrinsic to interpolation using equispaced data.

2.2Chebyshev points of first kind

The points and weights are given by

xj=cos(2j+12N+2π),wj=(1)jsin(2j+12N+2π),j=0,1,,Nx_j = \cos\left( \frac{2j+1}{2N+2} \pi \right), \quad w_j = (-1)^j \sin\left( \frac{2j+1} {2N+2} \pi \right), \quad j=0,1,\ldots,N

These weights vary by factors of O(N)O(N), since

maxwjminwjsin(2(N/2)+12N+2π)sin(2(0)+12N+2π)sin(12π)π2N+2=O(N)for large N\frac{\max w_j}{\min w_j} \approx \frac{ \sin\left( \frac{2(N/2)+1}{2N+2} \pi \right) } { \sin\left( \frac{2(0)+1}{2N+2} \pi \right)} \approx \frac{\sin(\half\pi)}{\frac{\pi} {2N+2}} = O(N) \quad \textrm{for large $N$}

not exponentially as in case of uniformly spaced points. The weights can be computed in O(N)O(N) operations. Newton interpolation always requires O(N2)O(N^2) operations for the divided differences.

2.3Chebyshev points of second kind

The points are given by

xi=cos(NiNπ),i=0,1,2,,Nx_i = \cos\left( \frac{N-i}{N} \pi \right), \qquad i=0,1,2,\ldots,N

and the weights are

w0=12,wi=(1)i,i=1,2,,N1,wN=12(1)Nw_0 = \half, \qquad w_i = (-1)^i, \quad i=1,2,\ldots,N-1, \qquad w_N = \half (-1)^N

3Using scipy.interpolate

scipy.interpolate provides many methods for polynomial interpolation, including barycentric form.

fun = lambda x: 1.0/(1.0 + 16.0 * x**2)
N   = 20
xi  = cos(linspace(0.0,pi,N+1))
yi  = fun(xi)
x   = linspace(xmin,xmax,100)

Using scipy.interpolate.barycentric_interpolate

from scipy.interpolate import barycentric_interpolate
y = barycentric_interpolate(xi, yi, x)
plot(x,  fun(x), '--', label='True function')
plot(xi, yi,     'o',  label='Data')
plot(x,  y,      '-',  label='Interpolant')
legend(), xlabel('x'), ylabel('y')
<Figure size 640x480 with 1 Axes>

Using scipy.interpolate.BarycentricInterpolater

from scipy.interpolate import BarycentricInterpolator
P = BarycentricInterpolator(xi, yi)
plot(x,  fun(x), '--', label='True function')
plot(xi, yi,     'o',  label='Data')
plot(x,  P(x),   '-',  label='Interpolant')
legend(), xlabel('x'), ylabel('y')
<Figure size 640x480 with 1 Axes>