Lagrange Interpolation with Python

Lagrange interpolation is a technique for computing a polynomial that passes through a set of nn points.

Interpolating a vector as a polynomial

Examples

A straight line through two points

Consider that if we have two points, they can be interpolated with a line. For example, given (1,1)(1, 1) and (2,2)(2, 2), we can draw a line that intersects both points, it would be a degree 11 polynomial y=xy = x.

A single point

Now consider that if we have one point, we can draw a line through that point with a degree 0 polynomial. For example, if the point is (3,5)(3, 5) we can draw a line through it y=5y = 5 (which is a degree 00 polynomial).

Three points and a parabola

The pattern that we can “draw a polynomial through” nn points with a (at most) degree n1n - 1 polynomial holds for any number of points. For example, the points (0,0),(1,1),(2,4)(0, 0), (1, 1), (2, 4) can be interpolated with y=x2y = x^2. If those points happened to be a straight line, e.g. (0,0),(1,1),(2,2)(0, 0), (1, 1), (2, 2), then we could draw a line through (1,1)(1, 1) and (2,2)(2, 2) with a degree 1 polynomial y=xy = x, but in general, three points won’t be collinear, so we’ll need a degree 2 polynomial to cross all the points.

Python code for Lagrange interpolation

For our purposes it isn’t important to understand how to compute this polynomial, as there are math libraries that will do it for us. The most common algorithm is Lagrange interpolation and we show how to do that in Python.

Float example

We can compute a polynomial p(x)p(x) that crosses through the points (1,4),(2,8),(3,2),(4,1)(1,4), (2,8), (3,2), (4,1) using Lagrange interpolation.

from scipy.interpolate import lagrange
x_values = [1, 2, 3, 4]
y_values = [4, 8, 2, 1]

print(lagrange(x_values, y_values))
#      3      2
# 2.5 x - 20 x + 46.5 x - 25

Finite field example

Let’s use the same polynomial as before, but this time we’ll use a finite field F17\mathbb{F}_{17} instead of floating point numbers.

import galois
import numpy as np
GF17 = galois.GF(17)

xs = GF17(np.array([1,2,3,4]))
ys = GF17(np.array([4,8,2,1]))

p = galois.lagrange_poly(xs, ys)

assert p(1) == GF17(4)
assert p(2) == GF17(8)
assert p(3) == GF17(2)
assert p(4) == GF17(1)

Uniqueness of the interpolating polynomial

Going back to our example of the points (1,1),(2,2)(1, 1), (2, 2), the lowest degree polynomial that interpolates them is y=xy = x. In general,

For a set of nn points, there is a unique lowest-degree polynomial of at most degree n1n - 1 that interpolates them.

The polynomial of lowest degree that interpolates the points is sometimes called the Lagrange polynomial.

The consequence of this is that

If we use the points (1,2,...,n)(1,2,...,n) as the xx values to convert a length nn vector to a polynomial via Lagrange interpolation, then the resulting polynomial is unique.

In other words, given a consistent basis of x-values to interpolate a vector over, there is a unique polynomial that interpolates a given vector. Spoken another way, every length nn vector has a unique polynomial representation.

Informally, every nn degree vector has a unique n1n - 1 degree polynomial that “represents” it. The degree could be less if, for example, the points are collinear, but the vector will be unique.

The “lowest degree” part is important. Given two points, there are an extremely large number of polynomials that cross those two points – but the lowest degree polynomial is unique.

Ready to Get Started?Join Thousands of Users Today

Start your free trial now and experience the difference. No credit card required.

© 2025 Better-Start. All rights reserved.