Skip to main content

Matrix

Struct Matrix 

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

A dense matrix stored in row-major order.

Implementations§

Source§

impl Matrix

Source

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

Creates a new matrix with the given dimensions, initialized to zero.

Source

pub fn identity(size: usize) -> Self

Creates a new identity matrix of the given size.

Source

pub fn from_rows(rows: Vec<Vec<Rational64>>) -> Self

Creates a new matrix from a Vec of row vectors.

Source

pub fn from_vec(rows: usize, cols: usize, data: Vec<Rational64>) -> Self

Creates a new matrix from a flat array in row-major order.

Source

pub fn nrows(&self) -> usize

Returns the number of rows.

Source

pub fn ncols(&self) -> usize

Returns the number of columns.

Source

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

Gets the element at position (row, col).

Source

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

Sets the element at position (row, col).

Source

pub fn row(&self, row: usize) -> &[Rational64]

Gets a reference to a row.

Source

pub fn swap_rows(&mut self, i: usize, j: usize)

Swaps two rows.

Source

pub fn add_row_multiple(&mut self, i: usize, j: usize, scalar: Rational64)

Adds a scalar multiple of one row to another: row_i += scalar * row_j

Source

pub fn scale_row(&mut self, row: usize, scalar: Rational64)

Multiplies a row by a scalar.

Source

pub fn mul_vec(&self, v: &[Rational64]) -> Vec<Rational64>

Matrix-vector multiplication: returns A * v

Source

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

Matrix-matrix multiplication: returns A * B

Source

pub fn transpose(&self) -> Matrix

Transposes the matrix.

Source

pub fn gaussian_elimination(&self) -> (Matrix, usize)

Performs Gaussian elimination with partial pivoting. Returns the row-echelon form and the rank of the matrix.

Source

pub fn lu_decomposition(&self) -> Option<(Matrix, Matrix, Vec<usize>)>

Performs LU decomposition with partial pivoting. Returns (L, U, P) where P is a permutation vector.

Source

pub fn solve(&self, b: &[Rational64]) -> Option<Vec<Rational64>>

Solves a linear system Ax = b using Gaussian elimination with back substitution. Returns None if the system has no unique solution.

Source

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

Computes the determinant using LU decomposition.

Source

pub fn qr_decomposition(&self) -> Option<(Matrix, Matrix)>

Performs QR decomposition using the modified Gram-Schmidt process. Returns (Q, R) where Q has orthogonal columns and R is upper triangular such that QR = A.

§Note

Since we’re working with exact rational arithmetic and can’t compute square roots, Q will not be orthonormal in the traditional sense. Instead, Q contains unnormalized orthogonal vectors, and R is adjusted accordingly so that QR = A holds exactly.

Specifically: R[j,j] = 1 and the j-th column of Q has squared norm stored implicitly.

Source

pub fn cholesky_decomposition(&self) -> Option<Matrix>

Performs Cholesky decomposition for symmetric positive definite matrices. Returns L where A = L * L^T and L is lower triangular.

§Returns

None if the matrix is not square, symmetric, or positive definite.

§Note

This requires taking square roots, which may not be exact for rationals. The function will fail (return None) if any diagonal element during decomposition is not a perfect rational square.

Source

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

Computes the inverse of a square matrix using LU decomposition. Returns None if the matrix is singular (non-invertible).

§Algorithm

Uses LU decomposition with partial pivoting to solve AX = I, where I is the identity matrix. Each column of the inverse is computed by solving a system with the corresponding column of I.

Source

pub fn is_identity(&self) -> bool

Checks if this matrix is the identity matrix.

Trait Implementations§

Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Matrix

Source§

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

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

impl Display for Matrix

Source§

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

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

impl PartialEq for Matrix

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Matrix

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.