Trait rulinalg::matrix::slice::BaseMatrixMut [] [src]

pub trait BaseMatrixMut<T>: BaseMatrix<T> {
    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 iter_mut<'a>(&mut self) -> SliceIterMut<'a, T> where T: 'a { ... }
    fn get_row_mut(&mut self, index: usize) -> Option<&mut [T]> { ... }
    unsafe fn get_row_unchecked_mut(&mut self, index: usize) -> &mut [T] { ... }
    fn iter_rows_mut(&mut self) -> RowsMut<T> { ... }
    fn set_to<M: BaseMatrix<T>>(self, target: M) where T: Copy { ... }
    fn apply(self, f: &Fn(T) -> 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 { ... }
}

Trait for mutable matrices.

Required Methods

Top left index of the slice.

Provided Methods

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();

Get a mutable reference to a point in the matrix without bounds checks.

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_eq!(a.into_vec(), vec![0,1,2,3,6,7,6,9,10]);

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 a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
let mut slice = a.sub_slice_mut([1,1], 2, 2);
{
    let row = slice.get_row_mut(1);
    let mut expected = vec![7usize, 8];
    assert_eq!(row, Some(&mut *expected));
}
assert!(slice.get_row_mut(5).is_none());

Returns a mutable reference to the row of a matrix at the given index without doing unbounds checking

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);
let row = unsafe { slice.get_row_unchecked_mut(1) };
let mut expected = vec![7usize, 8];
assert_eq!(row, &mut *expected);

Iterate over the mutable rows of the matrix.

Examples

use rulinalg::matrix::{Matrix, BaseMatrixMut};

let mut a = Matrix::new(3, 2, (0..6).collect::<Vec<usize>>());

for row in a.iter_rows_mut() {
    for r in row {
        *r = *r + 1;
    }
}

// Now contains the range 1..7
println!("{}", a);

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.

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.data(), vec![2.0; 4]);

Split the matrix at the specified axis returning two MatrixSliceMuts.

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);

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);

Implementors