Skip to content

Commit b4f8553

Browse files
committed
Cubic-Splines: Ensure interpolator is fitted when interpolating otherwise raise error
1 parent c63445e commit b4f8553

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

crates/RustQuant_math/src/interpolation/cubic_spline.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ where
377377
}
378378

379379
fn interpolate(&self, point: IndexType) -> Result<ValueType, RustQuantError> {
380+
if !self.fitted {
381+
return Err(RustQuantError::Unfitted);
382+
}
380383
let range = self.range();
381384
if point.partial_cmp(&range.0).unwrap() == std::cmp::Ordering::Less
382385
|| point.partial_cmp(&range.1).unwrap() == std::cmp::Ordering::Greater
@@ -491,6 +494,22 @@ mod tests_cubic_spline_interpolation {
491494
RUSTQUANT_EPSILON
492495
);
493496
}
497+
498+
#[test]
499+
fn test_cubic_spline_unfitted() {
500+
501+
let xs: Vec<f64> = vec![0., 1., 2., 3., 4.];
502+
let ys: Vec<f64> = vec![0., 1., 16., 81., 256.];
503+
504+
let interpolator: CubicSplineInterpolator<f64, f64> = CubicSplineInterpolator::new(xs, ys).unwrap();
505+
506+
assert!(
507+
matches!(
508+
interpolator.interpolate(2.5),
509+
Err(RustQuantError::Unfitted),
510+
)
511+
);
512+
}
494513
}
495514

496515
#[cfg(test)]

0 commit comments

Comments
 (0)