Mulai
WASMatrix adalah library matriks TypeScript yang didukung core AssemblyScript WebAssembly. Buffer matriks tetap berada di WASM memory, memakai storage f32 row-major, dan membutuhkan WebAssembly SIMD.
This guide walks through the first things most users need:
- install the package
- create matrices
- run matrix, elementwise, and linear algebra operations
- read results back only when you need them
- understand when cached factorizations and lazy views help
Installation
npm install wasmatrix
WASMatrix is ESM-only. Importing the package initializes the adjacent WASM asset with top-level await.
import Matrix from "wasmatrix";
The package ships JavaScript and WASM separately. The runtime resolves dist/wasmatrix.wasm with new URL("./wasmatrix.wasm", import.meta.url), so Node, Deno, Bun, browsers, CDNs, and bundlers can handle the asset in their normal way.
Runtime Requirements
WASMatrix needs ESM, top-level await, and WebAssembly SIMD. If SIMD validation fails, initialization throws instead of silently falling back to a scalar implementation.
import { isSimdSupported } from "wasmatrix";
console.log(isSimdSupported());
Run Your First Program
Create a file named hello.mjs and solve a small linear system.
import Matrix from "wasmatrix";
const A = Matrix.from(2, 2, [
4, 7,
2, 6
]);
const b = Matrix.from(2, 1, [
1,
0
]);
const x = A.solve(b);
console.log("det(A)", A.determinant());
console.log("solution", x.toArray());
console.log("check", A.matmul(x).equalsApprox(b));
node hello.mjs
bun hello.mjs
deno run --allow-read hello.mjs
The solve, determinant, and multiplication work runs inside WASM. JavaScript receives values only when toArray(), determinant(), or equalsApprox() reads them back.
Create Matrices
Matrix.from(rows, cols, values) uses row-major values. Static constructors such as zeros, ones, identity, and diagonal also carry structure tags for later shortcuts.
const A = Matrix.from(2, 3, [
1, 2, 3,
4, 5, 6
]);
const Z = Matrix.zeros(3, 3);
const O = Matrix.ones(3, 2);
const I = Matrix.identity(4);
const D = Matrix.diagonal([2, 4, 8]);
Keep Data In WASM
Most methods return another Matrix, and the result stays in WASM memory. Read back with toArray(), toFloat32Array(), row(), column(), or diagonal() only when JavaScript needs the values.
const result = A
.scale(2)
.addScalar(1)
.sqrt()
.clamp(0, 4);
const values = result.toArray();
Elementwise pipeline
Elementwise chains are represented lazily and fused into one WASM pass when materialized. Row and column vectors broadcast without expanding to dense matrices.
const Y = A
.add(B)
.subtract(0.25)
.hadamard(C)
.clamp(-2, 2)
.sqrt();
const centered = X.subtract(rowMeans);
const scaled = centered.divide(columnStddev);
Linear Algebra Without Dense Inverses
Prefer solve() when you want A^-1 B. WASMatrix can reuse versioned LU, Cholesky, and QR caches across compatible determinant, solve, inverse, and rank calls on the same matrix object.
const x = A.solve(b);
const det = A.determinant();
const again = A.solve(otherRightHandSide);
Mutation And Cache Invalidation
set() increments the matrix version and invalidates cached factorizations, reductions, packed operands, and materialized views. Keep the coefficient matrix object stable when you want cache reuse.
A.set(0, 0, A.at(0, 0) + 1);