abstract class TensorBase All fields are embedded, with zero dynamic allocation. DType is a numeric enum, letting a switch over it compile to a jump table. Mirrors the C++ pattern: buffer + byteOffset + dtype.
The wrapper hierarchy exists to keep dtype and ownership explicit across addon and WASM boundaries.
TensorBase carries buffer, byteOffset, and dtypeVectorBase adds rank-1 semanticsVectorView is a zero-copy borrowed wrapper over existing memoryVector owns its ArrayBufferimport { VectorView, Vector, DType, dot } from "numkong";
const raw = new Uint16Array([0x3c00, 0x4000, 0x4200]); // f16 payload, not uint16 values
const view = VectorView.from(raw, DType.F16);
const owned = new Vector(3, DType.F32);
owned.toTypedArray().set(new Float32Array([4, 5, 6]));
console.log(dot(view, view));
console.log(dot(owned, owned));
console.log(owned.byteLength);Use VectorView when the bytes already live somewhere else.
Use Vector when NumKong should own the storage.
Owned Vector and Matrix are fixed-capacity resizable. capacity reports the allocated element ceiling. tryResize(...) reshapes within capacity without moving the ArrayBuffer, so a toTypedArray() result stays valid. reserve(elements) may allocate a new ArrayBuffer to grow, which invalidates any TypedArray or row() handed out earlier. clear() empties the length while keeping capacity.
TensorBaseAbstract base class for all tensor types.7abstract class TensorBase All fields are embedded, with zero dynamic allocation. DType is a numeric enum, letting a switch over it compile to a jump table. Mirrors the C++ pattern: buffer + byteOffset + dtype.
bufferbuffer: ArrayBuffer byteOffsetbyteOffset: number dtypereadonly dtype: DType lengthreadonly length: number rankreadonly rank: number bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.readonly bytesPerElement: number byteLengthTotal byte length of the tensor data.readonly byteLength: number VectorBaseAbstract rank-1 tensor base class.7abstract class VectorBase extends TensorBase lengthlength: number rankreadonly rank: 1 bufferbyteOffsetdtypebytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.byteLengthTotal byte length of the tensor data.VectorViewNon-owning rank-1 tensor view (like std::span<T>).10class VectorView extends VectorBase Zero-copy wrapper for existing memory. Ideal for cross-module WASM interop where data already lives on the WASM heap.
VectorViewnew VectorView(
buffer: ArrayBuffer,
byteOffset: number,
length: number,
dtype: DType,
) fromCreate a VectorView from any TypedArray, inferring or accepting dtype.static from(arr: TypedArray, dtype?: DType): VectorView to*× 3toString(): string lengthrankbufferbyteOffsetdtypebytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.byteLengthTotal byte length of the tensor data.VectorOwning rank-1 tensor (like std::vector<T>).15class Vector extends VectorBase Allocates its own ArrayBuffer. Use for storing results or when you need independent ownership of the data.
Vectornew Vector(length: number, dtype: DType) new Vector(buffer: ArrayBuffer, length: number, dtype: DType) fromTypedArrayCreate an owning Vector by copying data from a TypedArray.static fromTypedArray(arr: TypedArray, dtype?: DType): Vector fromViewCreate an owning Vector by copying data from any TensorBase.static fromView(view: TensorBase): Vector toTypedArrayReturn a TypedArray view over this Vector's owned buffer, zero-copy.toTypedArray(): TypedArray tryResizeResize within capacity without moving storage — toTypedArray() views stay valid.tryResize(length: number): boolean length is negative or exceeds capacity().reserveGrow the allocated capacity to at least elements, reallocating and copying the live elements.reserve(elements: number): boolean A no-op when already large enough; otherwise it invalidates any TypedArray already returned by toTypedArray(), which keeps pointing at the old buffer.
clearReset to length 0 while keeping the allocated capacity.clear(): void capacityAllocated element capacity (>= length) — the ceiling tryResize honors.readonly capacity: number lengthrankbufferbyteOffsetdtypebytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.byteLengthTotal byte length of the tensor data.MatrixBaseAbstract rank-2 tensor base class.11abstract class MatrixBase extends TensorBase All 4 dimension fields are embedded — no dynamic allocation. Strides are in bytes to match the C API directly.
rowsrows: number columnscolumns: number rowStriderowStride: number colStridecolStride: number lengthreadonly length: number rankreadonly rank: 2 bufferbyteOffsetdtypebytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.byteLengthTotal byte length of the tensor data.MatrixOwning rank-2 tensor, row-major and C-contiguous by default.19class Matrix extends MatrixBase Strides are byte strides. Default for C-contiguous layout: rowStride = columns * bytesPerElement, colStride = bytesPerElement.
Matrixnew Matrix(rows: number, columns: number, dtype: DType) new Matrix(
buffer: ArrayBuffer,
byteOffset: number,
dtype: DType,
rows: number,
columns: number,
rowStride?: number,
colStride?: number,
) fromTypedArraystatic fromTypedArray(
array: TypedArray,
rows: number,
columns: number,
dtype?: DType,
): Matrix toTypedArraytoTypedArray(): TypedArray rowrow(index: number): VectorView tryResizeResize within capacity without moving storage (row()/toTypedArray() views stay valid), re-deriving C-contiguous strides.tryResize(rows: number, columns: number): boolean rows*columns exceeds
capacity().reserveGrow the allocated capacity to at least elements, reallocating and copying the live rows*columns elements.reserve(elements: number): boolean A no-op when already large enough; otherwise it invalidates any prior toTypedArray() / row() view. Intended for the C-contiguous owning layout.
clearReset to an empty 0x0 matrix while keeping the allocated capacity.clear(): void capacityAllocated element capacity (>= rows*columns) — the ceiling tryResize honors.readonly capacity: number rowscolumnsrowStridecolStridelengthrankbufferbyteOffsetdtypebytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.byteLengthTotal byte length of the tensor data.