Skip to main content

Matrix

Struct Matrix 

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

A 2D matrix of floating-point values (row-major storage).

§Examples

use aprender::primitives::Matrix;

let m = Matrix::from_vec(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).expect("data length matches rows * cols");
assert_eq!(m.shape(), (2, 3));

Implementations§

Source§

impl<T> Matrix<T>
where T: Copy,

Source

pub fn from_vec( rows: usize, cols: usize, data: Vec<T>, ) -> Result<Matrix<T>, &'static str>

Creates a new matrix from a vector of data.

§Errors

Returns an error if data length doesn’t match rows * cols.

Source

pub fn shape(&self) -> (usize, usize)

Returns the shape as (rows, cols).

Source

pub fn n_rows(&self) -> usize

Returns the number of rows.

Source

pub fn n_cols(&self) -> usize

Returns the number of columns.

Source

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

Gets element at (row, col).

§Panics

Panics if indices are out of bounds.

Source

pub fn set(&mut self, row: usize, col: usize, value: T)

Sets element at (row, col).

§Panics

Panics if indices are out of bounds.

Source

pub fn row(&self, row_idx: usize) -> Vector<T>

Returns a row as a Vector.

Source

pub fn column(&self, col_idx: usize) -> Vector<T>

Returns a column as a Vector.

Source

pub fn as_slice(&self) -> &[T]

Returns the underlying data as a slice.

Source§

impl Matrix<f32>

Source

pub fn zeros(rows: usize, cols: usize) -> Matrix<f32>

Creates a matrix of zeros.

Source

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

Creates a matrix of ones.

Source

pub fn eye(n: usize) -> Matrix<f32>

Creates an identity matrix.

Source

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

Transposes the matrix.

Source

pub fn matmul(&self, other: &Matrix<f32>) -> Result<Matrix<f32>, &'static str>

Matrix-matrix multiplication.

§Errors

Returns an error if dimensions don’t match.

Source

pub fn matvec(&self, vec: &Vector<f32>) -> Result<Vector<f32>, &'static str>

Matrix-vector multiplication.

§Errors

Returns an error if dimensions don’t match.

Source

pub fn add(&self, other: &Matrix<f32>) -> Result<Matrix<f32>, &'static str>

Adds another matrix element-wise.

§Errors

Returns an error if dimensions don’t match.

Source

pub fn sub(&self, other: &Matrix<f32>) -> Result<Matrix<f32>, &'static str>

Subtracts another matrix element-wise.

§Errors

Returns an error if dimensions don’t match.

Source

pub fn mul_scalar(&self, scalar: f32) -> Matrix<f32>

Multiplies each element by a scalar.

Source

pub fn cholesky_solve( &self, b: &Vector<f32>, ) -> Result<Vector<f32>, &'static str>

Solves the linear system Ax = b using Cholesky decomposition.

The matrix must be symmetric positive definite.

§Errors

Returns an error if the matrix is not square or not positive definite.

Trait Implementations§

Source§

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

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 for Matrix<T>
where T: Debug,

Source§

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

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

impl<'de, T> Deserialize<'de> for Matrix<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Matrix<T>, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

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

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> Serialize for Matrix<T>
where T: Serialize,

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> UnsafeUnpin for Matrix<T>

§

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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

Source§

impl<T> WasmNotSendSync for T

Source§

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