From 0ed67d047065343817bdcde62cfaf928a8c88519 Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Tue, 13 Nov 2018 16:17:00 -0500 Subject: [PATCH] Implement `Extend>` for `Matrix`. Extend a `Matrix` with columns drawn from an iterator of `Vector`s. --- src/base/edition.rs | 63 ++++++++++++++++++++++++++++++++++++++++++ src/base/matrix_vec.rs | 30 +++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/base/edition.rs b/src/base/edition.rs index 4384da39..28230752 100644 --- a/src/base/edition.rs +++ b/src/base/edition.rs @@ -772,3 +772,66 @@ where self.data.extend(iter); } } + +impl Extend> for Matrix +where + N: Scalar, + R: Dim, + S: Extend>, + RV: Dim, + SV: Storage +{ + /// Extends the number of columns of a `Matrix` with `Vector`s + /// from a given iterator. + /// + /// # Example + /// ``` + /// # use nalgebra::{DMatrix, Vector3, Matrix3x4}; + /// + /// let data = vec![0, 1, 2, // column 1 + /// 3, 4, 5]; // column 2 + /// + /// let mut matrix = DMatrix::from_vec(3, 2, data); + /// + /// matrix.extend( + /// vec![Vector3::new(6, 7, 8), // column 3 + /// Vector3::new(9, 10, 11)]); // column 4 + /// + /// assert!(matrix.eq(&Matrix3x4::new(0, 3, 6, 9, + /// 1, 4, 7, 10, + /// 2, 5, 8, 11))); + /// ``` + /// + /// # Panics + /// This function panics if the dimension of each `Vector` yielded + /// by the given iterator is not equal to the number of rows of + /// this `Matrix`. + /// + /// ```should_panic + /// # use nalgebra::{DMatrix, Vector2, Matrix3x4}; + /// let mut matrix = + /// DMatrix::from_vec(3, 2, + /// vec![0, 1, 2, // column 1 + /// 3, 4, 5]); // column 2 + /// + /// // The following panics because this matrix can only be extended with 3-dimensional vectors. + /// matrix.extend( + /// vec![Vector2::new(6, 7)]); // too few dimensions! + /// ``` + /// + /// ```should_panic + /// # use nalgebra::{DMatrix, Vector4, Matrix3x4}; + /// let mut matrix = + /// DMatrix::from_vec(3, 2, + /// vec![0, 1, 2, // column 1 + /// 3, 4, 5]); // column 2 + /// + /// // The following panics because this matrix can only be extended with 3-dimensional vectors. + /// matrix.extend( + /// vec![Vector4::new(6, 7, 8, 9)]); // too few dimensions! + /// ``` + fn extend>>(&mut self, iter: I) + { + self.data.extend(iter); + } +} diff --git a/src/base/matrix_vec.rs b/src/base/matrix_vec.rs index b821afdb..080ce16e 100644 --- a/src/base/matrix_vec.rs +++ b/src/base/matrix_vec.rs @@ -9,7 +9,7 @@ use base::allocator::Allocator; use base::default_allocator::DefaultAllocator; use base::dimension::{Dim, DimName, Dynamic, U1}; use base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut}; -use base::Scalar; +use base::{Scalar, Vector}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; @@ -259,6 +259,34 @@ impl Extend for MatrixVec } } +impl Extend> for MatrixVec +where + N: Scalar, + R: Dim, + RV: Dim, + SV: Storage +{ + /// Extends the number of columns of the `MatrixVec` with vectors + /// from the given iterator. + /// + /// # Panics + /// This function panics if the number of rows of each `Vector` + /// yielded by the iterator is not equal to the number of rows + /// of this `MatrixVec`. + fn extend>>(&mut self, iter: I) + { + let nrows = self.nrows.value(); + let iter = iter.into_iter(); + let (lower, _upper) = iter.size_hint(); + self.data.reserve(nrows * lower); + for vector in iter { + assert_eq!(nrows, vector.shape().0); + self.data.extend(vector.iter()); + } + self.ncols = Dynamic::new(self.data.len() / nrows); + } +} + impl Extend for MatrixVec { /// Extends the number of rows of the `MatrixVec` with elements