Struct Matrix

Source
pub struct Matrix<T: Clone> { /* private fields */ }
Expand description

A dynamically sized matrix with entries of a type T

Data is stored column-wise, so adjacent elements of the flatmap vector are in the same column (modulo column breaks)

If T is a Ring, many more useful operations open up. When T is an abstract data type, this is pretty much only useful for storage.

Implementations§

Source§

impl<T: Clone> Matrix<T>

Source

pub fn from_flatmap(rows: usize, cols: usize, flatmap: Vec<T>) -> Matrix<T>

Constructs a matrix from a flat vector

Source

pub fn from_index_def( rows: usize, cols: usize, at_index: &mut dyn FnMut(usize, usize) -> T, ) -> Matrix<T>

Constructs a matrix defined index-wise

Source

pub fn is_square(&self) -> bool

Returns whether or not this is a square matrix

Source

pub fn row_count(&self) -> usize

The amount of rows in this matrix

Source

pub fn col_count(&self) -> usize

The amount of columns in this matrix

Source

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

Returns a copy of the underlying vector of this matrix

Source

pub fn columns(&self) -> Vec<Matrix<T>>

Returns the columns of this matrix

Source

pub fn is_vector(&self) -> bool

Returns true if this matrix is really just a column vector

Source

pub fn is_row_vector(&self) -> bool

Returns whether or not this is a row vector

Source

pub fn get(&self, r: usize, c: usize) -> T

Returns a copy of the entry at row r and column c

Source

pub fn set(&mut self, r: usize, c: usize, x: T)

Sets the entry at row r and column c to x

Source

pub fn append_col_ref(&mut self, col: &mut Vec<T>)

Appends a reference to a column to this matrix on the right

Source

pub fn append_mat_right_ref(&mut self, mat: &mut Matrix<T>)

Appends a reference to a matrix to this matrix on the right

Source

pub fn append_col(&mut self, col: Vec<T>)

Appends a column to this matrix on the right

Source

pub fn append_mat_right(&mut self, mat: Matrix<T>)

Appends a matrix to this matrix, on the right

Source

pub fn append_row(&mut self, row: Vec<T>)

Appends a row to this matrix on the bottom

Source

pub fn append_mat_bottom(&mut self, mat: Matrix<T>)

Appends a matrix to this matrix, on the bottom

Source

pub fn applying_to_all<J: Ring>(&self, f: &dyn Fn(T) -> J) -> Matrix<J>

Applies a function to all entries in this matrix, returning the result as a separate matrix

Source

pub fn apply_to_all(&mut self, f: &dyn Fn(T) -> T)

Applies a function to all entries in this matrix, in place

Source

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

The transpose of this matrix

Source

pub fn get_submatrix( &self, row_range: Range<usize>, col_range: Range<usize>, ) -> Matrix<T>

Accesses a sub-matrix of this matrix

Source

pub fn set_submatrix( &mut self, row_range: Range<usize>, col_range: Range<usize>, submat: Matrix<T>, )

Writes to a sub-matrix of this matrix

Source§

impl<R: Ring> Matrix<R>

Source

pub fn from_cols(columns: Vec<Matrix<R>>) -> Matrix<R>

Constructs a matrix with the given columns

Source

pub fn new(rows: usize, cols: usize) -> Matrix<R>

Constructs a matrix of all zeroes for a given dimension

Source

pub fn ones(rows: usize, cols: usize) -> Matrix<R>

Constructs a matrix of all ones

Source

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

Returns the diagonal of this matrix as a list

Source

pub fn get_upperdiagonal(&self) -> Vec<R>

Returns the upper diagonal of this matrix as a list

Source

pub fn inner_product(&self, other: &Matrix<R>) -> R

Computes the inner-product of this vector with another vector

This operates in the entries in the flatmap of each matrix, so if the argument to this function are not proper vectors (i.e., they are matrices with both dimensions greater than 1) then the behavior here is not well-defined

Source

pub fn l2_norm_squared(&self) -> R

