argmin_math/ndarray_m/
minmax.rs

1// Copyright 2018-2024 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use 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// All code that does not depend on a linked ndarray-linalg backend can still be tested as normal.
81// To avoid dublicating tests and to allow convenient testing of functionality that does not need ndarray-linalg the tests are still included here.
82// The tests expect the name for the crate containing the tested functions to be argmin_math
83#[cfg(test)]
84use crate as argmin_math;
85include!(concat!(
86    env!("CARGO_MANIFEST_DIR"),
87    "/ndarray-tests-src/minmax.rs"
88));