Skip to main content

NDArray Class

numpy-ts provides two array classes:
  • NDArray — The full class with 100+ chainable instance methods. Returned by the default numpy-ts entry point.
  • NDArrayCore — The minimal class with only core properties and element access. Returned by the tree-shakeable numpy-ts/core entry point.
NDArray extends NDArrayCore. Every property and core method documented on this page exists on both classes.

Properties

shape

Tuple of array dimensions. A 2D array with 3 rows and 4 columns has shape [3, 4].

ndim

Number of array dimensions (axes). Equivalent to shape.length.

size

Total number of elements in the array. Equal to the product of all shape dimensions.

dtype

Data type of the array elements. One of: 'float64', 'float32', 'complex128', 'complex64', 'int64', 'int32', 'int16', 'int8', 'uint64', 'uint32', 'uint16', 'uint8', 'bool'.

data

The underlying JavaScript TypedArray buffer holding the raw element data. TypedArray is a union of Float64Array | Float32Array | BigInt64Array | Int32Array | Int16Array | Int8Array | BigUint64Array | Uint32Array | Uint16Array | Uint8Array.

strides

Stride for each dimension, measured in elements (not bytes). Used internally to map multi-dimensional indices to flat buffer positions.

offset

Offset into the underlying data buffer, in elements. Nonzero for views created by slicing.
The offset property is accessed via the internal storage object (a.storage.offset). On NDArrayCore it is not directly exposed as a top-level property but can be read from the storage.

base

If this array is a view of another array, base references the original array that owns the data. Returns null if the array owns its own data.

flags

Information about the memory layout and ownership of the array.

T

Transpose of the array. Returns a view with axes reversed. Equivalent to calling transpose() with no arguments.
T is a property (no parentheses needed). It is only available on NDArray, not NDArrayCore.

itemsize

Size of a single array element in bytes.

nbytes

Total number of bytes consumed by the array elements. Equal to size * itemsize.

Core methods

copy

Return a deep copy of the array. The returned array owns its own data and is not a view. Returns: NDArray — A new array with copied data.

astype

Cast the array to a specified data type. Returns: NDArray — Array with the requested dtype.

fill

Fill the array in-place with a scalar value. Returns: void

toArray

Convert the array to a nested JavaScript array. Scalar (0-d) arrays return a single number. Returns: number | number[] | number[][] | ... — Nested JavaScript array matching the shape.

tolist

Return the array as a nested list. Identical to toArray(). Returns: number | number[] | number[][] | ... — Same as toArray().

tobytes

Return the raw bytes of the array data as an ArrayBuffer. If the array is not C-contiguous, a contiguous copy is made first. Returns: ArrayBuffer — Raw byte representation.

get

Get a single element at the given multi-dimensional indices. Supports negative indexing. Returns: number | bigint | Complex — The scalar element value.

set

Set a single element at the given multi-dimensional indices. Supports negative indexing. Returns: void

slice

Slice the array using NumPy-style string syntax. Returns a view (shares data with the original). Returns: NDArray — A view into the original array.

row

Get a single row from a 2D+ array. Convenience method for slice(String(i), ':'). Returns: NDArray — The selected row as a view.

col

Get a single column from a 2D+ array. Convenience method for slice(':', String(j)). Returns: NDArray — The selected column as a view.

rows

Get a range of rows from a 2D+ array. Returns: NDArray — View containing the selected rows.

cols

Get a range of columns from a 2D+ array. Returns: NDArray — View containing the selected columns.

item

Extract a scalar value from the array. With no arguments, works on arrays with exactly one element. With one argument, treats it as a flat index. With multiple arguments, treats them as multi-dimensional indices. Returns: number | bigint | Complex — The scalar value.

Bracket indexing ([])

All NDArray instances support bracket indexing via a transparent Proxy, providing natural JavaScript syntax for element access and slicing.
Multi-dimensional set (arr[i] = value) is supported for ND arrays. Accepted value types:
  • number / bigint: fills all elements of the slice with that scalar.
  • Complex: fills all elements with the complex value.
  • NDArray: copies element-by-element (sizes must match).
  • number[] / nested array: flattened and copied element-by-element (sizes must match).
TypeScript types: arr[i] is typed as NDArray | number | bigint | Complex.Performance: benchmarked at ~66 ns/op for 1D scalar read — no measurable overhead vs direct .iget() on typical workloads.

Static methods

NDArray.fromStorage

Create an NDArray from an internal ArrayStorage object. This is primarily used internally by numpy-ts operations. Returns: NDArray — A new NDArray wrapping the given storage.
fromStorage is an internal API. Prefer np.array(), np.zeros(), and other creation functions for normal usage.