API गाइड
The default export is the Matrix class. Named exports include runtime helpers and global configuration.
import Matrix, {
SIMD_REQUIRED,
configure,
createRuntime,
isSimdSupported
} from "wasmatrix";
This page is generated from the TSDoc comments in index.d.ts, which is copied to dist/index.d.ts during the package build.
Runtime Helpers
SIMD_REQUIRED
const SIMD_REQUIRED: true
Whether this package requires WebAssembly SIMD support.
Remarks: This is always true. WASMatrix does not ship a scalar fallback.
isSimdSupported(wasmBytes?)
isSimdSupported(wasmBytes?: WasmBytes): boolean
Checks whether the current JavaScript runtime validates WASMatrix's SIMD WASM.
| Parameter | Type | Description |
|---|---|---|
wasmBytes? | WasmBytes | Optional explicit WASM bytes. When omitted, the default package WASM bytes loaded by the ESM entrypoint are used. |
Returns: true when WebAssembly.validate() accepts the bytes.
createRuntime(wasmBytes?)
createRuntime(wasmBytes?: WasmBytes): unknown
Creates a separate WASM runtime instance.
| Parameter | Type | Description |
|---|---|---|
wasmBytes? | WasmBytes | Optional explicit WASM bytes. When omitted, the default package WASM bytes loaded by the ESM entrypoint are used. |
Remarks: Most users should use the default runtime initialized by the package entrypoint. Separate runtimes are useful only when you intentionally want isolated WASM memories and caches.
Returns: An opaque runtime handle used internally by matrix instances.
Throws: Error if no default WASM bytes are loaded.
Throws: Error if SIMD validation or the ABI probe fails.
configure(options?)
configure(options?: WasmatrixOptions): void
Configures global optimization behavior.
| Parameter | Type | Description |
|---|---|---|
options? | WasmatrixOptions | Partial options to update. |
Remarks: Existing matrices keep their data, but cache sizing changes can evict cached buffers immediately.
Configuration Options
Process-wide optimization and cache options.
| Option | Type | Default | Description |
|---|---|---|---|
cacheLimitBytes | number | 64 * 1024 * 1024 | Maximum WASM heap budget used by reusable caches. The limit covers factorization, transpose, packed operand, reduction, and expression-result caches. Set to 0 to aggressively evict reusable cache entries. |
fastMath | boolean | false | Enables more aggressive algebraic rewrites. Keep this disabled when NaN, Infinity, -0, and strict floating-point rounding behavior are observable in your workflow. |
WasmBytes
type WasmBytes = ArrayBuffer | ArrayBufferView
Raw WebAssembly module bytes accepted by runtime helpers.
Matrix
Dense row-major f32 matrix backed by WASM memory.
Remarks: Most operations return another Matrix. Data stays in WASM memory until you call a readback method such as Matrix.toArray, Matrix.toFloat32Array, Matrix.row, or Matrix.column.
Constructor
new Matrix(rows, cols, data?, options?)
new Matrix(rows: number, cols: number, data?: ArrayLike<number>, options?: { copy?: boolean })
Creates a matrix from row-major values.
| Parameter | Type | Description |
|---|---|---|
rows | number | Positive row count. |
cols | number | Positive column count. |
data? | ArrayLike<number> | Optional row-major values. Omitted values create a zero matrix. |
options? | { copy?: boolean } | Reserved construction options. |
Throws: RangeError if rows, cols, or data.length are invalid.
Properties
| Property | Type | Description |
|---|---|---|
byteOffset | readonly number | Byte offset of the materialized matrix buffer inside WASM memory. Accessing this property can force lazy views or expressions to materialize. |
cols | readonly number | Number of matrix columns. |
data | readonly Float32Array | Snapshot copied from WASM memory when accessed. Mutating the returned typed array does not mutate the matrix. Use Matrix.set for scalar in-place writes. |
length | readonly number | Total number of elements, equal to rows * cols. |
rows | readonly number | Number of matrix rows. |
shape | readonly [number, number] | Matrix shape as [rows, cols]. |
Static Methods
Matrix.configure(options?)
Matrix.configure(options?: WasmatrixOptions): void
Equivalent to the top-level configure helper.
| Parameter | Type | Description |
|---|---|---|
options? | WasmatrixOptions | - |
Matrix.diagonal(values)
Matrix.diagonal(values: Matrix | ArrayLike<number>): Matrix
Creates a dense diagonal matrix from vector values.
| Parameter | Type | Description |
|---|---|---|
values | Matrix | ArrayLike<number> | - |
Remarks: Diagonal structure is tracked for determinant, solve, inverse, and diagonal matrix multiplication shortcuts.
Matrix.from(rows, cols, values)
Matrix.from(rows: number, cols: number, values: ArrayLike<number>): Matrix
Creates a matrix from row-major values.
| Parameter | Type | Description |
|---|---|---|
rows | number | Positive row count. |
cols | number | Positive column count. |
values | ArrayLike<number> | Row-major values with length rows * cols. |
Matrix.identity(size)
Matrix.identity(size: number): Matrix
Creates an identity matrix.
| Parameter | Type | Description |
|---|---|---|
size | number | - |
Remarks: Identity structure is tracked so operations such as I.matmul(A) can use structural shortcuts.
Matrix.matmulChain(...matrices)
Matrix.matmulChain(...matrices: Matrix[]): Matrix
Multiplies a compatible chain of matrices using a shape-based cost model.
| Parameter | Type | Description |
|---|---|---|
matrices | Matrix[] | - |
Remarks: The chain order is chosen by dynamic programming over matrix dimensions. Intermediate products are materialized and disposed where possible.
Matrix.matmulChain(matrices)
Matrix.matmulChain(matrices: Matrix[]): Matrix
Multiplies a compatible chain of matrices using a shape-based cost model.
| Parameter | Type | Description |
|---|---|---|
matrices | Matrix[] | - |
Remarks: Array overload for callers that already hold the chain in a collection.