diff --git a/Cargo.lock b/Cargo.lock index d5378adb..52d33d2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -564,7 +564,7 @@ dependencies = [ "include_dir", "itertools 0.12.1", "lazy_static", - "ndarray 0.15.6", + "ndarray 0.16.1", "ninterp", "nohash-hasher", "numpy", diff --git a/fastsim-core/Cargo.toml b/fastsim-core/Cargo.toml index 5f5d2c77..bc8e62b5 100644 --- a/fastsim-core/Cargo.toml +++ b/fastsim-core/Cargo.toml @@ -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 } diff --git a/fastsim-core/src/traits.rs b/fastsim-core/src/traits.rs index 042e880c..633ded27 100644 --- a/fastsim-core/src/traits.rs +++ b/fastsim-core/src/traits.rs @@ -31,14 +31,60 @@ impl Min for &[f64] { } impl Min for Vec { fn min(&self) -> anyhow::Result { - 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 { Ok(self.iter().fold(f64::INFINITY, |acc, curr| acc.min(**curr))) } } +impl Min for Vec<&f64> { + fn min(&self) -> anyhow::Result { + self.as_slice().min() + } +} +impl Min for &[Vec] { + fn min(&self) -> anyhow::Result { + self + .iter() + .map(|v| v.min()) + .try_fold(f64::INFINITY, |acc, x| Ok(acc.min(x?))) + } +} +impl Min for Vec> { + fn min(&self) -> anyhow::Result { + self.as_slice().min() + } +} +impl Min for &[Vec>] { + fn min(&self) -> anyhow::Result { + self.iter() + .map(|v| v.min()) + .try_fold(f64::INFINITY, |acc, x| Ok(acc.min(x?))) + } +} +impl Min for Vec>> { + fn min(&self) -> anyhow::Result { + self.as_slice().min() + } +} +impl Min for &ArrayD { + fn min(&self) -> anyhow::Result { + Ok(self.iter().fold(f64::INFINITY, |acc, x| acc.min(*x))) + } +} +impl Min for Interpolator { + fn min(&self) -> anyhow::Result { + 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; @@ -51,17 +97,61 @@ impl Max for &[f64] { } } impl Max for Vec { + fn max(&self) -> anyhow::Result { + self.as_slice().max() + } +} +impl Max for &[&f64] { fn max(&self) -> anyhow::Result { 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 { - Ok(self + self.as_slice().max() + } +} +impl Max for &[Vec] { + fn max(&self) -> anyhow::Result { + 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> { + fn max(&self) -> anyhow::Result { + self.as_slice().max() + } +} +impl Max for &[Vec>] { + fn max(&self) -> anyhow::Result { + self.iter() + .map(|v| v.max()) + .try_fold(f64::NEG_INFINITY, |acc, x| Ok(acc.max(x?))) + } +} +impl Max for Vec>> { + fn max(&self) -> anyhow::Result { + self.as_slice().max() + } +} +impl Max for &ArrayD { + fn max(&self) -> anyhow::Result { + Ok(self.iter().fold(f64::NEG_INFINITY, |acc, x| acc.max(*x))) + } +} +impl Max for Interpolator { + fn max(&self) -> anyhow::Result { + 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(), + } } } diff --git a/fastsim-core/src/utils/interp.rs b/fastsim-core/src/utils/interp.rs new file mode 100644 index 00000000..8a0d9950 --- /dev/null +++ b/fastsim-core/src/utils/interp.rs @@ -0,0 +1 @@ +use crate::imports::*; diff --git a/fastsim-core/src/utils/mod.rs b/fastsim-core/src/utils/mod.rs index 7c161c76..409f98c6 100755 --- a/fastsim-core/src/utils/mod.rs +++ b/fastsim-core/src/utils/mod.rs @@ -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";