pub struct Matrix<T> { /* private fields */ }
Expand description
Matrix<T>
means matrix.
Implementations§
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn get<I>(&self, index: I) -> Result<&I::Output>where
I: MatrixIndex<T>,
pub fn get<I>(&self, index: I) -> Result<&I::Output>where
I: MatrixIndex<T>,
Returns a reference to the MatrixIndex::Output
at given location.
§Errors
Error::IndexOutOfBounds
if out of bounds.
§Examples
use matreex::{matrix, Error};
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(matrix.get((1, 1)), Ok(&4));
assert_eq!(matrix.get((2, 3)), Err(Error::IndexOutOfBounds));
Sourcepub fn get_mut<I>(&mut self, index: I) -> Result<&mut I::Output>where
I: MatrixIndex<T>,
pub fn get_mut<I>(&mut self, index: I) -> Result<&mut I::Output>where
I: MatrixIndex<T>,
Returns a mutable reference to the MatrixIndex::Output
at given location.
§Errors
Error::IndexOutOfBounds
if out of bounds.
§Examples
use matreex::{matrix, Error};
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(matrix.get_mut((1, 1)), Ok(&mut 4));
assert_eq!(matrix.get_mut((2, 3)), Err(Error::IndexOutOfBounds));
Sourcepub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Outputwhere
I: MatrixIndex<T>,
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Outputwhere
I: MatrixIndex<T>,
Returns a reference to the MatrixIndex::Output
at given location, without performing any bounds checking.
§Safety
Calling this method with an out-of-bounds index is undefined behavior.
For a safe alternative see get
.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(unsafe { matrix.get_unchecked((1, 1)) }, &4);
Sourcepub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Outputwhere
I: MatrixIndex<T>,
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Outputwhere
I: MatrixIndex<T>,
Returns a mutable reference to the MatrixIndex::Output
at given location, without performing any bounds checking.
§Safety
Calling this method with an out-of-bounds index is undefined behavior.
For a safe alternative see get_mut
.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(unsafe { matrix.get_unchecked_mut((1, 1)) }, &mut 4);
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn iter_rows(&self) -> MatrixIter<'_, &T>
pub fn iter_rows(&self) -> MatrixIter<'_, &T>
Returns an iterator over the rows of the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut rows = matrix.iter_rows();
let mut row_0 = rows.next().unwrap();
assert_eq!(row_0.next(), Some(&0));
assert_eq!(row_0.next(), Some(&1));
assert_eq!(row_0.next(), Some(&2));
assert_eq!(row_0.next(), None);
let mut row_1 = rows.next().unwrap();
assert_eq!(row_1.next(), Some(&3));
assert_eq!(row_1.next(), Some(&4));
assert_eq!(row_1.next(), Some(&5));
assert_eq!(row_1.next(), None);
assert!(rows.next().is_none());
Sourcepub fn iter_cols(&self) -> MatrixIter<'_, &T>
pub fn iter_cols(&self) -> MatrixIter<'_, &T>
Returns an iterator over the columns of the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut cols = matrix.iter_cols();
let mut col_0 = cols.next().unwrap();
assert_eq!(col_0.next(), Some(&0));
assert_eq!(col_0.next(), Some(&3));
assert_eq!(col_0.next(), None);
let mut col_1 = cols.next().unwrap();
assert_eq!(col_1.next(), Some(&1));
assert_eq!(col_1.next(), Some(&4));
assert_eq!(col_1.next(), None);
let mut col_2 = cols.next().unwrap();
assert_eq!(col_2.next(), Some(&2));
assert_eq!(col_2.next(), Some(&5));
assert_eq!(col_2.next(), None);
assert!(cols.next().is_none());
Sourcepub fn iter_nth_row(&self, n: usize) -> Result<VectorIter<'_, &T>>
pub fn iter_nth_row(&self, n: usize) -> Result<VectorIter<'_, &T>>
Returns an iterator over the elements of the nth row in the matrix.
§Errors
Error::IndexOutOfBounds
ifn
is greater than or equal to the number of rows in the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut row_1 = matrix.iter_nth_row(1)?;
assert_eq!(row_1.next(), Some(&3));
assert_eq!(row_1.next(), Some(&4));
assert_eq!(row_1.next(), Some(&5));
assert_eq!(row_1.next(), None);
Sourcepub fn iter_nth_row_mut(&mut self, n: usize) -> Result<VectorIter<'_, &mut T>>
pub fn iter_nth_row_mut(&mut self, n: usize) -> Result<VectorIter<'_, &mut T>>
Returns an iterator that allows modifying each element of the nth row in the matrix.
§Errors
Error::IndexOutOfBounds
ifn
is greater than or equal to the number of rows in the matrix.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
for element in matrix.iter_nth_row_mut(1)? {
*element += 1;
}
assert_eq!(matrix, matrix![[0, 1, 2], [4, 5, 6]]);
Sourcepub fn iter_nth_col(&self, n: usize) -> Result<VectorIter<'_, &T>>
pub fn iter_nth_col(&self, n: usize) -> Result<VectorIter<'_, &T>>
Returns an iterator over the elements of the nth column in the matrix.
§Errors
Error::IndexOutOfBounds
ifn
is greater than or equal to the number of columns in the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut col_1 = matrix.iter_nth_col(1)?;
assert_eq!(col_1.next(), Some(&1));
assert_eq!(col_1.next(), Some(&4));
assert_eq!(col_1.next(), None);
Sourcepub fn iter_nth_col_mut(&mut self, n: usize) -> Result<VectorIter<'_, &mut T>>
pub fn iter_nth_col_mut(&mut self, n: usize) -> Result<VectorIter<'_, &mut T>>
Returns an iterator that allows modifying each element of the nth column in the matrix.
§Errors
Error::IndexOutOfBounds
ifn
is greater than or equal to the number of columns in the matrix.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
for element in matrix.iter_nth_col_mut(1)? {
*element += 1;
}
assert_eq!(matrix, matrix![[0, 2, 2], [3, 5, 5]]);
Sourcepub fn iter_elements(&self) -> impl ExactSizeDoubleEndedIterator<Item = &T>
pub fn iter_elements(&self) -> impl ExactSizeDoubleEndedIterator<Item = &T>
Returns an iterator over the elements of the matrix.
§Notes
The iteration order of elements is not guaranteed. In the current implementation, elements are iterated in memory order.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut data: Vec<&i32> = matrix.iter_elements().collect();
data.sort(); // sort because the order of the elements is not guaranteed
assert_eq!(data, vec![&0, &1, &2, &3, &4, &5]);
Sourcepub fn iter_elements_mut(
&mut self,
) -> impl ExactSizeDoubleEndedIterator<Item = &mut T>
pub fn iter_elements_mut( &mut self, ) -> impl ExactSizeDoubleEndedIterator<Item = &mut T>
Returns an iterator that allows modifying each element of the matrix.
§Notes
The iteration order of elements is not guaranteed. In the current implementation, elements are iterated in memory order.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.iter_elements_mut().for_each(|element| {
*element += 1;
});
assert_eq!(matrix, matrix![[1, 2, 3], [4, 5, 6]]);
Sourcepub fn into_iter_elements(self) -> impl ExactSizeDoubleEndedIterator<Item = T>
pub fn into_iter_elements(self) -> impl ExactSizeDoubleEndedIterator<Item = T>
Creates a consuming iterator, that is, one that moves each element out of the matrix.
§Notes
The iteration order of elements is not guaranteed. In the current implementation, elements are iterated in memory order.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut data: Vec<i32> = matrix.into_iter_elements().collect();
data.sort(); // sort because the order of the elements is not guaranteed
assert_eq!(data, vec![0, 1, 2, 3, 4, 5]);
Sourcepub fn iter_elements_with_index(
&self,
) -> impl ExactSizeDoubleEndedIterator<Item = (Index, &T)>
pub fn iter_elements_with_index( &self, ) -> impl ExactSizeDoubleEndedIterator<Item = (Index, &T)>
Returns an iterator over the elements of the matrix along with their indices.
§Notes
The iteration order of elements is not guaranteed. In the current implementation, elements are iterated in memory order.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.iter_elements_with_index()
.for_each(|(index, element)| {
assert_eq!(element, &matrix[index]);
});
Sourcepub fn iter_elements_mut_with_index(
&mut self,
) -> impl ExactSizeDoubleEndedIterator<Item = (Index, &mut T)>
pub fn iter_elements_mut_with_index( &mut self, ) -> impl ExactSizeDoubleEndedIterator<Item = (Index, &mut T)>
Returns an iterator that allows modifying each element of the matrix along with its index.
§Notes
The iteration order of elements is not guaranteed. In the current implementation, elements are iterated in memory order.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.iter_elements_mut_with_index()
.for_each(|(index, element)| {
*element += index.row as i32 + index.col as i32;
});
assert_eq!(matrix, matrix![[0, 2, 4], [4, 6, 8]]);
Sourcepub fn into_iter_elements_with_index(
self,
) -> impl ExactSizeDoubleEndedIterator<Item = (Index, T)>
pub fn into_iter_elements_with_index( self, ) -> impl ExactSizeDoubleEndedIterator<Item = (Index, T)>
Creates a consuming iterator, that is, one that moves each element out of the matrix along with its index.
§Notes
The iteration order of elements is not guaranteed. In the current implementation, elements are iterated in memory order.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.clone()
.into_iter_elements_with_index()
.for_each(|(index, element)| {
assert_eq!(element, matrix[index]);
});
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn elementwise_add<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
pub fn elementwise_add<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
Performs elementwise addition on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_add(&rhs);
assert_eq!(result, Ok(matrix![[2, 3, 4], [5, 6, 7]]));
Sourcepub fn elementwise_add_consume_self<R, U>(
self,
rhs: &Matrix<R>,
) -> Result<Matrix<U>>
pub fn elementwise_add_consume_self<R, U>( self, rhs: &Matrix<R>, ) -> Result<Matrix<U>>
Performs elementwise addition on two matrices, consuming self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_add_consume_self(&rhs);
assert_eq!(result, Ok(matrix![[2, 3, 4], [5, 6, 7]]));
Sourcepub fn elementwise_add_assign<R>(
&mut self,
rhs: &Matrix<R>,
) -> Result<&mut Self>
pub fn elementwise_add_assign<R>( &mut self, rhs: &Matrix<R>, ) -> Result<&mut Self>
Performs elementwise addition on two matrices, assigning the result
to self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::matrix;
let mut lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
lhs.elementwise_add_assign(&rhs)?;
assert_eq!(lhs, matrix![[2, 3, 4], [5, 6, 7]]);
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn elementwise_div<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
pub fn elementwise_div<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
Performs elementwise division on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_div(&rhs);
assert_eq!(result, Ok(matrix![[0, 0, 1], [1, 2, 2]]));
Sourcepub fn elementwise_div_consume_self<R, U>(
self,
rhs: &Matrix<R>,
) -> Result<Matrix<U>>
pub fn elementwise_div_consume_self<R, U>( self, rhs: &Matrix<R>, ) -> Result<Matrix<U>>
Performs elementwise division on two matrices, consuming self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_div_consume_self(&rhs);
assert_eq!(result, Ok(matrix![[0, 0, 1], [1, 2, 2]]));
Sourcepub fn elementwise_div_assign<R>(
&mut self,
rhs: &Matrix<R>,
) -> Result<&mut Self>
pub fn elementwise_div_assign<R>( &mut self, rhs: &Matrix<R>, ) -> Result<&mut Self>
Performs elementwise division on two matrices, assigning the result
to self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::matrix;
let mut lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
lhs.elementwise_div_assign(&rhs)?;
assert_eq!(lhs, matrix![[0, 0, 1], [1, 2, 2]]);
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn elementwise_mul<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
pub fn elementwise_mul<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
Performs elementwise multiplication on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_mul(&rhs);
assert_eq!(result, Ok(matrix![[0, 2, 4], [6, 8, 10]]));
Sourcepub fn elementwise_mul_consume_self<R, U>(
self,
rhs: &Matrix<R>,
) -> Result<Matrix<U>>
pub fn elementwise_mul_consume_self<R, U>( self, rhs: &Matrix<R>, ) -> Result<Matrix<U>>
Performs elementwise multiplication on two matrices, consuming self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_mul_consume_self(&rhs);
assert_eq!(result, Ok(matrix![[0, 2, 4], [6, 8, 10]]));
Sourcepub fn elementwise_mul_assign<R>(
&mut self,
rhs: &Matrix<R>,
) -> Result<&mut Self>
pub fn elementwise_mul_assign<R>( &mut self, rhs: &Matrix<R>, ) -> Result<&mut Self>
Performs elementwise multiplication on two matrices, assigning the result
to self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::matrix;
let mut lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
lhs.elementwise_mul_assign(&rhs)?;
assert_eq!(lhs, matrix![[0, 2, 4], [6, 8, 10]]);
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn multiply<R, U>(self, rhs: Matrix<R>) -> Result<Matrix<U>>
pub fn multiply<R, U>(self, rhs: Matrix<R>) -> Result<Matrix<U>>
Performs matrix multiplication on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
For performance reasons, this method consumes both self
and rhs
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[0, 1], [2, 3], [4, 5]];
let result = lhs.multiply(rhs);
assert_eq!(result, Ok(matrix![[10, 13], [28, 40]]));
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn elementwise_rem<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
pub fn elementwise_rem<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
Performs elementwise remainder operation on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_rem(&rhs);
assert_eq!(result, Ok(matrix![[0, 1, 0], [1, 0, 1]]));
Sourcepub fn elementwise_rem_consume_self<R, U>(
self,
rhs: &Matrix<R>,
) -> Result<Matrix<U>>
pub fn elementwise_rem_consume_self<R, U>( self, rhs: &Matrix<R>, ) -> Result<Matrix<U>>
Performs elementwise remainder operation on two matrices,
consuming self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_rem_consume_self(&rhs);
assert_eq!(result, Ok(matrix![[0, 1, 0], [1, 0, 1]]));
Sourcepub fn elementwise_rem_assign<R>(
&mut self,
rhs: &Matrix<R>,
) -> Result<&mut Self>
pub fn elementwise_rem_assign<R>( &mut self, rhs: &Matrix<R>, ) -> Result<&mut Self>
Performs elementwise remainder operation on two matrices,
assigning the result to self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::matrix;
let mut lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
lhs.elementwise_rem_assign(&rhs)?;
assert_eq!(lhs, matrix![[0, 1, 0], [1, 0, 1]]);
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn elementwise_sub<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
pub fn elementwise_sub<R, U>(&self, rhs: &Matrix<R>) -> Result<Matrix<U>>
Performs elementwise subtraction on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_sub(&rhs);
assert_eq!(result, Ok(matrix![[-2, -1, 0], [1, 2, 3]]));
Sourcepub fn elementwise_sub_consume_self<R, U>(
self,
rhs: &Matrix<R>,
) -> Result<Matrix<U>>
pub fn elementwise_sub_consume_self<R, U>( self, rhs: &Matrix<R>, ) -> Result<Matrix<U>>
Performs elementwise subtraction on two matrices, consuming self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_sub_consume_self(&rhs);
assert_eq!(result, Ok(matrix![[-2, -1, 0], [1, 2, 3]]));
Sourcepub fn elementwise_sub_assign<R>(
&mut self,
rhs: &Matrix<R>,
) -> Result<&mut Self>
pub fn elementwise_sub_assign<R>( &mut self, rhs: &Matrix<R>, ) -> Result<&mut Self>
Performs elementwise subtraction on two matrices, assigning the result
to self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::matrix;
let mut lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
lhs.elementwise_sub_assign(&rhs)?;
assert_eq!(lhs, matrix![[-2, -1, 0], [1, 2, 3]]);
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn is_square(&self) -> bool
pub fn is_square(&self) -> bool
Returns true
if the matrix is a square matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5], [6, 7, 8]];
assert!(matrix.is_square());
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert!(!matrix.is_square());
Sourcepub fn is_elementwise_operation_conformable<R>(&self, rhs: &Matrix<R>) -> bool
pub fn is_elementwise_operation_conformable<R>(&self, rhs: &Matrix<R>) -> bool
Returns true
if two matrices are conformable for elementwise
operations.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
assert!(lhs.is_elementwise_operation_conformable(&rhs));
let rhs = matrix![[0, 1], [2, 3], [4, 5]];
assert!(!lhs.is_elementwise_operation_conformable(&rhs));
Sourcepub fn is_multiplication_like_operation_conformable<R>(
&self,
rhs: &Matrix<R>,
) -> bool
pub fn is_multiplication_like_operation_conformable<R>( &self, rhs: &Matrix<R>, ) -> bool
Returns true
if two matrices are conformable for multiplication-like
operations.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[0, 1], [2, 3], [4, 5]];
assert!(lhs.is_multiplication_like_operation_conformable(&rhs));
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
assert!(!lhs.is_multiplication_like_operation_conformable(&rhs));
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn ensure_square(&self) -> Result<&Self>
pub fn ensure_square(&self) -> Result<&Self>
Ensures that the matrix is a square matrix.
§Errors
Error::SquareMatrixRequired
if the matrix is not a square matrix.
§Examples
use matreex::{matrix, Error};
let matrix = matrix![[0, 1, 2], [3, 4, 5], [6, 7, 8]];
let result = matrix.ensure_square();
assert!(result.is_ok());
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let result = matrix.ensure_square();
assert_eq!(result, Err(Error::SquareMatrixRequired));
Sourcepub fn ensure_elementwise_operation_conformable<R>(
&self,
rhs: &Matrix<R>,
) -> Result<&Self>
pub fn ensure_elementwise_operation_conformable<R>( &self, rhs: &Matrix<R>, ) -> Result<&Self>
Ensures that two matrices are conformable for elementwise operations.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::{matrix, Error};
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.ensure_elementwise_operation_conformable(&rhs);
assert!(result.is_ok());
let rhs = matrix![[0, 1], [2, 3], [4, 5]];
let result = lhs.ensure_elementwise_operation_conformable(&rhs);
assert_eq!(result, Err(Error::ShapeNotConformable));
Sourcepub fn ensure_multiplication_like_operation_conformable<R>(
&self,
rhs: &Matrix<R>,
) -> Result<&Self>
pub fn ensure_multiplication_like_operation_conformable<R>( &self, rhs: &Matrix<R>, ) -> Result<&Self>
Ensures that two matrices are conformable for multiplication-like operation.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::{matrix, Error};
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[0, 1], [2, 3], [4, 5]];
let result = lhs.ensure_multiplication_like_operation_conformable(&rhs);
assert!(result.is_ok());
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.ensure_multiplication_like_operation_conformable(&rhs);
assert_eq!(result, Err(Error::ShapeNotConformable));
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn elementwise_operation<R, F, U>(
&self,
rhs: &Matrix<R>,
op: F,
) -> Result<Matrix<U>>
pub fn elementwise_operation<R, F, U>( &self, rhs: &Matrix<R>, op: F, ) -> Result<Matrix<U>>
Performs elementwise operation on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_operation(&rhs, |x, y| x + y);
assert_eq!(result, Ok(matrix![[2, 3, 4], [5, 6, 7]]));
Sourcepub fn elementwise_operation_consume_self<R, F, U>(
self,
rhs: &Matrix<R>,
op: F,
) -> Result<Matrix<U>>
pub fn elementwise_operation_consume_self<R, F, U>( self, rhs: &Matrix<R>, op: F, ) -> Result<Matrix<U>>
Performs elementwise operation on two matrices, consuming self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
§Examples
use matreex::matrix;
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
let result = lhs.elementwise_operation_consume_self(&rhs, |x, y| x + y);
assert_eq!(result, Ok(matrix![[2, 3, 4], [5, 6, 7]]));
Sourcepub fn elementwise_operation_assign<R, F>(
&mut self,
rhs: &Matrix<R>,
op: F,
) -> Result<&mut Self>
pub fn elementwise_operation_assign<R, F>( &mut self, rhs: &Matrix<R>, op: F, ) -> Result<&mut Self>
Performs elementwise operation on two matrices, assigning the result
to self
.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Examples
use matreex::matrix;
let mut lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[2, 2, 2], [2, 2, 2]];
lhs.elementwise_operation_assign(&rhs, |x, y| *x += y)?;
assert_eq!(lhs, matrix![[2, 3, 4], [5, 6, 7]]);
Source§impl<L> Matrix<L>
impl<L> Matrix<L>
Sourcepub fn multiplication_like_operation<R, F, U>(
self,
rhs: Matrix<R>,
op: F,
) -> Result<Matrix<U>>
pub fn multiplication_like_operation<R, F, U>( self, rhs: Matrix<R>, op: F, ) -> Result<Matrix<U>>
Performs multiplication-like operation on two matrices.
§Errors
Error::ShapeNotConformable
if the matrices are not conformable.
§Notes
The resulting matrix will always have the same order as self
.
For performance reasons, this method consumes both self
and rhs
.
The closure op
is guaranteed to receive two non-empty, equal-length
vectors. It should always return a valid value derived from them.
§Examples
use matreex::{matrix, VectorIter};
let lhs = matrix![[0, 1, 2], [3, 4, 5]];
let rhs = matrix![[0, 1], [2, 3], [4, 5]];
let op = |lv: VectorIter<&i32>, rv: VectorIter<&i32>| {
lv.zip(rv).map(|(x, y)| x * y).reduce(|acc, p| acc + p).unwrap()
};
let result = lhs.multiplication_like_operation(rhs, op);
assert_eq!(result, Ok(matrix![[10, 13], [28, 40]]));
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn scalar_operation<S, F, U>(&self, scalar: &S, op: F) -> Matrix<U>
pub fn scalar_operation<S, F, U>(&self, scalar: &S, op: F) -> Matrix<U>
Performs scalar operation on the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let scalar = 2;
let output = matrix.scalar_operation(&scalar, |x, y| x + y);
assert_eq!(output, matrix![[2, 3, 4], [5, 6, 7]]);
Sourcepub fn scalar_operation_consume_self<S, F, U>(
self,
scalar: &S,
op: F,
) -> Matrix<U>
pub fn scalar_operation_consume_self<S, F, U>( self, scalar: &S, op: F, ) -> Matrix<U>
Performs scalar operation on the matrix, consuming self
.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let scalar = 2;
let output = matrix.scalar_operation_consume_self(&scalar, |x, y| x + y);
assert_eq!(output, matrix![[2, 3, 4], [5, 6, 7]]);
Sourcepub fn scalar_operation_assign<S, F>(&mut self, scalar: &S, op: F) -> &mut Self
pub fn scalar_operation_assign<S, F>(&mut self, scalar: &S, op: F) -> &mut Self
Performs scalar operation on the matrix, assigning the result
to self
.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
let scalar = 2;
matrix.scalar_operation_assign(&scalar, |x, y| *x += y);
assert_eq!(matrix, matrix![[2, 3, 4], [5, 6, 7]]);
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn with_default<S>(shape: S) -> Result<Self>
pub fn with_default<S>(shape: S) -> Result<Self>
Creates a new Matrix<T>
with the specified shape, filled with
the default value.
§Errors
Error::SizeOverflow
if size exceedsusize::MAX
.Error::CapacityOverflow
if total bytes stored exceedsisize::MAX
.
§Examples
use matreex::{matrix, Matrix};
let result = Matrix::with_default((2, 3));
assert_eq!(result, Ok(matrix![[0, 0, 0], [0, 0, 0]]));
Sourcepub fn with_value<S>(shape: S, value: T) -> Result<Self>
pub fn with_value<S>(shape: S, value: T) -> Result<Self>
Creates a new Matrix<T>
with the specified shape, filled with
the given value.
§Errors
Error::SizeOverflow
if size exceedsusize::MAX
.Error::CapacityOverflow
if total bytes stored exceedsisize::MAX
.
§Examples
use matreex::{matrix, Matrix};
let result = Matrix::with_value((2, 3), 0);
assert_eq!(result, Ok(matrix![[0, 0, 0], [0, 0, 0]]));
Sourcepub fn with_initializer<S, F>(shape: S, initializer: F) -> Result<Self>
pub fn with_initializer<S, F>(shape: S, initializer: F) -> Result<Self>
Creates a new Matrix<T>
with the specified shape, where each
element is initialized using its index.
§Errors
Error::SizeOverflow
if size exceedsusize::MAX
.Error::CapacityOverflow
if total bytes stored exceedsisize::MAX
.
§Examples
use matreex::{matrix, Matrix};
let result = Matrix::with_initializer((2, 3), |index| index.row + index.col);
assert_eq!(result, Ok(matrix![[0, 1, 2], [1, 2, 3]]));
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn swap<I, J>(&mut self, i: I, j: J) -> Result<&mut Self>where
I: MatrixIndex<T, Output = T>,
J: MatrixIndex<T, Output = T>,
pub fn swap<I, J>(&mut self, i: I, j: J) -> Result<&mut Self>where
I: MatrixIndex<T, Output = T>,
J: MatrixIndex<T, Output = T>,
Swaps the elements at the given indices.
§Errors
Error::IndexOutOfBounds
if out of bounds.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.swap((0, 0), (1, 1))?;
assert_eq!(matrix, matrix![[4, 1, 2], [3, 0, 5]]);
Sourcepub fn swap_rows(&mut self, m: usize, n: usize) -> Result<&mut Self>
pub fn swap_rows(&mut self, m: usize, n: usize) -> Result<&mut Self>
Swaps the rows at the given indices.
§Errors
Error::IndexOutOfBounds
if out of bounds.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.swap_rows(0, 1)?;
assert_eq!(matrix, matrix![[3, 4, 5], [0, 1, 2]]);
Sourcepub fn swap_cols(&mut self, m: usize, n: usize) -> Result<&mut Self>
pub fn swap_cols(&mut self, m: usize, n: usize) -> Result<&mut Self>
Swaps the columns at the given indices.
§Errors
Error::IndexOutOfBounds
if out of bounds.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.swap_cols(0, 1)?;
assert_eq!(matrix, matrix![[1, 0, 2], [4, 3, 5]]);
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn par_apply<F>(&mut self, f: F) -> &mut Self
pub fn par_apply<F>(&mut self, f: F) -> &mut Self
Applies a closure to each element of the matrix in parallel, modifying the matrix in place.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.par_apply(|x| *x += 1);
assert_eq!(matrix, matrix![[1, 2, 3], [4, 5, 6]]);
Sourcepub fn par_map<U, F>(self, f: F) -> Matrix<U>
pub fn par_map<U, F>(self, f: F) -> Matrix<U>
Applies a closure to each element of the matrix in parallel, returning a new matrix with the results.
§Examples
use matreex::matrix;
let matrix_i32 = matrix![[0, 1, 2], [3, 4, 5]];
let matrix_f64 = matrix_i32.par_map(|x| x as f64);
assert_eq!(matrix_f64, matrix![[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]);
Sourcepub fn par_map_ref<U, F>(&self, f: F) -> Matrix<U>
pub fn par_map_ref<U, F>(&self, f: F) -> Matrix<U>
Applies a closure to each element of the matrix in parallel, returning a new matrix with the results.
This method is similar to par_map
but passes references to the
elements instead of taking ownership of them.
§Examples
use matreex::matrix;
let matrix_i32 = matrix![[0, 1, 2], [3, 4, 5]];
let matrix_f64 = matrix_i32.par_map_ref(|x| *x as f64);
assert_eq!(matrix_f64, matrix![[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]);
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn par_iter_elements(&self) -> impl ParallelIterator<Item = &T>where
T: Sync,
pub fn par_iter_elements(&self) -> impl ParallelIterator<Item = &T>where
T: Sync,
Returns a parallel iterator over the elements of the matrix.
§Examples
use matreex::matrix;
use rayon::prelude::*;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let sum = matrix.par_iter_elements().sum::<i32>();
assert_eq!(sum, 15);
Sourcepub fn par_iter_elements_mut(&mut self) -> impl ParallelIterator<Item = &mut T>where
T: Send,
pub fn par_iter_elements_mut(&mut self) -> impl ParallelIterator<Item = &mut T>where
T: Send,
Returns a parallel iterator that allows modifying each element of the matrix.
§Examples
use matreex::matrix;
use rayon::prelude::*;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.par_iter_elements_mut()
.for_each(|element| *element += 1);
assert_eq!(matrix, matrix![[1, 2, 3], [4, 5, 6]]);
Sourcepub fn into_par_iter_elements(self) -> impl ParallelIterator<Item = T>where
T: Send,
pub fn into_par_iter_elements(self) -> impl ParallelIterator<Item = T>where
T: Send,
Creates a parallel consuming iterator, that is, one that moves each element out of the matrix.
§Examples
use matreex::matrix;
use rayon::prelude::*;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let sum = matrix.into_par_iter_elements().sum::<i32>();
assert_eq!(sum, 15);
Sourcepub fn par_iter_elements_with_index(
&self,
) -> impl ParallelIterator<Item = (Index, &T)>where
T: Sync,
pub fn par_iter_elements_with_index(
&self,
) -> impl ParallelIterator<Item = (Index, &T)>where
T: Sync,
Returns a parallel iterator over the elements of the matrix along with their indices.
§Examples
use matreex::matrix;
use rayon::prelude::*;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.par_iter_elements_with_index()
.for_each(|(index, element)| {
assert_eq!(element, &matrix[index]);
});
Sourcepub fn par_iter_elements_mut_with_index(
&mut self,
) -> impl ParallelIterator<Item = (Index, &mut T)>where
T: Send,
pub fn par_iter_elements_mut_with_index(
&mut self,
) -> impl ParallelIterator<Item = (Index, &mut T)>where
T: Send,
Returns a parallel iterator that allows modifying each element of the matrix along with its index.
§Examples
use matreex::matrix;
use rayon::prelude::*;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.par_iter_elements_mut_with_index()
.for_each(|(index, element)| {
*element += index.row as i32 + index.col as i32;
});
assert_eq!(matrix, matrix![[0, 2, 4], [4, 6, 8]]);
Sourcepub fn into_par_iter_elements_with_index(
self,
) -> impl ParallelIterator<Item = (Index, T)>where
T: Send,
pub fn into_par_iter_elements_with_index(
self,
) -> impl ParallelIterator<Item = (Index, T)>where
T: Send,
Creates a parallel consuming iterator, that is, one that moves each element out of the matrix along with its index.
§Examples
use matreex::matrix;
use rayon::prelude::*;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix
.clone()
.into_par_iter_elements_with_index()
.for_each(|(index, element)| {
assert_eq!(element, matrix[index]);
});
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn order(&self) -> Order
pub fn order(&self) -> Order
Returns the order of the matrix.
§Examples
use matreex::{matrix, Order};
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(matrix.order(), Order::default());
Sourcepub fn shape(&self) -> Shape
pub fn shape(&self) -> Shape
Returns the shape of the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
let shape = matrix.shape();
assert_eq!(shape.nrows(), 2);
assert_eq!(shape.ncols(), 3);
Sourcepub fn nrows(&self) -> usize
pub fn nrows(&self) -> usize
Returns the number of rows in the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(matrix.nrows(), 2);
Sourcepub fn ncols(&self) -> usize
pub fn ncols(&self) -> usize
Returns the number of columns in the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(matrix.ncols(), 3);
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the total number of elements in the matrix.
§Examples
use matreex::matrix;
let matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert_eq!(matrix.size(), 6);
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn transpose(&mut self) -> &mut Self
pub fn transpose(&mut self) -> &mut Self
Transposes the matrix.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.transpose();
// row 0
assert_eq!(matrix[(0, 0)], 0);
assert_eq!(matrix[(0, 1)], 3);
// row 1
assert_eq!(matrix[(1, 0)], 1);
assert_eq!(matrix[(1, 1)], 4);
// row 2
assert_eq!(matrix[(2, 0)], 2);
assert_eq!(matrix[(2, 1)], 5);
Sourcepub fn switch_order(&mut self) -> &mut Self
pub fn switch_order(&mut self) -> &mut Self
Switches the order of the matrix.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
let order = matrix.order();
matrix.switch_order();
assert_ne!(matrix.order(), order);
matrix.switch_order();
assert_eq!(matrix.order(), order);
Sourcepub fn switch_order_without_rearrangement(&mut self) -> &mut Self
pub fn switch_order_without_rearrangement(&mut self) -> &mut Self
Switches the order of the matrix without rearranging the underlying data. As a result, the matrix appears transposed when accessed.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
let order = matrix.order();
matrix.switch_order_without_rearrangement();
assert_ne!(matrix.order(), order);
// row 0
assert_eq!(matrix[(0, 0)], 0);
assert_eq!(matrix[(0, 1)], 3);
// row 1
assert_eq!(matrix[(1, 0)], 1);
assert_eq!(matrix[(1, 1)], 4);
// row 2
assert_eq!(matrix[(2, 0)], 2);
assert_eq!(matrix[(2, 1)], 5);
Sourcepub fn set_order(&mut self, order: Order) -> &mut Self
pub fn set_order(&mut self, order: Order) -> &mut Self
Sets the order of the matrix.
§Examples
use matreex::{matrix, Order};
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.set_order(Order::RowMajor);
assert_eq!(matrix.order(), Order::RowMajor);
matrix.set_order(Order::ColMajor);
assert_eq!(matrix.order(), Order::ColMajor);
Sourcepub fn set_order_without_rearrangement(&mut self, order: Order) -> &mut Self
pub fn set_order_without_rearrangement(&mut self, order: Order) -> &mut Self
Sets the order of the matrix without rearranging the underlying data. As a result, when the order is changed, the matrix appears transposed when accessed.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
let mut order = matrix.order();
order.switch();
matrix.set_order_without_rearrangement(order);
assert_eq!(matrix.order(), order);
// row 0
assert_eq!(matrix[(0, 0)], 0);
assert_eq!(matrix[(0, 1)], 3);
// row 1
assert_eq!(matrix[(1, 0)], 1);
assert_eq!(matrix[(1, 1)], 4);
// row 2
assert_eq!(matrix[(2, 0)], 2);
assert_eq!(matrix[(2, 1)], 5);
Sourcepub fn resize<S>(&mut self, shape: S) -> Result<&mut Self>
pub fn resize<S>(&mut self, shape: S) -> Result<&mut Self>
Resizes the matrix to the specified shape.
§Errors
Error::SizeOverflow
if size exceedsusize::MAX
.Error::CapacityOverflow
if total bytes stored exceedsisize::MAX
.
§Notes
Reducing the size does not automatically shrink the capacity.
This choice is made to avoid potential reallocation. Consider
explicitly calling Matrix::shrink_to_fit
if needed.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.resize((2, 2))?;
assert_eq!(matrix, matrix![[0, 1], [2, 3]]);
matrix.resize((2, 3))?;
assert_eq!(matrix, matrix![[0, 1, 2], [3, 0, 0]]);
Sourcepub fn reshape<S>(&mut self, shape: S) -> Result<&mut Self>
pub fn reshape<S>(&mut self, shape: S) -> Result<&mut Self>
Reshapes the matrix to the specified shape.
§Errors
Error::SizeMismatch
if the size of the new shape does not match the current size of the matrix.
§Examples
use matreex::{matrix, Error};
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.reshape((3, 2))?;
assert_eq!(matrix, matrix![[0, 1], [2, 3], [4, 5]]);
let result = matrix.reshape((2, 2));
assert_eq!(result, Err(Error::SizeMismatch));
Sourcepub fn shrink_to_fit(&mut self) -> &mut Self
pub fn shrink_to_fit(&mut self) -> &mut Self
Shrinks the capacity of the matrix as much as possible.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert!(matrix.capacity() >= 6);
matrix.resize((1, 3))?;
assert!(matrix.capacity() >= 6);
matrix.shrink_to_fit();
assert!(matrix.capacity() >= 3);
Sourcepub fn shrink_to(&mut self, min_capacity: usize) -> &mut Self
pub fn shrink_to(&mut self, min_capacity: usize) -> &mut Self
Shrinks the capacity of the matrix with a lower bound.
The capacity will remain at least as large as both the size and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
assert!(matrix.capacity() >= 6);
matrix.resize((1, 3))?;
assert!(matrix.capacity() >= 6);
matrix.shrink_to(4);
assert!(matrix.capacity() >= 4);
matrix.shrink_to(0);
assert!(matrix.capacity() >= 3);
Sourcepub fn overwrite(&mut self, source: &Self) -> &mut Selfwhere
T: Clone,
pub fn overwrite(&mut self, source: &Self) -> &mut Selfwhere
T: Clone,
Overwrites the overlapping part of this matrix with source
,
leaving the non-overlapping part unchanged.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 0, 0], [0, 0, 0]];
let source = matrix![[1, 1], [1, 1], [1, 1]];
matrix.overwrite(&source);
assert_eq!(matrix, matrix![[1, 1, 0], [1, 1, 0]]);
Sourcepub fn apply<F>(&mut self, f: F) -> &mut Self
pub fn apply<F>(&mut self, f: F) -> &mut Self
Applies a closure to each element of the matrix, modifying the matrix in place.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.apply(|x| *x += 1);
assert_eq!(matrix, matrix![[1, 2, 3], [4, 5, 6]]);
Sourcepub fn map<U, F>(self, f: F) -> Matrix<U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> Matrix<U>where
F: FnMut(T) -> U,
Applies a closure to each element of the matrix, returning a new matrix with the results.
§Examples
use matreex::matrix;
let matrix_i32 = matrix![[0, 1, 2], [3, 4, 5]];
let matrix_f64 = matrix_i32.map(|x| x as f64);
assert_eq!(matrix_f64, matrix![[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]);
Sourcepub fn map_ref<U, F>(&self, f: F) -> Matrix<U>
pub fn map_ref<U, F>(&self, f: F) -> Matrix<U>
Applies a closure to each element of the matrix, returning a new matrix with the results.
This method is similar to map
but passes references to the
elements instead of taking ownership of them.
§Examples
use matreex::matrix;
let matrix_i32 = matrix![[0, 1, 2], [3, 4, 5]];
let matrix_f64 = matrix_i32.map_ref(|x| *x as f64);
assert_eq!(matrix_f64, matrix![[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]);
Sourcepub fn clear(&mut self) -> &mut Self
pub fn clear(&mut self) -> &mut Self
Clears the matrix, removing all elements.
Note that this method has no effect on the allocated capacity of the matrix.
§Examples
use matreex::matrix;
let mut matrix = matrix![[0, 1, 2], [3, 4, 5]];
matrix.clear();
assert!(matrix.is_empty());
assert_eq!(matrix.nrows(), 0);
assert_eq!(matrix.ncols(), 0);
Trait Implementations§
Source§impl<L, R> AddAssign<&Matrix<R>> for Matrix<L>
impl<L, R> AddAssign<&Matrix<R>> for Matrix<L>
Source§fn add_assign(&mut self, rhs: &Matrix<R>)
fn add_assign(&mut self, rhs: &Matrix<R>)
+=
operation. Read moreSource§impl AddAssign<&f32> for Matrix<f32>
impl AddAssign<&f32> for Matrix<f32>
Source§fn add_assign(&mut self, rhs: &f32)
fn add_assign(&mut self, rhs: &f32)
+=
operation. Read moreSource§impl AddAssign<&f64> for Matrix<f64>
impl AddAssign<&f64> for Matrix<f64>
Source§fn add_assign(&mut self, rhs: &f64)
fn add_assign(&mut self, rhs: &f64)
+=
operation. Read moreSource§impl AddAssign<&i128> for Matrix<i128>
impl AddAssign<&i128> for Matrix<i128>
Source§fn add_assign(&mut self, rhs: &i128)
fn add_assign(&mut self, rhs: &i128)
+=
operation. Read moreSource§impl AddAssign<&i16> for Matrix<i16>
impl AddAssign<&i16> for Matrix<i16>
Source§fn add_assign(&mut self, rhs: &i16)
fn add_assign(&mut self, rhs: &i16)
+=
operation. Read moreSource§impl AddAssign<&i32> for Matrix<i32>
impl AddAssign<&i32> for Matrix<i32>
Source§fn add_assign(&mut self, rhs: &i32)
fn add_assign(&mut self, rhs: &i32)
+=
operation. Read moreSource§impl AddAssign<&i64> for Matrix<i64>
impl AddAssign<&i64> for Matrix<i64>
Source§fn add_assign(&mut self, rhs: &i64)
fn add_assign(&mut self, rhs: &i64)
+=
operation. Read moreSource§impl AddAssign<&i8> for Matrix<i8>
impl AddAssign<&i8> for Matrix<i8>
Source§fn add_assign(&mut self, rhs: &i8)
fn add_assign(&mut self, rhs: &i8)
+=
operation. Read moreSource§impl AddAssign<&isize> for Matrix<isize>
impl AddAssign<&isize> for Matrix<isize>
Source§fn add_assign(&mut self, rhs: &isize)
fn add_assign(&mut self, rhs: &isize)
+=
operation. Read moreSource§impl AddAssign<&u128> for Matrix<u128>
impl AddAssign<&u128> for Matrix<u128>
Source§fn add_assign(&mut self, rhs: &u128)
fn add_assign(&mut self, rhs: &u128)
+=
operation. Read moreSource§impl AddAssign<&u16> for Matrix<u16>
impl AddAssign<&u16> for Matrix<u16>
Source§fn add_assign(&mut self, rhs: &u16)
fn add_assign(&mut self, rhs: &u16)
+=
operation. Read moreSource§impl AddAssign<&u32> for Matrix<u32>
impl AddAssign<&u32> for Matrix<u32>
Source§fn add_assign(&mut self, rhs: &u32)
fn add_assign(&mut self, rhs: &u32)
+=
operation. Read moreSource§impl AddAssign<&u64> for Matrix<u64>
impl AddAssign<&u64> for Matrix<u64>
Source§fn add_assign(&mut self, rhs: &u64)
fn add_assign(&mut self, rhs: &u64)
+=
operation. Read moreSource§impl AddAssign<&u8> for Matrix<u8>
impl AddAssign<&u8> for Matrix<u8>
Source§fn add_assign(&mut self, rhs: &u8)
fn add_assign(&mut self, rhs: &u8)
+=
operation. Read moreSource§impl AddAssign<&usize> for Matrix<usize>
impl AddAssign<&usize> for Matrix<usize>
Source§fn add_assign(&mut self, rhs: &usize)
fn add_assign(&mut self, rhs: &usize)
+=
operation. Read moreSource§impl<L, R> AddAssign<Matrix<R>> for Matrix<L>
impl<L, R> AddAssign<Matrix<R>> for Matrix<L>
Source§fn add_assign(&mut self, rhs: Matrix<R>)
fn add_assign(&mut self, rhs: Matrix<R>)
+=
operation. Read moreSource§impl AddAssign<f32> for Matrix<f32>
impl AddAssign<f32> for Matrix<f32>
Source§fn add_assign(&mut self, rhs: f32)
fn add_assign(&mut self, rhs: f32)
+=
operation. Read moreSource§impl AddAssign<f64> for Matrix<f64>
impl AddAssign<f64> for Matrix<f64>
Source§fn add_assign(&mut self, rhs: f64)
fn add_assign(&mut self, rhs: f64)
+=
operation. Read more