Skip to article frontmatterSkip to article content

6.3Barycentric form

#%config InlineBackend.figure_format = 'svg'
from pylab import *

The Lagrange interpolation polynomial of degree NN is

p(x)=j=0Nf(xj)j(x)wherej(x)=i=0,ijN(xxi)i=0,ijN(xjxi)p(x) = \sum_{j=0}^N f(x_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)}

There are some issues in computing it as written above.

  • The denominators in j\ell_j do not depend on xx and 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.)

These problems can be overcome by using alternate forms of Lagrange interpolation Berrut & Trefethen, 2004, Trefethen, 2019.

6.3.1Improved Lagrange formula

Define

(x)=ωN(x)=(xx0)(xx1)(xxN)wj=1k=0,kjN(xjxk)=1(xj),j=0,1,,N\begin{gather} \ell(x) = \omega_N(x) = (x-x_0)(x-x_1) \ldots (x-x_N) \\ w_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 \end{gather}

The Lagrange polynomials can be written as

j(x)=(x)(xj)(xxj)=(x)wjxxj\ell_j(x) = \frac{\ell(x)}{\ell'(x_j) (x - x_j)} = \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 {wj}\{w_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.

6.3.2Barycentric formula

Since j\ell_j form a partition of unity

1=j=0Nj(x)=(x)j=0Nwjxxj(x)=1j=0Nwjxxj1 = \sum_{j=0}^N \ell_j(x) = \ell(x) \sum_{j=0}^N \frac{w_j}{x-x_j} \limplies \ell(x) = \frac{ 1 }{ \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, 2004 has shown that barycentric formula is numerically stable. This form is general since it takes any node sets and computes the weights.

6.3.3Weights

For special node distributions, we can simplify the weight calculation and obtain explicit expressions, as shown in the next sections.

6.3.3.1Equispaced points

If we have N+1N+1 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,

maxjwjminjwj2N\frac{\max_j |w_j|}{\min_j |w_j|} \approx 2^N

This leads to ill-conditioning and Runge phenomenon. This is not a consequence of the barycentric formula, but is intrinsic to interpolation using equispaced data.

6.3.3.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

maxjwjminjwjsin(2(N/2)+12N+2π)sin(2(0)+12N+2π)sin(12π)π2N+2=O(N)for large N\frac{\max_j |w_j|}{\min_j |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.

6.3.3.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

6.3.4Using 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'), title('Degree = '+str(N));
<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>

The second form is useful to construct a representation of the interpolant once, and repeatedly use it to evaluate at different values of xx.

References
  1. Berrut, J.-P., & Trefethen, L. N. (2004). Barycentric Lagrange Interpolation. SIAM Review, 46(3), 501–517. 10.1137/S0036144502417715
  2. Trefethen, L. N. (2019). Approximation Theory and Approximation Practice, Extended Edition. Society for Industrial. 10.1137/1.9781611975949
  3. Higham, N. J. (2004). The numerical stability of barycentric Lagrange interpolation. IMA Journal of Numerical Analysis, 24(4), 547–556. 10.1093/imanum/24.4.547