Post · 5 min read· pp. 135–137

Lagrange interpolation

Recall from our basics discussion that a polynomial ϕ\phi of degree dd is a vector of d+1d+1 coefficients:

ϕ=[ϕ0,ϕ1,ϕ2,…,ϕd]\begin{align} \phi &= [\phi_0, \phi_1, \phi_2, \dots, \phi_d] \end{align}

How to compute a polynomial’s coefficients from a bunch of its evaluations

Given nn pairs (xi,yi)i∈[n)(x_i, y_i)_{i\in[n)}, one can compute or interpolate a degree ≤n−1\le n-1 polynomial ϕ(X)\phi(X) such that: ϕ(xi)=yi,∀i∈[n)\phi(x_i)=y_i,\forall i\in[n)

Specifically, the Lagrange interpolation formula says that:

\labeleq:lagrange−formulaϕ(X)=∑i∈[n)yi⋅\lagri(X), where \lagri(X)=∏j∈[n),j≠iX−xjxi−xj\begin{align} \label{eq:lagrange-formula} \phi(X) &= \sum_{i\in[n)} y_i \cdot \lagr_i(X),\ \text{where}\ \lagr_i(X) = \prod_{j\in[n),j\ne i} \frac{X-x_j}{x_i-x_j} \end{align}

This formula is intimidating at first, but there’s a very simple intuition behind it. The key idea is that each Lagrange polynomial \lagri(X)\lagr_i(X) is defined so that it has two properties:

  1. \lagri(xi)=1,∀i∈[n)\lagr_i(x_i) = 1,\forall i\in[n)
  2. \lagri(xj)=0,∀j∈[n)∖{i}\lagr_i(x_j) = 0,\forall j \in [n)\setminus\{i\}

You can actually convince yourself that \lagri(X)\lagr_i(X) has these properties by plugging in xix_i and xjx_j to see what happens. For example, assume n=3n=3. Let’s look at L2(X)\mathcal{L}_2(X):

L2(X)=X−x1x2−x1⋅X−x3x2−x3\begin{align} \mathcal{L}_2(X) = \frac{X - x_1}{x_2 - x_1} \cdot \frac{X - x_3}{x_2 - x_3} \end{align}
Clearly, L2(x2)=1\mathcal{L}_2(x_2) = 1 and L2(x3)=0\mathcal{L}_2(x_3) = 0.

Important: The \lagri(X)\lagr_i(X) Lagrange polynomials are dependent on the set of xix_i’s only (and thus on nn)! Specifically each \lagri(X)\lagr_i(X) has degree n−1n-1 and has a root at each xjx_j when j≠ij\ne i! In this sense, a better notation for them would be \lagri[xi,n](X)\lagr_i^{[x_i, n]}(X) or \lagri[n)(X)\lagr_i^{[n)}(X) to indicate this dependence.

Example: Interpolating a polynomial from three evaluations

Consider the following example with n=3n=3 pairs of points. Then, by the Lagrange formula, we have:

ϕ(X)=y1\lagr1(X)+y2\lagr2(X)+y3\lagr3(X)\phi(X) = y_1 \lagr_1(X) + y_2 \lagr_2(X) + y_3 \lagr_3(X)

Next, by applying the two key properties of \lagri(X)\lagr_i(X) from above, you can easily check that ϕ(xi)=yi,∀i∈[3]\phi(x_i) = y_i,\forall i\in[3]:

ϕ(x1)=y1\lagr1(x1)+y2\lagr2(x1)+y3\lagr3(x1)=y1⋅1+y2⋅0+y3⋅0=y1ϕ(x2)=y1\lagr1(x2)+y2\lagr2(x2)+y3\lagr3(x2)=y1⋅0+y2⋅1+y3⋅0=y2ϕ(x3)=y1\lagr1(x3)+y2\lagr2(x3)+y3\lagr3(x3)=y1⋅0+y2⋅0+y3⋅1=y3\begin{align} \phi(x_1) &= y_1 \lagr_1(x_1) + y_2 \lagr_2(x_1) + y_3 \lagr_3(x_1) = y_1 \cdot 1 + y_2 \cdot 0 + y_3 \cdot 0 = y_1\\ \phi(x_2) &= y_1 \lagr_1(x_2) + y_2 \lagr_2(x_2) + y_3 \lagr_3(x_2) = y_1 \cdot 0 + y_2 \cdot 1 + y_3 \cdot 0 = y_2\\ \phi(x_3) &= y_1 \lagr_1(x_3) + y_2 \lagr_2(x_3) + y_3 \lagr_3(x_3) = y_1 \cdot 0 + y_2 \cdot 0 + y_3 \cdot 1 = y_3 \end{align}

An important detail is that the degree of the interpolated ϕ(X)\phi(X) is ≤n−1\le n-1 and not necessarily exactly equal to n−1n-1. To see this, consider interpolating the polynomial ϕ(X)\phi(X) such that ϕ(i)=i\phi(i) = i for all i∈[n)i\in [n). In other words, xi=yi=ix_i = y_i = i.

The inspired reader might notice that the polynomial ϕ(X)=X\phi(X) = X could satisfy our constraints. But is this what the Lagrange interpolation will return? After all, the interpolated ϕ(X)\phi(X) is a sum of degree n−1n-1 polynomials \lagri(X)\lagr_i(X), so could it have degree 1? Well, it turns out, yes, because things cancel out. To see this, take a simple example, with n=3n=3:

