Skip to content

Commit d82724c

Browse files
committed
deviation: Implement l_inf_dist
1 parent 7eadd0e commit d82724c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/deviation.rs

+37
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ where
2828
fn l1_dist(&self, other: &ArrayBase<S, D>) -> A
2929
where
3030
A: AddAssign + Clone + Signed;
31+
32+
fn l_inf_dist(&self, other: &ArrayBase<S, D>) -> A
33+
where
34+
A: Clone + PartialOrd + Signed;
3135
}
3236

3337
impl<A, S, D> DeviationExt<A, S, D> for ArrayBase<S, D>
@@ -92,6 +96,23 @@ where
9296

9397
r
9498
}
99+
100+
fn l_inf_dist(&self, other: &ArrayBase<S, D>) -> A
101+
where
102+
A: Clone + PartialOrd + Signed,
103+
{
104+
let mut max = A::zero();
105+
106+
Zip::from(self).and(other).apply(|self_i, other_i| {
107+
let (a, b) = (self_i.clone(), other_i.clone());
108+
let diff = (a - b).abs();
109+
if diff > max {
110+
max = diff;
111+
}
112+
});
113+
114+
max
115+
}
95116
}
96117

97118
#[cfg(test)]
@@ -168,4 +189,20 @@ mod tests {
168189
assert_eq!(a.l1_dist(&b), (&a - &b).mapv(f64::abs).sum());
169190
assert_eq!(c.l1_dist(&d), (&c - &d).mapv(i32::abs).sum());
170191
}
192+
193+
#[test]
194+
fn test_l_inf_dist() {
195+
let a = array![1., 1., 1.];
196+
let b = array![2., 1., 1.];
197+
let c = array![2., 3., 1.];
198+
let d = array![2., 3., 4.];
199+
200+
assert_eq!(a.l_inf_dist(&a), 0.);
201+
assert_eq!(a.l_inf_dist(&b), 1.);
202+
assert_eq!(b.l_inf_dist(&a), 1.);
203+
assert_eq!(a.l_inf_dist(&c), 2.);
204+
assert_eq!(c.l_inf_dist(&a), 2.);
205+
assert_eq!(a.l_inf_dist(&d), 3.);
206+
assert_eq!(d.l_inf_dist(&a), 3.);
207+
}
171208
}

0 commit comments

Comments
 (0)