Tensor Class
Multi-dimensional array (tensor) with typed storage.Core data structure for numerical computing. Supports N-dimensional arrays with any shape, multiple data types (float32, float64, int32, etc.), memory-efficient strided views, and device abstraction (CPU, WebGPU, WASM).
Properties
The dimensions of the tensor as a readonly array of numbers.
Data type of tensor elements. One of:
'float32', 'float64', 'int32', 'int64', 'uint8', 'bool', 'string'.Device where the tensor resides (
'cpu', 'webgpu', 'wasm').Total number of elements in the tensor.
Number of dimensions (rank) of the tensor.
Underlying data buffer. TypedArray for numeric types, string array for string dtype.
Memory strides for each dimension. Determines step size in the buffer for each axis.
Offset into the underlying data buffer.
Methods
Reshape the tensor to a new shape without copying data.Parameters:
newShape(Shape) - The desired shape for the tensor
Flatten the tensor to a 1-dimensional array.Returns: 1D tensor with shape
[size]Slice the tensor along one or more axes.Parameters:
...ranges(SliceRange[]) - Per-axis slice specifications (number, or{start?, end?, step?})
Get element at the specified indices.Parameters:
...indices(number[]) - Index for each dimension
Convert tensor to nested JavaScript arrays.Returns: Nested array representation
Return a human-readable string representation.Parameters:
maxElements(number) - Maximum elements to display per dimension (default: 6)
Creation Functions
Create a tensor from nested arrays or TypedArray.Primary function for creating tensors. Accepts nested JavaScript arrays, TypedArrays, or scalars.Parameters:
data(NestedArray | TypedArray) - Input dataopts(TensorCreateOptions) - Optional{ dtype?, device? }
Create a tensor filled with zeros.Parameters:
shape(Shape) - Tensor dimensionsopts(TensorCreateOptions) - Optional{ dtype?, device? }
Create a tensor filled with ones.Parameters:
shape(Shape) - Tensor dimensionsopts(TensorCreateOptions) - Optional{ dtype?, device? }
Create a tensor filled with a scalar value.Parameters:
shape(Shape) - Tensor dimensionsvalue(number | string) - Fill valueopts(TensorCreateOptions) - Optional{ dtype?, device? }
Create an uninitialized tensor (values are undefined).Parameters:
shape(Shape) - Tensor dimensionsopts(TensorCreateOptions) - Optional{ dtype?, device? }
Create a tensor with evenly spaced values within a given interval.Parameters:
start(number) - Start value (or stop if only one arg)stop(number) - Optional stop valuestep(number) - Step size (default: 1)opts(TensorCreateOptions) - Optional{ dtype?, device? }
Create evenly spaced numbers over a specified interval.Returns
num evenly spaced samples, calculated over the interval [start, stop].Parameters:start(number) - Starting valuestop(number) - End valuenum(number) - Number of samples (default: 50)endpoint(boolean) - If true, stop is the last sample (default: true)opts(TensorCreateOptions) - Optional{ dtype?, device? }
Create numbers spaced evenly on a log scale.In linear space, the sequence starts at
base^start and ends with base^stop.Parameters:start(number) -base^startis the starting valuestop(number) -base^stopis the final valuenum(number) - Number of samples (default: 50)base(number) - Base of the log space (default: 10)endpoint(boolean) - If true, stop is the last sample (default: true)opts(TensorCreateOptions) - Optional{ dtype?, device? }
Create numbers spaced evenly on a log scale (geometric progression).Each output value is a constant multiple of the previous.Parameters:
start(number) - Starting valuestop(number) - Final valuenum(number) - Number of samples (default: 50)endpoint(boolean) - If true, stop is the last sample (default: true)opts(TensorCreateOptions) - Optional{ dtype?, device? }
Create a 2D identity matrix.Returns a 2D tensor with ones on the diagonal and zeros elsewhere.Parameters:
n(number) - Number of rowsm(number) - Number of columns (default: n, making it square)k(number) - Index of the diagonal (default: 0, main diagonal)opts(TensorCreateOptions) - Optional{ dtype?, device? }
Create a tensor filled with random samples from a standard normal distribution.Uses Box-Muller transform to generate N(0, 1) distributed values.Parameters:
shape(Shape) - Tensor dimensionsopts(TensorCreateOptions) - Optional{ dtype?, device? }
Shape Manipulation
Reshape a tensor to a new shape without copying data.Parameters:
t(Tensor) - Input tensornewShape(Shape) - The desired shape (can use -1 for one inferred dimension)
Flatten a tensor to 1D.Parameters:
t(Tensor) - Input tensor
Transpose tensor dimensions.Reverses or permutes the axes of a tensor.Parameters:
t(Tensor) - Input tensoraxes(readonly number[]) - Optional permutation of axes. If undefined, reverses all axes
Expand tensor dimensions by inserting a new axis.Parameters:
t(Tensor) - Input tensoraxis(Axis) - Position where new axis is placed
Remove dimensions of size 1 from tensor shape.Parameters:
t(Tensor) - Input tensoraxis(Axis | readonly Axis[]) - Optional specific axis/axes to squeeze
Alias for
expandDims.Indexing
Slice a tensor along one or more axes.Parameters:
t(Tensor) - Input tensor...ranges(SliceRange[]) - Per-axis slice specifications
Gather values along an axis using indices.Parameters:
t(Tensor) - Input tensorindices(Tensor) - Index tensoraxis(Axis) - Axis along which to gather
Types
Options for tensor creation.
Recursive type for nested number arrays.
Slice range specification.
Union type representing either a Tensor or GradTensor.