1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::ops::{
    Add, AddAssign,
    Div, DivAssign,
    Mul, MulAssign,
    Neg,
    Rem, RemAssign,
    Sub, SubAssign,
};

use crate::{
    core::prelude::*,
    errors::prelude::*,
    numeric::prelude::*,
};

macro_rules! impl_op {
    ($op_trait: ident, $op_func: ident, $op_assign_trait: ident, $op_assign_func: ident) => {
        impl<N: NumericOps> $op_trait<Array<N>> for Array<N> {
            type Output = Array<N>;

            fn $op_func(self, other: Self) -> Self::Output {
                assert_eq!(self.get_shape(), other.get_shape());

                let elements = self.elements.into_iter()
                    .zip(other.elements.into_iter())
                    .map(|(a, b)| a.$op_func(b))
                    .collect();

                Array::new(elements, self.shape).unwrap()
            }
        }

        impl<N: NumericOps> $op_trait<N> for Array<N> {
            type Output = Result<Array<N>, ArrayError>;

            fn $op_func(self, other: N) -> Self::Output {
                self.map(|i| i.$op_func(other))
                    .reshape(&self.shape)
            }
        }

        impl<N: NumericOps> $op_assign_trait<Array<N>> for Array<N> {
            fn $op_assign_func(&mut self, other: Self) -> () {
                assert_eq!(self.get_shape(), other.get_shape());

                self.elements.iter_mut()
                    .zip(other.elements.into_iter())
                    .for_each(|(a, b)| a.$op_assign_func(b));
            }
        }

        impl<N: NumericOps> $op_assign_trait<N> for Array<N> {
            fn $op_assign_func(&mut self, other: N) -> () {
                self.elements.iter_mut()
                    .for_each(|a| a.$op_assign_func(other));
            }
        }
    };
}

impl_op!(Add, add, AddAssign, add_assign);
impl_op!(Sub, sub, SubAssign, sub_assign);
impl_op!(Mul, mul, MulAssign, mul_assign);
impl_op!(Div, div, DivAssign, div_assign);
impl_op!(Rem, rem, RemAssign, rem_assign);

// ==== Signed Ops

impl <N: SignedNumericOps> Neg for Array<N> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        let elements = self.elements.into_iter()
            .map(|a| -a)
            .collect();

        Array::new(elements, self.shape).unwrap()
    }
}