Trait rusty_machine::linalg::BaseMatrixMut
source · [−]pub trait BaseMatrixMut<T>: BaseMatrix<T> {
Show 18 methods
fn as_mut_ptr(&mut self) -> *mut T;
fn as_mut_slice(&mut self) -> MatrixSliceMut<'_, T> { ... }
unsafe fn get_unchecked_mut(&mut self, index: [usize; 2]) -> &mut T { ... }
fn get_mut(&mut self, index: [usize; 2]) -> Option<&mut T> { ... }
fn iter_mut<'a>(&mut self) -> SliceIterMut<'a, T>
where
T: 'a,
{ ... }
fn col_mut(&mut self, index: usize) -> ColumnMut<'_, T> { ... }
unsafe fn col_unchecked_mut(&mut self, index: usize) -> ColumnMut<'_, T> { ... }
fn row_mut(&mut self, index: usize) -> RowMut<'_, T> { ... }
unsafe fn row_unchecked_mut(&mut self, index: usize) -> RowMut<'_, T> { ... }
fn swap_rows(&mut self, a: usize, b: usize) { ... }
fn swap_cols(&mut self, a: usize, b: usize) { ... }
fn col_iter_mut(&mut self) -> ColsMut<'_, T> { ... }
fn row_iter_mut(&mut self) -> RowsMut<'_, T> { ... }
fn diag_iter_mut(&mut self, k: DiagOffset) -> DiagonalMut<'_, T, Self> { ... }
fn set_to<M>(self, target: M)
where
M: BaseMatrix<T>,
T: Copy,
{ ... }
fn apply(self, f: &dyn Fn(T)) -> Self
where
T: Copy,
{ ... }
fn split_at_mut(
&mut self,
mid: usize,
axis: Axes
) -> (MatrixSliceMut<'_, T>, MatrixSliceMut<'_, T>) { ... }
fn sub_slice_mut<'a>(
&mut self,
start: [usize; 2],
rows: usize,
cols: usize
) -> MatrixSliceMut<'a, T>
where
T: 'a,
{ ... }
}
Expand description
Trait for mutable matrices.
Required Methods
sourcefn as_mut_ptr(&mut self) -> *mut T
fn as_mut_ptr(&mut self) -> *mut T
Top left index of the slice.
Provided Methods
sourcefn as_mut_slice(&mut self) -> MatrixSliceMut<'_, T>
fn as_mut_slice(&mut self) -> MatrixSliceMut<'_, T>
Returns a MatrixSliceMut
over the whole matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut a = Matrix::new(3, 3, vec![2.0; 9]);
let b = a.as_mut_slice();
sourceunsafe fn get_unchecked_mut(&mut self, index: [usize; 2]) -> &mut T
unsafe fn get_unchecked_mut(&mut self, index: [usize; 2]) -> &mut T
Get a mutable reference to an element in the matrix without bounds checks.
sourcefn get_mut(&mut self, index: [usize; 2]) -> Option<&mut T>
fn get_mut(&mut self, index: [usize; 2]) -> Option<&mut T>
Get a mutable reference to an element in the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrix, BaseMatrixMut};
let mut mat = matrix![0, 1;
3, 4;
6, 7];
assert_eq!(mat.get_mut([0, 2]), None);
assert_eq!(mat.get_mut([3, 0]), None);
assert_eq!(*mat.get_mut([0, 0]).unwrap(), 0);
*mat.get_mut([0,0]).unwrap() = 2;
assert_eq!(*mat.get_mut([0, 0]).unwrap(), 2);
sourcefn iter_mut<'a>(&mut self) -> SliceIterMut<'a, T>where
T: 'a,
fn iter_mut<'a>(&mut self) -> SliceIterMut<'a, T>where
T: 'a,
Returns a mutable iterator over the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
{
let mut slice = a.sub_slice_mut([1,1], 2, 2);
for d in slice.iter_mut() {
*d = *d + 2;
}
}
// Only the matrix slice is updated.
assert_matrix_eq!(a, matrix![0, 1, 2; 3, 6, 7; 6, 9, 10]);
sourcefn col_mut(&mut self, index: usize) -> ColumnMut<'_, T>
fn col_mut(&mut self, index: usize) -> ColumnMut<'_, T>
Returns a mutable reference to the column of a matrix at the given index.
None
if the index is out of bounds.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut mat = matrix![0, 1, 2;
3, 4, 5;
6, 7, 8];
let mut slice = mat.sub_slice_mut([1,1], 2, 2);
{
let col = slice.col_mut(1);
let mut expected = matrix![5usize; 8];
assert_matrix_eq!(*col, expected);
}
Panics
Will panic if the column index is out of bounds.
sourceunsafe fn col_unchecked_mut(&mut self, index: usize) -> ColumnMut<'_, T>
unsafe fn col_unchecked_mut(&mut self, index: usize) -> ColumnMut<'_, T>
Returns a mutable reference to the column of a matrix at the given index without doing a bounds check.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut mat = matrix![0, 1, 2;
3, 4, 5;
6, 7, 8];
let mut slice = mat.sub_slice_mut([1,1], 2, 2);
let col = unsafe { slice.col_unchecked_mut(1) };
let mut expected = matrix![5usize; 8];
assert_matrix_eq!(*col, expected);
sourcefn row_mut(&mut self, index: usize) -> RowMut<'_, T>
fn row_mut(&mut self, index: usize) -> RowMut<'_, T>
Returns a mutable reference to the row of a matrix at the given index.
None
if the index is out of bounds.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut mat = matrix![0, 1, 2;
3, 4, 5;
6, 7, 8];
let mut slice = mat.sub_slice_mut([1,1], 2, 2);
{
let row = slice.row_mut(1);
let mut expected = matrix![7usize, 8];
assert_matrix_eq!(*row, expected);
}
Panics
Will panic if the row index is out of bounds.
sourceunsafe fn row_unchecked_mut(&mut self, index: usize) -> RowMut<'_, T>
unsafe fn row_unchecked_mut(&mut self, index: usize) -> RowMut<'_, T>
Returns a mutable reference to the row of a matrix at the given index without doing a bounds check.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut mat = matrix![0, 1, 2;
3, 4, 5;
6, 7, 8];
let mut slice = mat.sub_slice_mut([1,1], 2, 2);
let row = unsafe { slice.row_unchecked_mut(1) };
let mut expected = matrix![7usize, 8];
assert_matrix_eq!(*row, expected);
sourcefn swap_rows(&mut self, a: usize, b: usize)
fn swap_rows(&mut self, a: usize, b: usize)
Swaps two rows in a matrix.
If a == b
, this method does nothing.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut x = matrix![0, 1;
2, 3;
4, 5;
6, 7];
x.swap_rows(1, 3);
let expected = matrix![0, 1;
6, 7;
4, 5;
2, 3];
assert_matrix_eq!(x, expected);
Panics
Panics if a
or b
are out of bounds.
sourcefn col_iter_mut(&mut self) -> ColsMut<'_, T>
fn col_iter_mut(&mut self) -> ColsMut<'_, T>
Iterate over the mutable columns of the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut a = matrix![0, 1;
2, 3;
4, 5];
for mut col in a.col_iter_mut() {
*col += 1;
}
// Now contains the range 1..7
println!("{}", a);
sourcefn row_iter_mut(&mut self) -> RowsMut<'_, T>
fn row_iter_mut(&mut self) -> RowsMut<'_, T>
Iterate over the mutable rows of the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut a = matrix![0, 1;
2, 3;
4, 5];
for mut row in a.row_iter_mut() {
*row += 1;
}
// Now contains the range 1..7
println!("{}", a);
sourcefn diag_iter_mut(&mut self, k: DiagOffset) -> DiagonalMut<'_, T, Self>
fn diag_iter_mut(&mut self, k: DiagOffset) -> DiagonalMut<'_, T, Self>
Iterate over diagonal entries mutably
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut, DiagOffset};
let mut a = matrix![0, 1, 2;
3, 4, 5;
6, 7, 8];
// Increment super diag
for d in a.diag_iter_mut(DiagOffset::Above(1)) {
*d = *d + 1;
}
// Zero the sub-diagonal (sets 3 and 7 to 0)
// Equivalent to `diag_iter(DiagOffset::Below(1))`
for sub_d in a.diag_iter_mut(DiagOffset::from(-1)) {
*sub_d = 0;
}
println!("{}", a);
Panics
If using an Above
or Below
offset which is
out-of-bounds this function will panic.
This function will never panic if the Main
diagonal
offset is used.
sourcefn set_to<M>(self, target: M)where
M: BaseMatrix<T>,
T: Copy,
fn set_to<M>(self, target: M)where
M: BaseMatrix<T>,
T: Copy,
Sets the underlying matrix data to the target data.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
let mut mat = Matrix::<f32>::zeros(4,4);
let one_block = Matrix::<f32>::ones(2,2);
// Get a mutable slice of the upper left 2x2 block.
let mat_block = mat.sub_slice_mut([0,0], 2, 2);
// Set the upper left 2x2 block to be ones.
mat_block.set_to(one_block);
Panics
Panics if the dimensions of self
and target
are not the same.
sourcefn apply(self, f: &dyn Fn(T)) -> Selfwhere
T: Copy,
fn apply(self, f: &dyn Fn(T)) -> Selfwhere
T: Copy,
Applies a function to each element in the matrix.
Examples
use rulinalg::matrix::{Matrix, BaseMatrixMut};
fn add_two(a: f64) -> f64 {
a + 2f64
}
let a = Matrix::new(2, 2, vec![0.;4]);
let b = a.apply(&add_two);
assert_eq!(b, matrix![2.0, 2.0; 2.0, 2.0]);
sourcefn split_at_mut(
&mut self,
mid: usize,
axis: Axes
) -> (MatrixSliceMut<'_, T>, MatrixSliceMut<'_, T>)
fn split_at_mut(
&mut self,
mid: usize,
axis: Axes
) -> (MatrixSliceMut<'_, T>, MatrixSliceMut<'_, T>)
Split the matrix at the specified axis returning two MatrixSliceMut
s.
Examples
use rulinalg::matrix::{Axes, Matrix, BaseMatrixMut};
let mut a = Matrix::new(3,3, vec![2.0; 9]);
let (b, c) = a.split_at_mut(1, Axes::Col);
sourcefn sub_slice_mut<'a>(
&mut self,
start: [usize; 2],
rows: usize,
cols: usize
) -> MatrixSliceMut<'a, T>where
T: 'a,
fn sub_slice_mut<'a>(
&mut self,
start: [usize; 2],
rows: usize,
cols: usize
) -> MatrixSliceMut<'a, T>where
T: 'a,
Produce a MatrixSliceMut
from an existing matrix.
Examples
use rulinalg::matrix::{Matrix, MatrixSliceMut, BaseMatrixMut};
let mut a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
let mut slice = MatrixSliceMut::from_matrix(&mut a, [1,1], 2, 2);
let new_slice = slice.sub_slice_mut([0,0], 1, 1);