pub struct Matrix { /* private fields */ }Expand description
A dense matrix stored in row-major order.
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn zeros(rows: usize, cols: usize) -> Self
pub fn zeros(rows: usize, cols: usize) -> Self
Creates a new matrix with the given dimensions, initialized to zero.
Sourcepub fn from_rows(rows: Vec<Vec<Rational64>>) -> Self
pub fn from_rows(rows: Vec<Vec<Rational64>>) -> Self
Creates a new matrix from a Vec of row vectors.
Sourcepub fn from_vec(rows: usize, cols: usize, data: Vec<Rational64>) -> Self
pub fn from_vec(rows: usize, cols: usize, data: Vec<Rational64>) -> Self
Creates a new matrix from a flat array in row-major order.
Sourcepub fn get(&self, row: usize, col: usize) -> Rational64
pub fn get(&self, row: usize, col: usize) -> Rational64
Gets the element at position (row, col).
Sourcepub fn set(&mut self, row: usize, col: usize, value: Rational64)
pub fn set(&mut self, row: usize, col: usize, value: Rational64)
Sets the element at position (row, col).
Sourcepub fn row(&self, row: usize) -> &[Rational64] ⓘ
pub fn row(&self, row: usize) -> &[Rational64] ⓘ
Gets a reference to a row.
Sourcepub fn add_row_multiple(&mut self, i: usize, j: usize, scalar: Rational64)
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
Sourcepub fn scale_row(&mut self, row: usize, scalar: Rational64)
pub fn scale_row(&mut self, row: usize, scalar: Rational64)
Multiplies a row by a scalar.
Sourcepub fn mul_vec(&self, v: &[Rational64]) -> Vec<Rational64> ⓘ
pub fn mul_vec(&self, v: &[Rational64]) -> Vec<Rational64> ⓘ
Matrix-vector multiplication: returns A * v
Sourcepub fn gaussian_elimination(&self) -> (Matrix, usize)
pub fn gaussian_elimination(&self) -> (Matrix, usize)
Performs Gaussian elimination with partial pivoting. Returns the row-echelon form and the rank of the matrix.
Sourcepub fn lu_decomposition(&self) -> Option<(Matrix, Matrix, Vec<usize>)>
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.
Sourcepub fn solve(&self, b: &[Rational64]) -> Option<Vec<Rational64>>
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.
Sourcepub fn determinant(&self) -> Option<Rational64>
pub fn determinant(&self) -> Option<Rational64>
Computes the determinant using LU decomposition.
Sourcepub fn qr_decomposition(&self) -> Option<(Matrix, Matrix)>
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.
Sourcepub fn cholesky_decomposition(&self) -> Option<Matrix>
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.
Sourcepub fn inverse(&self) -> Option<Matrix>
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.
Sourcepub fn is_identity(&self) -> bool
pub fn is_identity(&self) -> bool
Checks if this matrix is the identity matrix.