From 9e763bbcff882c18cfcf9a8971218658b1dd0d33 Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Wed, 7 Nov 2018 18:33:17 -0500 Subject: [PATCH] doc tests for `from_vec_generic` and `from_vec` --- src/base/construction.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/base/construction.rs b/src/base/construction.rs index 64302a33..7a12dc0f 100644 --- a/src/base/construction.rs +++ b/src/base/construction.rs @@ -258,7 +258,23 @@ where DefaultAllocator: Allocator Self::from_fn_generic(nrows, ncols, |_, _| distribution.sample(rng)) } - /// Creates a matrix backed by a given vector. + /// Creates a matrix backed by a given `Vec`. + /// + /// The output matrix is filled column-by-column. + /// + /// # Example + /// ``` + /// # use nalgebra::{Dynamic, DMatrix, Matrix, U1}; + /// + /// let vec = vec![0, 1, 2, 3, 4, 5]; + /// let vec_ptr = vec.as_ptr(); + /// + /// let matrix = Matrix::from_vec_generic(Dynamic::new(vec.len()), U1, vec); + /// let matrix_storage_ptr = matrix.data.as_ptr(); + /// + /// // `matrix` is backed by exactly the same `Vec` as it was constructed from. + /// assert_eq!(matrix_storage_ptr, vec_ptr); + /// ``` #[inline] #[cfg(feature = "std")] pub fn from_vec_generic(nrows: R, ncols: C, data: Vec) -> Self { @@ -597,7 +613,26 @@ macro_rules! impl_constructors( Self::from_distribution_generic($($gargs, )* distribution, rng) } - /// Creates a matrix backed by a given vector. + /// Creates a matrix backed by a given `Vec`. + /// + /// The output matrix is filled column-by-column. + /// + /// # Example + /// ``` + /// # use nalgebra::{DMatrix, Matrix2x3}; + /// + /// let m = Matrix2x3::from_vec(vec![0, 1, 2, 3, 4, 5]); + /// + /// assert!(m.m11 == 0 && m.m12 == 2 && m.m13 == 4 && + /// m.m21 == 1 && m.m22 == 3 && m.m23 == 5); + /// + /// + /// // The two additional arguments represent the matrix dimensions. + /// let dm = DMatrix::from_vec(2, 3, vec![0, 1, 2, 3, 4, 5]); + /// + /// assert!(dm[(0, 0)] == 0 && dm[(0, 1)] == 2 && dm[(0, 2)] == 4 && + /// dm[(1, 0)] == 1 && dm[(1, 1)] == 3 && dm[(1, 2)] == 5); + /// ``` #[inline] #[cfg(feature = "std")] pub fn from_vec($($args: usize,)* data: Vec) -> Self {