From 15844d877a8ee7c7af22563cf535fa06f538144c Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Sat, 20 Oct 2018 11:36:35 +0200 Subject: [PATCH] Add doc-tests for componentwise operations. --- src/base/componentwise.rs | 107 +++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/src/base/componentwise.rs b/src/base/componentwise.rs index 94934715..958feb0a 100644 --- a/src/base/componentwise.rs +++ b/src/base/componentwise.rs @@ -11,11 +11,20 @@ use base::dimension::Dim; use base::storage::{Storage, StorageMut}; use base::{DefaultAllocator, Matrix, MatrixMN, MatrixSum, Scalar}; -/// The type of the result of a matrix componentwise operation. +/// The type of the result of a matrix component-wise operation. pub type MatrixComponentOp = MatrixSum; impl> Matrix { - /// Computes the componentwise absolute value. + /// Computes the component-wise absolute value. + /// + /// # Example + /// + /// ``` + /// # use nalgebra::Matrix2; + /// let a = Matrix2::new(0.0, 1.0, + /// -2.0, -3.0); + /// assert_eq!(a.abs(), Matrix2::new(0.0, 1.0, 2.0, 3.0)) + /// ``` #[inline] pub fn abs(&self) -> MatrixMN where @@ -135,12 +144,94 @@ macro_rules! component_binop_impl( component_binop_impl!( component_mul, component_mul_mut, component_mul_assign, cmpy, ClosedMul.mul.mul_assign, - "Componentwise matrix multiplication.", - "Computes componentwise `self[i] = alpha * a[i] * b[i] + beta * self[i]`.", - "Inplace componentwise matrix multiplication."; + r" + Componentwise matrix or vector multiplication. + + # Example + + ``` + # use nalgebra::Matrix2; + let a = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let b = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let expected = Matrix2::new(0.0, 5.0, 12.0, 21.0); + + assert_eq!(a.component_mul(&b), expected); + ``` + ", + r" + Computes componentwise `self[i] = alpha * a[i] * b[i] + beta * self[i]`. + + # Example + ``` + # use nalgebra::Matrix2; + let mut m = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let a = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let b = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let expected = (a.component_mul(&b) * 5.0) + m * 10.0; + + m.cmpy(5.0, &a, &b, 10.0); + assert_eq!(m, expected); + ``` + ", + r" + Inplace componentwise matrix or vector multiplication. + + # Example + ``` + # use nalgebra::Matrix2; + let mut a = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let b = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let expected = Matrix2::new(0.0, 5.0, 12.0, 21.0); + + a.component_mul_assign(&b); + + assert_eq!(a, expected); + ``` + "; component_div, component_div_mut, component_div_assign, cdpy, ClosedDiv.div.div_assign, - "Componentwise matrix division.", - "Computes componentwise `self[i] = alpha * a[i] / b[i] + beta * self[i]`.", - "Inplace componentwise matrix division."; + r" + Componentwise matrix or vector division. + + # Example + + ``` + # use nalgebra::Matrix2; + let a = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let b = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let expected = Matrix2::new(0.0, 1.0 / 5.0, 2.0 / 6.0, 3.0 / 7.0); + + assert_eq!(a.component_div(&b), expected); + ``` + ", + r" + Computes componentwise `self[i] = alpha * a[i] / b[i] + beta * self[i]`. + + # Example + ``` + # use nalgebra::Matrix2; + let mut m = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let a = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let b = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let expected = (a.component_div(&b) * 5.0) + m * 10.0; + + m.cdpy(5.0, &a, &b, 10.0); + assert_eq!(m, expected); + ``` + ", + r" + Inplace componentwise matrix or vector division. + + # Example + ``` + # use nalgebra::Matrix2; + let mut a = Matrix2::new(0.0, 1.0, 2.0, 3.0); + let b = Matrix2::new(4.0, 5.0, 6.0, 7.0); + let expected = Matrix2::new(0.0, 1.0 / 5.0, 2.0 / 6.0, 3.0 / 7.0); + + a.component_div_assign(&b); + + assert_eq!(a, expected); + ``` + "; // FIXME: add other operators like bitshift, etc. ? );