// Non-convensional componentwise operators. use num::Signed; use alga::general::{ClosedMul, ClosedDiv}; use core::{Scalar, Matrix, OwnedMatrix, MatrixSum}; use core::dimension::Dim; use core::storage::{Storage, StorageMut}; use core::allocator::SameShapeAllocator; use core::constraint::{ShapeConstraint, SameNumberOfRows, SameNumberOfColumns}; /// The type of the result of a matrix componentwise operation. pub type MatrixComponentOp = MatrixSum; impl> Matrix { /// Computes the componentwise absolute value. #[inline] pub fn abs(&self) -> OwnedMatrix where N: Signed { let mut res = self.clone_owned(); for e in res.iter_mut() { *e = e.abs(); } res } // FIXME: add other operators like component_ln, component_pow, etc. ? } macro_rules! component_binop_impl( ($($binop: ident, $binop_mut: ident, $Trait: ident . $binop_assign: ident);* $(;)*) => {$( impl> Matrix { /// Componentwise matrix multiplication. #[inline] pub fn $binop(&self, rhs: &Matrix) -> MatrixComponentOp where N: $Trait, R2: Dim, C2: Dim, SB: Storage, S::Alloc: SameShapeAllocator, ShapeConstraint: SameNumberOfRows + SameNumberOfColumns { let mut res = self.clone_owned_sum(); for (res, rhs) in res.iter_mut().zip(rhs.iter()) { res.$binop_assign(*rhs); } res } } impl> Matrix { /// Componentwise matrix multiplication. #[inline] pub fn $binop_mut(&mut self, rhs: &Matrix) where N: $Trait, R2: Dim, C2: Dim, SB: Storage, ShapeConstraint: SameNumberOfRows + SameNumberOfColumns { for (me, rhs) in self.iter_mut().zip(rhs.iter()) { me.$binop_assign(*rhs); } } } )*} ); component_binop_impl!( component_mul, component_mul_mut, ClosedMul.mul_assign; component_div, component_div_mut, ClosedDiv.div_assign; // FIXME: add other operators like bitshift, etc. ? );