Skip to main content

The NDArray

Every value in numpy-ts is an NDArray — a multidimensional, homogeneously-typed array backed by a JavaScript TypedArray. If you have used NumPy in Python, you already know the mental model: shape, dtype, strides, and element-wise operations.

Creating arrays from JavaScript data

Use array() to create an NDArray from nested JavaScript arrays or a flat list:
You can also specify a dtype explicitly:
When no dtype is provided, numpy-ts defaults to float64, matching NumPy’s behavior.

NDArray properties

Every NDArray exposes the same set of descriptive properties you would find in NumPy:

The T property — transposing

The T property returns a transposed view of the array. No data is copied; the view shares the same underlying buffer:
Because T returns a view, modifying the transpose also modifies the original:

Flags and views

The flags property tells you about the memory layout:
The base property indicates whether an array is a view of another array:

Inspecting arrays

toString()

Returns a summary string with shape and dtype:

array2string()

For NumPy-style formatted output, use the array2string function:

array_repr() and array_str()

Converting back to JavaScript

toArray()

Converts the NDArray into a nested JavaScript array matching the shape:

tolist()

Alias for toArray(). Provided for NumPy API compatibility:

item()

Extract a single scalar from a size-1 array:
You can also pass a flat index or multi-dimensional indices:

Accessing the raw typed array

The data property gives you direct access to the underlying TypedArray:
Modifying data directly bypasses numpy-ts and can lead to unexpected behavior, especially with non-contiguous views. Use get() / set() or operations instead.

Iteration

NDArrays implement the JavaScript iterator protocol. For 1-D arrays, iteration yields elements. For N-D arrays, it yields (N-1)-D sub-arrays along the first axis:

Method chaining

The full entry point (numpy-ts) returns NDArray objects that support method chaining, letting you write expressive pipelines:
If bundle size matters more than method chaining, use numpy-ts/core instead. See the Installation guide for details.

Next steps

Data Types

Understand the 13 supported dtypes and type promotion rules.

Slicing & Indexing

Select sub-arrays, rows, columns, and individual elements.