Decompositions

Each decomposition is a class constructed from a MaybeMatrix. Both a long name and a short alias are exported.

import {
  SingularValueDecomposition, // SVD
  EigenvalueDecomposition, // EVD
  LuDecomposition, // LU
  QrDecomposition, // QR
  CholeskyDecomposition, // CHO
  Nipals, // NIPALS
} from "@teakit/matrix";

SVD — SingularValueDecomposition / SVD

const svd = new SVD(A, {
  computeLeftSingularVectors: true,
  computeRightSingularVectors: true,
  autoTranspose: false,
});
svd.leftSingularVectors; // Matrix (U)
svd.rightSingularVectors; // Matrix (V)
svd.diagonal; // number[] (singular values)
svd.diagonalMatrix; // Matrix
svd.rank;
svd.condition;
svd.norm2;
svd.threshold;
svd.inverse(); // (pseudo)inverse via SVD
svd.solve(b); // least-squares solve
svd.solveForDiagonal(values);

Pass autoTranspose: true when the matrix has more columns than rows.

EVD — EigenvalueDecomposition / EVD

const e = new EVD(A, { assumeSymmetric: false });
e.realEigenvalues; // number[]
e.imaginaryEigenvalues; // number[]
e.eigenvectorMatrix; // Matrix
e.diagonalMatrix; // Matrix

LU — LuDecomposition / LU

const lu = new LU(A);
lu.lowerTriangularMatrix; // Matrix (L)
lu.upperTriangularMatrix; // Matrix (U)
lu.pivotPermutationVector; // number[]
lu.determinant; // number (square)
lu.isSingular();
lu.solve(b); // solve A·x = b

QR — QrDecomposition / QR

const qr = new QR(A);
qr.orthogonalMatrix; // Matrix (Q)
qr.upperTriangularMatrix; // Matrix (R)
qr.isFullRank();
qr.solve(b); // least-squares for rectangular A (fails if rank-deficient)

Cholesky — CholeskyDecomposition / CHO

For symmetric positive-definite A (A = L·Lᵀ).

const cho = new CHO(A); // throws if A is not symmetric
cho.lowerTriangularMatrix; // Matrix (L)
cho.isPositiveDefinite();
cho.solve(b); // throws if A is not positive-definite

NIPALS — Nipals / NIPALS

Iterative PLS / PCA-style factorization.

const n = new NIPALS(X, { Y, scaleScores: false, maxIterations: 1000, terminationCriteria: 1e-10 });
n.t; n.w; n.s; n.xResidual; // and, when Y is given: n.p, n.q, n.u, n.yResidual, n.betas