Skip to content

fionn/lagrange-polynomial

Repository files navigation

Lagrange Polynomials

Module to generate Lagrange polynomials over integers for 1-dimensional data. This is generally useful for interpolation.

Installation

Install with pip.

Usage

Example

from lagrange_polynomial import LagrangePolynomial

xs = range(100)
ys = [f(x) for x in xs]          # For some function f

lp = LagrangePolynomial(xs, ys)  # Instantiate a polynomial with sequences of
                                 # x- and y-coordinates.

for x in xs:
    assert ys[x] == lp(x)        # Polynomial will intersect original points
    coefficient = lp.basis[0](x) # Get the 0th basis vector at x

Interface

The LagrangePolynomial class takes two equally-sized sequences and an optional integer p. The instance is a Lagrange polynomial L: x -> L(x) over GF(p). If p is not provided, it defaults to the 8th Mersenne prime M31.

It has a basis property, a LagrangeBasis object subclassing Sequence. Each element ℓⱼ indexed by integers j in range(len(xs)) is a function taking x to its jth basis vector ℓⱼ(x).

Test

Test with make test.