Properties & Norms
Compute the matrix inverse A⁻¹ such that A·A⁻¹ = I. A must be square and non-singular. Prefer solve() over inv() when solving systems — it is more numerically stable.
Moore-Penrose pseudoinverse. Works for any matrix (including non-square and singular). Computed via SVD.
Compute the determinant of a square matrix. det(A) = 0 indicates a singular matrix.
Compute the sign and log of the absolute determinant. Numerically stable for matrices with very large or very small determinants.
Compute the numerical rank of a matrix (number of singular values above tolerance). Uses SVD.
Sum of the diagonal elements. For 2D: tr(A) = Σᵢ aᵢᵢ. Returns a scalar Tensor. The trace equals the sum of eigenvalues.
Condition number of a matrix (ratio of largest to smallest singular value). Large values indicate the matrix is near-singular and solutions may be numerically unstable.
Compute vector or matrix norm. Supports L1 (ord=1), L2/Euclidean (ord=2), Frobenius ('fro'), nuclear ('nuc'), and infinity (ord=Infinity) norms.
ord: number | 'fro' | 'nuc' - Norm order. Default: 2 (Euclidean/Frobenius)L2 Norm
Where:
- xᵢ = Vector elements
Frobenius Norm
Where:
- aᵢⱼ = Matrix elements
Condition Number
Where:
- σ = Singular values
import { inv, pinv, det, norm, matrixRank, cond } from "deepbox/linalg";import { tensor } from "deepbox/ndarray";const A = tensor([[1, 2], [3, 4]]);const Ainv = inv(A); // A⁻¹const d = det(A); // -2const r = matrixRank(A); // 2 (full rank)const k = cond(A); // condition number// Normsconst v = tensor([3, 4]);norm(v); // 5 (L2 norm: √(9+16))norm(v, 1); // 7 (L1 norm: |3|+|4|)norm(A, 'fro'); // Frobenius norm// Pseudoinverse (works for non-square)const B = tensor([[1, 2, 3], [4, 5, 6]]);const Bpinv = pinv(B); // shape: [3, 2]