C++ · Tensors and Views

Tensors and Views

#include <numkong/tensor.hpp>#include <numkong/vector.hpp>#include <numkong/random.hpp> 8.0.0-rc.1 · b1f866ab

The native containers are where most integration mistakes happen. They need to be documented explicitly.

  • vector<T, A> owns storage and defaults to aligned_allocator<T, 64>.
  • vector_view<T> is a const strided non-owning view.
  • vector_span<T> is a mutable strided non-owning view.
  • tensor<T, A, R> owns rank-R storage and also defaults to aligned allocation.
  • tensor_view<T> and tensor_span<T> are the view forms.
  • matrix, matrix_view, and matrix_span are rank-2 aliases.

The important layout rules are:

  • Signed strides are supported by the view types.
  • Reversed and sliced views are valid for many elementwise and reduction kernels.
  • reshape and flatten require contiguous layout.
  • Matrix-style kernels care about row contiguity, not just total tensor contiguity.
  • Negative strides are conceptually valid views, but matrix packing and packed matmul workflows are not written around them.

Memory ownership is explicit. vector and tensor deallocate through their allocator. vector_view, tensor_view, matrix_view, and spans never own memory. And heterogenous index types for operator[] enable more interesting access patterns:

#include <numkong/numkong.hpp>

namespace nk = ashvardanian::numkong;
using nk::slice, nk::all, nk::f32_t, nk::tensor, nk::tensor_view;

auto [t, status] = tensor<f32_t>::from({
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
});
assert(nk::succeeded(status) && "allocated");

f32_t scalar_at_2d_coordinate = t[1, -1];
f32_t scalar_at_global_offset = t[5];
assert(scalar_at_2d_coordinate == scalar_at_global_offset && "same value");

tensor_view<f32_t> scalar_as_tensor = t[1, 1, slice]; 
tensor_view<f32_t> second_row = t[1, slice];
tensor_view<f32_t> second_column = t[all, 1, slice];
assert(second_row[1] == second_column[1] && "same value");

You can also use a more traditional syntax with member functions, also leveraging built-in functionality for hardware-accelerated strided reductions and elementwise operations along any axis combination. Similar to NumPy, but statically typed:

tensor_view<f32_t> second_column = t[all, 1, slice]; // strided column view → {2, 5, 8}
auto minimum_index = nk::argmin(second_column);   // index of the minimum in the second column

The view types are conceptually close to std::mdspan from C++23. The main differences are sub-byte element support, signed strides, and the kernel dispatch integration that std::mdspan does not provide. If your codebase already uses std::mdspan, converting at the NumKong call boundary is straightforward:

#include <numkong/numkong.hpp>
#include <mdspan>

namespace nk = ashvardanian::numkong;

// Existing std::mdspan from your codebase
float data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
auto md = std::mdspan<float, std::extents<std::size_t, 3, 3>>(data);

// Wrap into a NumKong matrix_view — data pointer, extents, and strides map directly
auto view = nk::matrix_view<nk::f32_t>(
    reinterpret_cast<nk::f32_t const *>(md.data_handle()),
    md.extent(0), md.extent(1));

// Now use any NumKong kernel on it
auto [dot, status] = nk::dot<nk::f32_t>(view.row(0).as_vector(), view.row(1).as_vector());

§ Iterators and Enumeration

NumKong containers expose random-access iterators for element and row traversal.

  • dim_iterator — random-access iterator over element values, used by vector, vector_view, and vector_span. Supports all standard iterator operations plus index() to retrieve the current position.
  • axis_iterator — random-access iterator over sub-views (rows), used by tensor_view and tensor_span. Also exposes index().
  • enumerate() — free function returning a lightweight view that yields {index, value} pairs from any container with begin()/end()/size().
#include <numkong/numkong.hpp>

namespace nk = ashvardanian::numkong;

auto [v, v_status] = nk::vector<nk::f16_t>::zeros(128);
if (nk::failed(v_status)) return v_status;
for (auto [i, val] : nk::enumerate(v))
    std::printf("[%zu] = %f\n", i, val.to_f32());

// index() on raw iterators
for (auto it = v.begin(); it != v.end(); ++it)
    std::printf("[%zu] = %f\n", it.index(), (*it).to_f32());

Since tensor.hpp includes vector.hpp, enumerate() works on tensor row views too.

Tensors also support range-for over all logical scalar elements, yielding (position, value) pairs. For sub-byte types each dimension is a logical scalar. Use .dims() to iterate values without positions.

for (auto [pos, val] : matrix)          { /* pos is std::array<size_t, R> */ }
for (auto [pos, ref] : matrix.span())   { ref = nk::f32_t{1}; }
for (auto val : matrix.dims())          { /* scalar only, no position */ }

API

Types

axis_iteratorRandom-access iterator over slices along the leading dimension.
tensor.hpp:1123 ↗
template <typename view_type_>
class axis_iterator

For a rank-2 matrix, iterating yields rank-1 row views/spans. Dereference calls parent_.slice_leading(index_) to produce each sub-view.

keep_dims_tControls whether reduction collapses or preserves the reduced axis.
tensor.hpp:2321 ↗
enum keep_dims_t

Values

collapse_dims_kfalse
keep_dims_ktrue
all_tTag type for selecting all elements along a dimension.
vector.hpp:54 ↗
struct all_t
range_tSlicing descriptor: [start, stop) with optional step.
vector.hpp:61 ↗
struct range_t

step must be non-zero. Negative start/stop indices wrap from the end.

Fields

startstd::ptrdiff_t
stopstd::ptrdiff_t
stepstd::ptrdiff_t
dim_iteratorRandom-access iterator over logical dimensions.
vector.hpp:138 ↗
template <typename container_type_>
class dim_iterator

For sub-byte types (i4x2_t, u1x8_t), dereference returns a proxy reference. For normal types, dereference returns a direct reference.

Structscaled_tensorOwning, allocating block-scaled tensor.17
tensor.hpp:2132 ↗
template <typename format_,
          typename element_allocator_ = aligned_allocator<typename format_::element_t>,
          typename scale_allocator_ = aligned_allocator<typename format_::scale_t>,
          std::size_t max_rank_ = 8>
struct scaled_tensor
scaled_tensor
tensor.hpp:2153 ↗
scaled_tensor() noexcept=default;
scaled_tensor(scaled_tensor &&) noexcept=default;
scaled_tensor(scaled_tensor const &)=delete;
operator=
tensor.hpp:2155 ↗
scaled_tensor & operator=(scaled_tensor &&) noexcept=default;
scaled_tensor & operator=(scaled_tensor const &)=delete;
*× 2Contextual-bool: truthy when the tensor owns storage.
tensor.hpp:2222 ↗
constexpr operator bool() const noexcept;

Enables the if(!t) empty-check idiom.

capacityAllocated logical-element capacity — the ceiling coordinated resize() honors.
tensor.hpp:2225 ↗
size_type capacity() const noexcept;
resizeReshape in place to extents whose last dimension is a multiple of block_size, resizing both tensors without reallocating.
tensor.hpp:2235 ↗
status_t resize(size_type const *extents, size_type rank) noexcept;
status_t resize(std::initializer_list<size_type> extents) noexcept;

Statuses

  • unexpected_dimensions_k Leaving both untouched, if the rank is bad, the last extent is not block-aligned, or either component's capacity is exceeded.
reserveGrow capacity to at least values logical elements, plus the matching per-block scales; may reallocate/move the component buffers.
tensor.hpp:2262 ↗
status_t reserve(size_type values) noexcept;

No-op when already large enough.

clearDrop to an empty shape while keeping both component capacities.
tensor.hpp:2268 ↗
void clear() noexcept;
swapSwap contents: both component tensors and the per-tensor scale.
tensor.hpp:2274 ↗
void swap(scaled_tensor & other) noexcept;
elementsImmutable component accessors.
tensor.hpp:2282 ↗
element_tensor_type::view_type elements() const noexcept;
tensor_scalePer-tensor multiplier, NVFP4 only, absent for the MX family.
tensor.hpp:2287 ↗
requires (format_::has_tensor_scale())
f32_t tensor_scale() const noexcept;

cast() derives and writes it on encode through span().tensor_scale_slot().

viewImmutable whole-tensor view.
tensor.hpp:2294 ↗
view_type view() const noexcept;
spanMutable whole-tensor span, writes through to the per-tensor scale.
tensor.hpp:2297 ↗
span_type span() noexcept;
rowLeading-axis row view.
tensor.hpp:2301 ↗
template <std::integral index_type_>
view_type row(index_type_ i) const noexcept;
columnsBlock-aligned last-axis, column, range view.
tensor.hpp:2306 ↗
view_type columns(size_type start, size_type stop) const noexcept;
rows_viewsIterate leading-axis rows as immutable views.
tensor.hpp:2309 ↗
view_type::rows_views_t rows_views() const noexcept;
uninitializedFactory: allocate an uninitialized block-scaled tensor with the given extents.
tensor.hpp:2170 ↗
expected<scaled_tensor> uninitialized(
    size_type const *extents,
    size_type rank,
    element_allocator_ element_alloc = {},
    scale_allocator_ scale_alloc = {}) noexcept;
expected<scaled_tensor> uninitialized(
    std::initializer_list<size_type> extents,
    element_allocator_ element_alloc = {},
    scale_allocator_ scale_alloc = {}) noexcept;

Allocate from an extents list; see the pointer overload for the failure statuses.

Parameters

extentsinLogical extents; the last one must be a multiple of block_size.
rankinNumber of extents.
element_allocinAllocator of the element tensor.
scale_allocinAllocator of the block-scale tensor.

Returns

The tensor.

Statuses

from_componentsAdopt already-built component tensors and a per-tensor scale.
tensor.hpp:2202 ↗
expected<scaled_tensor> from_components(
    element_tensor_type elements,
    scale_tensor_type block_scales,
    f32_t tensor_scale = 1.0f) noexcept;

Returns

The tensor.

Statuses

Structscaled_tensor_spanNon-owning, mutable span of a block-scaled tensor, a write target for cast().6
tensor.hpp:2070 ↗
template <typename format_,
          std::size_t max_rank_>
struct scaled_tensor_span
scaled_tensor_span
tensor.hpp:2087 ↗
constexpr scaled_tensor_span() noexcept=default;
constexpr scaled_tensor_span(
    element_span_type elements,
    scale_span_type block_scales,
    f32_t *tensor_scale = nullptr) noexcept;
*× 6Contextual-bool: truthy when the handle is non-empty.
tensor.hpp:2099 ↗
constexpr operator bool() const noexcept;

Enables the if(!span) empty-check idiom.

Enables the if(!view) empty-check idiom.

Enables the if(!view) empty-check idiom.

Enables the if(!v) empty-check idiom.

elementsMutable span of the packed micro-float elements.
tensor.hpp:2102 ↗
element_span_type elements() const noexcept;
block_scalesMutable span of the per-block scales.
tensor.hpp:2105 ↗
scale_span_type block_scales() const noexcept;
tensor_scale_slotPointer to the per-tensor multiplier slot, NVFP4 only; kernels write it here.
tensor.hpp:2108 ↗
requires (format_::has_tensor_scale())
f32_t * tensor_scale_slot() const noexcept;
viewDecay to an immutable view.
tensor.hpp:2115 ↗
view_type view() const noexcept;
Structscaled_tensor_viewA block-scaled tensor = packed micro-float elements + per-block scales + an optional per-tensor scale, all on the last, quantized, axis.13
tensor.hpp:1954 ↗
template <typename format_,
          std::size_t max_rank_>
struct scaled_tensor_view

Non-owning, immutable view of a block-scaled tensor.

Composes two ordinary tensors rather than inventing a parallel container family:

*  - elements()   — tensor<format::element_t>   of the logical shape, packed sub-byte.
*  - block_scales()   — tensor<format::scale_t>   of the same shape with the last extent
*    divided by block_size  ; one scale per block.
*  - tensor_scale()   — a scalar f32_t   multiplier, NVFP4 only; the call is removed by a
*    requires   clause for the MX family, which has no per-tensor scale.
*  

Because the scales are a real reduced-shape tensor, slicing reuses the tensor machinery and slices both components in lockstep; a last-axis, column, range is divided by block_size. Encode / decode / transcode all go through cast() in cast.hpp; scaled_tensor itself is pure storage and never calls a kernel. The struct-of-arrays layout is exactly the NVFP4/MXFP8 GPU representation — elements() and block_scales() each export a DLPack / CUDA-array interface for zero-copy access.

Shape convention: elements() reports the logical shape [rows,columns] because its value type is the sub-byte-aware packed scalar, e.g. e2m1x2_t, so extent(last) is the element count even though storage is packed. Array-protocol bindings without a sub-byte scalar — the Python ScaledTensor.elements ndarray, Rust's packed slice — instead expose the packed byte shape [rows,columns] with the column extent scaled by element_bits/8, same bytes, reported per their type system.

scaled_tensor_view
tensor.hpp:1971 ↗
constexpr scaled_tensor_view() noexcept=default;
constexpr scaled_tensor_view(
    element_view_type elements,
    scale_view_type block_scales,
    f32_t tensor_scale = 1.0f) noexcept;
*× 4Number of dimensions.
tensor.hpp:1977 ↗
size_type rank() const noexcept;
extentLogical extent along dimension i.
tensor.hpp:1980 ↗
size_type extent(size_type i) const noexcept;
numelTotal number of logical elements.
tensor.hpp:1983 ↗
size_type numel() const noexcept;
*× 4True if empty.
tensor.hpp:1986 ↗
bool empty() const noexcept;
elementsImmutable view of the packed micro-float elements, DLPack/CAI-exportable.
tensor.hpp:1993 ↗
element_view_type elements() const noexcept;
block_scalesImmutable view of the per-block scales, one per block_size on the last axis.
tensor.hpp:1996 ↗
scale_view_type block_scales() const noexcept;
tensor_scalePer-tensor multiplier, NVFP4 only; the call is absent for the MX family.
tensor.hpp:1999 ↗
requires (format_::has_tensor_scale())
f32_t tensor_scale() const noexcept;
slice_leadingSlice along the leading axis, rank-reducing; shares the per-tensor scale.
tensor.hpp:2007 ↗
template <std::integral index_type_>
scaled_tensor_view slice_leading(index_type_ i) const noexcept;
*× 2Row access, an alias for slice_leading.
tensor.hpp:2013 ↗
template <std::integral index_type_>
scaled_tensor_view row(index_type_ i) const noexcept;
columnsBlock-aligned last-axis (column) range; preserves every leading axis.
tensor.hpp:2024 ↗
scaled_tensor_view columns(
    size_type start,
    size_type stop) const noexcept;

Zero-copy.

start and stop must be multiples of block_size so both the element and the scale offsets stay integral and land on container boundaries, essential for packed sub-byte elements. Returns an empty view otherwise; materialize sub-block ranges with cast() first.

row_iterator_tForward iterator over leading-axis row views.
tensor.hpp:2047 ↗
struct row_iterator_t

Fields

parentscaled_tensor_view
indexsize_type
rows_views_tRange of leading-axis row views, each carries the per-tensor scale.
tensor.hpp:2059 ↗
struct rows_views_t

Fields

parentscaled_tensor_view
StructtensorOwning, N-dimensional tensor: allocated once, reshaped freely within its capacity.51
tensor.hpp:1362 ↗
template <typename value_type_,
          typename allocator_type_ = aligned_allocator<value_type_>,
          std::size_t max_rank_ = 8>
struct tensor

resize() adjusts the shape within the allocated capacity() and fails beyond it, so data() is stable across resizes — the contract capture-replaying GPU code depends on. reserve() is the explicit opt-in that may reallocate to grow capacity(), and thus move data(). Allocate at the worst-case extents, or reserve() up front, then resize() to each step's live extents; clear() drops back to an empty shape while keeping the buffer.

tensor
tensor.hpp:1381 ↗
tensor() noexcept=default;
constexpr tensor(allocator_type_ const & alloc) noexcept;
constexpr tensor(tensor && other) noexcept;
tensor(tensor const &)=delete;
~tensor
tensor.hpp:1385 ↗
~tensor() noexcept;
operator=
tensor.hpp:1393 ↗
tensor & operator=(tensor && other) noexcept;
tensor & operator=(tensor const &)=delete;
capacityAllocated values — the ceiling resize() honors; at least numel().
tensor.hpp:1408 ↗
size_type capacity() const noexcept;
resizeReshape in place to contiguous extents, without reallocating: succeeds iff the new volume fits capacity(), the rank fits max_rank_ and the last extent is a whole number of storage values, so data() never moves — resizing to a step's live extents is safe under captured GPU graphs.
tensor.hpp:1419 ↗
status_t resize(std::initializer_list<size_type> extents) noexcept;
status_t resize(size_type const *extents, size_type rank) noexcept;

Reshape in place from an extents array of rank dims, mirroring the list overload.

Statuses

reserveGrow the allocated capacity() to at least values elements.
tensor.hpp:1441 ↗
status_t reserve(size_type values) noexcept;

Unlike resize(), this may reallocate and move data(); the live numel()-storage elements are preserved. No-op when capacity() >= values.

A failed growth leaves the state unchanged.

clearReset to a logically empty shape, empty() becomes true, while keeping capacity(), so the buffer can be refilled via resize() without reallocating.
tensor.hpp:1459 ↗
void clear() noexcept;

Storage only frees when the tensor is destroyed.

swapSwap contents: data, shape, capacity, and allocator if it propagates on swap.
tensor.hpp:1465 ↗
void swap(tensor & other) noexcept;
extentExtent along dimension i.
tensor.hpp:1634 ↗
size_type extent(size_type i) const noexcept;
stride_bytesStride in bytes along dimension i, signed.
tensor.hpp:1637 ↗
difference_type stride_bytes(size_type i) const noexcept;
*× 3Total number of elements.
tensor.hpp:1640 ↗
size_type numel() const noexcept;
dataTyped pointer to data.
tensor.hpp:1650 ↗
pointer data() noexcept;
value_type const * data() const noexcept;
raw_values_dataThe storage as C kernels take it: raw_t for NumKong's types, else the type itself.
tensor.hpp:1654 ↗
raw_value_type * raw_values_data() noexcept;
raw_value_type const * raw_values_data() const noexcept;
shapeShape storage.
tensor.hpp:1660 ↗
shape_storage_<max_rank_> const & shape() const noexcept;
get_allocatorAllocator.
tensor.hpp:1663 ↗
allocator_type get_allocator() const noexcept;
*× 2Create an immutable view.
tensor.hpp:1666 ↗
view_type view() const noexcept;
*× 2Create a mutable span.
tensor.hpp:1669 ↗
span_type span() noexcept;
rows_viewsIterate rows as immutable views.
tensor.hpp:1686 ↗
rows_views_t rows_views() const noexcept;
rows_spansIterate rows as mutable spans.
tensor.hpp:1689 ↗
rows_spans_t rows_spans() noexcept;
rowsIterate rows as immutable views, a convenience alias for rows_views.
tensor.hpp:1692 ↗
view_type::rows_views_t rows() const noexcept;
span_type::rows_spans_t rows() noexcept;

Iterate rows as mutable spans, a convenience alias for rows_spans.

beginConst element iterator, begin.
tensor.hpp:1698 ↗
tensor_view_iterator_<view_type> begin() const noexcept;
tensor_span_iterator_<span_type> begin() noexcept;

Mutable element iterator, begin.

endConst element iterator, end.
tensor.hpp:1701 ↗
tensor_view_iterator_<view_type> end() const noexcept;
tensor_span_iterator_<span_type> end() noexcept;

Mutable element iterator, end.

*× 3Number of logical scalar elements.
tensor.hpp:1710 ↗
size_type size() const noexcept;
dimsConst dimension-only view.
tensor.hpp:1713 ↗
tensor_dims_view_<tensor_view_iterator_<view_type>> dims() const noexcept;
tensor_dims_view_<tensor_span_iterator_<span_type>> dims() noexcept;

Mutable dimension-only view.

as_matrix_viewReinterpret as a 2D immutable matrix view.
tensor.hpp:1719 ↗
tensor_view<value_type_, 2> as_matrix_view() const noexcept;

Requires rank >= 2.

as_matrix_spanReinterpret as a 2D mutable matrix span.
tensor.hpp:1722 ↗
tensor_span<value_type_, 2> as_matrix_span() noexcept;

Requires rank >= 2.

transposeTranspose: reverse dimension order, immutable view.
tensor.hpp:1725 ↗
view_type transpose() const noexcept;
span_type transpose() noexcept;

Transpose: reverse dimension order, mutable span.

reshapeReshape, immutable view.
tensor.hpp:1733 ↗
template <std::size_t out_rank_ = max_rank_>
tensor_view<value_type_, out_rank_> reshape(
    std::initializer_list<size_type> new_extents) const noexcept;
template <std::size_t out_rank_ = max_rank_>
tensor_span<value_type_, out_rank_> reshape(
    std::initializer_list<size_type> new_extents) noexcept;

Reshape, mutable span.

The output rank may differ from the source; pass it as the template argument when narrowing or widening, given contiguous layout and matching element counts.

is_*_contiguous× 2Check if contiguous in memory.
tensor.hpp:1745 ↗
bool is__contiguous() const noexcept;

Always true for freshly-constructed tensors.

valuesStorage values as a contiguous span, refused like tensor_view::values.
tensor.hpp:1748 ↗
expected<std::span<value_type const>> values() const noexcept;
expected<std::span<value_type>> values() noexcept;

Mutable storage values as a contiguous span, refused like tensor_span::values.

slice_leadingSlice along leading dimension, immutable view.
tensor.hpp:1755 ↗
template <std::integral index_type_>
view_type slice_leading(index_type_ idx) const noexcept;
template <std::integral index_type_>
span_type slice_leading(index_type_ idx) noexcept;

Slice along leading dimension, mutable span.

rowRow access, immutable view, an alias for slice_leading.
tensor.hpp:1767 ↗
template <std::integral index_type_>
view_type row(index_type_ i) const noexcept;
template <std::integral index_type_>
span_type row(index_type_ i) noexcept;

Row access, mutable span, an alias for slice_leading.

operator[]Flat logical scalar access.
tensor.hpp:1779 ↗
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) noexcept;
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) const noexcept;

Const flat logical scalar access.

span_type operator[](tensor_slice_t) noexcept;

Trailing slice returns the same tensor view/span category.

view_type operator[](tensor_slice_t) const noexcept;
operator()Exact multi-dimensional scalar lookup via call syntax, C++20-portable.
tensor.hpp:1792 ↗
template <std::integral... index_types_>
requires (sizeof...(index_types_) >= 2)
decltype(auto) operator()(index_types_... idxs) noexcept;
template <std::integral... index_types_>
requires (sizeof...(index_types_) >= 2)
decltype(auto) operator()(index_types_... idxs) const noexcept;

Const multidimensional lookup via call syntax.

template <typename first_type_, typename second_type_, typename... rest_types_>
requires (trailing_tensor_slice_args_v<first_type_, second_type_, rest_types_...>)
auto operator()(
    first_type_ first,
    second_type_ second,
    rest_types_... rest) noexcept;

Prefix leading-axis slicing via call syntax, C++20-portable, narrowed like the span's.

template <typename first_type_, typename second_type_, typename... rest_types_>
requires (trailing_tensor_slice_args_v<first_type_, second_type_, rest_types_...>)
auto operator()(
    first_type_ first,
    second_type_ second,
    rest_types_... rest) const noexcept;

Const prefix leading-axis slicing via call syntax.

scalar_*_ref× 2Rank-0 mutable scalar access.
tensor.hpp:1852 ↗
decltype(auto) scalar__ref() noexcept;
*× 2Rank-0 const scalar access.
tensor.hpp:1855 ↗
decltype(auto) scalar() const noexcept;
as_vector_viewConvert to vector_view; requires rank == 1.
tensor.hpp:1858 ↗
vector_view<value_type> as_vector_view() const noexcept;
as_vector_spanConvert to vector_span; requires rank == 1.
tensor.hpp:1861 ↗
vector_span<value_type> as_vector_span() noexcept;
flattenFlatten, immutable view.
tensor.hpp:1865 ↗
template <std::size_t out_rank_ = max_rank_>
tensor_view<value_type_, out_rank_> flatten() const noexcept;
template <std::size_t out_rank_ = max_rank_>
tensor_span<value_type_, out_rank_> flatten() noexcept;

Flatten, mutable span.

Requires contiguous layout.

squeezeSqueeze, immutable view.
tensor.hpp:1876 ↗
view_type squeeze() const noexcept;
span_type squeeze() noexcept;

Squeeze, mutable span.

Removes size-1 dimensions.

fill_*_zeros× 3Zero-fill every element.
tensor.hpp:1882 ↗
status_t fill__zeros() noexcept;

memset on the contiguous fast path, one memset per element on the strided slow path.

Statuses

*× 2Fill every element with value.
tensor.hpp:1885 ↗
status_t fill(value_type value) noexcept;
copy_fromCopy from a same-shape view.
tensor.hpp:1888 ↗
status_t copy_from(view_type input) noexcept;
uninitializedFactory: allocate an uninitialized tensor from an extents array of rank dims.
tensor.hpp:1482 ↗
expected<tensor> uninitialized(
    size_type const *extents,
    size_type rank,
    allocator_type_ alloc = {}) noexcept;
expected<tensor> uninitialized(
    std::initializer_list<size_type> extents,
    allocator_type_ alloc = {}) noexcept;

Factory: allocate an uninitialized tensor.

Returns

The tensor, empty for a zero volume.

Warnings

Contents are uninitialized. Caller must fill before reading.

Statuses

zerosFactory: zero-initialized tensor from an extents array; fails like uninitialized.
tensor.hpp:1499 ↗
expected<tensor> zeros(
    size_type const *extents,
    size_type rank,
    allocator_type_ alloc = {}) noexcept;
expected<tensor> zeros(
    std::initializer_list<size_type> extents,
    allocator_type_ alloc = {}) noexcept;

Factory: allocate a zero-initialized tensor with the given extents.

Statuses

fullFactory: tensor filled with val from an extents array; fails like uninitialized.
tensor.hpp:1513 ↗
expected<tensor> full(
    size_type const *extents,
    size_type rank,
    value_type_ val,
    allocator_type_ alloc = {}) noexcept;
expected<tensor> full(
    std::initializer_list<size_type> extents,
    value_type_ val,
    allocator_type_ alloc = {}) noexcept;

Factory: allocate a tensor filled with a given value.

Statuses

onesFactory: allocate a tensor filled with ones.
tensor.hpp:1543 ↗
expected<tensor> ones(
    std::initializer_list<size_type> extents,
    allocator_type_ alloc = {}) noexcept;

Parameters

extentsinExtents, one per dimension, e.g. {3, 4}.
allocinAllocator instance.

Returns

The tensor.

Statuses

fromFactory: create a rank-1 tensor from an initializer list of values.
tensor.hpp:1585 ↗
expected<tensor> from(
    std::initializer_list<value_type_> values,
    allocator_type_ alloc = {}) noexcept;
requires (max_rank_ >= 2)
expected<tensor> from(
    std::initializer_list<std::initializer_list<value_type_>> rows,
    allocator_type_ alloc = {}) noexcept;

Factory: create a rank-2 tensor from a nested initializer list.

Parameters

valuesinValues to fill the tensor with.
allocinAllocator instance.
rowsinEach inner list is a row. All rows must have the same length.

Returns

The tensor.

Statuses

from_rawFactory: adopt raw memory, whose shape must end in a whole number of storage values.
tensor.hpp:1620 ↗
tensor from_raw(
    pointer ptr,
    shape_storage_<max_rank_> const & shape,
    allocator_type_ alloc = {}) noexcept;
rows_views_tRange of immutable row views, slicing along the leading dimension.
tensor.hpp:1672 ↗
struct rows_views_t

Fields

parentview_type
rows_spans_tRange of mutable row spans, slicing along the leading dimension.
tensor.hpp:1679 ↗
struct rows_spans_t

Fields

parentspan_type
Structtensor_spanNon-owning, mutable, N-dimensional view.26
tensor.hpp:568 ↗
template <typename value_type_,
          std::size_t max_rank_ = 8>
struct tensor_span
tensor_spanConvenience constructor for rank-2 spans from typed pointer, rows, and columns.
tensor.hpp:582 ↗
constexpr tensor_span() noexcept=default;
constexpr tensor_span(
    char *data,
    shape_storage_<max_rank_> const & shape) noexcept;
requires (max_rank_ >= 2)
constexpr tensor_span(
    value_type *data,
    size_type rows,
    size_type columns) noexcept;

Convenience constructor for rank-2 spans from typed pointer, rows, and columns.

requires (max_rank_ >= 1)
constexpr tensor_span(value_type *data, size_type count) noexcept;

Convenience constructor for a rank-1 span from a typed pointer and element count.

template <std::size_t span_extent_>
requires (max_rank_ >= 1)
constexpr tensor_span(
    std::span<value_type, span_extent_> values) noexcept;

Spans contiguous storage values as a rank-1 tensor of every logical dimension they hold, the inverse of values.

constexpr tensor_span(
    value_type *data,
    std::initializer_list<size_type> extents) noexcept;

Convenience constructor from a typed pointer and an explicit extents list, where rank equals the list size.

template <std::size_t array_rank_>
requires (array_rank_ <= max_rank_)
constexpr tensor_span(
    value_type *data,
    std::array<size_type, array_rank_> const & extents) noexcept;

Convenience constructor from a typed pointer and a fixed-size extents array, where rank equals array_rank_.

An out-of-range rank, empty or greater than max_rank_, yields an empty handle rather than overflowing the fixed-capacity shape storage.

*× 2Extent along the i-th dimension.
tensor.hpp:631 ↗
size_type extent(size_type i) const noexcept;
stride_*_bytes× 2Stride in bytes along the i-th dimension, signed.
tensor.hpp:634 ↗
difference_type stride__bytes(size_type i) const noexcept;
byte_*_data× 3Raw byte pointer.
tensor.hpp:647 ↗
char * byte__data() const noexcept;
*× 2Typed pointer, assumes data is contiguous from this pointer.
tensor.hpp:650 ↗
value_type * data() const noexcept;
raw_values_*_values_data× 4The storage as C kernels take it: raw_t for NumKong's types, else the type itself.
tensor.hpp:653 ↗
raw_value_type * raw_values__values_data() const noexcept;
*× 2Access the shape storage.
tensor.hpp:656 ↗
shape_storage_<max_rank_> const & shape() const noexcept;
slice_leadingSlice along leading dimension.
tensor.hpp:665 ↗
template <std::integral index_type_>
tensor_span slice_leading(index_type_ idx) const noexcept;
rowMutable row access, an alias for slice_leading.
tensor.hpp:683 ↗
template <std::integral index_type_>
tensor_span row(index_type_ i) const noexcept;
operator[]Flat logical scalar access — constexpr like the other accessors, so it stays device-callable.
tensor.hpp:690 ↗
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) const noexcept;
tensor_span operator[](tensor_slice_t) const noexcept;

Trailing slice returns the same span.

operator()Exact multi-dimensional scalar lookup via call syntax, C++20-portable.
tensor.hpp:697 ↗
template <std::integral... index_types_>
requires (sizeof...(index_types_) >= 2)
decltype(auto) operator()(index_types_... idxs) const noexcept;
template <typename first_type_, typename second_type_, typename... rest_types_>
requires (trailing_tensor_slice_args_v<first_type_, second_type_, rest_types_...>)
auto operator()(
    first_type_ first,
    second_type_ second,
    rest_types_... rest) const noexcept;

Prefix leading-axis slicing via call syntax, C++20-portable.

Every integral index drops one static rank.

with_max_rankThe same span under the static rank out_rank_, which must hold its runtime rank; an empty span otherwise.
tensor.hpp:739 ↗
template <std::size_t out_rank_>
tensor_span<value_type_, out_rank_> with_max_rank() const noexcept;
as_vectorConvert to vector_span; requires rank == 1.
tensor.hpp:768 ↗
vector_span<value_type> as_vector() const noexcept;
as_*_matrix× 2Narrow an already-2D handle to a rank-2 type.
tensor.hpp:776 ↗
tensor_span<value_type_, 2> as__matrix() const noexcept;

Requires rank == 2; use reshape<2>({...}) to collapse a higher-rank tensor.

valuesStorage values as a contiguous span; unexpected_dimensions_k for a strided span or one whose last extent ends mid-value.
tensor.hpp:793 ↗
expected<std::span<value_type>> values() const noexcept;
*× 2Transpose: reverse the order of all dimensions, swapping extents and strides.
tensor.hpp:801 ↗
tensor_span transpose() const noexcept;
*× 2Reshape to new extents, requires contiguous layout and matching element count.
tensor.hpp:819 ↗
template <std::size_t out_rank_ = max_rank_>
tensor_span<value_type_, out_rank_> reshape(
    std::initializer_list<size_type> new_extents) const noexcept;

The output rank may differ from the source; pass it as the template argument when narrowing or widening. Returns an empty span if not contiguous, if element counts don't match, or if the last extent is not a whole number of storage values.

The output rank may differ from the source; pass it as the template argument when narrowing or widening. Returns an empty view when not contiguous, when element counts disagree, or when the last extent is not a whole number of storage values.

beginMutable element iterator, begin: yields (position,ref_or_proxy) pairs.
tensor.hpp:841 ↗
tensor_span_iterator_<tensor_span> begin() const noexcept;
endMutable element iterator, end.
tensor.hpp:844 ↗
tensor_span_iterator_<tensor_span> end() const noexcept;
dimsMutable dimension-only view.
tensor.hpp:852 ↗
tensor_dims_view_<tensor_span_iterator_<tensor_span>> dims() const noexcept;
flattenFlatten to 1D span, requires contiguous layout.
tensor.hpp:858 ↗
template <std::size_t out_rank_ = max_rank_>
tensor_span<value_type_, out_rank_> flatten() const noexcept;

Returns empty span if not contiguous.

fill_zerosZero-fill every element; declared here, defined after the free fill_zeros.
tensor.hpp:863 ↗
status_t fill_zeros() noexcept;
fillFill every element with value; declared here, defined after the free fill.
tensor.hpp:866 ↗
status_t fill(value_type value) noexcept;
copy_fromCopy from a same-shape view; declared here, defined after the free copy.
tensor.hpp:869 ↗
status_t copy_from(tensor_view<value_type_, max_rank_> input) noexcept;
*× 2Remove dimensions of size 1.
tensor.hpp:872 ↗
tensor_span squeeze() const noexcept;
rows_spans_tRange of mutable sub-spans along the leading dimension.
tensor.hpp:830 ↗
struct rows_spans_t

Fields

parenttensor_span
Structtensor_viewNon-owning, immutable, N-dimensional view.14
tensor.hpp:255 ↗
template <typename value_type_,
          std::size_t max_rank_ = 8>
struct tensor_view
tensor_viewConvenience constructor for rank-2 views from typed pointer, rows, and columns.
tensor.hpp:266 ↗
constexpr tensor_view() noexcept=default;
constexpr tensor_view(
    char const *data,
    shape_storage_<max_rank_> const & shape) noexcept;
requires (max_rank_ >= 2)
constexpr tensor_view(
    value_type const *data,
    size_type rows,
    size_type columns) noexcept;

Convenience constructor for rank-2 views from typed pointer, rows, and columns.

requires (max_rank_ >= 1)
constexpr tensor_view(value_type const *data, size_type count) noexcept;

Convenience constructor for a rank-1 view from a typed pointer and element count.

template <typename span_value_type_, std::size_t span_extent_>
requires (max_rank_ >= 1 && std::is_same_v<std::remove_const_t<span_value_type_>, value_type>)
constexpr tensor_view(
    std::span<span_value_type_, span_extent_> values) noexcept;

Views contiguous storage values as a rank-1 tensor of every logical dimension they hold, the inverse of values.

constexpr tensor_view(
    value_type const *data,
    std::initializer_list<size_type> extents) noexcept;

Convenience constructor from a typed pointer and an explicit extents list, where rank equals the list size.

template <std::size_t array_rank_>
requires (array_rank_ <= max_rank_)
constexpr tensor_view(
    value_type const *data,
    std::array<size_type, array_rank_> const & extents) noexcept;

Convenience constructor from a typed pointer and a fixed-size extents array, where rank equals array_rank_.

An out-of-range rank, empty or greater than max_rank_, yields an empty handle rather than overflowing the fixed-capacity shape storage.

slice_leadingSlice along the leading dimension.
tensor.hpp:347 ↗
template <std::integral index_type_>
tensor_view<value_type_, max_rank_> slice_leading(
    index_type_ idx) const noexcept;
operator[]Rank-0 scalar access — constexpr like the accessors, so it stays device-callable.
tensor.hpp:371 ↗
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) const noexcept;
tensor_view operator[](tensor_slice_t) const noexcept;

Trailing slice returns the same view.

operator()Exact multi-dimensional scalar lookup via call syntax, C++20-portable.
tensor.hpp:378 ↗
template <std::integral... index_types_>
requires (sizeof...(index_types_) >= 2)
decltype(auto) operator()(index_types_... idxs) const noexcept;
template <typename first_type_, typename second_type_, typename... rest_types_>
requires (trailing_tensor_slice_args_v<first_type_, second_type_, rest_types_...>)
auto operator()(
    first_type_ first,
    second_type_ second,
    rest_types_... rest) const noexcept;

Prefix leading-axis slicing with a trailing slice marker, call syntax, C++20-portable.

Every integral index drops one static rank.

with_max_rankThe same view under the static rank out_rank_, which must hold its runtime rank; an empty view otherwise.
tensor.hpp:420 ↗
template <std::size_t out_rank_>
tensor_view<value_type_, out_rank_> with_max_rank() const noexcept;
scalarRank-0 scalar access.
tensor.hpp:437 ↗
decltype(auto) scalar() const noexcept;
as_vectorConvert to vector_view; requires rank == 1.
tensor.hpp:444 ↗
vector_view<value_type> as_vector() const noexcept;
is_contiguousCheck if the tensor is contiguous in memory.
tensor.hpp:465 ↗
bool is_contiguous() const noexcept;
valuesStorage values as a contiguous span; unexpected_dimensions_k for a strided view or one whose last extent ends mid-value.
tensor.hpp:469 ↗
expected<std::span<value_type const>> values() const noexcept;
beginElement iterator, begin: yields (position,scalar) pairs.
tensor.hpp:517 ↗
tensor_view_iterator_<tensor_view> begin() const noexcept;
endElement iterator, end.
tensor.hpp:520 ↗
tensor_view_iterator_<tensor_view> end() const noexcept;
dimsDimension-only view: iterate scalars without positions.
tensor.hpp:526 ↗
tensor_dims_view_<tensor_view_iterator_<tensor_view>> dims() const noexcept;
flattenFlatten to 1D view, requires contiguous layout.
tensor.hpp:532 ↗
template <std::size_t out_rank_ = max_rank_>
tensor_view<value_type_, out_rank_> flatten() const noexcept;

Returns empty view if not contiguous.

rows_views_tRange of sub-views along the leading dimension.
tensor.hpp:506 ↗
struct rows_views_t

Fields

parenttensor_view
StructvectorOwning, resizable-within-capacity, SIMD-aligned vector.25
vector.hpp:625 ↗
template <typename value_type_,
          typename allocator_type_ = aligned_allocator<value_type_>>
struct vector

Use the zeros()/uninitialized() factories for non-throwing construction, which return the vector with the status_t explaining a failure, or from_raw() to adopt existing memory. resize() changes the size within the allocated capacity() without moving values_data(); reserve() is the explicit opt-in that may reallocate to grow capacity.

Supports signed indexing (v[-1]), sub-byte types via proxy references, and slicing via operator[](range_t).

vectorDefault constructor — empty vector with default allocator.
vector.hpp:652 ↗
vector() noexcept=default;
constexpr vector(allocator_type_ const & alloc) noexcept;

Construct with custom allocator.

constexpr vector(vector && other) noexcept;

Move constructor.

vector(vector const &)=delete;
~vectorDestructor — deallocates memory.
vector.hpp:658 ↗
~vector() noexcept;
operator=Move assignment with allocator propagation.
vector.hpp:668 ↗
vector & operator=(vector && other) noexcept;
vector & operator=(vector const &)=delete;
swapSwap with another vector.
vector.hpp:764 ↗
void swap(vector & other) noexcept;
capacityAllocated storage-value capacity — the ceiling resize() honors.
vector.hpp:773 ↗
size_type capacity() const noexcept;
resizeResize in place to dims dimensions without reallocating: succeeds iff dims is a whole number of storage values that fits capacity(), so values_data() never moves.
vector.hpp:782 ↗
status_t resize(size_type dims) noexcept;

Statuses

reserveGrow the allocated capacity() to at least values storage values.
vector.hpp:796 ↗
status_t reserve(size_type values) noexcept;

Unlike resize(), this may reallocate and move values_data(); the live elements are preserved. No-op when already large enough.

A failed growth leaves the state unchanged.

clearReset to an empty size while keeping capacity(); storage frees on destruction.
vector.hpp:812 ↗
void clear() noexcept;
*× 3Number of logical dimensions.
vector.hpp:815 ↗
size_type size() const noexcept;
*× 3Check if empty.
vector.hpp:818 ↗
bool empty() const noexcept;
operator boolContextual-bool: truthy when the vector owns storage.
vector.hpp:822 ↗
constexpr operator bool() const noexcept;

Enables the if(!v) empty-check idiom.

size_valuesNumber of storage values.
vector.hpp:825 ↗
size_type size_values() const noexcept;
size_bytesSize in bytes.
vector.hpp:828 ↗
size_type size_bytes() const noexcept;
values_dataPointer to underlying data.
vector.hpp:831 ↗
value_type * values_data() noexcept;
value_type const * values_data() const noexcept;
dataPointer to underlying data, so the vector satisfies vector_of through std::data.
vector.hpp:835 ↗
value_type * data() noexcept;
value_type const * data() const noexcept;
get_allocatorGet a copy of the allocator.
vector.hpp:844 ↗
allocator_type get_allocator() const noexcept;
operator[]Signed dimension access.
vector.hpp:851 ↗
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) noexcept;
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) const noexcept;
vector_span<value_type> operator[](range_t r) noexcept;

Slice via range_t, returns a vector_span.

vector_view<value_type> operator[](range_t r) const noexcept;

Slice via range_t (const), returns a vector_view.

vector_span<value_type> operator[](all_t) noexcept;

Select all elements as a span.

vector_view<value_type> operator[](all_t) const noexcept;

Select all elements as a view.

Negative indices wrap from end.

Returns

A proxy reference for sub-byte types, or a direct reference for normal types.

beginDimension iterator to beginning.
vector.hpp:901 ↗
iterator begin() noexcept;
const_iterator begin() const noexcept;
endDimension iterator to end.
vector.hpp:906 ↗
iterator end() noexcept;
const_iterator end() const noexcept;
copy_*_from× 2Copy from a same-size view.
vector.hpp:917 ↗
status_t copy__from(vector_view<value_type> input) noexcept;

memcpy on the contiguous fast path, per-element copy on the strided slow path.

Statuses

zerosFactory: allocate a zero-initialized vector with dims dimensions.
vector.hpp:689 ↗
expected<vector> zeros(
    size_type dims,
    allocator_type_ alloc = {}) noexcept;

Returns

The vector, empty for zero dims.

Statuses

onesFactory: allocate a vector filled with ones.
vector.hpp:709 ↗
expected<vector> ones(
    size_type dims,
    allocator_type_ alloc = {}) noexcept;

Returns

The vector.

Statuses

fullFactory: allocate a vector filled with val.
vector.hpp:720 ↗
expected<vector> full(
    size_type dims,
    value_type_ val,
    allocator_type_ alloc = {}) noexcept;

Returns

The vector, empty for zero dims.

Statuses

uninitializedFactory: allocate an uninitialized vector.
vector.hpp:735 ↗
expected<vector> uninitialized(
    size_type dims,
    allocator_type_ alloc = {}) noexcept;

Returns

The vector, empty for zero dims.

Warnings

Contents are uninitialized. Caller must fill before reading.

Statuses

from_rawFactory: adopt raw memory.
vector.hpp:754 ↗
vector from_raw(
    pointer ptr,
    size_type dims,
    allocator_type_ alloc = {}) noexcept;

Caller transfers ownership.

Parameters

ptrinPointer to data (must have been allocated by alloc).
dimsinNumber of logical dimensions, a whole number of storage values.
allocinAllocator instance.
Structvector_spanNon-owning, mutable, strided view into a vector.10
vector.hpp:387 ↗
template <typename value_type_>
struct vector_span

Same as vector_view but allows mutation. Implicitly converts to vector_view.

vector_spanConstruct from contiguous typed pointer.
vector.hpp:405 ↗
constexpr vector_span() noexcept=default;
template <std::unsigned_integral dims_type_, std::integral stride_type_>
constexpr vector_span(
    char *data,
    dims_type_ dims,
    stride_type_ stride) noexcept;
template <std::unsigned_integral dims_type_>
constexpr vector_span(value_type *data, dims_type_ dims) noexcept;

Construct from contiguous typed pointer.

template <std::size_t span_extent_>
constexpr vector_span(
    std::span<value_type, span_extent_> values) noexcept;

Spans contiguous storage values as every logical dimension they hold, the inverse of values.

stride_bytesStride in bytes.
vector.hpp:436 ↗
difference_type stride_bytes() const noexcept;
is_contiguousTrue if contiguous.
vector.hpp:439 ↗
bool is_contiguous() const noexcept;
*× 2Typed pointer (only valid if contiguous).
vector.hpp:447 ↗
value_type * data() const noexcept;
valuesStorage values as a contiguous span; unexpected_dimensions_k for a strided span or one ending mid-value.
vector.hpp:454 ↗
expected<std::span<value_type>> values() const noexcept;
operator vector_view< value_type >Implicit conversion to const view.
vector.hpp:461 ↗
constexpr operator vector_view< value_type >() const noexcept;
operator[]Mutable integral indexing.
vector.hpp:467 ↗
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) const noexcept;
vector_span operator[](range_t r) const noexcept;

Sub-slice via range.

vector_span operator[](all_t) const noexcept;

Select all elements.

Signed negatives wrap from end.

*× 2Dimension iterator to beginning.
vector.hpp:488 ↗
iterator begin() const noexcept;

The handle is shallow-const, so iteration yields mutable refs.

*× 2Dimension iterator to end.
vector.hpp:492 ↗
iterator end() const noexcept;
fillFill every element with value, mirroring the free fill on tensor_span: 1-byte storage uses a single byte_pattern memset, multi-byte storage a typed broadcast loop.
vector.hpp:531 ↗
status_t fill(value_type value) noexcept;

Statuses

Structvector_viewNon-owning, immutable, strided view into a vector.7
vector.hpp:267 ↗
template <typename value_type_>
struct vector_view

Supports signed indexing (v[-1] = last element) and strided access. For sub-byte types, only contiguous views are meaningful.

vector_viewConstruct from contiguous typed pointer.
vector.hpp:281 ↗
constexpr vector_view() noexcept=default;
template <std::unsigned_integral dims_type_, std::integral stride_type_>
constexpr vector_view(
    char const *data,
    dims_type_ dims,
    stride_type_ stride) noexcept;
template <std::unsigned_integral dims_type_>
constexpr vector_view(value_type const *data, dims_type_ dims) noexcept;

Construct from contiguous typed pointer.

template <typename span_value_type_, std::size_t span_extent_>
requires std::is_same_v<std::remove_const_t<span_value_type_>, value_type>
constexpr vector_view(
    std::span<span_value_type_, span_extent_> values) noexcept;

Views contiguous storage values as every logical dimension they hold, the inverse of values.

stride_bytesStride in bytes between consecutive elements.
vector.hpp:313 ↗
difference_type stride_bytes() const noexcept;
is_contiguousTrue if elements are stored contiguously.
vector.hpp:316 ↗
bool is_contiguous() const noexcept;
byte_dataRaw byte pointer to the first element.
vector.hpp:321 ↗
char const * byte_data() const noexcept;
valuesStorage values as a contiguous span; unexpected_dimensions_k for a strided view or one ending mid-value.
vector.hpp:333 ↗
expected<std::span<value_type const>> values() const noexcept;
operator[]Integral indexing: signed negatives wrap from end.
vector.hpp:341 ↗
template <std::integral index_type_>
decltype(auto) operator[](index_type_ idx) const noexcept;
vector_view operator[](range_t r) const noexcept;

Sub-slice via range.

vector_view operator[](all_t) const noexcept;

Select all elements (identity).

revCreate a reversed view by negating the stride and pointing to the last element.
vector.hpp:362 ↗
vector_view rev() const noexcept;

Iterating the returned view visits elements in reverse order.

Functions

clampLightweight clamp to avoid pulling in <algorithm> for std::clamp.
random.hpp:22 ↗
template <typename scalar_type_>
scalar_type_ clamp(
    scalar_type_ val,
    scalar_type_ low,
    scalar_type_ high) noexcept;
fill_uniformFill array with uniform random values.
random.hpp:39 ↗
template <typename value_type_, typename generator_type_, typename component_type_ = typename value_type_::component_t>
void fill_uniform(
    generator_type_ & generator,
    value_type_ *values_ptr,
    std::size_t values_count,
    component_type_ min_val,
    component_type_ max_val) noexcept;
template <typename value_type_, typename generator_type_>
void fill_uniform(
    generator_type_ & generator,
    value_type_ *values_ptr,
    std::size_t values_count) noexcept;

Fill array with uniform random values within specified range.

Parameters

generatorinoutThe random number generator.
values_ptroutPointer to the array to fill.
values_countinNumber of storage values to fill, not dimensions for sub-byte types.
min_valinLowest value drawn.
max_valinHighest value drawn.
fill_lognormalFill array with lognormal distribution, good for detecting numerical edge cases.
random.hpp:105 ↗
template <typename value_type_, typename generator_type_>
void fill_lognormal(
    generator_type_ & generator,
    value_type_ *values_ptr,
    std::size_t values_count,
    double mean = 0.0,
    double stddev = 0.5) noexcept;

Probability Density Function (PDF):

f(x; μ, σ) = 1 / (x · σ · √(2π)) · exp(−(ln x − μ)² / (2σ²)),  x > 0

Equivalently, if X ~ 𝒩(μ, σ²), then eˣ ~ LogNormal(μ, σ). Values span many orders of magnitude, which is useful for exercising overflow/underflow paths in low-precision arithmetic. Common in modeling latencies, file sizes, and NLP token frequencies.

fill_cauchyFill array with Cauchy distribution, heavy tails for stress testing.
random.hpp:160 ↗
template <typename value_type_, typename generator_type_>
void fill_cauchy(
    generator_type_ & generator,
    value_type_ *values_ptr,
    std::size_t values_count,
    double location = 0.0,
    double scale = 1.0) noexcept;

Probability Density Function (PDF):

f(x; x₀, γ) = 1 / (π · γ · (1 + ((x − x₀) / γ)²))

The Cauchy distribution has no defined mean or variance, as all its moments diverge, making it ideal for stress-testing numerical stability with extreme outliers. Common in robust Bayesian priors, half-Cauchy, metaheuristic Levy flights, and adversarial robustness evaluation.

fill_coordinatesFill arrays with latitude and longitude coordinate values in radians.
random.hpp:198 ↗
template <typename value_type_, typename generator_type_>
void fill_coordinates(
    generator_type_ & generator,
    value_type_ *lats_ptr,
    value_type_ *lons_ptr,
    std::size_t values_count) noexcept;
