Skip to content

Add maths/numerical_analysis/lagrange_interpolation.py#14700

Open
ltianyi992 wants to merge 2 commits into
TheAlgorithms:masterfrom
ltianyi992:feature/lagrange-interpolation
Open

Add maths/numerical_analysis/lagrange_interpolation.py#14700
ltianyi992 wants to merge 2 commits into
TheAlgorithms:masterfrom
ltianyi992:feature/lagrange-interpolation

Conversation

@ltianyi992
Copy link
Copy Markdown

Description

Adds Lagrange polynomial interpolation to the numerical analysis module — a classic method currently missing from the repository.

What it does

Given n+1 data points (x_0, y_0), ..., (x_n, y_n) with distinct x-values, the Lagrange interpolating polynomial is the unique polynomial of degree ≤ n passing through all given points:

P(x) = Σ_i  y_i · L_i(x)

where each basis polynomial is:

L_i(x) = Π_{j ≠ i} (x - x_j) / (x_i - x_j)

Why it complements the existing code

The repo already has newton_forward_interpolation.py, which requires equidistant x-values. Lagrange interpolation works for arbitrarily spaced data — a natural complement.

Example

>>> from maths.numerical_analysis.lagrange_interpolation import lagrange_interpolation

# Interpolate through x^2 data — exact for degree-2 polynomials
>>> lagrange_interpolation([1, 2, 3], [1, 4, 9], 2.5)
6.25

# Four points from x^3, non-grid query
>>> lagrange_interpolation([0, 1, 2, 3], [0, 1, 8, 27], 1.5)
3.375

Checklist

  • All functions have type hints
  • 9 passing doctests (happy-path + edge cases + error cases)
  • ruff check --isolated passes with no violations
  • python -m doctest passes with no failures
  • References to Wikipedia and Mathworld in docstring

References

james and others added 2 commits May 18, 2026 21:03
Implements Lagrange polynomial interpolation for arbitrarily spaced
data points. Unlike Newton's forward-difference formula (which requires
equidistant x-values), Lagrange interpolation works with any distinct
x_points.

Includes full type hints, 9 doctests, and raises ValueError for
mismatched lengths, too few points, or duplicate x-values.

References:
- https://en.wikipedia.org/wiki/Lagrange_polynomial
- https://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant