GitHub
deepbox/ndarray

Tensor Creation

Create N-dimensional arrays (tensors) backed by TypedArrays. Tensors are the fundamental data structure in Deepbox — every operation accepts and returns tensors.

Tensor Properties

  • shape: readonly number[] — dimensions of the tensor (e.g., [2, 3] for a 2×3 matrix)
  • dtype: DType — data type of elements (float32, float64, int32, int64, uint8, bool, string)
  • device: Device — compute device ('cpu')
  • data: TypedArray | readonly string[] — raw backing storage
  • strides: readonly number[] — step sizes for each dimension
  • offset: number — starting index into the data buffer
  • size: number — total number of elements
  • ndim: number — number of dimensions (shape.length)
  • toArray(): NestedArray — convert to nested JavaScript arrays
  • at(...indices): number | string | bigint — access a single element by coordinates
tensor
tensor(data: TensorLike, opts?: TensorCreateOptions): Tensor

Create a tensor from a nested array, TypedArray, scalar, or other TensorLike value. Infers shape and dtype from the data. The primary way to create tensors.

Parameters:
data: TensorLike - Nested array, TypedArray, scalar number/boolean/string/bigint
opts.dtype: DType - Override inferred dtype
opts.device: Device - Target device (default: config default)
zeros
zeros(shape: Shape, opts?: TensorCreateOptions): Tensor

Create a tensor filled with zeros. Useful for initializing accumulators, masks, and placeholder tensors.

Parameters:
shape: Shape - Desired output shape
ones
ones(shape: Shape, opts?: TensorCreateOptions): Tensor

Create a tensor filled with ones. Commonly used for bias initialization and identity-like operations.

full
full(shape: Shape, value: number | string | boolean, opts?: TensorCreateOptions): Tensor

Create a tensor filled with a constant value.

empty
empty(shape: Shape, opts?: TensorCreateOptions): Tensor

Create an uninitialized tensor. Contents are undefined — use only when you will immediately write all values.

eye
eye(n: number, m?: number, k?: number, opts?: TensorCreateOptions): Tensor

Create an identity matrix (ones on the diagonal, zeros elsewhere). Supports rectangular matrices and shifted diagonals.

Parameters:
n: number - Number of rows
m: number - Number of columns (default: n)
k: number - Diagonal offset (0=main, positive=above, negative=below)
arange
arange(start: number, stop: number, step?: number, opts?: TensorCreateOptions): Tensor

Create a 1D tensor with evenly spaced values in [start, stop). Similar to Python's range() but returns a Tensor.

Parameters:
start: number - Start value (inclusive)
stop: number - End value (exclusive)
step: number - Step size (default: 1)
linspace
linspace(start: number, stop: number, num: number, opts?: TensorCreateOptions): Tensor

Create a 1D tensor with num evenly spaced values between start and stop (both inclusive). Useful for creating coordinate grids and evaluation points.

Parameters:
num: number - Number of points to generate
logspace
logspace(start: number, stop: number, num: number, opts?: TensorCreateOptions): Tensor

Create a 1D tensor with values logarithmically spaced between 10^start and 10^stop. Useful for logarithmic scales (e.g., learning rate sweeps).

geomspace
geomspace(start: number, stop: number, num: number, opts?: TensorCreateOptions): Tensor

Create a 1D tensor with values geometrically spaced between start and stop. Each value is a constant multiple of the previous.

randn
randn(shape: Shape, opts?: TensorCreateOptions): Tensor

Create a tensor with values from the standard normal distribution (mean=0, std=1). Uses Box-Muller transform.

tensor-creation.ts
import { tensor, zeros, ones, eye, arange, linspace, full, randn } from "deepbox/ndarray";// From nested arrayconst a = tensor([[1, 2, 3], [4, 5, 6]]); // shape: [2, 3], dtype: float32// With explicit dtypeconst b = tensor([1, 2, 3], { dtype: "int32" });// From TypedArrayconst c = tensor(new Float64Array([1.5, 2.5, 3.5]));// Creation functionsconst z = zeros([3, 3]);                // 3×3 zerosconst o = ones([2, 2, 2]);              // 2×2×2 onesconst I = eye(4);                       // 4×4 identityconst f = full([2, 3], 7);              // 2×3 filled with 7const r = arange(0, 10, 2);             // [0, 2, 4, 6, 8]const l = linspace(0, 1, 11);           // [0, 0.1, 0.2, ..., 1.0]const n = randn([100, 50]);             // 100×50 standard normal// Tensor propertiesconsole.log(a.shape);  // [2, 3]console.log(a.dtype);  // 'float32'console.log(a.size);   // 6console.log(a.ndim);   // 2console.log(a.at(0, 1)); // 2