Decompositions
Singular Value Decomposition. Factorizes A into A = UΣVᵀ where U and Vᵀ are orthogonal matrices and Σ is a diagonal matrix of singular values (sorted descending). Works on any m×n matrix.
A: Tensor - Input matrix of shape [m, n]QR Decomposition. Factorizes A into A = QR where Q is orthogonal and R is upper triangular. Used for solving least-squares problems and as a subroutine in eigenvalue algorithms.
LU Decomposition. Factorizes A into A = LU where L is lower triangular (with unit diagonal) and U is upper triangular. Efficient for solving multiple linear systems with the same coefficient matrix.
Cholesky Decomposition. Factorizes symmetric positive-definite A into A = LLᵀ where L is lower triangular. Twice as efficient as LU. Fails if A is not positive definite.
Eigendecomposition of a general square matrix. Returns eigenvalues and corresponding eigenvectors. Eigenvalues may be complex for non-symmetric matrices.
Eigendecomposition for symmetric (Hermitian) matrices. More numerically stable and efficient than eig() for symmetric input. All eigenvalues are real.
Compute eigenvalues only (no eigenvectors). Faster when you only need the eigenvalues.
Compute eigenvalues of a symmetric matrix. Faster and more stable than eigvals for symmetric input.
SVD
Where:
- Σ = Diagonal matrix of singular values
QR
Where:
- Q = Orthogonal matrix (QᵀQ = I)
LU
Where:
- L = Lower triangular
- U = Upper triangular
Cholesky
Where:
- A = Must be symmetric positive definite
Eigendecomposition
Where:
- λ = Eigenvalue
- v = Eigenvector
import { svd, qr, lu, cholesky, eig, eigh } from "deepbox/linalg";import { tensor } from "deepbox/ndarray";const A = tensor([[4, 2], [2, 3]]);// SVD: A = UΣVᵀ — returns [U, S, Vt] tupleconst [U, S, Vt] = svd(A);// QR: A = QR — returns [Q, R] tupleconst [Q, R] = qr(A);// LU: A = LU — returns [L, U, P] tupleconst [L, LU_U, P] = lu(A);// Cholesky: A = LLᵀ (A must be positive definite)const Lchol = cholesky(A);// Eigendecomposition (symmetric) — returns [eigenvalues, eigenvectors] tupleconst [eigenvalues, eigenvectors] = eigh(A);console.log(eigenvalues); // sorted ascendingWhen to Use
- SVD — PCA, pseudoinverse, matrix approximation, recommender systems
- QR — Least-squares solving, stable Gram-Schmidt orthogonalization
- LU — Solving many Ax=b systems with same A, computing determinants
- Cholesky — Fastest for symmetric positive-definite systems (covariance matrices, Gaussian processes)
- eig/eigh — Spectral analysis, graph Laplacians, vibration modes, PCA via covariance matrix