ϕ(X)=∑i∈[3]i⋅\lagri(X)=∑i∈[3]i⋅∏j∈[3]∖iX−ji−j=1⋅X−21−2X−31−3+2⋅X−12−1X−32−3+3⋅X−13−1X−23−2=X−2−1X−3−2+2⋅X−11X−3−1+3⋅X−12X−21=12(X−2)(X−3)−2(X−1)(X−3)+32(X−1)(X−2)=12[(X−2)(X−3)+3(X−1)(X−2)]−2(X−1)(X−3)=12[(X−2)(4X−6)]−2(X−1)(X−3)=(X−2)(2X−3)−2(X−1)(X−3)=(2X2−4X−3X+6)−2(X2−4X+3)=(2X2−7X+6)−2X2+8X−6=X\begin{align} \phi(X) &=\sum_{i\in [3]} i \cdot \lagr_i(X) = \sum_{i\in [3]} i \cdot \prod_{j\in[3]\setminus{i}} \frac{X - j}{i - j}\\ &= 1\cdot \frac{X-2}{1-2}\frac{X-3}{1-3} + 2\cdot \frac{X-1}{2-1}\frac{X-3}{2-3} + 3\cdot\frac{X-1}{3-1}\frac{X-2}{3-2}\\ &= \frac{X-2}{-1}\frac{X-3}{-2} + 2\cdot \frac{X-1}{1}\frac{X-3}{-1} + 3\cdot \frac{X-1}{2}\frac{X-2}{1}\\ &= \frac{1}{2}(X-2)(X-3) - 2(X-1)(X-3) + \frac{3}{2}(X-1)(X-2)\\ &= \frac{1}{2}[(X-2)(X-3) + 3(X-1)(X-2)] - 2(X-1)(X-3)\\ &= \frac{1}{2}[(X-2)(4X-6)] - 2(X-1)(X-3)\\ &= (X-2)(2X-3) - 2(X-1)(X-3)\\ &= (2X^2 - 4X - 3X + 6) - 2(X^2 - 4X +3)\\ &= (2X^2 - 7X + 6) - 2X^2 + 8X - 6\\ &= X \end{align}

Computational overhead of Lagrange interpolation

If done naively, interpolating ϕ(X)\phi(X) using the Lagrange formula in Equation \refeq:lagrange−formula\ref{eq:lagrange-formula} will take O(n2)O(n^2) time.

However, there are known techniques for computing ϕ(X)\phi(X) in O(nlog⁡2n)O(n\log^2{n}) time. We described part of these techniques in a previous blog post, but for the full techniques please refer to the “Modern Computer Algebra” book [^vG13ModernCh10].

Roots of unity

Let:

A(X)=∏j∈[n)(X−xj)A’(X)=∑i∈[n)∏j∈[n),j≠i(X−xj)\begin{align} A(X) &= \prod_{j\in[n)} (X - x_j)\\ A’(X) &= \sum_{i\in[n)} \prod_{j\in[n),j\ne i} (X - x_j) \end{align}

See this post for how the derivative A’(X)A’(X) of A(X)A(X) is derived.

Then, the Lagrange polynomials from Eq. \refeq:lagrange−formula\ref{eq:lagrange-formula} can be re-written as:

\begin{align} \lagr_i(X) &= \prod_{j\in[n),j\ne i} \frac{X-x_j}{x_i-x_j}\\\ &= \frac{\prod_{j\in[n),j\ne i} X-x_j}{\prod_{j\in[n),j\ne i} x_i-x_j} = \frac{\left(\prod_{j\in[n)} X-x_j\right) / (X-x_i)}{\prod_{j\in[n),j\ne i} x_i-x_j}\\\ &= \frac{A(X)/(X-x_i)}{A’(x_i)} = \emph{\frac{A(X)}{(X-x_i)\cdot A’(x_i)}} \end{align}

Let ω∈\F\omega\in \F denote a primitive nnth root of unity, where \F\F is a finite field.

Now, consider interpolating a polynomial ϕ\phi such that ϕ(ωi)=yi,∀i∈[n)\bydef[0,n−1]\phi(\omega^i)=y_i,\forall i\in[n)\bydef[0, n-1]. In other words, the xix_i’s are ωi\omega^i’s.

Then, we can use a simpler expression for the Lagrange polynomials. First, since ω\omega is a primitive nnth root of unity, then the polynomial A(X)A(X) simplifies to Xn−1X^n - 1. Second, its derivative A’(X)=(Xn−1)’=(Xn)’=nXn−1A’(X) = (X^n - 1)’ = (X^n)’ = nX^{n-1}.

\begin{align} \lagr_i(X) &= \frac{A(X)}{(X-\omega^i)\cdot A’(\omega^i)}\\ &= \frac{X^n - 1}{(X-\omega^i)\cdot n \omega^{i(n-1)}}\\ &= \frac{X^n - 1}{(X-\omega^i)\cdot n \omega^{in-i}}\\ &= \frac{X^n - 1}{(X-\omega^i)\cdot n \omega^{-i}} = \emph{\frac{(X^n - 1)\cdot \omega^i}{(X-\omega^i)\cdot n}}\\ \end{align}

Barycentric formula

Leveraging the above, we can re-write the interpolation formula:

\begin{align} \label{eq:barycentric-formula} \phi(X) = \sum_{i\in[n)} y_i \frac{(X^n - 1)\cdot \omega^i}{(X-\omega^i)\cdot n} &= \emph{\frac{X^n - 1}{n} \sum_{i\in[n)} y_i \frac{\omega^i}{X-\omega^i}}\\ &= \emph{\frac{1 - X^n}{n} \sum_{i\in[n)} y_i \frac{\omega^i}{\omega^i - X}}\\ \end{align}
This is known as the Barycentric formula.

[^vG13ModernCh10]: Fast polynomial evaluation and interpolation, by von zur Gathen, Joachim and Gerhard, Jurgen, in Modern Computer Algebra, 2013