#![cfg(feature = "data-fitting")]
use approx::assert_abs_diff_eq;
use onemkl::data_fitting::CubicSpline1d;
#[test]
fn cubic_spline_passes_through_knots() {
let x: Vec<f64> = (0..5).map(|i| i as f64).collect();
let y: Vec<f64> = x.iter().map(|t| t * t).collect();
let spline = CubicSpline1d::natural(x.clone(), y.clone()).unwrap();
let evaluated = spline.interpolate(&x).unwrap();
for (yi, ei) in y.iter().zip(&evaluated) {
assert_abs_diff_eq!(*yi, *ei, epsilon = 1e-10);
}
}
#[test]
fn cubic_spline_interpolates_smoothly() {
let x: Vec<f64> = (0..6).map(|i| i as f64).collect();
let y: Vec<f64> = x.iter().map(|t| t * t).collect();
let spline = CubicSpline1d::natural(x.clone(), y.clone()).unwrap();
let probe = vec![0.5_f64, 1.5, 2.5, 3.5, 4.5];
let evaluated = spline.interpolate(&probe).unwrap();
for (s, t) in probe.iter().zip(&evaluated) {
assert!((s * s - *t).abs() < 0.1, "site {} → {}", s, t);
}
}
#[test]
fn integrate_constant_function() {
let x = vec![0.0_f64, 1.0, 2.0, 3.0, 4.0];
let y = vec![5.0_f64; 5];
let spline = CubicSpline1d::natural(x, y).unwrap();
let res = spline.integrate(&[0.0_f64], &[4.0_f64]).unwrap();
assert_abs_diff_eq!(res[0], 20.0, epsilon = 1e-9);
}
#[test]
fn integrate_linear_function() {
let x = vec![0.0_f64, 1.0, 2.0, 3.0, 4.0];
let y: Vec<f64> = x.iter().map(|t| 2.0 * t).collect();
let spline = CubicSpline1d::natural(x, y).unwrap();
let res = spline.integrate(&[0.0_f64], &[4.0_f64]).unwrap();
assert_abs_diff_eq!(res[0], 16.0, epsilon = 1e-9);
}
#[test]
fn invalid_lengths_rejected() {
let r = CubicSpline1d::<f64>::natural(vec![1.0, 2.0], vec![1.0]);
assert!(r.is_err());
}
#[test]
fn too_few_knots_rejected() {
let r = CubicSpline1d::<f64>::natural(vec![1.0], vec![1.0]);
assert!(r.is_err());
}