pub struct Matrix<T> { /* private fields */ }
Expand description
§Struct Matrix
A classic implementation of the Matrix data structure
Includes
- The possibility to create a Matrix structure starting from various other structures
- The possibility to manipulate entire matrices as they were individual items
- Operator overloading
- Matrix math
- Safely reshaping and transposing matrices
- Trapping you in a simulation
§A demonstration
use quickbrain::quick_math::matrix::Matrix;
let m : Matrix<f64> = Matrix::from_array([[1., 2., 3.], [4., 5., 6.]]);
// [1, 2, 3]
// [4, 5, 6]
let m2 : Matrix<f64> = Matrix::from_array([[1., 2.], [3., 4.], [5., 6.]]);
// [1, 2]
// [3, 4]
// [5, 6]
// Multiplying matrices by a scalar
// Multiplying matrices by matrices
// Mapping a function to a matrix
let r = m.dot(&(m2 * 2.0).map(|x| x * x));
Implementations§
Source§impl<T: Copy> Matrix<T>
impl<T: Copy> Matrix<T>
Sourcepub fn get_data(&self) -> &Vec<T>
pub fn get_data(&self) -> &Vec<T>
§Get Data
Returns a reference to the plain vector holding the raw data of the matrix Likely not an useful method
pub fn get_data_mut(&mut self) -> &mut Vec<T>
Sourcepub fn get_shape(&self) -> Vec<usize>
pub fn get_shape(&self) -> Vec<usize>
§Get Shape
Returns a Vec<usize>
representing the shape of the Matrix
Sourcepub fn get_row_slice(&self, row: usize) -> &[T]
pub fn get_row_slice(&self, row: usize) -> &[T]
§Get row slice
Returns a row of the matrix as a slice, it’s very fast!
Sourcepub fn get_row_slice_mut(&mut self, row: usize) -> &[T]
pub fn get_row_slice_mut(&mut self, row: usize) -> &[T]
§Get row slice mut
Returns a mutable reference to a row of the matrix as a slice
Sourcepub fn get_row_mut(&mut self, row: usize) -> impl Iterator<Item = &mut T>
pub fn get_row_mut(&mut self, row: usize) -> impl Iterator<Item = &mut T>
Sourcepub fn get_col_mut(&mut self, col: usize) -> impl Iterator<Item = &mut T>
pub fn get_col_mut(&mut self, col: usize) -> impl Iterator<Item = &mut T>
Sourcepub fn get_cols(&self) -> usize
pub fn get_cols(&self) -> usize
§Get cols
A simple getter for the number of columns in the Matrix
Sourcepub fn map(&self, f: fn(T) -> T) -> Matrix<T>
pub fn map(&self, f: fn(T) -> T) -> Matrix<T>
§Map
Returns a copy of the matrix with a function f applied to it
Sourcepub fn apply(&mut self, f: fn(T) -> T)
pub fn apply(&mut self, f: fn(T) -> T)
§Apply
Mutates the matrix by applying a function F to each element
Sourcepub fn reshape(
&self,
new_rows: usize,
new_cols: usize,
) -> Result<Matrix<T>, MatrixError>
pub fn reshape( &self, new_rows: usize, new_cols: usize, ) -> Result<Matrix<T>, MatrixError>
§Reshape
Returns MatrixError::InvalidReshape if the data doesn’t fit the new size Returns the reshaped matrix otherwise
pub fn repeat_h(&self, times: usize) -> Matrix<T>
Source§impl Matrix<f64>
impl Matrix<f64>
Sourcepub fn rand(rows: usize, cols: usize) -> Matrix<f64>
pub fn rand(rows: usize, cols: usize) -> Matrix<f64>
§Rand
Creates a matrix filled with random values in the range [0, 1) of a given size
Source§impl Matrix<Var>
impl Matrix<Var>
pub fn apply_to_value(&mut self, f: fn(x: Var) -> f64)
pub fn g_from_array<const R: usize, const C: usize>( tape: &GradTape, arr: [[f64; C]; R], ) -> Matrix<Var>
Sourcepub fn g_fill(&mut self, tape: &GradTape, value: f64)
pub fn g_fill(&mut self, tape: &GradTape, value: f64)
§G Fill
Mutates the matrix by filling it with the given value
Sourcepub fn g_dot(
&self,
tape: &GradTape,
other: &Matrix<Var>,
) -> Result<Matrix<Var>, MatrixError>
pub fn g_dot( &self, tape: &GradTape, other: &Matrix<Var>, ) -> Result<Matrix<Var>, MatrixError>
§Dot
Returns the result of a Matrix multiplication operation -> Dot product