argmin_math/ndarray_m/
add.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::ArgminAdd;
9use ndarray::{Array1, Array2};
10use num_complex::Complex;
11
12macro_rules! make_add {
13    ($t:ty) => {
14        impl ArgminAdd<$t, Array1<$t>> for Array1<$t> {
15            #[inline]
16            fn add(&self, other: &$t) -> Array1<$t> {
17                self + *other
18            }
19        }
20
21        impl ArgminAdd<Array1<$t>, Array1<$t>> for $t {
22            #[inline]
23            fn add(&self, other: &Array1<$t>) -> Array1<$t> {
24                *self + other
25            }
26        }
27
28        impl ArgminAdd<Array1<$t>, Array1<$t>> for Array1<$t> {
29            #[inline]
30            fn add(&self, other: &Array1<$t>) -> Array1<$t> {
31                self + other
32            }
33        }
34
35        impl ArgminAdd<Array2<$t>, Array2<$t>> for Array2<$t> {
36            #[inline]
37            fn add(&self, other: &Array2<$t>) -> Array2<$t> {
38                self + other
39            }
40        }
41
42        impl ArgminAdd<$t, Array2<$t>> for Array2<$t> {
43            #[inline]
44            fn add(&self, other: &$t) -> Array2<$t> {
45                self + *other
46            }
47        }
48    };
49}
50
51make_add!(i8);
52make_add!(i16);
53make_add!(i32);
54make_add!(i64);
55make_add!(u8);
56make_add!(u16);
57make_add!(u32);
58make_add!(u64);
59make_add!(f32);
60make_add!(f64);
61make_add!(Complex<f32>);
62make_add!(Complex<f64>);
63
64// All code that does not depend on a linked ndarray-linalg backend can still be tested as normal.
65// To avoid dublicating tests and to allow convenient testing of functionality that does not need ndarray-linalg the tests are still included here.
66// The tests expect the name for the crate containing the tested functions to be argmin_math
67#[cfg(test)]
68use crate as argmin_math;
69include!(concat!(
70    env!("CARGO_MANIFEST_DIR"),
71    "/ndarray-tests-src/add.rs"
72));