la
bun add @stopcock/la@stopcock/la provides vector and matrix operations over Float64Array data.
The main entry point exposes Vec and Mat namespaces, lower-level Fast and
Primitives kernels, and optional acceleration hooks.
import { Vec, Mat } from '@stopcock/la'
const direction = Vec.create(3, 4)const unit = Vec.normalize(direction)
const transform = Mat.fromArray(2, 2, [2, 0, 0, 3])const inverse = Mat.inverse(transform)Vectors
Section titled “Vectors”Vectors are Float64Array values. Most operations allocate a new vector;
the *Into variants write into a caller-owned output buffer.
import { Vec } from '@stopcock/la'
const a = Vec.create(1, 2, 3)const b = Vec.create(4, 5, 6)
Vec.add(a, b) // Float64Array [5, 7, 9]Vec.dot(a, b) // 32Vec.cross(a, b) // Float64Array [-3, 6, -3]Vec.norm(a) // sqrt(14)Vec.distance(a, b)Vec.lerp(a, b, 0.5)Vec.axpy(2, a, b) // 2a + bVector API
Section titled “Vector API”type Vec = Float64Array
Vec.create(...values): VecVec.zeros(length): VecVec.ones(length): VecVec.add(a, b): VecVec.sub(a, b): VecVec.scale(vector, scalar): VecVec.dot(a, b): numberVec.cross(a, b): VecVec.norm(vector): numberVec.normalize(vector): VecVec.distance(a, b): numberVec.lerp(a, b, t): VecVec.axpy(alpha, x, y): Vec
Vec.addInto(out, a, b): VecVec.scaleInto(out, vector, scalar): VecVec.axpyInto(out, alpha, x, y): VecMatrices
Section titled “Matrices”Matrices are row-major objects containing a Float64Array, row count, and
column count. Shape checks run before arithmetic and solve operations.
import { Mat, Vec } from '@stopcock/la'
const a = Mat.fromArray(2, 2, [4, 1, 2, 3])const b = Vec.create(9, 8)
const x = Mat.solve(a, b)const product = Mat.multiply(a, Mat.identity(2))const { L, U, P } = Mat.lu(a)Matrix API
Section titled “Matrix API”type Mat = { data: Float64Array; rows: number; cols: number }
Mat.create(rows, cols, data?): MatMat.zeros(rows, cols): MatMat.identity(size): MatMat.fromArray(rows, cols, values): MatMat.get(matrix, row, col): numberMat.set(matrix, row, col, value): Mat
Mat.add(a, b): MatMat.sub(a, b): MatMat.scale(matrix, scalar): MatMat.multiply(a, b): MatMat.multiplyInto(out, a, b): MatMat.transpose(matrix): MatMat.trace(matrix): numberMat.determinant(matrix): numberMat.norm(matrix): number
Mat.lu(matrix): { L: Mat; U: Mat; P: number[] }Mat.qr(matrix): { Q: Mat; R: Mat }Mat.svd(matrix): { U: Mat; S: Vec; V: Mat }Mat.cholesky(matrix): Mat | nullMat.eigenvalues(matrix): VecMat.solve(matrix, vector): VecMat.inverse(matrix): Mat | nullHot-path kernels
Section titled “Hot-path kernels”Fast contains fixed-size, unrolled operations. Primitives contains
caller-owned-buffer kernels used by image and signal workloads.
import { Fast, Primitives } from '@stopcock/la'
Fast.dot3(a, b)Fast.mul3x3(left, right, out)Fast.mul3vec(matrix, x, y, z, out)
Primitives.sumOfSquares(values, values.length)Primitives.convolve1dFloat(out, source, kernel, source.length, kernel.length)Primitives.applyColorMatrix3x3(outPixels, sourcePixels, matrix, pixelCount)Import the same namespaces through subpaths when bundle boundaries matter:
import * as Fast from '@stopcock/la/fast'import * as Primitives from '@stopcock/la/primitives'Optional acceleration
Section titled “Optional acceleration”Install an accelerator once to let large dot products, AXPY operations, matrix multiplication, convolution, and color-matrix kernels use an external runtime. The JavaScript implementations remain the fallback.
import { accelerate, decelerate, isAccelerated } from '@stopcock/la'import type { WasmAccelerator } from '@stopcock/la/accel'
declare const accelerator: WasmAccelerator
accelerate(accelerator)isAccelerated() // truedecelerate()