|
28 | 28 | fn l1_dist(&self, other: &ArrayBase<S, D>) -> A
|
29 | 29 | where
|
30 | 30 | A: AddAssign + Clone + Signed;
|
| 31 | + |
| 32 | + fn l_inf_dist(&self, other: &ArrayBase<S, D>) -> A |
| 33 | + where |
| 34 | + A: Clone + PartialOrd + Signed; |
31 | 35 | }
|
32 | 36 |
|
33 | 37 | impl<A, S, D> DeviationExt<A, S, D> for ArrayBase<S, D>
|
|
92 | 96 |
|
93 | 97 | r
|
94 | 98 | }
|
| 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 | + } |
95 | 116 | }
|
96 | 117 |
|
97 | 118 | #[cfg(test)]
|
@@ -168,4 +189,20 @@ mod tests {
|
168 | 189 | assert_eq!(a.l1_dist(&b), (&a - &b).mapv(f64::abs).sum());
|
169 | 190 | assert_eq!(c.l1_dist(&d), (&c - &d).mapv(i32::abs).sum());
|
170 | 191 | }
|
| 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 | + } |
171 | 208 | }
|
0 commit comments