Matrix

Struct Matrix 

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

Source

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

Source

pub fn get_data_mut(&mut self) -> &mut Vec<T>

Source

pub fn get_shape(&self) -> Vec<usize>

§Get Shape

Returns a Vec<usize> representing the shape of the Matrix

Source

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!

Source

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

Source

pub fn get_row(&self, row: usize) -> impl Iterator<Item = &T>

§Get row

Returns a reference to a given row of the Matrix as an Iterator

Source

pub fn get_row_mut(&mut self, row: usize) -> impl Iterator<Item = &mut T>

§Get row mut

Returns a mutable reference to a given row of the Matrix as an Iterator

Source

pub fn get_col(&self, col: usize) -> impl Iterator<Item = &T>

§Get column

Returns a reference to a given column of the Matrix as an Iterator

Source

pub fn get_col_mut(&mut self, col: usize) -> impl Iterator<Item = &mut T>

§Get column mutable

Returns a mutable reference to a given column of the Matrix as an Iterator

Source

pub fn get_rows(&self) -> usize

§Get rows

A simple getter for the number of rows in the Matrix

Source

pub fn get_cols(&self) -> usize

§Get cols

A simple getter for the number of columns in the Matrix

Source

pub fn map(&self, f: fn(T) -> T) -> Matrix<T>

§Map

Returns a copy of the matrix with a function f applied to it

Source

pub fn apply(&mut self, f: fn(T) -> T)

§Apply

Mutates the matrix by applying a function F to each element

Source

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

Source

pub fn repeat_h(&self, times: usize) -> Matrix<T>

Source§

impl Matrix<f64>

Source

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

pub fn from_array<const R: usize, const C: usize>( arr: [[f64; C]; R], ) -> Matrix<f64>

§From array

Creates a new Matrix from a 2D array of any shape

§WARNING: Uses static dispatch, this is mostly used for a bunch of constants and for

testing, to not push the limits of it or you will end up with a hige executable

Source

pub fn zero(rows: usize, cols: usize) -> Matrix<f64>

§Zero

Creates a new Matrix of the given shape and fills it with ZEROS

Source

pub fn one(rows: usize, cols: usize) -> Matrix<f64>

§One

Creates a new Matrix of the given shape and fills it with ONES Useful for initializing matrices with dummy values that do not give 0 as a multiplication result

Source

pub fn fill(&mut self, value: f64)

§Fill

Mutates the matrix by filling it with the given value

Source

pub fn dot(&self, other: &Matrix<f64>) -> Result<Matrix<f64>, MatrixError>

§Dot

Returns the result of a Matrix multiplication operation -> Dot product

Source

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

§Transpose

Returns a copy of the transposed matrix

Source§

impl Matrix<Var>

Source

pub fn g_rand(tape: &GradTape, rows: usize, cols: usize) -> Matrix<Var>

§Random

Creates a new Matrix of the given shape and fills it with random values

Source

pub fn apply_to_value(&mut self, f: fn(x: Var) -> f64)

Source

pub fn g_from_array<const R: usize, const C: usize>( tape: &GradTape, arr: [[f64; C]; R], ) -> Matrix<Var>

Source

pub fn g_zero(tape: &GradTape, rows: usize, cols: usize) -> Matrix<Var>

§G Zero

Creates a new Matrix of the given shape and fills it with ZERO Vars

Source

pub fn g_one(tape: &GradTape, rows: usize, cols: usize) -> Matrix<Var>

§G One

Creates a new Matrix of the given shape and fills it with ONE Vars

Source

pub fn g_fill(&mut self, tape: &GradTape, value: f64)

§G Fill

Mutates the matrix by filling it with the given value

Source

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

Source

pub fn transpose(&self, tape: &GradTape) -> Matrix<Var>

§Transpose

Returns a copy of the transposed matrix

Source

pub fn value(&self) -> Matrix<f64>

§Value

Returns a copy of the matrix with all the variables replaced with their values

Trait Implementations§

Source§

impl<T: Copy + Clone + Add<Output = T>> Add<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: &Matrix<T>) -> Matrix<T>

Performs the + operation. Read more
Source§

impl<T: Copy + Clone + Add<Output = T>> Add<&Matrix<T>> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: &Matrix<T>) -> Matrix<T>

Performs the + operation. Read more
Source§

impl<T: Copy + Clone + Add<U, Output = T>, U: Copy + Add<T>> Add<Matrix<U>> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Matrix<U>) -> Matrix<T>

Performs the + operation. Read more
Source§

impl<T: Copy + Clone + Add<f64, Output = T>> Add<f64> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> Matrix<T>

Performs the + operation. Read more
Source§

impl<T: Copy + Clone + Add<f64, Output = T>> Add<f64> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> Matrix<T>

Performs the + operation. Read more
Source§

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

Source§

fn clone(&self) -> Matrix<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Matrix<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Copy + Clone + Div<Output = T>> Div<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: &Matrix<T>) -> Matrix<T>

Performs the / operation. Read more
Source§

impl<T: Copy + Clone + Div<Output = T>> Div<&Matrix<T>> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: &Matrix<T>) -> Matrix<T>

Performs the / operation. Read more
Source§

impl<T: Copy + Clone + Div<f64, Output = T>> Div<f64> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> Matrix<T>

Performs the / operation. Read more
Source§

impl<T: Copy + Clone + Div<f64, Output = T>> Div<f64> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> Matrix<T>

Performs the / operation. Read more
Source§

impl<T: Copy + Clone + Div<Output = T>> Div for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: Matrix<T>) -> Matrix<T>

Performs the / operation. Read more
Source§

impl<T> Index<(usize, usize)> for Matrix<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<(usize, usize)> for Matrix<T>

Source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: Copy + Clone + Mul<Output = T>> Mul<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Matrix<T>) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: Copy + Clone + Mul<Output = T>> Mul<&Matrix<T>> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Matrix<T>) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: Copy + Clone + Mul<f64, Output = T>> Mul<f64> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: Copy + Clone + Mul<f64, Output = T>> Mul<f64> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: Copy + Clone + Mul<Output = T>> Mul for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Matrix<T>) -> Matrix<T>

Performs the * operation. Read more
Source§

impl<T: PartialEq> PartialEq for Matrix<T>

Source§

fn eq(&self, other: &Matrix<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy + Clone + Add<Output = T>> Sub<&Matrix<T>> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: &Matrix<T>) -> Matrix<T>

Performs the - operation. Read more
Source§

impl<T: Copy + Clone + Sub<Output = T>> Sub<&Matrix<T>> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: &Matrix<T>) -> Matrix<T>

Performs the - operation. Read more
Source§

impl<T: Copy + Clone + Sub<f64, Output = T>> Sub<f64> for &Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> Matrix<T>

Performs the - operation. Read more
Source§

impl<T: Copy + Clone + Sub<f64, Output = T>> Sub<f64> for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> Matrix<T>

Performs the - operation. Read more
Source§

impl<T: Copy + Clone + Sub<Output = T>> Sub for Matrix<T>

Source§

type Output = Matrix<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T> StructuralPartialEq for Matrix<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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