JavaScript · Tensors and Views

Tensors and Views

import { TensorBase } from 'numkong';import { VectorBase } from 'numkong';import { VectorView } from 'numkong';import { Vector } from 'numkong';import { MatrixBase } from 'numkong';import { Matrix } from 'numkong'; 8.0.0-rc.1 · b1f866ab

The wrapper hierarchy exists to keep dtype and ownership explicit across addon and WASM boundaries.

  • TensorBase carries buffer, byteOffset, and dtype
  • VectorBase adds rank-1 semantics
  • VectorView is a zero-copy borrowed wrapper over existing memory
  • Vector owns its ArrayBuffer
import { 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.

API

ClassTensorBaseAbstract base class for all tensor types.7
types.ts:83 ↗
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.

buffer
types.ts:85 ↗
buffer: ArrayBuffer
byteOffset
types.ts:86 ↗
byteOffset: number
dtype
types.ts:87 ↗
readonly dtype: DType
length
types.ts:95 ↗
readonly length: number
rank
types.ts:96 ↗
readonly rank: number
bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.
types.ts:99 ↗
readonly bytesPerElement: number
byteLengthTotal byte length of the tensor data.
types.ts:109 ↗
readonly byteLength: number
ClassVectorBaseAbstract rank-1 tensor base class.7
types.ts:113 ↗
abstract class VectorBase extends TensorBase
length
types.ts:115 ↗
length: number
rank
types.ts:122 ↗
readonly rank: 1
buffer
types.ts:85 ↗

Inherited from TensorBase

buffer: ArrayBuffer
byteOffset
types.ts:86 ↗

Inherited from TensorBase

byteOffset: number
dtype
types.ts:87 ↗

Inherited from TensorBase

readonly dtype: DType
bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.
types.ts:99 ↗

Inherited from TensorBase

readonly bytesPerElement: number
byteLengthTotal byte length of the tensor data.
types.ts:109 ↗

Inherited from TensorBase

readonly byteLength: number
ClassVectorViewNon-owning rank-1 tensor view (like std::span<T>).10
types.ts:129 ↗
class VectorView extends VectorBase

Zero-copy wrapper for existing memory. Ideal for cross-module WASM interop where data already lives on the WASM heap.

VectorView
types.ts:130 ↗
new VectorView(
    buffer: ArrayBuffer,
    byteOffset: number,
    length: number,
    dtype: DType,
)
fromCreate a VectorView from any TypedArray, inferring or accepting dtype.
types.ts:143 ↗
static from(arr: TypedArray, dtype?: DType): VectorView
to*× 3
types.ts:134 ↗
toString(): string
length
types.ts:115 ↗

Inherited from VectorBase

length: number
rank
types.ts:122 ↗

Inherited from VectorBase

readonly rank: 1
buffer
types.ts:85 ↗

Inherited from TensorBase

buffer: ArrayBuffer
byteOffset
types.ts:86 ↗

Inherited from TensorBase

byteOffset: number
dtype
types.ts:87 ↗

Inherited from TensorBase

readonly dtype: DType
bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.
types.ts:99 ↗

Inherited from TensorBase

readonly bytesPerElement: number
byteLengthTotal byte length of the tensor data.
types.ts:109 ↗

Inherited from TensorBase

readonly byteLength: number
ClassVectorOwning rank-1 tensor (like std::vector<T>).15
types.ts:153 ↗
class Vector extends VectorBase

Allocates its own ArrayBuffer. Use for storing results or when you need independent ownership of the data.

Vector
types.ts:154 ↗
new Vector(length: number, dtype: DType)
new Vector(buffer: ArrayBuffer, length: number, dtype: DType)
fromTypedArrayCreate an owning Vector by copying data from a TypedArray.
types.ts:182 ↗
static fromTypedArray(arr: TypedArray, dtype?: DType): Vector
fromViewCreate an owning Vector by copying data from any TensorBase.
types.ts:188 ↗
static fromView(view: TensorBase): Vector
toTypedArrayReturn a TypedArray view over this Vector's owned buffer, zero-copy.
types.ts:193 ↗
toTypedArray(): TypedArray
tryResizeResize within capacity without moving storage — toTypedArray() views stay valid.
types.ts:214 ↗
tryResize(length: number): boolean

Returns

false, length unchanged, if length is negative or exceeds capacity().
reserveGrow the allocated capacity to at least elements, reallocating and copying the live elements.
types.ts:223 ↗
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.
types.ts:233 ↗
clear(): void
capacityAllocated element capacity (>= length) — the ceiling tryResize honors.
types.ts:206 ↗
readonly capacity: number
length
types.ts:115 ↗

Inherited from VectorBase

length: number
rank
types.ts:122 ↗

Inherited from VectorBase

readonly rank: 1
buffer
types.ts:85 ↗

Inherited from TensorBase

buffer: ArrayBuffer
byteOffset
types.ts:86 ↗

Inherited from TensorBase

byteOffset: number
dtype
types.ts:87 ↗

Inherited from TensorBase

readonly dtype: DType
bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.
types.ts:99 ↗

Inherited from TensorBase

readonly bytesPerElement: number
byteLengthTotal byte length of the tensor data.
types.ts:109 ↗

Inherited from TensorBase

readonly byteLength: number
ClassMatrixBaseAbstract rank-2 tensor base class.11
types.ts:242 ↗
abstract class MatrixBase extends TensorBase

All 4 dimension fields are embedded — no dynamic allocation. Strides are in bytes to match the C API directly.

rows
types.ts:244 ↗
rows: number
columns
types.ts:245 ↗
columns: number
rowStride
types.ts:246 ↗
rowStride: number
colStride
types.ts:247 ↗
colStride: number
length
types.ts:260 ↗
readonly length: number
rank
types.ts:261 ↗
readonly rank: 2
buffer
types.ts:85 ↗

Inherited from TensorBase

buffer: ArrayBuffer
byteOffset
types.ts:86 ↗

Inherited from TensorBase

byteOffset: number
dtype
types.ts:87 ↗

Inherited from TensorBase

readonly dtype: DType
bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.
types.ts:99 ↗

Inherited from TensorBase

readonly bytesPerElement: number
byteLengthTotal byte length of the tensor data.
types.ts:109 ↗

Inherited from TensorBase

readonly byteLength: number
ClassMatrixOwning rank-2 tensor, row-major and C-contiguous by default.19
types.ts:268 ↗
class Matrix extends MatrixBase

Strides are byte strides. Default for C-contiguous layout: rowStride = columns * bytesPerElement, colStride = bytesPerElement.

Matrix
types.ts:269 ↗
new Matrix(rows: number, columns: number, dtype: DType)
new Matrix(
    buffer: ArrayBuffer,
    byteOffset: number,
    dtype: DType,
    rows: number,
    columns: number,
    rowStride?: number,
    colStride?: number,
)
fromTypedArray
types.ts:313 ↗
static fromTypedArray(
    array: TypedArray,
    rows: number,
    columns: number,
    dtype?: DType,
): Matrix
toTypedArray
types.ts:319 ↗
toTypedArray(): TypedArray
row
types.ts:331 ↗
row(index: number): VectorView
tryResizeResize within capacity without moving storage (row()/toTypedArray() views stay valid), re-deriving C-contiguous strides.
types.ts:346 ↗
tryResize(rows: number, columns: number): boolean

Returns

false, unchanged, if either extent is negative or rows*columns exceeds capacity().
reserveGrow the allocated capacity to at least elements, reallocating and copying the live rows*columns elements.
types.ts:358 ↗
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.
types.ts:368 ↗
clear(): void
capacityAllocated element capacity (>= rows*columns) — the ceiling tryResize honors.
types.ts:336 ↗
readonly capacity: number
rows
types.ts:244 ↗

Inherited from MatrixBase

rows: number
columns
types.ts:245 ↗

Inherited from MatrixBase

columns: number
rowStride
types.ts:246 ↗

Inherited from MatrixBase

rowStride: number
colStride
types.ts:247 ↗

Inherited from MatrixBase

colStride: number
length
types.ts:260 ↗

Inherited from MatrixBase

readonly length: number
rank
types.ts:261 ↗

Inherited from MatrixBase

readonly rank: 2
buffer
types.ts:85 ↗

Inherited from TensorBase

buffer: ArrayBuffer
byteOffset
types.ts:86 ↗

Inherited from TensorBase

byteOffset: number
dtype
types.ts:87 ↗

Inherited from TensorBase

readonly dtype: DType
bytesPerElementBytes per element for this tensor's dtype; the switch compiles to a jump table.
types.ts:99 ↗

Inherited from TensorBase

readonly bytesPerElement: number
byteLengthTotal byte length of the tensor data.
types.ts:109 ↗

Inherited from TensorBase

readonly byteLength: number