pub struct Matrix {
pub data: Vec<f32>,
pub rows: usize,
pub cols: usize,
}Expand description
Matrix implementation
A mathematical data structure. Read more about matrices here: https://en.wikipedia.org/wiki/Matrix_(mathematics)
Fields§
§data: Vec<f32>§rows: usize§cols: usizeImplementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new(rows: usize, cols: usize) -> Self
pub fn new(rows: usize, cols: usize) -> Self
Create a new empty matrix
Uses rows and cols for defining the size of the matrix. All values within the matrix is 0.0. Returns an instance of Matrix.
Sourcepub fn identity(order: usize) -> Self
pub fn identity(order: usize) -> Self
Create a identity matrix from the given order
Creates an empty matrix from the given order. Sets the value 1.0 on the main diagonal of the matrix Read more about Identity Matrix: https://en.wikipedia.org/wiki/Identity_matrix
Sourcepub fn from_vec(cols: usize, data: Vec<f32>) -> Self
pub fn from_vec(cols: usize, data: Vec<f32>) -> Self
Create a new matrix based on given data
Given the amount of rows and columns and the vector of data, it creates a new instance of the matrix. Creates a matrix based on the given amount of columns. Will add any missing values as default value 0.0. Makes sure that the Matrix has completed rows.
pub fn with_rand_range( rows: usize, cols: usize, value_range: RangeInclusive<f32>, ) -> Self
pub fn with_rand_bin(rows: usize, cols: usize) -> Self
pub fn with_rand_0_to_10(rows: usize, cols: usize) -> Self
pub fn with_rand_neg10_to_10(rows: usize, cols: usize) -> Self
Sourcepub fn get(&self, row: usize, col: usize) -> Option<&f32>
pub fn get(&self, row: usize, col: usize) -> Option<&f32>
Get an item from the Matrix
Given the row and column of the item, retrieve a reference to the item. If there is not item, or if the row anc column given was to high, it returns None.
Sourcepub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut f32>
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut f32>
Get an item from the Matrix (as mutable reference)
Given the row and column of the item, retrieve a mutable reference to the item. If there is not item, or if the row anc column given was to high, it returns None.
Sourcepub fn get_row(&self, index: usize) -> Option<Vec<f32>>
pub fn get_row(&self, index: usize) -> Option<Vec<f32>>
Get a single row from the Matrix
Takes the index of the row, and returns a vector of all the values in the given row index. Returns None if the index is out of range.
Sourcepub fn get_row_mut(&mut self, index: usize) -> Option<&mut [f32]>
pub fn get_row_mut(&mut self, index: usize) -> Option<&mut [f32]>
Get a single row from the Matrix (as a mutable reference)
Takes the index of the row, and returns a vector of all the values in the given row index. Returns None if the index is out of range.
Sourcepub fn get_row_as_slice(&self, index: usize) -> Option<&[f32]>
pub fn get_row_as_slice(&self, index: usize) -> Option<&[f32]>
Get a single row from the Matrix (as slice)
Takes the index of the row and returns a slice from the Matrix. Returns None if the index is out of range.
Sourcepub fn get_col(&self, index: usize) -> Option<Vec<f32>>
pub fn get_col(&self, index: usize) -> Option<Vec<f32>>
Get a single col from the Matrix
Takes the index of the col, and returns a vector of all the values in the given col index. Returns None if the index is out of range.
Sourcepub fn get_col_mut(&mut self, index: usize) -> Option<Vec<&mut f32>>
pub fn get_col_mut(&mut self, index: usize) -> Option<Vec<&mut f32>>
Get a single col from the Matrix (as a mutable reference)
Takes the index of the col, and returns a vector of all the values in the given col index.
This code uses unsafe, and is therefor not recommended. Use get_mut if possible.
Returns None if the index is out of range.
Sourcepub fn get_col_as_slice(&self, index: usize) -> Option<Vec<&f32>>
pub fn get_col_as_slice(&self, index: usize) -> Option<Vec<&f32>>
Get a single row from the Matrix (as slice)
Takes the index of the row and returns a slice from the Matrix. Returns None if the index is out of range.
Sourcepub fn get_diagonal(&self) -> Option<Vec<f32>>
pub fn get_diagonal(&self) -> Option<Vec<f32>>
Get the numbers across the main diagonal if the rows == cols.
Returns a new vector of all the numbers across the diagonal Returns none if the amount of rows is not equal to the amount of columns
Sourcepub fn get_diagonal_as_slice(&self) -> Option<Vec<&f32>>
pub fn get_diagonal_as_slice(&self) -> Option<Vec<&f32>>
Get the numbers across the main diagonal if the rows == cols (as slice)
Returns a new vector of all the numbers across the diagonal Returns none if the amount of rows is not equal to the amount of columns
Sourcepub fn get_cross_diagonal(&self) -> Option<Vec<f32>>
pub fn get_cross_diagonal(&self) -> Option<Vec<f32>>
Get the numbers across the cross diagonal if the rows == cols.
The cross diagonal is the diagonal from top right corner to the bottom left corner of the matrix. Not to be mistaken with the main diagonal. Returns a new vector of all the numbers across the diagonal Returns none if the amount of rows is not equal to the amount of columns
Sourcepub fn get_cross_diagonal_as_slice(&self) -> Option<Vec<&f32>>
pub fn get_cross_diagonal_as_slice(&self) -> Option<Vec<&f32>>
Get the numbers across the cross diagonal if the rows == cols (as a slice)
The cross diagonal is the diagonal from top right corner to the bottom left corner of the matrix. Not to be mistaken with the main diagonal. Returns a new vector of all the numbers across the diagonal Returns none if the amount of rows is not equal to the amount of columns
Sourcepub fn shape(&self) -> String
pub fn shape(&self) -> String
Get the shape of the Matrix.
Format of the string is “ROWSxCOLUMNS”. Created with the format macro.
Sourcepub fn reshape(&mut self, new_rows: usize, new_cols: usize)
pub fn reshape(&mut self, new_rows: usize, new_cols: usize)
Reshapes the matrix to the new shape based on the given new rows and columns
If the new matrix is bigger than the original, then the method adds default values: 0.0.
If the new matrix is smaller than the original, then the method removes the extra data at the end of the matrix.
Sourcepub fn submatrix(
&self,
rows: Range<usize>,
cols: Range<usize>,
) -> Result<Matrix, MatrixError>
pub fn submatrix( &self, rows: Range<usize>, cols: Range<usize>, ) -> Result<Matrix, MatrixError>
Get a sub mutable matrix of the given matrix
Ranges start from 0 and are not inclusive of the end value.
Returns a Result with either the submatrix (Matrix) or the matrix error (MatrixError)
Error that can occur is when the ranges are bigger than the shape of the Matrix
Sourcepub fn submatrix_as_slice(
&self,
rows: Range<usize>,
cols: Range<usize>,
) -> Result<Matrix, MatrixError>
pub fn submatrix_as_slice( &self, rows: Range<usize>, cols: Range<usize>, ) -> Result<Matrix, MatrixError>
Not Implemented
Sourcepub fn multiply(&self, mat: &Matrix) -> Result<Matrix, MatrixError>
pub fn multiply(&self, mat: &Matrix) -> Result<Matrix, MatrixError>
Multiply two matrices
Condition for multiplication of matrices:
- Given matrix
(mxn)and(qxp) - Columns
nmust equal rowsq
Returns Result based on if this condition is met
Sourcepub fn get_transposed(&self) -> Matrix
pub fn get_transposed(&self) -> Matrix
Not implemented
Sourcepub fn get_inverse(&self) -> Matrix
pub fn get_inverse(&self) -> Matrix
Not implemented
Sourcepub fn is_vector(&self) -> bool
pub fn is_vector(&self) -> bool
Check if Matrix can act as a vector
Returns true if it contains one row or one column
Sourcepub fn sub_f(&mut self, numb: f32)
pub fn sub_f(&mut self, numb: f32)
Subtracts all values in the matrix by a given number (f32)
Mutates the matrix and makes the change.
Sourcepub fn add_f(&mut self, numb: f32)
pub fn add_f(&mut self, numb: f32)
Add a number (f32) to all values in the matrix
Mutates the matrix and makes the change.
Sourcepub fn scale_f(&mut self, numb: f32)
pub fn scale_f(&mut self, numb: f32)
Scale all values in the matrix by a given scalar (f32)
Mutates the matrix and makes the change.
Sourcepub fn div_f(&mut self, numb: f32) -> Result<(), MatrixError>
pub fn div_f(&mut self, numb: f32) -> Result<(), MatrixError>
Scale all values in the matrix by a given scalar (f32)
Mutates the matrix and makes the change. Checks for division by 0. Returns a result based on this condition
Sourcepub fn mod_f(&mut self, numb: f32)
pub fn mod_f(&mut self, numb: f32)
Scale all values in the matrix by a given scalar (f32)
Mutates the matrix and makes the change.