pub struct NmlMatrix<T> {
pub num_rows: u32,
pub num_cols: u32,
pub data: Box<[T]>,
pub is_square: bool,
}
Expand description
Nml_Matrix represents a matrix with a given number of rows and columns, the Data is stored in a one dimensional array using row-major-ordering (data[i][j] = data_new[i * m +j], where m is the number of columns) The Library contains a few methods to create matrices with or without data.
Fields§
§num_rows: u32
§num_cols: u32
§data: Box<[T]>
§is_square: bool
Implementations§
Source§impl<T> NmlMatrix<T>where
T: Num + Copy + Default + Signed + PartialOrd + MulAssign + AddAssign + Display + SampleUniform,
impl<T> NmlMatrix<T>where
T: Num + Copy + Default + Signed + PartialOrd + MulAssign + AddAssign + Display + SampleUniform,
Sourcepub fn new(num_rows: u32, num_cols: u32) -> NmlMatrix<T>
pub fn new(num_rows: u32, num_cols: u32) -> NmlMatrix<T>
creates a matrix without data and reserves the capacity for the Data Vector
Sourcepub fn new_with_2d_vec(
num_rows: u32,
num_cols: u32,
data_2d: &mut Vec<Vec<T>>,
) -> Result<Self, NmlError>
pub fn new_with_2d_vec( num_rows: u32, num_cols: u32, data_2d: &mut Vec<Vec<T>>, ) -> Result<Self, NmlError>
use a 2d Vector to initialize the matrix. Each Vector in the 2d Vector is a row and the length of these vectors are the columns
Sourcepub fn new_with_data(
num_rows: u32,
num_cols: u32,
data: Box<[T]>,
) -> Result<Self, NmlError>
pub fn new_with_data( num_rows: u32, num_cols: u32, data: Box<[T]>, ) -> Result<Self, NmlError>
Constructor that uses a vector to initialize the matrix. checks if the entered rows and columns fit the vector size
Sourcepub fn nml_mat_rnd(num_rows: u32, num_cols: u32, minimum: T, maximum: T) -> Self
pub fn nml_mat_rnd(num_rows: u32, num_cols: u32, minimum: T, maximum: T) -> Self
Returns a matrix with defined size and random data between minimum and maximum values
Sourcepub fn nml_mat_sqr(size: u32) -> Self
pub fn nml_mat_sqr(size: u32) -> Self
Creates a square matrix of a given size, where all cells are filled with 0.0
Sourcepub fn nml_mat_eye(size: u32) -> Self
pub fn nml_mat_eye(size: u32) -> Self
creates a identity matrix with the given size
Sourcepub fn nml_mat_cp(matrix: &NmlMatrix<T>) -> Self
pub fn nml_mat_cp(matrix: &NmlMatrix<T>) -> Self
Creates a new matrix which is a copy of a given matrix. Uses only the reference to the matrix, so that the original matrix is not moved
Sourcepub fn equality(&self, matrix: &NmlMatrix<T>) -> bool
pub fn equality(&self, matrix: &NmlMatrix<T>) -> bool
Checks if two matrices are equal by checking their dimensions and after that every cell. Method is also used for implementation of trait PatialEq
Sourcepub fn equality_in_tolerance(&self, matrix: NmlMatrix<T>, tolerance: T) -> bool
pub fn equality_in_tolerance(&self, matrix: NmlMatrix<T>, tolerance: T) -> bool
Checks if two matrices are equal with a given tolerance
Sourcepub fn at(&self, row: u32, col: u32) -> Result<T, NmlError>
pub fn at(&self, row: u32, col: u32) -> Result<T, NmlError>
Returns the value a specified at A[i,j]
Sourcepub fn get_column(&self, column: u32) -> Result<Self, NmlError>
pub fn get_column(&self, column: u32) -> Result<Self, NmlError>
Returns a result with a specified Column of a matrix, which in itself also is a matrix. If the specified matrix is not in the matrix the result will contain an error
Sourcepub fn get_row(&self, row: u32) -> Result<Self, NmlError>
pub fn get_row(&self, row: u32) -> Result<Self, NmlError>
Returns a result which either contains a row of a matrix (which is also a matrix) or a error
Sourcepub fn set_value(&mut self, row: u32, col: u32, data: T) -> Result<(), NmlError>
pub fn set_value(&mut self, row: u32, col: u32, data: T) -> Result<(), NmlError>
Method sets the value of a given cell in the matrix through a mutable reference
Sourcepub fn set_all_values(&mut self, value: T)
pub fn set_all_values(&mut self, value: T)
Method sets the values of all cells ro a given value
Sourcepub fn set_dig_values(&mut self, value: T) -> Result<(), NmlError>
pub fn set_dig_values(&mut self, value: T) -> Result<(), NmlError>
checks if the matrix is square and sets the diagonal values to a given value
Sourcepub fn multiply_row_scalar(
&mut self,
row: u32,
scalar: T,
) -> Result<(), NmlError>
pub fn multiply_row_scalar( &mut self, row: u32, scalar: T, ) -> Result<(), NmlError>
multiplies a given row with a given scalar in place. If the row-index is not in the matrix the returned Result will contain an error
Sourcepub fn multiply_col_scalar(
&mut self,
col: u32,
scalar: T,
) -> Result<(), NmlError>
pub fn multiply_col_scalar( &mut self, col: u32, scalar: T, ) -> Result<(), NmlError>
multiplies a given column with a given scalar in place. If the row-index is not in the matrix the returned Result will contain an error
Sourcepub fn multiply_matrix_scalar(&mut self, scalar: T)
pub fn multiply_matrix_scalar(&mut self, scalar: T)
multiplies the matrix in place with a given scalar
Sourcepub fn add_rows(
&mut self,
row_1: u32,
scalar_1: T,
row_2: u32,
scalar_2: T,
) -> Result<(), NmlError>
pub fn add_rows( &mut self, row_1: u32, scalar_1: T, row_2: u32, scalar_2: T, ) -> Result<(), NmlError>
row_1 is multiplied with scalar_1, this is analog for 2. row_1 will be modified with the solution (row_1 = row_1 * scalar + row_2 * scalar_2)
Sourcepub fn swap_rows(&mut self, row_1: u32, row_2: u32) -> Result<(), NmlError>
pub fn swap_rows(&mut self, row_1: u32, row_2: u32) -> Result<(), NmlError>
Method that swaps two given rows of a matrix object in place. Returns either nothing or an NmlError if the specified rows are not in the matrix
Sourcepub fn swap_columns(&mut self, col_1: u32, col_2: u32) -> Result<(), NmlError>
pub fn swap_columns(&mut self, col_1: u32, col_2: u32) -> Result<(), NmlError>
Method that swaps two given rows of a matrix object in place. Returns either nothing or an NmlError if the specified rows are not in the matrix
Sourcepub fn remove_column(&self, col: u32) -> Result<Self, NmlError>
pub fn remove_column(&self, col: u32) -> Result<Self, NmlError>
Tries to remove a column of a matrix and returns the rest of the matrix as a now on. Does not move the original matrix. If the column is not in the original matrix the result will return an error
Sourcepub fn remove_row(&self, row: u32) -> Result<Self, NmlError>
pub fn remove_row(&self, row: u32) -> Result<Self, NmlError>
Tries to remove a column of a matrix and returns the rest of the matrix as a now on. Does not move the original matrix. If the column is not in the original matrix the result will return an error
pub fn get_sub_mtr( &self, row_start: u32, row_end: u32, col_start: u32, col_end: u32, ) -> Result<Self, NmlError>
Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Computes the transpose matrix b’ of a matrix b. This is achieved by going from row-major-ordering to a more efficient storage. The input matrix will not be modified or moved.
Sourcepub fn mul_transpose(&self, other: &Self) -> Result<Self, NmlError>
pub fn mul_transpose(&self, other: &Self) -> Result<Self, NmlError>
The naive matrix multiplication algorihm applied with the transponse of the one matrix
Sourcepub fn mul_naive(&self, other: &Self) -> Result<Self, NmlError>
pub fn mul_naive(&self, other: &Self) -> Result<Self, NmlError>
The naive matrix multiplication algorithm. It iterates trough all values of both matrices. These matrices are not moved or modified