pub fn poly_fit_exact(
points: &[(Ratio<BigInt>, Ratio<BigInt>)],
degree: usize,
) -> Result<Vec<Ratio<BigInt>>, SymplexError>Expand description
Exact least-squares polynomial fit over ℚ.
Solves the normal equations AᵀA·c = Aᵀy for the Vandermonde matrix
A with exact rational Gaussian elimination, so the returned
coefficients (in ascending degree) are the exact least-squares
solution — for consistent data, the exact interpolating polynomial.
§Errors
SymplexError::InvalidArgument if degree >= points.len();
SymplexError::ComputationFailed if the normal matrix is singular
(fewer than degree + 1 distinct abscissae).
§Examples
use num_bigint::BigInt;
use num_rational::Ratio;
use symplex::optimize::poly_fit_exact;
let q = |p: i64, d: i64| Ratio::new(BigInt::from(p), BigInt::from(d));
// y = x²/3 − x/2 + 1/7 sampled at x = 0, 1, 2, 3, 4 (five points, degree 2).
let pts = [
(q(0, 1), q(1, 7)),
(q(1, 1), q(-1, 42)),
(q(2, 1), q(10, 21)),
(q(3, 1), q(23, 14)),
(q(4, 1), q(73, 21)),
];
let c = poly_fit_exact(&pts, 2).unwrap();
assert_eq!(c, vec![q(1, 7), q(-1, 2), q(1, 3)]);