argmin_math/ndarray_m/
minmax.rs1use crate::ArgminMinMax;
9use ndarray::{Array1, Array2};
10
11macro_rules! make_minmax {
12 ($t:ty) => {
13 impl ArgminMinMax for Array1<$t> {
14 #[inline]
15 fn min(x: &Self, y: &Self) -> Array1<$t> {
16 assert_eq!(x.shape(), y.shape());
17 x.iter()
18 .zip(y)
19 .map(|(&a, &b)| if a < b { a } else { b })
20 .collect()
21 }
22
23 #[inline]
24 fn max(x: &Self, y: &Self) -> Array1<$t> {
25 assert_eq!(x.shape(), y.shape());
26 x.iter()
27 .zip(y)
28 .map(|(&a, &b)| if a > b { a } else { b })
29 .collect()
30 }
31 }
32
33 impl ArgminMinMax for Array2<$t> {
34 #[inline]
35 fn min(x: &Self, y: &Self) -> Array2<$t> {
36 assert_eq!(x.shape(), y.shape());
37 let m = x.shape()[0];
38 let n = x.shape()[1];
39 let mut out = x.clone();
40 for i in 0..m {
41 for j in 0..n {
42 let a = x[(i, j)];
43 let b = y[(i, j)];
44 out[(i, j)] = if a < b { a } else { b };
45 }
46 }
47 out
48 }
49
50 #[inline]
51 fn max(x: &Self, y: &Self) -> Array2<$t> {
52 assert_eq!(x.shape(), y.shape());
53 let m = x.shape()[0];
54 let n = x.shape()[1];
55 let mut out = x.clone();
56 for i in 0..m {
57 for j in 0..n {
58 let a = x[(i, j)];
59 let b = y[(i, j)];
60 out[(i, j)] = if a > b { a } else { b };
61 }
62 }
63 out
64 }
65 }
66 };
67}
68
69make_minmax!(i8);
70make_minmax!(u8);
71make_minmax!(i16);
72make_minmax!(u16);
73make_minmax!(i32);
74make_minmax!(u32);
75make_minmax!(i64);
76make_minmax!(u64);
77make_minmax!(f32);
78make_minmax!(f64);
79
80#[cfg(test)]
84use crate as argmin_math;
85include!(concat!(
86 env!("CARGO_MANIFEST_DIR"),
87 "/ndarray-tests-src/minmax.rs"
88));