matrix-sync
v6.15.0
Published
Dense matrix algebra for JavaScript and TypeScript
Maintainers
Readme
matrix-sync
Dense matrix algebra for JavaScript and TypeScript.
Create matrices, run element-wise math, multiply, invert, and factorize — in Node.js and the browser.
| | | | --- | --- | | npm | matrix-sync | | repo | sync-package/matrix | | docs | API reference | | license | MIT | | author | browndev88 · [email protected] |
Contents
- Install
- Quick start
- What it covers
- Build a matrix
- Arithmetic
- Shape and stats
- Linear algebra
- Module formats
- License
Install
npm install matrix-syncRequires a current Node.js release. Type definitions ship in the package (matrix.d.ts).
Quick start
import { Matrix, inverse, solve, QrDecomposition } from 'matrix-sync';
const A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
A.mmul(inverse(A)); // ~ identityWhat it covers
| Area | API |
| --- | --- |
| Storage | Matrix, SymmetricMatrix, DistanceMatrix, 1D/2D wrappers |
| Construction | zeros, ones, eye, rand, diag, columnVector, rowVector |
| Arithmetic | add sub mul div mod max min — static or in-place |
| Products | mmul, mmulByTranspose, transposeMultiply, kronecker, gram |
| Element math | abs, exp, log, sqrt, sin, cos, … (same set as Math) |
| Structure | transpose, concat, diag, set / get, row & column views |
| Reductions | sum, mean, prod, norm, applyAlongAxis |
| Factorizations | SVD, EVD, QR, LU, Cholesky, NIPALS |
| Solvers | inverse, pseudoInverse, solve, linearDependencies |
| Stats | covariance, correlation |
Build a matrix
import { Matrix } from 'matrix-sync';
const fromRows = new Matrix([
[1, 2],
[3, 4],
]);
Matrix.zeros(3, 2); // 3×2 zeros
Matrix.ones(2, 3); // 2×3 ones
Matrix.eye(3, 4); // 3×4 identity-padded
Matrix.columnVector([5, 6]);Join along a dimension. Operands are not mutated. Row concat needs matching columns; column concat needs matching rows.
fromRows.concat([[5, 6]]);
fromRows.concat(Matrix.columnVector([5, 6]), 'column');Arithmetic
Static (returns a new matrix):
Matrix.add(A, B);
Matrix.sub(A, B);
A.mmul(B); // matrix product
Matrix.mul(A, 10); // scale
Matrix.div(A, 10);
Matrix.mod(B, 2);
Matrix.max(A, B);
Matrix.min(A, B);In-place (mutates the receiver):
C.add(A);
C.sub(A);
C.mul(10);
C.div(10);
C.mod(2);Element-wise math — Matrix.exp(A), Matrix.cos(A), Matrix.abs(A), or A.abs() in place.
abs acos acosh asin asinh atan atanh cbrt ceil clz32 cos cosh exp expm1 floor fround log log1p log10 log2 round sign sin sinh sqrt tan tanh trunc
Shape and stats
A.rows;
A.columns;
A.size;
A.get(0, 0);
A.set(1, 0, 10);
A.isSquare();
A.isSymmetric();
A.diag();
A.mean();
A.prod();
A.norm(); // Frobenius
A.transpose();Reduce along an axis with any callback. The callback receives a plain array and its index.
const M = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);
const sum = (vector) => vector.reduce((n, x) => n + x, 0);
M.applyAlongAxis(sum, 'row'); // [6, 15]
M.applyAlongAxis(sum, 'column'); // [5, 7, 9]Linear algebra
import {
Matrix,
inverse,
solve,
linearDependencies,
QrDecomposition,
LuDecomposition,
CholeskyDecomposition,
EigenvalueDecomposition,
} from 'matrix-sync';Inverse
const A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
inverse(A);
A.mmul(inverse(A)); // ~ I
// Singular / rank-deficient: SVD-based inverse
const S = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
inverse(S, true);Pseudo-inverse
const A = new Matrix([
[1, 2],
[3, 4],
[5, 6],
]);
const Ap = A.pseudoInverse();
A.mmul(Ap).mmul(A); // ~ ALeast squares
Solve A x = B.
const A = new Matrix([
[3, 1],
[4.25, 1],
[5.5, 1],
[8, 1],
]);
const B = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);
const x = solve(A, B);
Matrix.sub(B, A.mmul(x)); // residualPass true as the third argument to solve to use SVD when A is singular.
Factorizations
| Method | Class | Pieces |
| --- | --- | --- |
| QR | QrDecomposition | orthogonalMatrix, upperTriangularMatrix |
| LU | LuDecomposition | lowerTriangularMatrix, upperTriangularMatrix, pivotPermutationVector |
| Cholesky | CholeskyDecomposition | lowerTriangularMatrix |
| EVD | EigenvalueDecomposition | realEigenvalues, imaginaryEigenvalues, eigenvectorMatrix |
const QR = new QrDecomposition(A);
QR.orthogonalMatrix;
QR.upperTriangularMatrix;
const LU = new LuDecomposition(A);
LU.lowerTriangularMatrix;
LU.upperTriangularMatrix;
LU.pivotPermutationVector; // e.g. [1, 2, 0]
const chol = new CholeskyDecomposition(A);
chol.lowerTriangularMatrix;
const evd = new EigenvalueDecomposition(A);
evd.realEigenvalues;
evd.imaginaryEigenvalues;
evd.eigenvectorMatrix;Row dependencies
const A = new Matrix([
[2, 0, 0, 1],
[0, 1, 6, 0],
[0, 3, 0, 1],
[0, 0, 1, 0],
[0, 1, 2, 0],
]);
linearDependencies(A);
// zero row → independent
// e.g. [0, 0, 0, 4, 1] → row 1 = 4 × row 3 + row 4Module formats
| Environment | How |
| --- | --- |
| ESM | import { Matrix } from 'matrix-sync' |
| CommonJS | const { Matrix } = require('matrix-sync') |
| Browser (UMD) | matrix.umd.js — global matrixSync |
| Types | matrix.d.ts included |
import { Matrix } from 'matrix-sync';
const ones = Matrix.ones(5, 5);const { Matrix } = require('matrix-sync');
const ones = Matrix.ones(5, 5);Full method list: API documentation.
License
MIT. See LICENSE.
Maintained by sync-package · browndev88 ([email protected]).
