Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data.Tensor

Dense multi-dimensional tensors over a flat FloatBuf.

A Tensor is a flat buffer of f64 words plus three lists, one entry per axis: the axis shape (its extent), its strides (the flat-offset step for a unit step along that axis), and its axes (a name). Reading t[i, j] is a strided lookup: offset = sum_k idx[k] * strides[k], then a single tbuf_get. Because the layout lives entirely in the stride list, transposing by name is a permutation of the three lists with no data movement, and the underlying buffer is shared until a write forces a copy (the buffer’s own rc==1 in-place / shared-copy discipline).

The surface covers construction, indexing, transpose-by-name, and a contiguity-checked reshape. Elementwise math, broadcasting, and matmul are deliberately absent; when they land, reduction order will be the source loop order (deterministic by construction).

t[i, j] and t[i, j] := v are surface sugar for at_tensor / tensor_set.

Types

Tensor

type Tensor = MkTensor(FloatBuf, List(Int), List(Int), List(String))

A dense tensor: flat storage plus per-axis shape, strides, and names.

Functions and Values

buf

buf : (Data.Tensor.Tensor) -> FloatBuf

The flat backing buffer.

shape

shape : (Data.Tensor.Tensor) -> List(Int)

The extent of each axis, outermost first.

strides

strides : (Data.Tensor.Tensor) -> List(Int)

The flat-offset stride of each axis.

axes

axes : (Data.Tensor.Tensor) -> List(String)

The name of each axis.

rank

rank : (Data.Tensor.Tensor) -> Int

The number of axes.

size

size : (Data.Tensor.Tensor) -> Int

The total number of elements: the product of the shape.

new

new : (List(Int), Float) -> Data.Tensor.Tensor

A tensor of the given shape with every element set to fill, row-major.

from_list

from_list : (List(Int), List(Float)) -> Data.Tensor.Tensor

A row-major tensor of the given shape filled from a flat list of values. Extra list elements past the shape’s size are ignored; missing ones stay 0.

at_tensor

at_tensor : (Data.Tensor.Tensor, List(Int)) -> Float ! {Fail}

The element at a multi-index, or fail() if the offset is out of range. Backs t[i, j].

tensor_set

tensor_set : (Data.Tensor.Tensor, List(Int), Float) -> Data.Tensor.Tensor ! {Fail}

A tensor equal to t but with the element at a multi-index set to v, or fail() if out of range. Backs t[i, j] := v.

transpose

transpose : (Data.Tensor.Tensor, String, String) -> Data.Tensor.Tensor ! {Fail}

Transpose two named axes: a permutation of the shape, strides, and names with no data movement (the buffer is shared). Reading a transposed tensor walks the same buffer in the permuted stride order.

reshape

reshape : (Data.Tensor.Tensor, List(Int)) -> Data.Tensor.Tensor ! {Fail}

Reinterpret the elements under a new shape of the same size. Requires the tensor to be contiguous (row-major strides); a transposed view must be copied first, so reshaping one is a fail(). The new axes get default names.