From 5bc7631073612efcdcb247a44ea99a8034c29b7f Mon Sep 17 00:00:00 2001 From: bluss Date: Wed, 17 Mar 2021 22:29:10 +0100 Subject: [PATCH] FEAT: Add "forwards" of the approx methods .abs_diff_eq and .relative_eq This is a nod to the all_close method that was removed. The replacement methods still require the "approx" feature gate, but we forward them as inherent methods on the array, to make them easier to call (no trait import needed). --- src/array_approx.rs | 47 ++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 8 ++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/array_approx.rs b/src/array_approx.rs index a2e9c2327..bb6804a27 100644 --- a/src/array_approx.rs +++ b/src/array_approx.rs @@ -21,9 +21,10 @@ where if self.shape() != other.shape() { return false; } + Zip::from(self) .and(other) - .all(|a, b| A::abs_diff_eq(a, b, epsilon.clone())) + .all(move |a, b| A::abs_diff_eq(a, b, epsilon.clone())) } } @@ -49,9 +50,10 @@ where if self.shape() != other.shape() { return false; } + Zip::from(self) .and(other) - .all(|a, b| A::relative_eq(a, b, epsilon.clone(), max_relative.clone())) + .all(move |a, b| A::relative_eq(a, b, epsilon.clone(), max_relative.clone())) } } @@ -72,12 +74,51 @@ where if self.shape() != other.shape() { return false; } + Zip::from(self) .and(other) - .all(|a, b| A::ulps_eq(a, b, epsilon.clone(), max_ulps)) + .all(move |a, b| A::ulps_eq(a, b, epsilon.clone(), max_ulps)) } } +impl ArrayBase +where + S: Data, + D: Dimension, +{ + /// A test for equality that uses the elementwise absolute difference to compute the + /// approximate equality of two arrays. + /// + /// **Requires crate feature `"approx"`** + pub fn abs_diff_eq(&self, other: &ArrayBase, epsilon: A::Epsilon) -> bool + where + A: AbsDiffEq, + A::Epsilon: Clone, + S2: Data, + { + >::abs_diff_eq(self, other, epsilon) + } + + /// A test for equality that uses an elementwise relative comparison if the values are far + /// apart; and the absolute difference otherwise. + /// + /// **Requires crate feature `"approx"`** + pub fn relative_eq( + &self, + other: &ArrayBase, + epsilon: A::Epsilon, + max_relative: A::Epsilon, + ) -> bool + where + A: RelativeEq, + A::Epsilon: Clone, + S2: Data + { + >::relative_eq(self, other, epsilon, max_relative) + } +} + + #[cfg(test)] mod tests { use crate::prelude::*; diff --git a/src/lib.rs b/src/lib.rs index f48da4b32..7a7a201ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,8 +168,6 @@ mod aliases; #[macro_use] mod itertools; mod argument_traits; -#[cfg(feature = "approx")] -mod array_approx; #[cfg(feature = "serde")] mod array_serde; mod arrayformat; @@ -1520,6 +1518,9 @@ impl<'a, A> CowRepr<'a, A> { } } +// NOTE: The order of modules decides in which order methods on the type ArrayBase +// (mainly mentioning that as the most relevant type) show up in the documentation. +// Consider the doc effect of ordering modules here. mod impl_clone; mod impl_internal_constructors; @@ -1613,6 +1614,9 @@ pub mod linalg; mod impl_ops; pub use crate::impl_ops::ScalarOperand; +#[cfg(feature = "approx")] +mod array_approx; + // Array view methods mod impl_views;