pub struct DynMatrix<T> { /* private fields */ }Expand description
Dynamically-sized heap-allocated matrix.
Row-major Vec<T> storage, matching the layout of fixed-size Matrix.
Dimensions are set at runtime. Implements MatrixRef and MatrixMut,
so all generic linalg free functions work with DynMatrix out of the box.
§Examples
use numeris::DynMatrix;
let a = DynMatrix::from_slice(2, 2, &[1.0_f64, 2.0, 3.0, 4.0]);
assert_eq!(a[(0, 1)], 2.0);
assert_eq!(a.nrows(), 2);
assert_eq!(a.ncols(), 2);
let b = DynMatrix::eye(3, 0.0_f64);
assert_eq!(b[(0, 0)], 1.0);
assert_eq!(b[(0, 1)], 0.0);Implementations§
Source§impl<T: Scalar> DynMatrix<T>
impl<T: Scalar> DynMatrix<T>
Sourcepub fn block(&self, i: usize, j: usize, rows: usize, cols: usize) -> Self
pub fn block(&self, i: usize, j: usize, rows: usize, cols: usize) -> Self
Extract a sub-matrix of size rows x cols starting at (i, j).
Panics if the block extends beyond the matrix bounds.
use numeris::DynMatrix;
let m = DynMatrix::from_fn(3, 3, |i, j| (i * 3 + j) as f64);
let b = m.block(1, 1, 2, 2);
assert_eq!(b[(0, 0)], 4.0);
assert_eq!(b[(1, 1)], 8.0);Sourcepub fn set_block(&mut self, i: usize, j: usize, src: &DynMatrix<T>)
pub fn set_block(&mut self, i: usize, j: usize, src: &DynMatrix<T>)
Write a sub-matrix into self starting at position (i, j).
Panics if the block extends beyond the matrix bounds.
use numeris::DynMatrix;
let mut m = DynMatrix::zeros(3, 3, 0.0_f64);
let patch = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
m.set_block(1, 1, &patch);
assert_eq!(m[(1, 1)], 1.0);
assert_eq!(m[(2, 2)], 4.0);Sourcepub fn top_left(&self, rows: usize, cols: usize) -> Self
pub fn top_left(&self, rows: usize, cols: usize) -> Self
Extract the top-left corner of size rows x cols.
Sourcepub fn top_right(&self, rows: usize, cols: usize) -> Self
pub fn top_right(&self, rows: usize, cols: usize) -> Self
Extract the top-right corner of size rows x cols.
Sourcepub fn bottom_left(&self, rows: usize, cols: usize) -> Self
pub fn bottom_left(&self, rows: usize, cols: usize) -> Self
Extract the bottom-left corner of size rows x cols.
Sourcepub fn bottom_right(&self, rows: usize, cols: usize) -> Self
pub fn bottom_right(&self, rows: usize, cols: usize) -> Self
Extract the bottom-right corner of size rows x cols.
Sourcepub fn bottom_rows(&self, n: usize) -> Self
pub fn bottom_rows(&self, n: usize) -> Self
Extract the last n rows.
Sourcepub fn right_cols(&self, n: usize) -> Self
pub fn right_cols(&self, n: usize) -> Self
Extract the last n columns.
Source§impl<T: LinalgScalar> DynMatrix<T>
impl<T: LinalgScalar> DynMatrix<T>
Sourcepub fn lu(&self) -> Result<DynLu<T>, LinalgError>
pub fn lu(&self) -> Result<DynLu<T>, LinalgError>
LU decomposition with partial pivoting.
Sourcepub fn cholesky(&self) -> Result<DynCholesky<T>, LinalgError>
pub fn cholesky(&self) -> Result<DynCholesky<T>, LinalgError>
Cholesky decomposition (A = L * L^H).
Sourcepub fn qr(&self) -> Result<DynQr<T>, LinalgError>
pub fn qr(&self) -> Result<DynQr<T>, LinalgError>
QR decomposition using Householder reflections.
Sourcepub fn solve(&self, b: &DynVector<T>) -> Result<DynVector<T>, LinalgError>
pub fn solve(&self, b: &DynVector<T>) -> Result<DynVector<T>, LinalgError>
Solve Ax = b for x via LU decomposition.
use numeris::{DynMatrix, DynVector};
let a = DynMatrix::from_slice(2, 2, &[2.0_f64, 1.0, 5.0, 3.0]);
let b = DynVector::from_slice(&[4.0, 11.0]);
let x = a.solve(&b).unwrap();
assert!((x[0] - 1.0).abs() < 1e-12);
assert!((x[1] - 2.0).abs() < 1e-12);Sourcepub fn inverse(&self) -> Result<DynMatrix<T>, LinalgError>
pub fn inverse(&self) -> Result<DynMatrix<T>, LinalgError>
Matrix inverse via LU decomposition.
use numeris::DynMatrix;
let a = DynMatrix::from_slice(2, 2, &[4.0_f64, 7.0, 2.0, 6.0]);
let a_inv = a.inverse().unwrap();
let id = &a * &a_inv;
assert!((id[(0, 0)] - 1.0).abs() < 1e-12);
assert!((id[(0, 1)]).abs() < 1e-12);Source§impl<T: Scalar> DynMatrix<T>
impl<T: Scalar> DynMatrix<T>
Sourcepub fn frobenius_norm_squared(&self) -> T
pub fn frobenius_norm_squared(&self) -> T
Squared Frobenius norm (sum of all elements squared).
Source§impl<T: LinalgScalar> DynMatrix<T>
impl<T: LinalgScalar> DynMatrix<T>
Sourcepub fn frobenius_norm(&self) -> T::Real
pub fn frobenius_norm(&self) -> T::Real
Frobenius norm (square root of sum of squared moduli).
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0_f64, 2.0, 3.0, 4.0]);
assert!((m.frobenius_norm() - 30.0_f64.sqrt()).abs() < 1e-12);Source§impl<T: Scalar> DynMatrix<T>
impl<T: Scalar> DynMatrix<T>
Sourcepub fn element_mul(&self, rhs: &Self) -> Self
pub fn element_mul(&self, rhs: &Self) -> Self
Element-wise (Hadamard) product: c[i][j] = a[i][j] * b[i][j].
use numeris::DynMatrix;
let a = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let b = DynMatrix::from_slice(2, 2, &[5.0, 6.0, 7.0, 8.0]);
let c = a.element_mul(&b);
assert_eq!(c[(0, 0)], 5.0);
assert_eq!(c[(1, 1)], 32.0);Sourcepub fn element_div(&self, rhs: &Self) -> Self
pub fn element_div(&self, rhs: &Self) -> Self
Element-wise division: c[i][j] = a[i][j] / b[i][j].
use numeris::DynMatrix;
let a = DynMatrix::from_slice(2, 2, &[10.0, 12.0, 21.0, 32.0]);
let b = DynMatrix::from_slice(2, 2, &[5.0, 6.0, 7.0, 8.0]);
let c = a.element_div(&b);
assert_eq!(c[(0, 0)], 2.0);
assert_eq!(c[(1, 1)], 4.0);Source§impl<T> DynMatrix<T>
impl<T> DynMatrix<T>
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
View the entire matrix as a flat slice in row-major order.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(m.as_slice(), &[1.0, 2.0, 3.0, 4.0]);Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
View the entire matrix as a mutable flat slice.
Sourcepub fn row_slice(&self, i: usize) -> &[T]
pub fn row_slice(&self, i: usize) -> &[T]
View row i as a slice.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert_eq!(m.row_slice(0), &[1.0, 2.0, 3.0]);
assert_eq!(m.row_slice(1), &[4.0, 5.0, 6.0]);Sourcepub fn row_slice_mut(&mut self, i: usize) -> &mut [T]
pub fn row_slice_mut(&mut self, i: usize) -> &mut [T]
View row i as a mutable slice.
Source§impl<T: Scalar> DynMatrix<T>
impl<T: Scalar> DynMatrix<T>
Sourcepub fn trace(&self) -> T
pub fn trace(&self) -> T
Sum of diagonal elements.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(m.trace(), 5.0);Sourcepub fn diag(&self) -> DynVector<T>
pub fn diag(&self) -> DynVector<T>
Extract the diagonal as a DynVector.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let d = m.diag();
assert_eq!(d[0], 1.0);
assert_eq!(d[1], 4.0);Sourcepub fn from_diag(v: &DynVector<T>) -> Self
pub fn from_diag(v: &DynVector<T>) -> Self
Create a square diagonal matrix from a vector.
use numeris::{DynMatrix, DynVector};
let v = DynVector::from_slice(&[2.0, 3.0]);
let m = DynMatrix::from_diag(&v);
assert_eq!(m[(0, 0)], 2.0);
assert_eq!(m[(1, 1)], 3.0);
assert_eq!(m[(0, 1)], 0.0);Sourcepub fn pow(&self, n: u32) -> Self
pub fn pow(&self, n: u32) -> Self
Integer matrix power via repeated squaring.
pow(0) returns the identity matrix. Panics if not square.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0, 1.0, 0.0, 1.0]);
let m3 = m.pow(3);
assert_eq!(m3[(0, 1)], 3.0);Sourcepub fn is_symmetric(&self) -> bool
pub fn is_symmetric(&self) -> bool
Check if the matrix is symmetric (A == A^T).
use numeris::DynMatrix;
let sym = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 2.0, 3.0]);
assert!(sym.is_symmetric());Source§impl<T: LinalgScalar> DynMatrix<T>
impl<T: LinalgScalar> DynMatrix<T>
Source§impl<T> DynMatrix<T>
impl<T> DynMatrix<T>
Sourcepub fn map<U>(&self, f: impl Fn(T) -> U) -> DynMatrix<U>where
T: Copy,
pub fn map<U>(&self, f: impl Fn(T) -> U) -> DynMatrix<U>where
T: Copy,
Apply a function to every element, producing a new matrix.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0_f64, 4.0, 9.0, 16.0]);
let r = m.map(|x: f64| x.sqrt());
assert_eq!(r[(0, 0)], 1.0);
assert_eq!(r[(1, 1)], 4.0);Source§impl<T: FloatScalar> DynMatrix<T>
impl<T: FloatScalar> DynMatrix<T>
Sourcepub fn abs(&self) -> Self
pub fn abs(&self) -> Self
Element-wise absolute value.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0_f64, -2.0, -3.0, 4.0]);
let a = m.abs();
assert_eq!(a[(0, 1)], 2.0);
assert_eq!(a[(1, 0)], 3.0);Sourcepub fn element_max(&self, rhs: &Self) -> Self
pub fn element_max(&self, rhs: &Self) -> Self
Element-wise maximum: c[i][j] = max(a[i][j], b[i][j]).
use numeris::DynMatrix;
let a = DynMatrix::from_slice(2, 2, &[1.0_f64, 5.0, 3.0, 2.0]);
let b = DynMatrix::from_slice(2, 2, &[4.0, 2.0, 1.0, 6.0]);
let c = a.element_max(&b);
assert_eq!(c[(0, 0)], 4.0);
assert_eq!(c[(0, 1)], 5.0);Source§impl<T: Scalar> DynMatrix<T>
impl<T: Scalar> DynMatrix<T>
Sourcepub fn row(&self, i: usize) -> DynVector<T>
pub fn row(&self, i: usize) -> DynVector<T>
Extract row i as a DynVector.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let r = m.row(0);
assert_eq!(r[0], 1.0);
assert_eq!(r[1], 2.0);Source§impl<T: Scalar> DynMatrix<T>
impl<T: Scalar> DynMatrix<T>
Sourcepub fn zeros(nrows: usize, ncols: usize, _zero: T) -> Self
pub fn zeros(nrows: usize, ncols: usize, _zero: T) -> Self
Create an nrows x ncols matrix filled with value.
use numeris::DynMatrix;
let m = DynMatrix::zeros(2, 3, 0.0_f64);
assert_eq!(m.nrows(), 2);
assert_eq!(m.ncols(), 3);
assert_eq!(m[(1, 2)], 0.0);Sourcepub fn fill(nrows: usize, ncols: usize, value: T) -> Self
pub fn fill(nrows: usize, ncols: usize, value: T) -> Self
Create a matrix filled with a given value.
use numeris::DynMatrix;
let m = DynMatrix::fill(2, 3, 7.0_f64);
assert_eq!(m[(0, 0)], 7.0);
assert_eq!(m[(1, 2)], 7.0);Sourcepub fn eye(n: usize, _zero: T) -> Self
pub fn eye(n: usize, _zero: T) -> Self
Create an n x n identity matrix.
The _zero parameter is only used for type inference.
use numeris::DynMatrix;
let id = DynMatrix::eye(3, 0.0_f64);
assert_eq!(id[(0, 0)], 1.0);
assert_eq!(id[(0, 1)], 0.0);
assert_eq!(id[(2, 2)], 1.0);Sourcepub fn from_slice(nrows: usize, ncols: usize, slice: &[T]) -> Self
pub fn from_slice(nrows: usize, ncols: usize, slice: &[T]) -> Self
Create a matrix from a flat slice in row-major order.
Panics if slice.len() != nrows * ncols.
use numeris::DynMatrix;
let m = DynMatrix::from_slice(2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert_eq!(m[(0, 2)], 3.0);
assert_eq!(m[(1, 0)], 4.0);Sourcepub fn from_vec(nrows: usize, ncols: usize, data: Vec<T>) -> Self
pub fn from_vec(nrows: usize, ncols: usize, data: Vec<T>) -> Self
Create a matrix from an owned Vec<T> in row-major order.
Panics if data.len() != nrows * ncols.
use numeris::DynMatrix;
let m = DynMatrix::from_vec(2, 2, vec![1.0, 2.0, 3.0, 4.0]);
assert_eq!(m[(0, 0)], 1.0);
assert_eq!(m[(1, 1)], 4.0);Source§impl<T> DynMatrix<T>
impl<T> DynMatrix<T>
Sourcepub fn from_fn(
nrows: usize,
ncols: usize,
f: impl Fn(usize, usize) -> T,
) -> Self
pub fn from_fn( nrows: usize, ncols: usize, f: impl Fn(usize, usize) -> T, ) -> Self
Create a matrix by calling f(row, col) for each element.
use numeris::DynMatrix;
let m = DynMatrix::from_fn(3, 3, |i, j| if i == j { 1.0_f64 } else { 0.0 });
assert_eq!(m[(0, 0)], 1.0);
assert_eq!(m[(0, 1)], 0.0);Trait Implementations§
Source§impl<T: Scalar> AddAssign<&DynMatrix<T>> for DynMatrix<T>
impl<T: Scalar> AddAssign<&DynMatrix<T>> for DynMatrix<T>
Source§fn add_assign(&mut self, rhs: &DynMatrix<T>)
fn add_assign(&mut self, rhs: &DynMatrix<T>)
+= operation. Read moreSource§impl<T: Scalar> AddAssign for DynMatrix<T>
impl<T: Scalar> AddAssign for DynMatrix<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<T: Scalar> DivAssign<T> for DynMatrix<T>
impl<T: Scalar> DivAssign<T> for DynMatrix<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/= operation. Read moreSource§impl<'a, T> IntoIterator for &'a DynMatrix<T>
impl<'a, T> IntoIterator for &'a DynMatrix<T>
Source§impl<'a, T> IntoIterator for &'a mut DynMatrix<T>
impl<'a, T> IntoIterator for &'a mut DynMatrix<T>
Source§impl<T: Scalar> MulAssign<T> for DynMatrix<T>
impl<T: Scalar> MulAssign<T> for DynMatrix<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moreSource§impl<T: Scalar> SubAssign<&DynMatrix<T>> for DynMatrix<T>
impl<T: Scalar> SubAssign<&DynMatrix<T>> for DynMatrix<T>
Source§fn sub_assign(&mut self, rhs: &DynMatrix<T>)
fn sub_assign(&mut self, rhs: &DynMatrix<T>)
-= operation. Read moreSource§impl<T: Scalar> SubAssign for DynMatrix<T>
impl<T: Scalar> SubAssign for DynMatrix<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<T: Scalar, const M: usize, const N: usize> TryFrom<&DynMatrix<T>> for Matrix<T, M, N>
impl<T: Scalar, const M: usize, const N: usize> TryFrom<&DynMatrix<T>> for Matrix<T, M, N>
Source§fn try_from(d: &DynMatrix<T>) -> Result<Self, Self::Error>
fn try_from(d: &DynMatrix<T>) -> Result<Self, Self::Error>
Try to convert a DynMatrix into a fixed-size Matrix.
Fails if the runtime dimensions don’t match M x N.
use numeris::{Matrix, DynMatrix};
let d = DynMatrix::from_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let m: Matrix<f64, 2, 2> = (&d).try_into().unwrap();
assert_eq!(m[(0, 0)], 1.0);
assert_eq!(m[(1, 1)], 4.0);