GitHub
deepbox/linalg

Properties & Norms

Matrix properties (determinant, rank, trace, condition number) and vector/matrix norms.
inv
inv(A: Tensor): Tensor

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.

pinv
pinv(A: Tensor): Tensor

Moore-Penrose pseudoinverse. Works for any matrix (including non-square and singular). Computed via SVD.

det
det(A: Tensor): number

Compute the determinant of a square matrix. det(A) = 0 indicates a singular matrix.

slogdet
slogdet(A: Tensor): { sign: number; logabsdet: number }

Compute the sign and log of the absolute determinant. Numerically stable for matrices with very large or very small determinants.

matrixRank
matrixRank(A: Tensor, tol?: number): number

Compute the numerical rank of a matrix (number of singular values above tolerance). Uses SVD.

trace
trace(A: Tensor, offset?: number, axis1?: number, axis2?: number): Tensor

Sum of the diagonal elements. For 2D: tr(A) = Σᵢ aᵢᵢ. Returns a scalar Tensor. The trace equals the sum of eigenvalues.

cond
cond(A: Tensor): number

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.

norm
norm(t: Tensor, ord?: number | 'fro' | 'nuc', axis?: number): Tensor | number

Compute vector or matrix norm. Supports L1 (ord=1), L2/Euclidean (ord=2), Frobenius ('fro'), nuclear ('nuc'), and infinity (ord=Infinity) norms.

Parameters:
ord: number | 'fro' | 'nuc' - Norm order. Default: 2 (Euclidean/Frobenius)

L2 Norm

‖x‖₂ = √(Σᵢ xᵢ²)

Where:

  • xᵢ = Vector elements

Frobenius Norm

‖A‖_F = √(Σᵢⱼ aᵢⱼ²)

Where:

  • aᵢⱼ = Matrix elements

Condition Number

κ(A) = σ_max / σ_min

Where:

  • σ = Singular values
properties.ts
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]