Skip to content

Commit

Permalink
refactor: tiny refactor of interpolators
Browse files Browse the repository at this point in the history
  • Loading branch information
avhz authored and avhz committed Jun 30, 2024
1 parent 6d0ef24 commit ed912ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/math/interpolation/exponential_interpolator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use super::Interpolator;
use crate::math::interpolation::{InterpolationError, InterpolationIndex, InterpolationValue};
use num::Float;
use std::cmp::Ordering;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STRUCTS & ENUMS
Expand Down Expand Up @@ -96,9 +97,10 @@ where
fn interpolate(&self, point: IndexType) -> Result<ValueType, InterpolationError> {
let range = self.range();

if point.partial_cmp(&range.0).unwrap() == std::cmp::Ordering::Less
|| point.partial_cmp(&range.1).unwrap() == std::cmp::Ordering::Greater
{
let check1 = point.partial_cmp(&range.0).unwrap() == Ordering::Less;
let check2 = point.partial_cmp(&range.1).unwrap() == Ordering::Greater;

if check1 || check2 {
return Err(InterpolationError::OutsideOfRange);
}

Expand All @@ -110,14 +112,19 @@ where
}

let idx_r = self.xs.partition_point(|&x| x < point);
let idx_l = idx_r - 1;

let x_l = self.xs[idx_r - 1];
let y_l = self.ys[idx_r - 1];
let x_l = self.xs[idx_l];
let y_l = self.ys[idx_l];

let x_r = self.xs[idx_r];
let y_r = self.ys[idx_r];

let result = ((y_r.ln() - y_l.ln()) * ((point - x_l) / (x_r - x_l)) + y_l.ln()).exp();
let term1 = y_r.ln() - y_l.ln();
let term2 = (point - x_l) / (x_r - x_l);
let term3 = y_l.ln();

let result = (term1 * term2 + term3).exp();

Ok(result)
}
Expand Down
12 changes: 9 additions & 3 deletions src/math/interpolation/linear_interpolator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ where
let idx_r = self.xs.partition_point(|&x| x < point);
let idx_l = idx_r - 1;

let term_1 = self.ys[idx_r] - self.ys[idx_l];
let term_2 = (point - self.xs[idx_l]) / (self.xs[idx_r] - self.xs[idx_l]);
let x_l = self.xs[idx_l];
let x_r = self.xs[idx_r];

let result = self.ys[idx_l] + term_1 * term_2;
let y_l = self.ys[idx_l];
let y_r = self.ys[idx_r];

let term_1 = y_r - y_l;
let term_2 = (point - x_l) / (x_r - x_l);

let result = y_l + term_1 * term_2;

Ok(result)
}
Expand Down

0 comments on commit ed912ff

Please sign in to comment.