Skip to content

Commit

Permalink
FEAT: Add "forwards" of the approx methods .abs_diff_eq and .relative_eq
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bluss committed Mar 18, 2021
1 parent 09884fc commit 5bc7631
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
47 changes: 44 additions & 3 deletions src/array_approx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}

Expand All @@ -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()))
}
}

Expand All @@ -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<A, S, D> ArrayBase<S, D>
where
S: Data<Elem = A>,
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<S2>(&self, other: &ArrayBase<S2, D>, epsilon: A::Epsilon) -> bool
where
A: AbsDiffEq<S2::Elem>,
A::Epsilon: Clone,
S2: Data,
{
<Self as AbsDiffEq<_>>::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<S2>(
&self,
other: &ArrayBase<S2, D>,
epsilon: A::Epsilon,
max_relative: A::Epsilon,
) -> bool
where
A: RelativeEq<S2::Elem>,
A::Epsilon: Clone,
S2: Data
{
<Self as RelativeEq<_>>::relative_eq(self, other, epsilon, max_relative)
}
}


#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5bc7631

Please sign in to comment.