GitHub
deepbox/linalg

Decompositions

Matrix factorizations that decompose a matrix into products of structured matrices. Essential for solving linear systems, computing eigenvalues, and numerical optimization.
svd
svd(A: Tensor, fullMatrices?: boolean): [Tensor, Tensor, Tensor]

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.

Parameters:
A: Tensor - Input matrix of shape [m, n]
qr
qr(A: Tensor, mode?: 'reduced' | 'complete'): [Tensor, Tensor]

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
lu(A: Tensor): [Tensor, Tensor, Tensor]

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
cholesky(A: Tensor): Tensor

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.

eig
eig(A: Tensor, options?: EigOptions): [Tensor, Tensor]

Eigendecomposition of a general square matrix. Returns eigenvalues and corresponding eigenvectors. Eigenvalues may be complex for non-symmetric matrices.

eigh
eigh(A: Tensor, options?: EigOptions): [Tensor, Tensor]

Eigendecomposition for symmetric (Hermitian) matrices. More numerically stable and efficient than eig() for symmetric input. All eigenvalues are real.

eigvals
eigvals(A: Tensor): Tensor

Compute eigenvalues only (no eigenvectors). Faster when you only need the eigenvalues.

eigvalsh
eigvalsh(A: Tensor): Tensor

Compute eigenvalues of a symmetric matrix. Faster and more stable than eigvals for symmetric input.

SVD

A = UΣVᵀ

Where:

  • Σ = Diagonal matrix of singular values

QR

A = QR

Where:

  • Q = Orthogonal matrix (QᵀQ = I)

LU

A = LU

Where:

  • L = Lower triangular
  • U = Upper triangular

Cholesky

A = LLᵀ

Where:

  • A = Must be symmetric positive definite

Eigendecomposition

Av = λv

Where:

  • λ = Eigenvalue
  • v = Eigenvector
decompositions.ts
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 ascending

When 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