The squared L2 norm of this vector

Source

pub fn hadamard(&self, other: Matrix<R>) -> Matrix<R>

Computes the point-wise product of this and another matrix of the same dimension

Source

pub fn identity(rows: usize, cols: usize) -> Matrix<R>

Constructs the rows * cols identity matrix

Source

pub fn from_diagonal(diagonal: Vec<R>) -> Matrix<R>

Creates a diagonal matrix from a given diagonal

Source

pub fn from_block_diagonal(blocks: Vec<Matrix<R>>) -> Matrix<R>

Creates a block-diagonal matrix from square blocks

Source

pub fn from_bidiagonal(diagonal: Vec<R>, superdiagonal: Vec<R>) -> Matrix<R>

Creates an upper bidiagonal matrix from a diagonal and superdiagonal

Source

pub fn is_identity(&self) -> bool

Returns true if this matrix is the identity matrix

Source§

impl<F: Field> Matrix<F>

Source

pub fn is_orthogonal_to(&self, other: Matrix<F>) -> bool

Returns whether or not two vectors are orthogonal

Source

pub fn is_orthogonal(&self) -> bool

Returns whether or not a matrix is orthogonal (meaning its columns are each orthogonal and of unit length)

Source

pub fn proj_onto(&self, other: Matrix<F>) -> Matrix<F>

Projects this vector onto another vector

Source

pub fn gram_schmidt(&self) -> Matrix<F>

Performs Gram-Schmidt Orthogonalization on a matrix, returning a matrix whose columns are orthogonal, and span the same column space as the original matrix. This does NOT normalize the GS vectors.

Source§

impl Matrix<f64>

Source

pub fn random_normal( rows: usize, cols: usize, mean: f64, variance: f64, ) -> Matrix<f64>

Constructs a random matrix with gaussianly distributed entries

Source

pub fn normalize(&mut self)

Normalizes this vector

Source

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

Returns the normalized version of this vector

Source

pub fn angle(&self) -> f64

Returns the angle of this 2D vector with the x-axis

Trait Implementations§

Source§

impl<R: Ring> Add for Matrix<R>

Source§

type Output = Matrix<R>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<R: Ring> AddAssign for Matrix<R>

Source§

fn add_assign(&mut self, rhs: Matrix<R>)

Performs the += operation. Read more
Source§

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

Source§

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

Returns a copy 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 + Clone> Debug for Matrix<T>

Source§

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

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

impl<F: Field> Div<F> for Matrix<F>

Source§

type Output = Matrix<F>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: F) -> Matrix<F>

Performs the / operation. Read more
Source§

impl<F: Field> DivAssign<F> for Matrix<F>

Source§

fn div_assign(&mut self, rhs: F)

Performs the /= operation. Read more
Source§

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

Source§

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

Returns the column index as a slice

Source§

type Output = [T]

The returned type after indexing.
Source§

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

Source§

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

Returns a mutable reference to the column index

Source§

impl<R: Ring> Mul<R> for Matrix<R>

Source§

type Output = Matrix<R>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: R) -> Self::Output

Performs the * operation. Read more
Source§

impl<R: Ring> Mul for Matrix<R>

Source§

type Output = Matrix<R>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Matrix<R>) -> Self::Output

Performs the * operation. Read more
Source§

impl<R: Ring> MulAssign<R> for Matrix<R>

Source§

fn mul_assign(&mut self, rhs: R)

Performs the *= operation. Read more
Source§

impl<R: Ring> MulAssign for Matrix<R>

Source§

fn mul_assign(&mut self, rhs: Matrix<R>)

Performs in-place multiplication, only valid on square matrices

Source§

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

Source§

fn eq(&self, other: &Self) -> 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<R: Ring> Sub for Matrix<R>

Source§

type Output = Matrix<R>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<R: Ring> SubAssign for Matrix<R>

Source§

fn sub_assign(&mut self, rhs: Matrix<R>)

Performs the -= operation. Read more

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