Skip to content

Commit

Permalink
make extrapolate field private
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasBoss committed Aug 1, 2023
1 parent d832d5e commit d5e2d59
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/interp1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -286,7 +286,7 @@ where
})
})),
data,
strategy: Linear { extrapolate: false },
strategy: Linear::new(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/interp1d/strategies/cubic_spline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ impl CubicSpline {

(c_a, c_b)
}

pub fn new() -> Self {
Self
}
}

#[derive(Debug)]
Expand Down
13 changes: 12 additions & 1 deletion src/interp1d/strategies/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{Interp1DStrategy, Interp1DStrategyBuilder};
/// Linear Interpolation Strategy
#[derive(Debug)]
pub struct Linear {
pub extrapolate: bool,
extrapolate: bool,
}

impl<Sd, Sx, D> Interp1DStrategyBuilder<Sd, Sx, D> for Linear
Expand Down Expand Up @@ -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<T>((x1, y1): (T, T), (x2, y2): (T, T), x: T) -> T
where
Expand Down
6 changes: 6 additions & 0 deletions src/interp2d/strategies/biliniar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ where
Ok(())
}
}

impl Biliniar {
pub fn new() -> Self {
Self
}
}
8 changes: 4 additions & 4 deletions tests/cubic_spline_strat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -52,23 +52,23 @@ 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(_))));
}

#[test]
fn enough_data() {
Interp1D::builder(array![1.0, 2.0, 1.0])
.strategy(CubicSpline)
.strategy(CubicSpline::new())
.build()
.unwrap();
}

#[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);
Expand Down
10 changes: 5 additions & 5 deletions tests/interp1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit d5e2d59

Please sign in to comment.