Post · 5 min read· pp. 135–137
Recall from our basics discussion that a polynomial ϕ of degree d is a vector of d+1 coefficients:
ϕ=[ϕ0,ϕ1,ϕ2,…,ϕd]
How to compute a polynomial’s coefficients from a bunch of its evaluations
Given n pairs (xi,yi)i∈[n), one can compute or interpolate a degree ≤n−1 polynomial ϕ(X) such that:
ϕ(xi)=yi,∀i∈[n)
Specifically, the Lagrange interpolation formula says that:
\labeleq:lagrange−formulaϕ(X)=i∈[n)∑yi⋅\lagri(X), where \lagri(X)=j∈[n),j=i∏xi−xjX−xj
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) is defined so that it has two properties:
- \lagri(xi)=1,∀i∈[n)
- \lagri(xj)=0,∀j∈[n)∖{i}
You can actually convince yourself that \lagri(X) has these properties by plugging in xi and xj to see what happens. For example, assume n=3. Let’s look at L2(X):
L2(X)=x2−x1X−x1⋅x2−x3X−x3
Clearly,
L2(x2)=1 and
L2(x3)=0.
Important: The \lagri(X) Lagrange polynomials are dependent on the set of xi’s only (and thus on n)! Specifically each \lagri(X) has degree n−1 and has a root at each xj when j=i!
In this sense, a better notation for them would be \lagri[xi,n](X) or \lagri[n)(X) to indicate this dependence.
Example: Interpolating a polynomial from three evaluations
Consider the following example with n=3 pairs of points.
Then, by the Lagrange formula, we have:
ϕ(X)=y1\lagr1(X)+y2\lagr2(X)+y3\lagr3(X)
Next, by applying the two key properties of \lagri(X) from above, you can easily check that ϕ(xi)=yi,∀i∈[3]:
ϕ(x1)ϕ(x2)ϕ(x3)=y1\lagr1(x1)+y2\lagr2(x1)+y3\lagr3(x1)=y1⋅1+y2⋅0+y3⋅0=y1=y1\lagr1(x2)+y2\lagr2(x2)+y3\lagr3(x2)=y1⋅0+y2⋅1+y3⋅0=y2=y1\lagr1(x3)+y2\lagr2(x3)+y3\lagr3(x3)=y1⋅0+y2⋅0+y3⋅1=y3
An important detail is that the degree of the interpolated ϕ(X) is ≤n−1 and not necessarily exactly equal to n−1.
To see this, consider interpolating the polynomial ϕ(X) such that ϕ(i)=i for all i∈[n).
In other words, xi=yi=i.
The inspired reader might notice that the polynomial ϕ(X)=X could satisfy our constraints.
But is this what the Lagrange interpolation will return?
After all, the interpolated ϕ(X) is a sum of degree n−1 polynomials \lagri(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=3:
ϕ(X)=i∈[3]∑i⋅\lagri(X)=i∈[3]∑i⋅j∈[3]∖i∏i−jX−j=1⋅1−2X−21−3X−3+2⋅2−1X−12−3X−3+3⋅3−1X−13−2X−2=−1X−2−2X−3+2⋅1X−1−1X−3+3⋅2X−11X−2=21(X−2)(X−3)−2(X−1)(X−3)+23(X−1)(X−2)=21[(X−2)(X−3)+3(X−1)(X−2)]−2(X−1)(X−3)=21[(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
Computational overhead of Lagrange interpolation
If done naively, interpolating ϕ(X) using the Lagrange formula in Equation \refeq:lagrange−formula will take O(n2) time.
However, there are known techniques for computing ϕ(X) in O(nlog2n) 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)A’(X)=j∈[n)∏(X−xj)=i∈[n)∑j∈[n),j=i∏(X−xj)
See this post for how the derivative A’(X) of A(X) is derived.
Then, the Lagrange polynomials from Eq. \refeq: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 denote a primitive nth root of unity, where \F is a finite field.
Now, consider interpolating a polynomial ϕ such that ϕ(ωi)=yi,∀i∈[n)\bydef[0,n−1].
In other words, the xi’s are ωi’s.
Then, we can use a simpler expression for the Lagrange polynomials.
First, since ω is a primitive nth root of unity, then the polynomial A(X) simplifies to Xn−1.
Second, its derivative A’(X)=(Xn−1)’=(Xn)’=nXn−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}
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