diff --git a/CHANGELOG.md b/CHANGELOG.md index 74cbd51..aa81af0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - add 2d interpolation - add biliniar interpolation strategy - rename `Strategy` to `Interp1DStrategy` and `StrategyBuilder` to `Interp1DStrategyBuilder` + - make extrapolate filed of `Linear` private add `extrapolate(bool)` method instead. # 0.2.1 - change interp_array such that it can be called with any diff --git a/src/interp1d.rs b/src/interp1d.rs index c0a2531..3bd436c 100644 --- a/src/interp1d.rs +++ b/src/interp1d.rs @@ -140,7 +140,7 @@ where /// /// let interpolator = Interp1DBuilder::new(data) /// .x(x) - /// .strategy(Linear{extrapolate: false}) + /// .strategy(Linear::new()) /// .build().unwrap(); /// let result = interpolator.interp_array(&query).unwrap(); /// # assert_abs_diff_eq!(result, expected, epsilon=f64::EPSILON); @@ -180,7 +180,7 @@ where /// /// let interpolator = Interp1DBuilder::new(data) /// .x(x) - /// .strategy(Linear{extrapolate: false}) + /// .strategy(Linear::new()) /// .build().unwrap(); /// let result = interpolator.interp_array(&query).unwrap(); /// # assert_abs_diff_eq!(result, expected, epsilon=f64::EPSILON); @@ -286,7 +286,7 @@ where }) })), data, - strategy: Linear { extrapolate: false }, + strategy: Linear::new(), } } } diff --git a/src/interp1d/strategies/cubic_spline.rs b/src/interp1d/strategies/cubic_spline.rs index 7780e05..aa68ec6 100644 --- a/src/interp1d/strategies/cubic_spline.rs +++ b/src/interp1d/strategies/cubic_spline.rs @@ -244,6 +244,10 @@ impl CubicSpline { (c_a, c_b) } + + pub fn new() -> Self { + Self + } } #[derive(Debug)] diff --git a/src/interp1d/strategies/linear.rs b/src/interp1d/strategies/linear.rs index fa267ce..57b0830 100644 --- a/src/interp1d/strategies/linear.rs +++ b/src/interp1d/strategies/linear.rs @@ -10,7 +10,7 @@ use super::{Interp1DStrategy, Interp1DStrategyBuilder}; /// Linear Interpolation Strategy #[derive(Debug)] pub struct Linear { - pub extrapolate: bool, + extrapolate: bool, } impl Interp1DStrategyBuilder for Linear @@ -70,6 +70,17 @@ where } impl Linear { + /// create a linear interpolation stratgy + pub fn new() -> Self { + Self { extrapolate: false } + } + + /// set the extrapolate property, default is `false` + pub fn extrapolate(mut self, extrapolate: bool) -> Self { + self.extrapolate = extrapolate; + self + } + /// linearly interpolate/exrapolate between two points pub(crate) fn calc_frac((x1, y1): (T, T), (x2, y2): (T, T), x: T) -> T where diff --git a/src/interp2d/strategies/biliniar.rs b/src/interp2d/strategies/biliniar.rs index 3ad24e2..5661ee8 100644 --- a/src/interp2d/strategies/biliniar.rs +++ b/src/interp2d/strategies/biliniar.rs @@ -78,3 +78,9 @@ where Ok(()) } } + +impl Biliniar { + pub fn new() -> Self { + Self + } +} diff --git a/tests/cubic_spline_strat.rs b/tests/cubic_spline_strat.rs index 0ad8a4d..4ca2884 100644 --- a/tests/cubic_spline_strat.rs +++ b/tests/cubic_spline_strat.rs @@ -7,7 +7,7 @@ use ndarray_interp::{BuilderError, InterpolateError}; fn interp() { let data = array![1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 0.0, 2.0, 4.0, 6.0, 8.0]; let interp = Interp1D::builder(data) - .strategy(CubicSpline) + .strategy(CubicSpline::new()) .build() .unwrap(); let q = Array1::linspace(0.0, 11.0, 30); @@ -52,7 +52,7 @@ fn interp() { #[test] fn to_little_data() { let err = Interp1D::builder(array![1.0, 2.0]) - .strategy(CubicSpline) + .strategy(CubicSpline::new()) .build(); assert!(matches!(err, Err(BuilderError::NotEnoughData(_)))); } @@ -60,7 +60,7 @@ fn to_little_data() { #[test] fn enough_data() { Interp1D::builder(array![1.0, 2.0, 1.0]) - .strategy(CubicSpline) + .strategy(CubicSpline::new()) .build() .unwrap(); } @@ -68,7 +68,7 @@ fn enough_data() { #[test] fn extrapolate() { let interp = Interp1D::builder(array![1.0, 2.0, 1.0]) - .strategy(CubicSpline) + .strategy(CubicSpline::new()) .build() .unwrap(); let err = interp.interp_scalar(-0.5); diff --git a/tests/interp1d.rs b/tests/interp1d.rs index 89053d9..c3f17c1 100644 --- a/tests/interp1d.rs +++ b/tests/interp1d.rs @@ -32,7 +32,7 @@ fn interp_y_only() { #[test] fn extrapolate_y_only() { let interp = Interp1D::builder(array![1.0, 2.0, 1.5]) - .strategy(Linear { extrapolate: true }) + .strategy(Linear::new().extrapolate(true)) .build() .unwrap(); assert_eq!(*interp.interp(-1.0).unwrap().first().unwrap(), 0.0); @@ -43,7 +43,7 @@ fn extrapolate_y_only() { fn interp_with_x_and_y() { let interp = Interp1DBuilder::new(array![1.5, 2.0, 3.0, 4.0, 5.0, 7.0, 7.0, 8.0, 9.0, 10.5]) .x(array![-4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) - .strategy(Linear { extrapolate: false }) + .strategy(Linear::new()) .build() .unwrap(); assert_eq!(*interp.interp(-4.0).unwrap().first().unwrap(), 1.5); @@ -59,7 +59,7 @@ fn interp_with_x_and_y_expspaced() { .x(array![ 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0 ]) - .strategy(Linear { extrapolate: false }) + .strategy(Linear::new()) .build() .unwrap(); assert_eq!(*interp.interp(1.0).unwrap().first().unwrap(), 1.0); @@ -72,7 +72,7 @@ fn interp_with_x_and_y_expspaced() { fn extrapolate_with_x_and_y() { let interp = Interp1DBuilder::new(array![1.0, 0.0, 1.5]) .x(array![0.0, 1.0, 1.5]) - .strategy(Linear { extrapolate: true }) + .strategy(Linear::new().extrapolate(true)) .build() .unwrap(); assert_eq!(*interp.interp(-1.0).unwrap().first().unwrap(), 2.0); @@ -106,7 +106,7 @@ fn interp_y_only_out_of_bounds() { fn interp_with_x_and_y_out_of_bounds() { let interp = Interp1DBuilder::new(array![1.0, 2.0, 3.0]) .x(array![-4.0, -3.0, 2.0]) - .strategy(Linear { extrapolate: false }) + .strategy(Linear::new()) .build() .unwrap(); assert!(matches!(