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
use crate::matrix::Matrix; use crate::number::Number; use rayon::prelude::*; use std::ops::Add; fn add<T>(lhs: Matrix<T>, rhs: &Matrix<T>) -> Matrix<T> where T: Number, { if !lhs.is_same_size(rhs) { panic!("dimension mismatch") } let mut lhs = lhs; lhs.elements .par_iter_mut() .zip(rhs.elements.par_iter()) .map(|(l, &r)| { *l += r; }) .collect::<Vec<_>>(); lhs } impl<T: Number> Add<Matrix<T>> for Matrix<T> { type Output = Matrix<T>; fn add(self, rhs: Matrix<T>) -> Self::Output { add(self, &rhs) } } impl<T: Number> Add<&Matrix<T>> for Matrix<T> { type Output = Matrix<T>; fn add(self, rhs: &Matrix<T>) -> Self::Output { add(self, rhs) } } impl<T: Number> Add<Matrix<T>> for &Matrix<T> { type Output = Matrix<T>; fn add(self, rhs: Matrix<T>) -> Self::Output { add(rhs, self) } }