pub struct Matrix { /* private fields */ }
Expand description
A simple 2-dimensional matrix with basic operations
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new(rows: usize, cols: usize) -> Self
pub fn new(rows: usize, cols: usize) -> Self
Creates a new matrix with the given number of rows and columns, initialized to zero.
Sourcepub fn random(rows: usize, cols: usize) -> Self
pub fn random(rows: usize, cols: usize) -> Self
Creates a new matrix with the given number of rows and columns, initialized with random values between -1.0 and 1.0.
Sourcepub fn from_vec(data: Vec<Vec<f64>>) -> Self
pub fn from_vec(data: Vec<Vec<f64>>) -> Self
Creates a new matrix from a 2D vector. The outer vector represents the rows, and the inner vectors represent the columns. Panics if the inner vectors have different lengths.
Sourcepub fn from_col_vec(data: Vec<f64>) -> Self
pub fn from_col_vec(data: Vec<f64>) -> Self
Creates a new matrix from a column vector.
Sourcepub fn col(&self, index: usize) -> Vec<f64>
pub fn col(&self, index: usize) -> Vec<f64>
Returns the column at the given index as a vector. Panics if the index is out of bounds.
Sourcepub fn data_mut(&mut self) -> &mut Vec<Vec<f64>>
pub fn data_mut(&mut self) -> &mut Vec<Vec<f64>>
Returns a mutable reference to the data in the matrix.
Sourcepub fn get(&self, row: usize, col: usize) -> f64
pub fn get(&self, row: usize, col: usize) -> f64
Returns the value at the given row and column. Panics if the indices are out of bounds.
Sourcepub fn get_mut(&mut self, row: usize, col: usize) -> &mut f64
pub fn get_mut(&mut self, row: usize, col: usize) -> &mut f64
Returns a mutable reference to the value at the given row and column. Panics if the indices are out of bounds.
Sourcepub fn set(&mut self, row: usize, col: usize, value: f64)
pub fn set(&mut self, row: usize, col: usize, value: f64)
Sets the value at the given row and column. Panics if the indices are out of bounds.
Sourcepub fn map<F>(&self, f: F) -> Matrix
pub fn map<F>(&self, f: F) -> Matrix
Returns the matrix resulting from
applying the function f
to each element of the matrix.
Sourcepub fn map_mut<F>(&mut self, f: F)
pub fn map_mut<F>(&mut self, f: F)
Applies the function f
to each element of the matrix in place.
This is an in-place operation.
pub fn hadamar_product(&mut self, other: &Matrix)
Trait Implementations§
Source§impl AddAssign<&Matrix> for Matrix
impl AddAssign<&Matrix> for Matrix
Source§fn add_assign(&mut self, other: &Matrix)
fn add_assign(&mut self, other: &Matrix)
Adds another matrix to this matrix, component-wise. Panics if the matrices have different dimensions. This is an in-place operation.