matrixflow-js
v3.2.2
Published
Dense matrix computation and linear algebra for JavaScript
Maintainers
Readme
matrixflow-js
Dense matrix computation and linear algebra for JavaScript.
matrixflow-js is a typed library for building, transforming, and decomposing dense matrices in Node.js and the browser. Create matrices, run element-wise and linear-algebra operations, and factorize with SVD, EVD, LU, QR, Cholesky, and NIPALS — all from one package.
Homepage: https://matrixflow-js.github.io/matrixflow-js/
Features
- Dense
Matrixwith Float64-backed storage and a full operator surface - Views that slice, transpose, or flip without copying data
SymmetricMatrixandDistanceMatrixfor structured data- Decompositions: SVD, EVD, LU, QR, Cholesky, NIPALS
- Solvers: inverse, pseudo-inverse, least squares
- Statistics: mean, variance, covariance, correlation, center, scale
- ESM, CommonJS, and a minified UMD build
- TypeScript definitions included
Install
npm install matrixflow-jsQuick start
ES modules
import { Matrix } from 'matrixflow-js';
const matrix = Matrix.ones(5, 5);CommonJS
const { Matrix } = require('matrixflow-js');
const matrix = Matrix.ones(5, 5);Browser (CDN)
<script src="https://cdn.jsdelivr.net/npm/matrixflow-js/matrix.umd.js"></script>
<script>
const { Matrix } = matrixflow;
console.log(Matrix.eye(3).toString());
</script>Usage
Create matrices
import { Matrix } from 'matrixflow-js';
const A = new Matrix([
[1, 1],
[2, 2],
]);
const zeros = Matrix.zeros(3, 2);
const ones = Matrix.ones(2, 3);
const identity = Matrix.eye(3, 4);
const diagonal = Matrix.diag([1, 2, 3]);Arithmetic
import { Matrix } from 'matrixflow-js';
const A = new Matrix([
[1, 1],
[2, 2],
]);
const B = new Matrix([
[3, 3],
[1, 1],
]);
Matrix.add(A, B); // [[4, 4], [3, 3]]
Matrix.sub(A, B); // [[-2, -2], [1, 1]]
A.mmul(B); // matrix product
Matrix.mul(A, 10); // scalar multiply
Matrix.div(A, 10); // scalar divide
Matrix.max(A, B);
Matrix.min(A, B);In-place variants mutate the receiver:
const C = B.clone();
C.add(A);
C.mul(10);Math functions
const A = new Matrix([
[1, 1],
[-1, -1],
]);
Matrix.exp(A);
Matrix.cos(A);
Matrix.abs(A);
A.clone().abs(); // in placeAvailable: 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.
Inspect and reshape
A.rows;
A.columns;
A.size;
A.get(0, 0);
A.set(1, 0, 10);
A.diag();
A.mean();
A.prod();
A.norm();
A.transpose();
A.isSquare();
A.isSymmetric();Rows, columns, and concatenation
const M = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);
const sumOf = (vector) => vector.reduce((total, value) => total + value, 0);
M.applyAlongAxis(sumOf, 'row'); // [6, 15]
M.applyAlongAxis(sumOf, 'column'); // [5, 7, 9]
M.concat([[7, 8, 9]]);
M.concat(Matrix.columnVector([7, 8]), 'column');Linear algebra
import {
Matrix,
inverse,
solve,
linearDependencies,
QrDecomposition,
LuDecomposition,
CholeskyDecomposition,
EigenvalueDecomposition,
SingularValueDecomposition,
} from 'matrixflow-js';Inverse and pseudo-inverse
const A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
const inverseA = inverse(A);
A.mmul(inverseA); // ~ identity
const singular = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
inverse(singular, true); // SVD-based inverse
const tall = new Matrix([
[1, 2],
[3, 4],
[5, 6],
]);
tall.pseudoInverse();Least 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);Pass true as the third argument to use SVD when A is singular.
Decompositions
const A = new Matrix([
[2, 3, 5],
[4, 1, 6],
[1, 3, 0],
]);
const QR = new QrDecomposition(A);
QR.orthogonalMatrix;
QR.upperTriangularMatrix;
const LU = new LuDecomposition(A);
LU.lowerTriangularMatrix;
LU.upperTriangularMatrix;
LU.pivotPermutationVector;
const cholesky = new CholeskyDecomposition(A);
cholesky.lowerTriangularMatrix;
const evd = new EigenvalueDecomposition(A);
evd.realEigenvalues;
evd.imaginaryEigenvalues;
evd.eigenvectorMatrix;
const svd = new SingularValueDecomposition(A);
svd.diagonal;
svd.leftSingularVectors;
svd.rightSingularVectors;Linear 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);Documentation
Full homepage, live playground, and API overview:
https://matrixflow-js.github.io/matrixflow-js/
The homepage deploys automatically on every push to main. In the GitHub repository, set Settings → Pages → Source to GitHub Actions.
TypeScript definitions ship with the package (matrix.d.ts).
License
MIT © LinNianPing ([email protected])
