Skip to content

Commit 141facd

Browse files
authored
Merge pull request #946 from rust-ndarray/array-approx
Add "forwards" of the approx methods .abs_diff_eq and .relative_eq
2 parents 0970fc5 + 5bc7631 commit 141facd

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/array_approx.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ where
2121
if self.shape() != other.shape() {
2222
return false;
2323
}
24+
2425
Zip::from(self)
2526
.and(other)
26-
.all(|a, b| A::abs_diff_eq(a, b, epsilon.clone()))
27+
.all(move |a, b| A::abs_diff_eq(a, b, epsilon.clone()))
2728
}
2829
}
2930

@@ -49,9 +50,10 @@ where
4950
if self.shape() != other.shape() {
5051
return false;
5152
}
53+
5254
Zip::from(self)
5355
.and(other)
54-
.all(|a, b| A::relative_eq(a, b, epsilon.clone(), max_relative.clone()))
56+
.all(move |a, b| A::relative_eq(a, b, epsilon.clone(), max_relative.clone()))
5557
}
5658
}
5759

@@ -72,12 +74,51 @@ where
7274
if self.shape() != other.shape() {
7375
return false;
7476
}
77+
7578
Zip::from(self)
7679
.and(other)
77-
.all(|a, b| A::ulps_eq(a, b, epsilon.clone(), max_ulps))
80+
.all(move |a, b| A::ulps_eq(a, b, epsilon.clone(), max_ulps))
7881
}
7982
}
8083

84+
impl<A, S, D> ArrayBase<S, D>
85+
where
86+
S: Data<Elem = A>,
87+
D: Dimension,
88+
{
89+
/// A test for equality that uses the elementwise absolute difference to compute the
90+
/// approximate equality of two arrays.
91+
///
92+
/// **Requires crate feature `"approx"`**
93+
pub fn abs_diff_eq<S2>(&self, other: &ArrayBase<S2, D>, epsilon: A::Epsilon) -> bool
94+
where
95+
A: AbsDiffEq<S2::Elem>,
96+
A::Epsilon: Clone,
97+
S2: Data,
98+
{
99+
<Self as AbsDiffEq<_>>::abs_diff_eq(self, other, epsilon)
100+
}
101+
102+
/// A test for equality that uses an elementwise relative comparison if the values are far
103+
/// apart; and the absolute difference otherwise.
104+
///
105+
/// **Requires crate feature `"approx"`**
106+
pub fn relative_eq<S2>(
107+
&self,
108+
other: &ArrayBase<S2, D>,
109+
epsilon: A::Epsilon,
110+
max_relative: A::Epsilon,
111+
) -> bool
112+
where
113+
A: RelativeEq<S2::Elem>,
114+
A::Epsilon: Clone,
115+
S2: Data
116+
{
117+
<Self as RelativeEq<_>>::relative_eq(self, other, epsilon, max_relative)
118+
}
119+
}
120+
121+
81122
#[cfg(test)]
82123
mod tests {
83124
use crate::prelude::*;

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ mod aliases;
170170
#[macro_use]
171171
mod itertools;
172172
mod argument_traits;
173-
#[cfg(feature = "approx")]
174-
mod array_approx;
175173
#[cfg(feature = "serde")]
176174
mod array_serde;
177175
mod arrayformat;
@@ -1520,6 +1518,9 @@ impl<'a, A> CowRepr<'a, A> {
15201518
}
15211519
}
15221520

1521+
// NOTE: The order of modules decides in which order methods on the type ArrayBase
1522+
// (mainly mentioning that as the most relevant type) show up in the documentation.
1523+
// Consider the doc effect of ordering modules here.
15231524
mod impl_clone;
15241525

15251526
mod impl_internal_constructors;
@@ -1613,6 +1614,9 @@ pub mod linalg;
16131614
mod impl_ops;
16141615
pub use crate::impl_ops::ScalarOperand;
16151616

1617+
#[cfg(feature = "approx")]
1618+
mod array_approx;
1619+
16161620
// Array view methods
16171621
mod impl_views;
16181622

0 commit comments

Comments
 (0)