Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature-gate approx trait implementations
- Loading branch information
1 parent
984cf22
commit 79b2541
Showing
5 changed files
with
166 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use crate::imp_prelude::*; | ||
use approx::{AbsDiffEq, RelativeEq, UlpsEq}; | ||
|
||
/// **Requires crate feature `"approx"`** | ||
impl<A, S, D> AbsDiffEq for ArrayBase<S, D> | ||
where | ||
A: AbsDiffEq, | ||
A::Epsilon: Clone, | ||
S: Data<Elem = A>, | ||
D: Dimension, | ||
{ | ||
type Epsilon = A::Epsilon; | ||
|
||
fn default_epsilon() -> A::Epsilon { | ||
A::default_epsilon() | ||
} | ||
|
||
fn abs_diff_eq(&self, other: &ArrayBase<S, D>, epsilon: A::Epsilon) -> bool { | ||
if self.shape() != other.shape() { | ||
return false; | ||
} | ||
Zip::from(self) | ||
.and(other) | ||
.fold_while(true, |_, a, b| { | ||
if A::abs_diff_ne(a, b, epsilon.clone()) { | ||
FoldWhile::Done(false) | ||
} else { | ||
FoldWhile::Continue(true) | ||
} | ||
}) | ||
.into_inner() | ||
} | ||
} | ||
|
||
/// **Requires crate feature `"approx"`** | ||
impl<A, S, D> RelativeEq for ArrayBase<S, D> | ||
where | ||
A: RelativeEq, | ||
A::Epsilon: Clone, | ||
S: Data<Elem = A>, | ||
D: Dimension, | ||
{ | ||
fn default_max_relative() -> A::Epsilon { | ||
A::default_max_relative() | ||
} | ||
|
||
fn relative_eq( | ||
&self, | ||
other: &ArrayBase<S, D>, | ||
epsilon: A::Epsilon, | ||
max_relative: A::Epsilon, | ||
) -> bool { | ||
if self.shape() != other.shape() { | ||
return false; | ||
} | ||
Zip::from(self) | ||
.and(other) | ||
.fold_while(true, |_, a, b| { | ||
if A::relative_ne(a, b, epsilon.clone(), max_relative.clone()) { | ||
FoldWhile::Done(false) | ||
} else { | ||
FoldWhile::Continue(true) | ||
} | ||
}) | ||
.into_inner() | ||
} | ||
} | ||
|
||
/// **Requires crate feature `"approx"`** | ||
impl<A, S, D> UlpsEq for ArrayBase<S, D> | ||
where | ||
A: UlpsEq, | ||
A::Epsilon: Clone, | ||
S: Data<Elem = A>, | ||
D: Dimension, | ||
{ | ||
fn default_max_ulps() -> u32 { | ||
A::default_max_ulps() | ||
} | ||
|
||
fn ulps_eq(&self, other: &ArrayBase<S, D>, epsilon: A::Epsilon, max_ulps: u32) -> bool { | ||
if self.shape() != other.shape() { | ||
return false; | ||
} | ||
Zip::from(self) | ||
.and(other) | ||
.fold_while(true, |_, a, b| { | ||
if A::ulps_ne(a, b, epsilon.clone(), max_ulps) { | ||
FoldWhile::Done(false) | ||
} else { | ||
FoldWhile::Continue(true) | ||
} | ||
}) | ||
.into_inner() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use approx::{ | ||
assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne, | ||
assert_ulps_eq, assert_ulps_ne, | ||
}; | ||
|
||
#[test] | ||
fn abs_diff_eq() { | ||
let a: Array2<f32> = array![[0., 2.], [-0.000010001, 100000000.]]; | ||
let mut b: Array2<f32> = array![[0., 1.], [-0.000010002, 100000001.]]; | ||
assert_abs_diff_ne!(a, b); | ||
b[(0, 1)] = 2.; | ||
assert_abs_diff_eq!(a, b); | ||
|
||
// Check epsilon. | ||
assert_abs_diff_eq!(array![0.0f32], array![1e-40f32], epsilon = 1e-40f32); | ||
assert_abs_diff_ne!(array![0.0f32], array![1e-40f32], epsilon = 1e-41f32); | ||
|
||
// Make sure we can compare different shapes without failure. | ||
let c = array![[1., 2.]]; | ||
assert_abs_diff_ne!(a, c); | ||
} | ||
|
||
#[test] | ||
fn relative_eq() { | ||
let a: Array2<f32> = array![[1., 2.], [-0.000010001, 100000000.]]; | ||
let mut b: Array2<f32> = array![[1., 1.], [-0.000010002, 100000001.]]; | ||
assert_relative_ne!(a, b); | ||
b[(0, 1)] = 2.; | ||
assert_relative_eq!(a, b); | ||
|
||
// Check epsilon. | ||
assert_relative_eq!(array![0.0f32], array![1e-40f32], epsilon = 1e-40f32); | ||
assert_relative_ne!(array![0.0f32], array![1e-40f32], epsilon = 1e-41f32); | ||
|
||
// Make sure we can compare different shapes without failure. | ||
let c = array![[1., 2.]]; | ||
assert_relative_ne!(a, c); | ||
} | ||
|
||
#[test] | ||
fn ulps_eq() { | ||
let a: Array2<f32> = array![[1., 2.], [-0.000010001, 100000000.]]; | ||
let mut b: Array2<f32> = array![[1., 1.], [-0.000010002, 100000001.]]; | ||
assert_ulps_ne!(a, b); | ||
b[(0, 1)] = 2.; | ||
assert_ulps_eq!(a, b); | ||
|
||
// Check epsilon. | ||
assert_ulps_eq!(array![0.0f32], array![1e-40f32], epsilon = 1e-40f32); | ||
assert_ulps_ne!(array![0.0f32], array![1e-40f32], epsilon = 1e-41f32); | ||
|
||
// Make sure we can compare different shapes without failure. | ||
let c = array![[1., 2.]]; | ||
assert_ulps_ne!(a, c); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters