Overview
Matrix decompositions factorize a matrix into products of simpler matrices, enabling efficient computation of inverses, solutions to linear systems, eigenvalues, and other properties.svd
Singular Value Decomposition (SVD). Factorizes matrix A into three matrices: A = U × Σ × V^TSignature
Parameters
Input matrix of shape (M, N). Must be 2D.
If true, U has shape (M, M) and Vt has shape (N, N).
If false, U has shape (M, K) and Vt has shape (K, N) where K = min(M, N).
Returns
Tuple of three tensors[U, s, Vt]:
- U: Left singular vectors of shape (M, M) or (M, K)
- s: Singular values of shape (K,) in descending order
- Vt: Right singular vectors transposed of shape (N, N) or (K, N)
Algorithm
One-sided Jacobi (Hestenes) SVD:- Apply cyclic Jacobi rotations to orthogonalize columns of A
- Column norms converge to singular values
- Right singular vectors are accumulated as the product of rotations
- Left singular vectors are normalized columns of the rotated matrix
Properties
- A = U @ diag(s) @ Vt
- U and V are orthogonal matrices
- Singular values are non-negative and sorted in descending order
Errors
- ShapeError: If input is not 2D
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
qr
QR decomposition using Householder reflections. Factorizes matrix A into Q × R where Q is orthogonal and R is upper triangular.Signature
Parameters
Input matrix of shape (M, N). Must be 2D.
Decomposition mode:
'reduced': Q has shape (M, K), R has shape (K, N) where K = min(M, N)'complete': Q has shape (M, M), R has shape (M, N)
Returns
Tuple of two tensors[Q, R]:
- Q: Orthogonal matrix (Q^T × Q = I)
- R: Upper triangular matrix
Algorithm
Householder reflections (Golub & Van Loan, Algorithm 5.2.1):- Iteratively zeros out elements below the diagonal using orthogonal reflections
- Numerically stable and efficient for dense matrices
Properties
- A = Q @ R
- Q is orthogonal: Q^T @ Q = I
- R is upper triangular
Errors
- ShapeError: If input is not 2D
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
lu
LU decomposition with partial pivoting. Factorizes matrix A into P × A = L × U where P is a permutation matrix, L is lower triangular with unit diagonal, and U is upper triangular.Signature
Parameters
Input matrix of shape (M, N). Must be 2D.
Returns
Tuple of three tensors[P, L, U]:
- P: Permutation matrix of shape (M, M)
- L: Lower triangular matrix of shape (M, K) where K = min(M, N)
- U: Upper triangular matrix of shape (K, N)
Algorithm
Gaussian elimination with partial pivoting (Golub & Van Loan, Algorithm 3.4.1):- Partial pivoting ensures numerical stability
- Rank-deficient matrices with zero pivots are handled gracefully
Properties
- P @ A = L @ U
- L has unit diagonal (L[i,i] = 1)
- Partial pivoting ensures numerical stability
Errors
- ShapeError: If input is not 2D
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
cholesky
Cholesky decomposition for symmetric positive definite matrices. Factorizes matrix A into L × L^T where L is lower triangular.Signature
Parameters
Symmetric positive definite matrix of shape (N, N). Must be 2D and square.
Returns
Lower triangular matrix L where A = L × L^TAlgorithm
Cholesky-Banachiewicz algorithm:- Time Complexity: O(N³/3) - approximately half the cost of LU decomposition
- No pivoting required due to positive definiteness
- More stable than LU for positive definite matrices
Properties
- A = L @ L^T
- L is lower triangular (L[i,j] = 0 for j > i)
- L has positive diagonal elements
- Much faster than LU decomposition for positive definite matrices
- Determinant: det(A) = (product of L[i,i])²
Requirements
- Matrix must be symmetric: A = A^T
- Matrix must be positive definite: x^T × A × x > 0 for all x ≠ 0
Errors
- ShapeError: If input is not 2D square matrix
- DTypeError: If input has string dtype
- DataValidationError: If matrix is not symmetric
- DataValidationError: If matrix is not positive definite
- DataValidationError: If input contains non-finite values (NaN, Infinity)
eig
Compute eigenvalues and eigenvectors of a square matrix. Solves A × v = λ × v where λ are eigenvalues and v are eigenvectors.Signature
Parameters
Square matrix of shape (N, N). Must be 2D.
Optional configuration:
maxIter: Maximum QR iterations (default: 300)tol: Convergence tolerance for subdiagonal norm (default: 1e-10)
Returns
Tuple of two tensors[eigenvalues, eigenvectors]:
- eigenvalues: Real values of shape (N,)
- eigenvectors: Column vectors of shape (N, N) where eigenvectors[:,i] corresponds to eigenvalues[i]
Algorithm
- Symmetric matrices: Jacobi iteration (stable, accurate)
- General matrices: QR iteration with Hessenberg reduction
Properties
- A @ v[:,i] = λ[i] × v[:,i]
- Eigenvectors are normalized
Limitations
- Only real eigenvalues are supported
- Matrices with complex eigenvalues will throw InvalidParameterError
- For symmetric/Hermitian matrices, use
eigh()for better performance - May not converge for some matrices (bounded QR iterations)
Errors
- ShapeError: If input is not square matrix
- DTypeError: If input has string dtype
- DataValidationError: If input contains non-finite values (NaN, Infinity)
- InvalidParameterError: If matrix has complex eigenvalues
- ConvergenceError: If QR iteration fails to converge
eigvals
Compute eigenvalues only (faster than eig).Signature
Parameters
Square matrix of shape (N, N)
Same as
eig() optionsReturns
Array of real eigenvaluesLimitations
- Matrices with complex eigenvalues are not supported and will throw InvalidParameterError
eigh
Compute eigenvalues and eigenvectors of a symmetric/Hermitian matrix. More efficient thaneig() for symmetric matrices.
Signature
Parameters
Symmetric matrix of shape (N, N)
Returns
Tuple of two tensors[eigenvalues, eigenvectors]:
- eigenvalues: Real values in ascending order
- eigenvectors: Orthonormal column vectors
Properties
- All eigenvalues are real
- Eigenvectors are orthonormal
- More efficient and numerically stable than
eig()for symmetric matrices
Errors
- ShapeError: If input is not square matrix
- DTypeError: If input has string dtype
- DataValidationError: If input is not symmetric
- DataValidationError: If input contains non-finite values (NaN, Infinity)
eigvalsh
Compute eigenvalues only of symmetric matrix (faster than eigh).Signature
Parameters
Symmetric square matrix of shape (N, N)
Returns
Array of real eigenvalues in ascending orderErrors
- ShapeError: If input is not square matrix
- DTypeError: If input has string dtype
- DataValidationError: If input is not symmetric
- DataValidationError: If input contains non-finite values (NaN, Infinity)