Skip to content

Matrix

Source: core/Matrix.luau

Raw 2D-matrix linear algebra on flat row-major arrays. No graph, no requires. Used by matrix preconditioners (Muon Newton-Schulz, SOAP eigenbasis). All arrays are row-major row*cols+col, 1-indexed.

Methods

mm(a: {number}, b: {number}, ma: number, na: number, mb: number, nb: number) -> {number}

matmul: A (ma x na) times B (mb x nb), requires na == mb.

trans(a: {number}, m: number, n: number) -> {number}

eye(n: number) -> {number}

fro(a: {number}) -> number

scale(a: {number}, s: number) -> {number}

copy(a: {number}) -> {number}

zeros(n: number) -> {number}

dot(a: {number}, b: {number}) -> number

Frobenius inner product of two equally-sized arrays.

eigh(a: {number}, n: number) -> ({number}, {number})

Jacobi eigenvalues of a real SYMMETRIC matrix a (flat n x n, row-major). Returns (eigenvalues, eigenvectors) where eigenvectors columns (i.e. V such that a = V diag(w) V'); each eigenvector is a column of V (row-major V).

qr(x: {number}, m: number, n: number) -> ({number}, {number})

QR factorization (column Gram-Schmidt). x flat m x n (m >= n). Returns (q, r), q is m x n with orthonormal columns, r is n x n upper triangular.

newtonSchulz(g: {number}, rows: number, cols: number, iters: number) -> {number}

Newton-Schulz orthogonalization of a matrix g (flat rows x cols). Mirrors the Muon blog implementation: work in the min-dimension orientation (rows <= cols), normalize so the top singular value is <= 1, then iterate 5+ steps of phi(x) = ax + bx^3 + c*x^5. Returns the orthogonalized matrix (same orientation as input).