Skip to content

Add "forwards" of the approx methods .abs_diff_eq and .relative_eq #946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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