Add methods to set a row or a column of the matrix.

This commit is contained in:
Sébastien Crozet 2017-05-17 22:37:18 +02:00 committed by Sébastien Crozet
parent 136a3306e7
commit b78dcb3155
4 changed files with 57 additions and 2 deletions

View File

@ -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]

View File

@ -8,4 +8,4 @@ bench:
cargo bench
test:
CARGO_INCREMENTAL=1 cargo test --features "arbitrary serde-serialize"
cargo test --features "arbitrary serde-serialize"

View File

@ -31,6 +31,9 @@ pub type ColumnVector<N, D, S> = Matrix<N, D, U1, S>;
/// An owned matrix with one column and `D` rows.
pub type OwnedColumnVector<N, D, A> = OwnedMatrix<N, D, U1, A>;
/// A matrix with one row and `D` columns.
pub type RowVector<N, D, S> = Matrix<N, U1, D, S>;
/// An owned matrix with one row and `D` columns.
pub type OwnedRowVector<N, D, A> = OwnedMatrix<N, U1, D, A>;
@ -301,6 +304,24 @@ impl<N: Scalar, R: Dim, C: Dim, S: StorageMut<N, R, C>> Matrix<N, R, C, S> {
*e = value
}
}
/// Fills the selected row of this matrix with the content of the given vector.
#[inline]
pub fn set_row<C2: Dim, S2>(&mut self, i: usize, row: &RowVector<N, C2, S2>)
where S2: Storage<N, U1, C2>,
S::Alloc: Allocator<N, U1, C>,
ShapeConstraint: SameNumberOfColumns<C, C2> {
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<R2: Dim, S2>(&mut self, i: usize, column: &ColumnVector<N, R2, S2>)
where S2: Storage<N, R2, U1>,
S::Alloc: Allocator<N, R, U1>,
ShapeConstraint: SameNumberOfRows<R, R2> {
self.column_mut(i).copy_from(column);
}
}
impl<N: Scalar, R: Dim, C: Dim, S: OwnedStorage<N, R, C>> Matrix<N, R, C, S>

View File

@ -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!{
/*