Matrix

Struct Matrix 

Source
pub struct Matrix<T> {
    pub data: Vec<T>,
    pub row_size: usize,
    pub col_size: usize,
}
Expand description

MxN Matrix

Fields§

§data: Vec<T>§row_size: usize§col_size: usize

Implementations§

Source§

impl<T> Matrix<T>
where T: PartialOrd + Default + Copy + Mul<Output = T>,

Source

pub fn relu(&self) -> Matrix<T>
where T: PartialOrd + Default + Copy,

Apply the ReLU activation function onto a Matrix

Source

pub fn leaky_relu(&self, alpha: T) -> Matrix<T>
where T: PartialOrd + Default + Copy + Mul<Output = T>,

Apply the Leaky ReLU activation function onto a Matrix

Source

pub fn relu_backward(&self) -> Matrix<T>
where T: Copy + PartialOrd + Default + Add<T, Output = T> + Mul<T, Output = T> + From<u8>,

Apply backward pass for the ReLU activation function onto a Matrix

Source

pub fn gelu(&self) -> Matrix<T>
where T: Copy + PartialOrd + Default + From<f64> + Into<f64>,

Apply the GeLU activation function onto a Matrix NOTE: Smoother (near 0) than ReLU & potential for regularization effects.

Source§

impl<T: Default + Clone> Matrix<T>

Source

pub fn new(row_size: usize, col_size: usize) -> Self

Construct a new non-empty and sized Matrix

Source

pub fn new_random(row_size: usize, col_size: usize) -> Self
where T: Random,

Construct a new non-empty and sized Matrix with random values of T

Source

pub fn get(&self, row: usize, col: usize) -> Option<&T>

Try to get a reference to the value at a given row and column from the matrix

Source

pub fn get_diagonal(&self) -> Vec<T>

Get a vector of the diagonal elements of the matrix

Source

pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T>

Try to get a mutable reference to the value at a given row and column from the matrix

Source

pub fn set(&mut self, row: usize, column: usize, value: T) -> bool

Try to set a value at a given row and column in the matrix

Source

pub fn try_get_column(&self, column: usize) -> Option<Vec<T>>

Try to get all the values for a given column

NOTE: If you pass a column value larger than the number of columns this function will return None.

Source

pub fn try_get_row(&self, row: usize) -> Option<Vec<T>>

Try to get all the values for a given row

NOTE: If you pass a row value larger than the number of rows this function will return None.

Source

pub fn from_columns(cols: Vec<Vec<T>>) -> Matrix<T>

Create a Matrix from a columns (vec of vec)

Source

pub fn sub_matrix(&self, skip_row: usize, skip_col: usize) -> Matrix<T>

Create a sub matrix with a specific row and column to exclude

Source

pub fn transpose(&self) -> Matrix<T>

Perform a transpose operation (swap rows for columns and vice versa)

Source§

impl<T> Matrix<T>
where T: Default + Clone + Add<Output = T>,

Source

pub fn trace(&self) -> T

Perform the trace operation that computes the sum of all diagonal elements in the matrix.

NOTE: off-diagnonal elements do NOT contribute to the trace of the matrix, so 2 very different matrices can have the same trace.

Source

pub fn sum(&self) -> T

Perform a summation over the matrix.

Source§

impl<T> Matrix<T>
where T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone + Default,

Source

pub fn scalar_multiply(&self, scalar: T) -> Matrix<T>

Multiply a matrix by a single number (scalar) NOTE: The scalar type MUST match the matrix type.

Source

pub fn multiply(&self, multiplier: &Matrix<T>) -> Option<Matrix<T>>

Multiply Matrix with another Matrix using standard matrix multiplication NOTE: The matrices inner dimensions MUST match else returns None

Source

pub fn vector_multiply(&self, multiplier: &[T]) -> Option<Vec<T>>

Multiply the Matrix by a vector NOTE: The vectors length MUST match the vector columns, else returns None

Source

pub fn determinant(&self) -> Option<T>

Compute a unique determinant for a Matrix NOTE: Only computable for square (M x M) matrices. NOTE: The determinant is 0 for a Matrix with rank r < M (non-invertable).

Source§

impl<T> Matrix<T>
where T: Default + Clone + From<f64>,

Source

pub fn identity(size: usize) -> Matrix<T>

Create an identity matrix of given size

Source§

impl<T> Matrix<T>
where T: Default + Clone + Mul<Output = T> + Add<Output = T> + Into<f64>,

Source

pub fn frobenius_norm(&self) -> f64

Compute the frobenius norm of a Matrix

Source§

impl<T> Matrix<T>
where T: Copy + PartialOrd + Default + From<f64> + Into<f64> + Sub<Output = T> + Add<Output = T> + Mul<Output = T> + Div<Output = T> + Abs,

Source

pub fn inverse(&self) -> Option<Matrix<T>>

Compute the inverse of a Matrix NOTE: Only computable for square (M x M) matrices. NOTE: Only computable for a Matrix where r = M (full rank).

Trait Implementations§

Source§

impl<T> Add for Matrix<T>
where T: Add<Output = T> + Clone + Default + Debug,

Source§

fn add(self, rhs: Self) -> Matrix<T>

Matrix addition NOTE: the matrices you add MUST have the same dimensionality

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

impl<T: Default + Clone> Default for Matrix<T>

Source§

fn default() -> Self

Create a default Matrix instance

Source§

impl<T> Sub for Matrix<T>
where T: Sub<Output = T> + Clone + Default + Debug,

Source§

fn sub(self, rhs: Self) -> Matrix<T>

Subtract a matrix by another matrix NOTE: the matrix you subtract by MUST have the same dimensionality

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.

Auto Trait Implementations§

§

impl<T> Freeze for Matrix<T>

§

impl<T> RefUnwindSafe for Matrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for Matrix<T>
where T: Send,

§

impl<T> Sync for Matrix<T>
where T: Sync,

§

impl<T> Unpin for Matrix<T>
where T: Unpin,

§

impl<T> UnwindSafe for Matrix<T>
where T: UnwindSafe,

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.