This project is a raw Rust implementation of Lagrange Interpolation. It takes a set of distinct points
The program calculates the Lagrange Interpolating Polynomial using the formula:
Where
-
ljFunction: Calculates the coefficients for a specific basis polynomial$L_j(x)$ given a specific point$(x_j, y_j)$ and the list of all$x$ values. It handles the polynomial expansion (multiplication of binomials) and normalization by the denominator. -
sum_of_listsFunction: Performs vector addition to sum the coefficients of the individual basis polynomials. -
mainFunction: Defines the dataset, iterates through all points to generate the basis polynomials, and accumulates them into the final polynomial.
You must have the Rust toolchain installed. If you don't have it, install it via rustup.rs.
-
Save the file: Ensure your code is saved as
main.rs. -
Compile and Run: You can use
rustcdirectly or run it inside a Cargo project.Option A: Using
rustc(easiest for single file)rustc main.rs ./main # On Windows, use .\main.exeOption B: Using Cargo
- Create a project:
cargo new lagrange_interpolation - Replace
src/main.rswith this code. - Run:
cargo run
- Create a project:
Currently, the input vectors are hardcoded in the main function:
- X values:
[1.2, 2.1, 3.2] - Y values:
[2.4, 3.0, 4.1]
The program outputs a vector representing the coefficients of the polynomial in descending order of power (highest degree first).
Example Output:
[0.60927..., -1.4398..., 3.235...]