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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::iter::Sum;
use std::ops::*;

use crate::Matrix;

use num_traits::Num;

/// Implements addition between `Matrix<T>` and `Matrix<T>`
impl<T: Num + Clone + Copy> Add for Matrix<T> {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        if self.size() != other.size() {
            unimplemented!();
        } else {
            let mut new_body = Vec::new();
            let other_slice = other.as_slice();

            for (index, i) in self.data.iter().enumerate() {
                new_body.push(*i + other_slice[index]);
            }

            Self::from_vec(self.size(), new_body)
        }
    }
}

/// Implements subtraction between `Matrix<T>` and `Matrix<T>`
impl<T: Num + Clone + Copy> Sub for Matrix<T> {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        if self.size() != other.size() {
            unimplemented!();
        } else {
            let mut new_body = Vec::new();
            let other_slice = other.as_slice();

            for (index, i) in self.data.iter().enumerate() {
                new_body.push(*i - other_slice[index]);
            }

            Self::from_vec(self.size(), new_body)
        }
    }
}

/// Implements multiplication between `Matrix<T>` and `Matrix<T>`
impl<T: Num + Clone + Copy + Sum> Mul for Matrix<T> {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        if self.width != other.height {
            panic!("Incorrect bounds for the two Matrices");
        }

        let mut body: Vec<Vec<T>> = vec![Vec::new(); self.height];

        for (i, row) in self.as_vec().iter().enumerate() {
            for col in other.cols() {
                body[i].push(
                    row.iter()
                        .zip(col.iter())
                        .map(|(&left, &right)| left * right)
                        .sum::<T>()
                        .into(),
                );
            }
        }

        Self::from_vec(
            (body.len(), body[0].len()),
            body.iter().flat_map(|row| row.iter().copied()).collect(),
        )
    }
}

/// Implements addition between `Matrix<T>` and `T`
impl<T: Num + Clone + Copy> Add<T> for Matrix<T> {
    type Output = Self;

    #[inline]
    fn add(self, rhs: T) -> Self::Output {
        self.scalar_add(rhs)
    }
}

/// Implements subtraction between `Matrix<T>` and `T`
impl<T: Num + Clone + Copy> Sub<T> for Matrix<T> {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: T) -> Self::Output {
        self.scalar_sub(rhs)
    }
}

/// Implements multiplication between `Matrix<T>` and `T`
impl<T: Num + Clone + Copy> Mul<T> for Matrix<T> {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: T) -> Self::Output {
        self.scalar_mul(rhs)
    }
}

/// Implements division between `Matrix<T>` and `T`
impl<T: Num + Clone + Copy> Div<T> for Matrix<T> {
    type Output = Self;

    #[inline]
    fn div(self, rhs: T) -> Self::Output {
        self.scalar_div(rhs)
    }
}

#[cfg(test)]
mod math_tests {
    use crate::Matrix;

    #[test]
    fn test_ops() {
        let matrix: Matrix<f32> = Matrix::from_slice((1, 3), &[1.0, 4.0, 7.0]);

        assert_eq!(
            matrix.clone() + 3.0,
            Matrix::from_slice((1, 3), &[4.0, 7.0, 10.0])
        );
        assert_eq!(
            matrix.clone() - 1.0,
            Matrix::from_slice((1, 3), &[0.0, 3.0, 6.0])
        );
        assert_eq!(
            matrix.clone() / 2.0,
            Matrix::from_slice((1, 3), &[0.5, 2.0, 3.5])
        );
        assert_eq!(
            matrix.clone() * 2.0,
            Matrix::from_slice((1, 3), &[2.0, 8.0, 14.0])
        );
    }
}