Matrix

Struct Matrix 

Source
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: usize

Implementations§

Source§

impl Matrix

Source

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.

Source

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

Source

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.

Source

pub fn with_rand_range( rows: usize, cols: usize, value_range: RangeInclusive<f32>, ) -> Self

Source

pub fn with_rand_bin(rows: usize, cols: usize) -> Self

Source

pub fn with_rand_0_to_10(rows: usize, cols: usize) -> Self

Source

pub fn with_rand_neg10_to_10(rows: usize, cols: usize) -> Self

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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

Source

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

Source

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

Source

pub fn shape(&self) -> String

Get the shape of the Matrix.

Format of the string is “ROWSxCOLUMNS”. Created with the format macro.

Source

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.

Source

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

Source

pub fn submatrix_as_slice( &self, rows: Range<usize>, cols: Range<usize>, ) -> Result<Matrix, MatrixError>

Not Implemented

Source

pub fn multiply(&self, mat: &Matrix) -> Result<Matrix, MatrixError>

Multiply two matrices

Condition for multiplication of matrices:

  • Given matrix (mxn) and (qxp)
  • Columns n must equal rows q

Returns Result based on if this condition is met

Source

pub fn transpose(&mut self)

Not implemented

Source

pub fn get_transposed(&self) -> Matrix

Not implemented

Source

pub fn inverse(&mut self)

Not implemented

Source

pub fn get_inverse(&self) -> Matrix

Not implemented

Source

pub fn is_vector(&self) -> bool

Check if Matrix can act as a vector

Returns true if it contains one row or one column

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

pub fn is_orthogonal(&self) -> bool

Source

pub fn det_2x2()

Source

pub fn det_3x3()

Auto Trait Implementations§

§

impl Freeze for Matrix

§

impl RefUnwindSafe for Matrix

§

impl Send for Matrix

§

impl Sync for Matrix

§

impl Unpin for Matrix

§

impl UnwindSafe for Matrix

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V