diff --git a/CHANGELOG.md b/CHANGELOG.md index 04479117..2a9575d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added * `.kronecker(a, b)` computes the kronecker product (i.e. matrix tensor product) of two matrices. + * `.set_row(i, row)` sets the i-th row of the matrix. + * `.set_column(j, column)` sets the i-th column of the matrix. ## [0.12.0] diff --git a/Makefile b/Makefile index ab97c6f9..b0eb255d 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ bench: cargo bench test: - CARGO_INCREMENTAL=1 cargo test --features "arbitrary serde-serialize" + cargo test --features "arbitrary serde-serialize" diff --git a/src/core/matrix.rs b/src/core/matrix.rs index d87ddb3f..661fb329 100644 --- a/src/core/matrix.rs +++ b/src/core/matrix.rs @@ -31,6 +31,9 @@ pub type ColumnVector = Matrix; /// An owned matrix with one column and `D` rows. pub type OwnedColumnVector = OwnedMatrix; +/// A matrix with one row and `D` columns. +pub type RowVector = Matrix; + /// An owned matrix with one row and `D` columns. pub type OwnedRowVector = OwnedMatrix; @@ -301,6 +304,24 @@ impl> Matrix { *e = value } } + + /// Fills the selected row of this matrix with the content of the given vector. + #[inline] + pub fn set_row(&mut self, i: usize, row: &RowVector) + where S2: Storage, + S::Alloc: Allocator, + ShapeConstraint: SameNumberOfColumns { + self.row_mut(i).copy_from(row); + } + + /// Fills the selected column of this matrix with the content of the given vector. + #[inline] + pub fn set_column(&mut self, i: usize, column: &ColumnVector) + where S2: Storage, + S::Alloc: Allocator, + ShapeConstraint: SameNumberOfRows { + self.column_mut(i).copy_from(column); + } } impl> Matrix diff --git a/tests/matrix.rs b/tests/matrix.rs index bc5d44e5..2b319498 100644 --- a/tests/matrix.rs +++ b/tests/matrix.rs @@ -15,7 +15,7 @@ use alga::linear::FiniteDimInnerSpace; use na::{U8, U15, DVector, DMatrix, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6, - RowVector4, + RowVector4, RowVector5, Matrix1, Matrix2, Matrix3, Matrix4, Matrix5, Matrix6, MatrixNM, Matrix2x3, Matrix3x2, Matrix3x4, Matrix4x3, Matrix2x4, Matrix4x5, Matrix4x6}; @@ -475,6 +475,38 @@ fn kronecker() { assert_eq!(a.kronecker(&b), expected); } +#[test] +fn set_row_column() { + let a = Matrix4x5::new( + 11, 12, 13, 14, 15, + 21, 22, 23, 24, 25, + 31, 32, 33, 34, 35, + 41, 42, 43, 44, 45); + + let expected1 = Matrix4x5::new( + 11, 12, 13, 14, 15, + 42, 43, 44, 45, 46, + 31, 32, 33, 34, 35, + 41, 42, 43, 44, 45); + + let expected2 = Matrix4x5::new( + 11, 12, 100, 14, 15, + 42, 43, 101, 45, 46, + 31, 32, 102, 34, 35, + 41, 42, 103, 44, 45); + + let row = RowVector5::new(42, 43, 44, 45, 46); + let col = Vector4::new(100, 101, 102, 103); + + let mut computed = a; + + computed.set_row(1, &row); + assert_eq!(expected1, computed); + + computed.set_column(2, &col); + assert_eq!(expected2, computed); +} + #[cfg(feature = "arbitrary")] quickcheck!{ /*