fill_nearby_coordinatesFill destination coordinate arrays within a max angular separation of origin coordinates.
random.hpp:219 ↗
template <typename value_type_, typename generator_type_>
void fill_nearby_coordinates(
    generator_type_ & generator,
    value_type_ const *origin_lats_ptr,
    value_type_ const *origin_lons_ptr,
    value_type_ *dest_lats_ptr,
    value_type_ *dest_lons_ptr,
    std::size_t values_count,
    double max_separation_rad) noexcept;

Uses the great-circle destination formula to place each destination point at a random angular distance (uniform in [0, max_separation_rad]) and random bearing from the corresponding origin point. Coordinates are in radians.

fill_probabilityFill array as a probability distribution (sums to 1.0).
random.hpp:244 ↗
template <typename value_type_, typename generator_type_>
void fill_probability(
    generator_type_ & generator,
    value_type_ *values_ptr,
    std::size_t values_count) noexcept;
fill_sorted_uniqueFill array with sorted unique random integer values, in-place.
random.hpp:268 ↗
template <typename value_type_, typename generator_type_>
void fill_sorted_unique(
    generator_type_ & generator,
    value_type_ *values_ptr,
    std::size_t values_count,
    value_type_ max_val) noexcept;

Draws values_count values from [0, max_val - values_count], sorts once, then adds each element's index. Duplicates become distinct, which guarantees uniqueness and keeps sorted order in O(n log n) with one sort and no retry loop. Requires max_val ≥ values_count.

*× 2Non-member swap.
tensor.hpp:1893 ↗
template <typename V, typename A, std::size_t R>
void swap(tensor<V, A, R> & a, tensor<V, A, R> & b) noexcept;
Symbola, b
swaptensor<V, A, R> &
swapvector<value_type_, allocator_type_> &
fill_zerosZero-fill every element of output with one memset per maximal contiguous run, valid for every dtype as is_memset_zero_safe_v holds for all of them; returns misaligned_k on a layout that no contiguous run can cover.
tensor.hpp:2741 ↗
template <typename value_type_, std::size_t max_rank_>
status_t fill_zeros(
    tensor_span<value_type_, max_rank_> output) noexcept;
fillFill every element of output with value.
tensor.hpp:2755 ↗
template <typename value_type_, std::size_t max_rank_>
status_t fill(
    tensor_span<value_type_, max_rank_> output,
    value_type_ value) noexcept;

Two-step strategy: first memsets the buffer to binary zero, avoiding NaN/inf propagation from uninitialised memory, then overlays the value via memset for 1-byte storage or a typed scalar broadcast loop for multi-byte storage. Returns misaligned_k on an unsupported layout.

copyCopy input element-by-element into output.
tensor.hpp:2785 ↗
template <typename value_type_, std::size_t max_rank_>
status_t copy(
    tensor_view<value_type_, max_rank_> input,
    tensor_span<value_type_, max_rank_> output) noexcept;
template <typename value_type_, std::size_t max_rank_ = 8, typename allocator_type_ = aligned_allocator<value_type_>>
expected<tensor<value_type_, allocator_type_, max_rank_>> copy(
    tensor_view<value_type_, max_rank_> input,
    allocator_type_ alloc = {}) noexcept;

Allocating copy: returns a fresh contiguous tensor with the same shape and contents as input, empty for an empty input, or the allocation's or the copy's failure.

Recursive traversal collapses contiguous tail dims into one memcpy per leaf, and a strided last dim, as in a transposed view, copies value by value. Returns unexpected_dimensions_k on a shape mismatch and misaligned_k on a layout it does not support.

rangeBuilds a range_t, so slices read v[nk::range(1, 4)].
vector.hpp:71 ↗
template <std::integral start_type_, std::integral stop_type_, std::integral step_type_ = int>
range_t range(
    start_type_ start,
    stop_type_ stop,
    step_type_ step = 1) noexcept;
enumerateReturns a view yielding (index, value) pairs over a vector, view, or span.
vector.hpp:246 ↗
template <typename container_type_>
enumerate_view_<container_type_> enumerate(
    container_type_ & c) noexcept;

Constants

allGlobal instance of all_t for use in slicing expressions.
vector.hpp:57 ↗
all_t all;
TypesMember types60
scaled_tensor::format_typeformat_
scaled_tensor::element_typetypename format_::element_t
scaled_tensor::scale_typetypename format_::scale_t
scaled_tensor::element_tensor_typetensor<element_type, element_allocator_, max_rank_>
scaled_tensor::scale_tensor_typetensor<scale_type, scale_allocator_, max_rank_>
scaled_tensor::view_typescaled_tensor_view<format_, max_rank_>
scaled_tensor::span_typescaled_tensor_span<format_, max_rank_>
scaled_tensor::size_typestd::size_t
scaled_tensor_span::format_typeformat_
scaled_tensor_span::element_typetypename format_::element_t
scaled_tensor_span::scale_typetypename format_::scale_t
scaled_tensor_span::element_span_typetensor_span<element_type, max_rank_>
scaled_tensor_span::scale_span_typetensor_span<scale_type, max_rank_>
scaled_tensor_span::view_typescaled_tensor_view<format_, max_rank_>
scaled_tensor_span::size_typestd::size_t
scaled_tensor_view::format_typeformat_
scaled_tensor_view::element_typetypename format_::element_t
scaled_tensor_view::scale_typetypename format_::scale_t
scaled_tensor_view::element_view_typetensor_view<element_type, max_rank_>
scaled_tensor_view::scale_view_typetensor_view<scale_type, max_rank_>
scaled_tensor_view::size_typestd::size_t
scaled_tensor_view::difference_typestd::ptrdiff_t
tensor::value_typevalue_type_
tensor::raw_value_typetypename raw_pod_type<value_type>::type
tensor::allocator_typeallocator_type_
tensor::alloc_traitsstd::allocator_traits<allocator_type_>
tensor::size_typestd::size_t
tensor::difference_typestd::ptrdiff_t
tensor::pointervalue_type_ *
tensor::view_typetensor_view<value_type_, max_rank_>
tensor::span_typetensor_span<value_type_, max_rank_>
tensor_span::value_typevalue_type_
tensor_span::raw_value_typetypename raw_pod_type<value_type>::type
tensor_span::size_typestd::size_t
tensor_span::difference_typestd::ptrdiff_t
tensor_view::value_typevalue_type_
tensor_view::raw_value_typetypename raw_pod_type<value_type>::type
tensor_view::size_typestd::size_t
tensor_view::difference_typestd::ptrdiff_t
vector::value_typevalue_type_
vector::raw_value_typetypename raw_pod_type<value_type>::type
vector::allocator_typeallocator_type_
vector::alloc_traitsstd::allocator_traits<allocator_type_>
vector::size_typestd::size_t
vector::difference_typestd::ptrdiff_t
vector::pointervalue_type_ *
vector::const_pointervalue_type_ const *
vector::iteratordim_iterator<vector>
vector::const_iteratordim_iterator<vector const>
vector_span::value_typevalue_type_
vector_span::raw_value_typetypename raw_pod_type<value_type>::type
vector_span::size_typestd::size_t
vector_span::difference_typestd::ptrdiff_t
vector_span::iteratordim_iterator<vector_span>
vector_span::const_iteratordim_iterator<vector_span const>
vector_view::value_typevalue_type_
vector_view::raw_value_typetypename raw_pod_type<value_type>::type
vector_view::size_typestd::size_t
vector_view::difference_typestd::ptrdiff_t
vector_view::const_iteratordim_iterator<vector_view const>