Skip to main content

qualia_core_db/solvers/interpolation/
mod.rs

1//! **Interpolation & function approximation** (Gap analysis §3.7).
2//!
3//! * [`lagrange`] — Lagrange and Newton divided-difference polynomial interpolation.
4//! * [`spline`] — natural cubic spline (tridiagonal Thomas solve) and linear interpolation.
5//! * [`least_squares`] — polynomial least-squares fit via the normal equations.
6//!
7//! Fail-closed ([`InterpolationError`]): empty/mismatched data, duplicate nodes, an
8//! over-high fit degree, or a singular system return an error rather than a fabricated
9//! curve.
10
11pub mod lagrange;
12pub mod least_squares;
13pub mod spline;
14
15pub use lagrange::{lagrange_eval, newton_coefficients, newton_eval};
16pub use least_squares::{poly_eval, poly_fit};
17pub use spline::{linear_interp, CubicSpline};
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum InterpolationError {
21    /// Fewer points than the method needs, or x/y length mismatch.
22    InsufficientData,
23    /// Two sample nodes share an x (interpolant undefined).
24    DuplicateNodes,
25    /// Requested fit degree ≥ number of points, or otherwise invalid.
26    InvalidDegree,
27    /// The linear system was singular / rank-deficient.
28    Singular,
29}
30
31impl core::fmt::Display for InterpolationError {
32    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33        match self {
34            InterpolationError::InsufficientData => write!(f, "insufficient interpolation data"),
35            InterpolationError::DuplicateNodes => write!(f, "duplicate interpolation nodes"),
36            InterpolationError::InvalidDegree => write!(f, "invalid fit degree"),
37            InterpolationError::Singular => write!(f, "singular linear system"),
38        }
39    }
40}
41impl std::error::Error for InterpolationError {}