Skip to content

Generators

Tensors with values but no memory: constants, the identity, runs of numbers, functions of the index.

Types

Name Description
Constant The same value at every index: a scalar with a shape.
Generated fn(i, j, ...) at every index of S: a tensor whose elements are computed from their index.

Functions

zeros

template <typename T, shape_like S>
constexpr Constant<T, S> zeros(const S &shape)

T{} at every index: zeros<float>(Shape<3, 4>{}).

ones

template <typename T, shape_like S>
constexpr Constant<T, S> ones(const S &shape)

T(1) at every index: ones<float>(shape).

full

template <shape_like S, typename T>
constexpr Constant<std::remove_cvref_t<T>, S> full(const S &shape, T &&value)

value at every index, the constant being of the type of value: full(shape, 2.5).

generate

template <shape_like S, typename F>
constexpr Generated<S, std::remove_cvref_t<F>> generate(const S &shape, F &&fn)

fn(i, j, ...) at every index, lazily: generate(shape, fn).

The indices are std::size_t. A function that turns them into floating point does it fastest through std::ptrdiff_t: x86 converts signed integers in one instruction, unsigned ones in several.

Parameters

Name Description
shape The shape of the result.
fn Called with one index per axis; its result, without reference or const, is the value type.

eye

template <typename T, shape_like S>
constexpr Generated<S, detail::Diagonal<T>> eye(const S &shape) noexcept

1 where every index is the same, 0 elsewhere, in any rank: eye<float>(Shape<4, 4>{}).

iota

template <typename T>
constexpr Generated<Shape<Dyn>, detail::Arithmetic<T>> iota(std::size_t count, T start=T(0), T step=T(1)) noexcept

count values from start, step apart: start + i * step, as std::views::iota and numpy.arange.

The result is a vector of dynamic extent count: iota<float>(5) is 0, 1, 2, 3, 4.

Precondition

count is at most PTRDIFF_MAX.

linspace

template <std::floating_point T>
constexpr Generated<Shape<Dyn>, detail::Spaced<T>> linspace(T first, T last, std::size_t count) noexcept

count values evenly from first to last, both included, as numpy.linspace.

The last value is exactly last; one value is first. The result is a vector of dynamic extent count: linspace(0.0F, 1.0F, 5).

Precondition

count is at most PTRDIFF_MAX.

zeros_like

template <tensor_like E>
constexpr auto zeros_like(const E &expression)

zeros shaped like expression, static extents included, and of its value type.

For code that has an expression, a view or a tensor at hand rather than its shape, as numpy.zeros_like.

ones_like

template <tensor_like E>
constexpr auto ones_like(const E &expression)

ones shaped like expression, and of its value type.

full_like

template <tensor_like E, typename T>
constexpr auto full_like(const E &expression, T &&value)

full shaped like expression: the constant takes the type of value, as full does.