Skip to content

Commit

Permalink
Merge pull request #946 from rust-ndarray/array-approx
Browse files Browse the repository at this point in the history
Add "forwards" of the approx methods .abs_diff_eq and .relative_eq
  • Loading branch information
bluss authored Mar 18, 2021
2 parents 0970fc5 + 5bc7631 commit 141facd
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 @@ -170,8 +170,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 141facd

Please sign in to comment.