Skip to content

Commit

Permalink
add min and max for ninterp stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarow committed Jan 14, 2025
1 parent e3729f7 commit 45daa06
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fastsim-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ include_dir = { version = "0.7.3", optional = true }
itertools = "0.12.0"
lazy_static = "1.4.0"
regex = "1.10.3"
ndarray = "0.15.6"
ndarray = "0.16.1"
toml = { version = "0.8.12", optional = true }
derive_more = { version = "1.0.0", features = ["from_str", "from", "is_variant", "try_into"] }
ureq = { version = "2.9.1", optional = true }
Expand Down
100 changes: 95 additions & 5 deletions fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,60 @@ impl Min for &[f64] {
}
impl Min for Vec<f64> {
fn min(&self) -> anyhow::Result<f64> {
Ok(self.iter().fold(f64::INFINITY, |acc, curr| acc.min(*curr)))
self.as_slice().min()
}
}
impl Min for Vec<&f64> {
impl Min for &[&f64] {
fn min(&self) -> anyhow::Result<f64> {
Ok(self.iter().fold(f64::INFINITY, |acc, curr| acc.min(**curr)))
}
}
impl Min for Vec<&f64> {
fn min(&self) -> anyhow::Result<f64> {
self.as_slice().min()
}
}
impl Min for &[Vec<f64>] {
fn min(&self) -> anyhow::Result<f64> {
self
.iter()
.map(|v| v.min())
.try_fold(f64::INFINITY, |acc, x| Ok(acc.min(x?)))
}
}
impl Min for Vec<Vec<f64>> {
fn min(&self) -> anyhow::Result<f64> {
self.as_slice().min()
}
}
impl Min for &[Vec<Vec<f64>>] {
fn min(&self) -> anyhow::Result<f64> {
self.iter()
.map(|v| v.min())
.try_fold(f64::INFINITY, |acc, x| Ok(acc.min(x?)))
}
}
impl Min for Vec<Vec<Vec<f64>>> {
fn min(&self) -> anyhow::Result<f64> {
self.as_slice().min()
}
}
impl Min for &ArrayD<f64> {
fn min(&self) -> anyhow::Result<f64> {
Ok(self.iter().fold(f64::INFINITY, |acc, x| acc.min(*x)))
}
}
impl Min for Interpolator {
fn min(&self) -> anyhow::Result<f64> {
match self {
Interpolator::Interp0D(value) => Ok(*value),
Interpolator::Interp1D(interp) => interp.f_x().min(),
Interpolator::Interp2D(interp) => interp.f_xy().min(),
Interpolator::Interp3D(interp) => interp.f_xyz().min(),
Interpolator::InterpND(interp) => interp.values().min(),
}
}
}

pub trait Max {
fn max(&self) -> anyhow::Result<f64>;
Expand All @@ -51,17 +97,61 @@ impl Max for &[f64] {
}
}
impl Max for Vec<f64> {
fn max(&self) -> anyhow::Result<f64> {
self.as_slice().max()
}
}
impl Max for &[&f64] {
fn max(&self) -> anyhow::Result<f64> {
Ok(self
.iter()
.fold(f64::NEG_INFINITY, |acc, curr| acc.max(*curr)))
.fold(f64::NEG_INFINITY, |acc, curr| acc.max(**curr)))
}
}
impl Max for Vec<&f64> {
fn max(&self) -> anyhow::Result<f64> {
Ok(self
self.as_slice().max()
}
}
impl Max for &[Vec<f64>] {
fn max(&self) -> anyhow::Result<f64> {
self
.iter()
.fold(f64::NEG_INFINITY, |acc, curr| acc.max(**curr)))
.map(|v| v.max())
.try_fold(f64::NEG_INFINITY, |acc, x| Ok(acc.max(x?)))
}
}
impl Max for Vec<Vec<f64>> {
fn max(&self) -> anyhow::Result<f64> {
self.as_slice().max()
}
}
impl Max for &[Vec<Vec<f64>>] {
fn max(&self) -> anyhow::Result<f64> {
self.iter()
.map(|v| v.max())
.try_fold(f64::NEG_INFINITY, |acc, x| Ok(acc.max(x?)))
}
}
impl Max for Vec<Vec<Vec<f64>>> {
fn max(&self) -> anyhow::Result<f64> {
self.as_slice().max()
}
}
impl Max for &ArrayD<f64> {
fn max(&self) -> anyhow::Result<f64> {
Ok(self.iter().fold(f64::NEG_INFINITY, |acc, x| acc.max(*x)))
}
}
impl Max for Interpolator {
fn max(&self) -> anyhow::Result<f64> {
match self {
Interpolator::Interp0D(value) => Ok(*value),
Interpolator::Interp1D(interp) => interp.f_x().max(),
Interpolator::Interp2D(interp) => interp.f_xy().max(),
Interpolator::Interp3D(interp) => interp.f_xyz().max(),
Interpolator::InterpND(interp) => interp.values().max(),
}
}
}

Expand Down
1 change: 1 addition & 0 deletions fastsim-core/src/utils/interp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use crate::imports::*;
3 changes: 3 additions & 0 deletions fastsim-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::imports::*;
use paste::paste;
use regex::Regex;

pub mod interp;
pub use interp::*;

impl Init for ninterp::Interpolator {}
impl SerdeAPI for ninterp::Interpolator {
const RESOURCE_PREFIX: &'static str = "interpolators";
Expand Down

0 comments on commit 45daa06

Please sign in to comment.