NumericAL
A header-only C++20 lexer built on REAL
Loading...
Searching...
No Matches
Classes | Namespaces | Concepts | Typedefs | Enumerations | Functions | Variables
tensor_ops.hpp File Reference

Element-wise arithmetic with NumPy's broadcasting, reductions, reshaping and creation of tensors. More...

#include <algorithm>
#include <array>
#include <bit>
#include <cmath>
#include <complex>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <functional>
#include <limits>
#include <numeric>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "complex_math.hpp"
#include "elementary.hpp"
#include "simd.hpp"
#include "tensor.hpp"
Include dependency graph for tensor_ops.hpp:

Go to the source code of this file.

Classes

struct  numerical::detail::axis_walk
 The extents and strides that walk a tensor of shape with axis axis taken out: outer positions before it, inner after it, the axis itself extent long at stride inner. More...
 
struct  numerical::detail::library_sine< false >
 library_sine's sin. More...
 
struct  numerical::detail::library_sine< true >
 library_sine's cos. More...
 
struct  numerical::detail::laid_axis< T >
 A transpose laid out (tensor::laid_out) and the axis matching one of its view's. More...
 
struct  numerical::detail::numpy_loops
 Which of NumPy's complex loops fuse their multiply-adds: its product's vector loop, its product's scalar loop (detail::numpy_loop_is_scalar), its quotient's and its product's reduction loop. They follow how its wheels were built, not only the processor, so the binding measures them on the NumPy it runs beside. More...
 
struct  numerical::detail::through_doubles_piece
 A *_into kernel of doubles as a kernel of floats: each piece of 512 converted to doubles on the stack, the kernel of them, each rounded back once (through_doubles's, piece by piece). More...
 
struct  numerical::detail::arithmetic< T >
 The element operations of T the tensor operators apply: the standard ones. More...
 
struct  numerical::detail::arithmetic< T >
 A signed integer's operations through its unsigned type, so that an overflow wraps modulo 2ⁿ as NumPy's integers do (two's complement) where the signed operation is undefined. More...
 
struct  numerical::detail::arithmetic< T >::plus
 a + b, wrapped. More...
 
struct  numerical::detail::arithmetic< T >::minus
 a - b, wrapped. More...
 
struct  numerical::detail::arithmetic< T >::multiplies
 a · b, wrapped. More...
 
struct  numerical::detail::arithmetic< T >::negate
 -a, wrapped (the most negative value its own negation). More...
 
struct  numerical::detail::decimal_rounding< R, C >
 A real of type R rounded to decimals places, half to even, as numpy.round: nearbyint(x · 10ᵈ) / 10ᵈ (nearbyint(x / 10⁻ᵈ) · 10⁻ᵈ for a negative d), computed in C. More...
 
class  numerical::negative_power_error
 Thrown when an integer is raised to a negative integer power, as NumPy refuses it: no integer is the answer. More...
 

Namespaces

namespace  numerical
 The NumericAL public API.
 
namespace  numerical::detail
 

Concepts

concept  numerical::detail::radix_orderable
 The types whose indices order_run sorts by the elements' bits.
 

Typedefs

using numerical::detail::doubles_into = void(*)(const double *, double *, std::size_t)
 A function of count contiguous doubles into as many (the *_into kernels).
 
template<typename T >
using numerical::detail::order_key_t = std::conditional_t< sizeof(T)==8, std::uint64_t, std::uint32_t >
 The unsigned integer of T's width, whose order order_key maps T's onto.
 
template<typename U >
using numerical::detail::byte_counts = std::array< std::array< std::size_t, 256 >, sizeof(U)>
 How many keys of U hold each value of each byte: the byte at bit 8·b counted in [b].
 

Enumerations

enum class  numerical::take_mode : unsigned char { numerical::raise , numerical::wrap , numerical::clip }
 What take does with an index outside its axis, as numpy.take's mode: refuse it, take it modulo the extent, or take the nearer end. More...
 

Functions

axis_walk numerical::detail::walk_along (const std::vector< std::size_t > &shape, std::size_t axis)
 axis_walk of shape along axis; outer and inner 0 where the result is empty.
 
template<std::floating_point R>
R numerical::detail::numpy_muladd (R x, R y, R z, bool fused)
 x · y + z: one fused multiply-add where fused, else the product rounded first (statements apart, so no compiler contracts them).
 
template<split_complex C>
C numerical::detail::numpy_multiply (const C &a, const C &b, bool fused)
 a · b of complex numbers as NumPy's array loop computes it: ai·bi and ai·br rounded, then ar·br − ai·bi and ar·bi + ai·br, each by a fused multiply-add where fused (NumPy's vectors on arm64 and on x86-64 with FMA), else each product rounded first. No recovery of an infinity from a NaN, as std::complex's (C Annex G) makes: 1j · (inf + inf·j) is NaN, (1e308 + 1e308j) · (1e308 − 1e308j) inf + inf·j when fused.
 
template<split_complex C>
C numerical::detail::numpy_multiply_late (const C &a, const C &b)
 a · b of complex numbers as NumPy's reduction loop computes it where MSVC contracts it (its Windows wheels from 2.5): ai·bi and ar·bi rounded, then ar·br − ai·bi and ai·br + ar·bi each one fused multiply-add (the imaginary part's other product fused than in numpy_multiply).
 
template<std::floating_point R>
R numerical::detail::divided_by_zero (R x) noexcept
 x divided by +0, as IEEE divides it, spelled out: a NaN for a zero or a NaN, else an infinity of x's sign (a division by a zero the compiler sees is MSVC's C4723 under /WX).
 
template<split_complex C>
C numerical::detail::numpy_divide (const C &a, const C &b, bool fused)
 a / b of complex numbers as NumPy's array loop computes it: Smith's division by the larger part of b (an exact zero divided part by part, to an infinity or a NaN), each x + y·z of it one fused multiply-add where fused (NumPy's build contracts them on arm64, not on x86-64).
 
template<split_complex C>
C numerical::detail::numpy_reciprocal (const C &x, bool fused)
 NumPy's complex reciprocal 1 / x (its reciprocal, the loop x ** -1 takes): the ratio of the smaller part to the larger, d the larger plus the smaller times it (one fused multiply-add where fused), then 1 / d and −ratio / d — not numpy_divide's quotient of 1.
 
template<split_complex C>
C numerical::detail::library_power (const C &a, const C &b)
 a ** b by std::pow: libstdc++'s calls the C library's cpow, which NumPy calls (the same bits under glibc); MSVC's is its own.
 
template<split_complex C>
C numerical::detail::numpy_power (const C &a, const C &b, bool fused)
 NumPy's complex power a ** b (npymath's npy_cpow): 1 for a zero exponent, 0 or NaN for a zero base; an integer exponent below 100 in magnitude by products (numpy_multiply, from 1 past the cubes) and, when negative, the quotient of 1 by them (numpy_divide), each fused where fused; any other by the C library's cpow, as NumPy (library_power).
 
template<typename T >
T numerical::detail::root (const T &x)
 The square root of a scalar, as std::sqrt gives it.
 
template<typename Into >
tensor< float > numerical::detail::through_doubles (const float *data, const std::vector< std::size_t > &shape, Into into)
 The function into (one of the *_into of doubles) of every one of the row-major floats at data, of shape shape: each piece of 512 converted to doubles on the stack, into of them, each rounded back once. The double within 0.51 of its ulp, the float is within half of its own and 2⁻²⁸ more, where the C library's float functions are within one or two; and the doubles' vectors are faster than its scalar calls (glibc's sinhf 4.4 times the doubles' sinh, atanf 3.5, log1pf 2.9).
 
template<typename T >
T numerical::detail::elementary_exp (const T &x)
 exp of a scalar, as std::exp gives it.
 
double numerical::detail::elementary_exp (double x)
 exp of a double, as exp_into gives it: table_exp, or std::exp below exp_normal_bound (and throughout where exp_by_table says no).
 
void numerical::detail::exp_into (const double *in, double *out, std::size_t count)
 exp of the count doubles at in into out, each elementary_exp's: table_exp in vectors (with the FMA instructions on an x86-64 processor, run_fused), then std::exp where the result is subnormal or zero, below exp_normal_bound, which the table's two scalings would round twice; std::exp throughout where exp_by_table says no.
 
template<typename T >
T numerical::detail::elementary_expm1 (const T &x)
 expm1 of a scalar, as std::expm1 gives it.
 
double numerical::detail::elementary_expm1 (double x)
 expm1 of a double, as expm1_into gives it: table_expm1, or std::expm1 where exp_by_table says no.
 
void numerical::detail::expm1_into (const double *in, double *out, std::size_t count)
 expm1 of the count doubles at in into out, each elementary_expm1's: table_expm1 in vectors (with the FMA instructions on an x86-64 processor, run_fused), std::expm1 throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::expm1_of (const T *data, const std::vector< std::size_t > &shape)
 expm1 of every one of the row-major scalars at data, of shape shape: std::expm1 of each.
 
tensor< double > numerical::detail::expm1_of (const double *data, const std::vector< std::size_t > &shape)
 expm1 of every one of the row-major doubles at data, of shape shape: expm1_into by chunks of elementwise_chunk among the threads.
 
float numerical::detail::elementary_expm1 (float x)
 expm1 of a float, as expm1_of gives it: elementary_expm1 of the double, rounded once.
 
tensor< float > numerical::detail::expm1_of (const float *data, const std::vector< std::size_t > &shape)
 expm1 of every one of the row-major floats at data, of shape shape: expm1_into of them as doubles (through_doubles).
 
template<typename T >
T numerical::detail::elementary_atan (const T &x)
 atan of a scalar, as std::atan gives it.
 
double numerical::detail::elementary_atan (double x)
 atan of a double, as atan_into gives it: table_atan, or std::atan where exp_by_table says no.
 
void numerical::detail::atan_into (const double *in, double *out, std::size_t count)
 atan of the count doubles at in into out, each elementary_atan's: table_atan in vectors (with the FMA instructions on an x86-64 processor, run_fused), std::atan throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::atan_of (const T *data, const std::vector< std::size_t > &shape)
 atan of every one of the row-major scalars at data, of shape shape: std::atan of each.
 
tensor< double > numerical::detail::atan_of (const double *data, const std::vector< std::size_t > &shape)
 atan of every one of the row-major doubles at data, of shape shape: atan_into by chunks of elementwise_chunk among the threads.
 
float numerical::detail::elementary_atan (float x)
 atan of a float, as atan_of gives it: elementary_atan of the double, rounded once.
 
tensor< float > numerical::detail::atan_of (const float *data, const std::vector< std::size_t > &shape)
 atan of every one of the row-major floats at data, of shape shape: atan_into of them as doubles (through_doubles).
 
template<typename T >
T numerical::detail::elementary_sinh (const T &x)
 sinh of a scalar, as std::sinh gives it.
 
double numerical::detail::elementary_sinh (double x)
 sinh of a double, as sinh_into gives it: table_sinh, or std::sinh where exp_by_table says no.
 
void numerical::detail::sinh_into (const double *in, double *out, std::size_t count)
 sinh of the count doubles at in into out, each elementary_sinh's: table_sinh in vectors (with the FMA instructions on an x86-64 processor, run_fused), std::sinh throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::sinh_of (const T *data, const std::vector< std::size_t > &shape)
 sinh of every one of the row-major scalars at data, of shape shape: std::sinh of each.
 
tensor< double > numerical::detail::sinh_of (const double *data, const std::vector< std::size_t > &shape)
 sinh of every one of the row-major doubles at data, of shape shape: sinh_into by chunks of elementwise_chunk among the threads.
 
float numerical::detail::elementary_sinh (float x)
 sinh of a float, as sinh_of gives it: elementary_sinh of the double, rounded once.
 
tensor< float > numerical::detail::sinh_of (const float *data, const std::vector< std::size_t > &shape)
 sinh of every one of the row-major floats at data, of shape shape: sinh_into of them as doubles (through_doubles).
 
template<typename T >
T numerical::detail::elementary_cosh (const T &x)
 cosh of a scalar, as std::cosh gives it.
 
double numerical::detail::elementary_cosh (double x)
 cosh of a double, as cosh_into gives it: table_cosh, or std::cosh where exp_by_table says no.
 
void numerical::detail::cosh_into (const double *in, double *out, std::size_t count)
 cosh of the count doubles at in into out, each elementary_cosh's: table_cosh in vectors (with the FMA instructions on an x86-64 processor, run_fused), std::cosh throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::cosh_of (const T *data, const std::vector< std::size_t > &shape)
 cosh of every one of the row-major scalars at data, of shape shape: std::cosh of each.
 
tensor< double > numerical::detail::cosh_of (const double *data, const std::vector< std::size_t > &shape)
 cosh of every one of the row-major doubles at data, of shape shape: cosh_into by chunks of elementwise_chunk among the threads.
 
float numerical::detail::elementary_cosh (float x)
 cosh of a float, as cosh_of gives it: elementary_cosh of the double, rounded once.
 
tensor< float > numerical::detail::cosh_of (const float *data, const std::vector< std::size_t > &shape)
 cosh of every one of the row-major floats at data, of shape shape: cosh_into of them as doubles (through_doubles).
 
template<typename T >
T numerical::detail::elementary_exp2 (const T &x)
 exp2 of a scalar, as std::exp2 gives it.
 
double numerical::detail::elementary_exp2 (double x)
 exp2 of a double, as exp2_into gives it: table_exp2, or std::exp2 below exp2_normal_bound (and throughout where exp_by_table says no).
 
void numerical::detail::exp2_into (const double *in, double *out, std::size_t count)
 exp2 of the count doubles at in into out, each elementary_exp2's: table_exp2 in vectors (with the FMA instructions on an x86-64 processor, run_fused), then std::exp2 below exp2_normal_bound, as exp_into; std::exp2 throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::exp2_of (const T *data, const std::vector< std::size_t > &shape)
 exp2 of every one of the row-major scalars at data, of shape shape: std::exp2 of each.
 
tensor< double > numerical::detail::exp2_of (const double *data, const std::vector< std::size_t > &shape)
 exp2 of every one of the row-major doubles at data, of shape shape: exp2_into by chunks of elementwise_chunk among the threads.
 
template<typename T >
T numerical::detail::elementary_log (const T &x)
 log of a scalar, as std::log gives it.
 
template<typename T >
T numerical::detail::elementary_log1p (const T &x)
 log1p of a scalar, as std::log1p gives it.
 
double numerical::detail::elementary_log1p (double x)
 log1p of a double, as log1p_into gives it: table_log1p, or std::log1p where exp_by_table says no.
 
void numerical::detail::log1p_into (const double *in, double *out, std::size_t count)
 log1p of the count doubles at in into out, each elementary_log1p's: table_log1p in vectors (with the FMA instructions on an x86-64 processor, run_fused), std::log1p throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::log1p_of (const T *data, const std::vector< std::size_t > &shape)
 log1p of every one of the row-major scalars at data, of shape shape: std::log1p of each.
 
tensor< double > numerical::detail::log1p_of (const double *data, const std::vector< std::size_t > &shape)
 log1p of every one of the row-major doubles at data, of shape shape: log1p_into by chunks of elementwise_chunk among the threads.
 
float numerical::detail::elementary_log1p (float x)
 log1p of a float, as log1p_of gives it: elementary_log1p of the double, rounded once.
 
tensor< float > numerical::detail::log1p_of (const float *data, const std::vector< std::size_t > &shape)
 log1p of every one of the row-major floats at data, of shape shape: log1p_into of them as doubles (through_doubles).
 
template<typename T >
T numerical::detail::elementary_sin (const T &x)
 sin of a scalar, as std::sin gives it.
 
template<typename T >
T numerical::detail::elementary_cos (const T &x)
 cos of a scalar, as std::cos gives it.
 
template<bool Cosine>
double numerical::detail::float_sine (double x)
 sin (Cosine false) or cos of a double that a float holds: reduced_sine to sine_reduction_bound, the C library's past it (and for an infinity or a NaN).
 
template<bool Cosine>
void numerical::detail::float_sines_into (const double *in, double *out, std::size_t count)
 sin (Cosine false) or cos of the count doubles at in, each a float's, into out, each float_sine's: reduced_sine in vectors (with the FMA instructions on an x86-64 processor, run_fused), then the C library's past sine_reduction_bound, behind a branch; the C library's throughout where exp_by_table says no.
 
template<bool Cosine>
double numerical::detail::double_sine (double x)
 sin (Cosine false) or cos of a double, as double_sines_into gives it: table_sine to sine_reduction_bound, the C library's past it (and for an infinity or a NaN) and where exp_by_table says no.
 
template<bool Cosine>
void numerical::detail::double_sines_into (const double *in, double *out, std::size_t count)
 sin (Cosine false) or cos of the count doubles at in into out, each double_sine's: table_sine to sine_reduction_bound (with the FMA instructions on an x86-64 processor, run_fused), the C library's past it, element by element (the table's then the library's over every element past it took 1.4 times the library's alone: 10⁶ values to ±10⁷, 12 → 17 ms); the C library's throughout where exp_by_table says no.
 
double numerical::detail::elementary_sin (double x)
 sin of a double, as double_sines_into gives it (double_sine).
 
double numerical::detail::elementary_cos (double x)
 cos of a double, as double_sines_into gives it (double_sine).
 
float numerical::detail::elementary_sin (float x)
 sin of a float, as float_sines_into gives it: float_sine of the double, rounded once.
 
float numerical::detail::elementary_cos (float x)
 cos of a float, as float_sines_into gives it: float_sine of the double, rounded once.
 
template<typename T >
tensor< T > numerical::detail::sin_of (const T *data, const std::vector< std::size_t > &shape)
 sin of every one of the row-major scalars at data, of shape shape: std::sin of each.
 
tensor< float > numerical::detail::sin_of (const float *data, const std::vector< std::size_t > &shape)
 sin of every one of the row-major floats at data, of shape shape: float_sines_into of them as doubles (through_doubles).
 
tensor< double > numerical::detail::sin_of (const double *data, const std::vector< std::size_t > &shape)
 sin of every one of the row-major doubles at data, of shape shape: double_sines_into by chunks of elementwise_chunk on the threads.
 
template<typename T >
tensor< T > numerical::detail::cos_of (const T *data, const std::vector< std::size_t > &shape)
 cos of every one of the row-major scalars at data, of shape shape: std::cos of each.
 
tensor< double > numerical::detail::cos_of (const double *data, const std::vector< std::size_t > &shape)
 cos of every one of the row-major doubles at data, of shape shape: double_sines_into by chunks of elementwise_chunk on the threads.
 
tensor< float > numerical::detail::cos_of (const float *data, const std::vector< std::size_t > &shape)
 cos of every one of the row-major floats at data, of shape shape: float_sines_into of them as doubles (through_doubles).
 
template<typename T >
T numerical::detail::elementary_tan (const T &x)
 tan of a scalar, as std::tan gives it.
 
template<typename T >
T numerical::detail::elementary_tanh (const T &x)
 tanh of a scalar, as std::tanh gives it.
 
double numerical::detail::elementary_tanh (double x)
 tanh of a double, as tanh_into gives it: table_tanh, or std::tanh where exp_by_table says no.
 
void numerical::detail::tanh_into (const double *in, double *out, std::size_t count)
 tanh of the count doubles at in into out, each elementary_tanh's: table_tanh in vectors (with the FMA instructions on an x86-64 processor, run_fused), std::tanh throughout where exp_by_table says no.
 
template<typename T >
tensor< T > numerical::detail::tanh_of (const T *data, const std::vector< std::size_t > &shape)
 tanh of every one of the row-major scalars at data, of shape shape: elementary_tanh of each.
 
tensor< double > numerical::detail::tanh_of (const double *data, const std::vector< std::size_t > &shape)
 tanh of every one of the row-major doubles at data, of shape shape: tanh_into by chunks of elementwise_chunk among the threads.
 
float numerical::detail::elementary_tanh (float x)
 tanh of a float, as tanh_of gives it: elementary_tanh of the double, rounded once.
 
tensor< float > numerical::detail::tanh_of (const float *data, const std::vector< std::size_t > &shape)
 tanh of every one of the row-major floats at data, of shape shape: tanh_into of them as doubles (through_doubles).
 
template<split_complex C>
C::value_type numerical::detail::numpy_absolute (const C &x, bool fused)
 NumPy's complex magnitude (its absolute's vector loop): the parts' magnitudes, an infinite one making both infinite and then a NaN both NaN, then the larger times sqrt(ratio² + 1) for the ratio of the smaller to it (0 where the larger is 0 or the smaller infinite), ratio² + 1 one fused multiply-add where fused.
 
bool numerical::detail::numpy_fuses_products () noexcept
 Whether NumPy's complex product fuses its multiply-adds on this processor: on arm64, and on x86-64 where its AVX2 loop runs (FMA with it); elsewhere its loop rounds each product.
 
bool numerical::detail::numpy_fuses_scalar_products () noexcept
 Whether NumPy's complex product fuses its multiply-adds in its scalar loop (where numpy_loop_is_scalar holds), as its PyPI wheels are built: that loop sits beside the vector one, and clang (macOS) and MSVC (Windows) contract it where the vector loop has FMA; GCC (Linux, ISO C) does not.
 
bool numerical::detail::numpy_fuses_reductions () noexcept
 Whether NumPy's complex product fuses its multiply-adds in its reduction loop (the product's loop whose output is its own first operand: numpy.prod along an axis nothing follows, numpy.cumprod past two elements), as its PyPI wheels are built: a scalar loop, contracted as numpy_fuses_scalar_products says (GCC's on Linux rounds each product, though its vector loop fuses).
 
bool numerical::detail::numpy_fuses_quotients () noexcept
 Whether NumPy's complex quotient fuses its multiply-adds, as its PyPI wheels are built: on macOS arm64, whose clang contracts them (FMA is arm64's baseline); not on Linux, whose GCC does not, nor on x86-64, whose baseline has no FMA.
 
bool numerical::detail::numpy_loop_is_scalar (const std::vector< std::size_t > &a_shape, const std::vector< std::size_t > &b_shape) noexcept
 Whether NumPy multiplies an operand of shape a_shape by one of b_shape in its scalar loop rather than its vector one: when the result is one element and an operand of rank above 0 is broadcast to a higher rank, NumPy's iterator hands its loop an output step of 0, which the vector loop refuses.
 
template<typename T >
bool numerical::detail::numpy_loop_is_scalar (const tensor< T > &a, const tensor< T > &b) noexcept
 numpy_loop_is_scalar of the shapes of a and b.
 
template<std::floating_point T>
T numerical::detail::divided_by_count (T value, std::size_t n)
 A mean: value divided by the count n, in the wider of T and double and back to T, the count exact (to 2^53), as NumPy divides by its integer count; a float's count rounded to a float past 2^24 (2^24 + 1 ones averaged to 1, NumPy's 0.99999994).
 
template<std::floating_point T, typename Order >
T numerical::detail::settled_zero (T value, const T *first, std::size_t count, std::size_t stride, const Order &larger)
 The extreme value of count elements stride apart from first, its zero's sign settled as IEEE 754-2019's maximum and minimum settle it, and NumPy's on arm64: no zero is larger than the other, so the first met stayed; max gives +0 where one is among them, min −0, whatever their order. A NaN, or any other value, as it is.
 
std::vector< std::size_t > numerical::detail::without_axis (std::vector< std::size_t > shape, std::size_t axis)
 The shape with axis axis removed.
 
template<typename T , typename Reduce >
tensor< T > numerical::detail::reduce_along (const tensor< T > &t, std::size_t axis, const Reduce &reduce)
 Reduces each line of t along axis by reduce, which takes the line's first element, its length and its stride.
 
template<typename R , typename T , typename Step >
tensor< R > numerical::detail::fold_along (const tensor< T > &t, std::size_t axis, const R &init, const Step &step)
 Folds each line of t along axis in order, step(running, x) from init: a tensor of the result type R, the axis taken out. Where the lines are columns (something follows the axis), each task takes a band of up to band_columns of them and walks it row by row, contiguously; each line's steps are the same, in the same order, either way.
 
template<typename T >
T numerical::detail::square (const T &x)
 x · x, by the type's own product (a complex one's C Annex G, as the library's *).
 
template<typename T >
bool numerical::detail::is_nan (const T &)
 A NaN? Never for a scalar without one.
 
bool numerical::detail::is_nan (double x)
 A NaN?
 
template<typename T , typename Order >
T numerical::detail::contiguous_extreme (const T *first, std::size_t count, const Order &larger)
 The largest (larger is std::greater) or smallest of count contiguous elements, or a NaN if one is among them, as NumPy's: the comparisons run without a branch over native vectors (or eight scalar lanes), each lane its own running best, and the NaN is tracked apart.
 
template<typename T , typename Order >
T numerical::detail::parallel_extreme (const T *first, std::size_t count, const Order &larger)
 contiguous_extreme of count elements on the threads: runs of pairwise_leaf each its own, then their extremes'. Exact whatever the runs (a maximum rounds nothing), so the same answer on any number of threads. Four runs or fewer: on the caller, as parallel_pairwise_sum.
 
template<typename T , typename Order >
T numerical::detail::walked_extreme (const T *first, std::size_t count, std::size_t stride, const Order &larger)
 extreme of a line walked element by element: a NaN wins, as in NumPy (every comparison with it is false, so a NaN candidate is taken apart, and once taken nothing compares larger than it).
 
template<std::floating_point T, typename Order >
T numerical::detail::extreme (const T *first, std::size_t count, std::size_t stride, const Order &larger)
 The largest (larger is std::greater) or smallest element of a line of reals: a contiguous one by the comparison without a branch (so it vectorises) and the NaN tracked apart, a NaN anywhere the answer, as NumPy's; a strided one walked (walked_extreme).
 
template<typename T , typename Order >
requires (!std::floating_point<T>)
T numerical::detail::extreme (const T *first, std::size_t count, std::size_t stride, const Order &larger)
 The largest (larger is std::greater) or smallest element of a line of another type (walked_extreme).
 
template<typename T >
constexpr bool numerical::detail::by_reduction (std::size_t inner) noexcept
 Whether extremes_along takes the reduction's path: an exact type, or an axis of single elements. A function, where the test written in the condition joined a constant to the axis's, which MSVC 19.44 (Visual Studio 2022's) rejects (C4127, an error under /WX).
 
template<typename T , typename Order >
tensor< T > numerical::detail::extremes_along (const tensor< T > &t, std::size_t axis, const Order &larger)
 The largest (larger is std::greater) or smallest elements along axis. Along an inner axis each line is contiguous (extreme); along an outer one the slabs are compared element by element, so the loop runs over contiguous memory rather than down a column.
 
std::vector< std::size_t > numerical::detail::view_axes (const std::vector< std::size_t > &order)
 The axes that put the elements of a tensor laid out in order (tensor::laid_out, its view's axes in the order they lie) back in the view's order: the permutation to transpose a result of the same axes by.
 
std::vector< std::size_t > numerical::detail::reduced_axes (const std::vector< std::size_t > &order, std::size_t axis)
 The axes that put a result along axis of a tensor laid out in order (view_axes), that axis taken out, back in the view's order.
 
template<typename T >
std::optional< laid_axis< T > > numerical::detail::laid_along (const tensor< T > &t, std::size_t axis)
 t laid out and the axis matching axis: nothing for any tensor but a transpose, or an axis past the rank (the operation's own to refuse).
 
template<typename T , typename F >
auto numerical::detail::as_laid_along (const tensor< T > &t, std::size_t axis, const F &f) -> std::optional< decltype(f(t, axis))>
 Along axis of a transpose (laid_along): f (f(elements, axis)) of its elements as they lie along the matching axis, none copied, the result (of the same axes) put back in the view's order. Each line the same elements in the same order: what is computed line by line, the same. Nothing for any other tensor, an axis past the rank, or the view's last axis where its elements lie apart (a transpose's columns): copied out as rows, those lines are sorted, scanned and transformed faster than in place as columns (2000 of 2000 reals sorted, 8.0 ms against 12.4, M1 Pro), where along any other axis they are columns either way.
 
template<typename T , typename F >
auto numerical::detail::as_laid_reduced (const tensor< T > &t, std::size_t axis, const F &f) -> std::optional< decltype(f(t, axis))>
 as_laid_along of an f whose result takes the axis out, along any axis: a reduction of columns runs across them as fast as along rows (2000 sums of 2000, 0.74 ms against 1.19 copied out first, M1 Pro).
 
template<typename T >
bool numerical::detail::lies_close (const tensor< T > &t) noexcept
 Whether an element-wise operation reads the view t's elements where they lie: stepping within a cache line along its last axis (a slice).
 
template<typename T , typename F >
auto numerical::detail::parallel_map (const T *data, const std::vector< std::size_t > &shape, const F &f) -> tensor< std::decay_t< decltype(f(std::declval< const T & >()))> >
 f(x) for every one of the row-major elements at data, of shape shape (another array's storage, read where it lies).
 
template<typename T , typename F >
auto numerical::detail::parallel_map (const tensor< T > &t, const F &f) -> tensor< std::decay_t< decltype(f(std::declval< const T & >()))> >
 f(x) for every element of t, by chunks of elementwise_chunk among the threads (as broadcast_apply's same-shape pairs): for a stateless f, the library's element-wise functions.
 
template<typename T , typename Op >
auto numerical::detail::broadcast_rows (const T *a, const std::vector< std::size_t > &a_shape, const T *b, const std::vector< std::size_t > &b_shape, const Op &op)
 op(a, b) element by element of the row-major a (a_shape) and b (b_shape), broadcast against each other, in op's result type: broadcast_apply's paths for two contiguous tensors, so their bits, for elements no tensor holds (the binding's NumPy arrays). One element against as many axes or more is a number.
 
template<typename T , typename Op >
auto numerical::broadcast_apply (const tensor< T > &a, const tensor< T > &b, const Op &op)
 op(a, b) element by element, the operands broadcast against each other, in op's result type (a comparison's bools): a view read where it lies close (a slice), copied otherwise. One element against a tensor of as many axes or more is a number, the other read as detail::parallel_map reads it (a transpose's elements as they lie); two transposes alike, their elements paired as they lie.
 
template<split_complex C>
C numerical::detail::numpy_reduction_multiply (const C &a, const C &b, const numpy_loops &loops)
 a · b as NumPy's reduction loop computes it, fused as loops says.
 
numpy_loops numerical::detail::numpy_wheel_loops () noexcept
 NumPy's loops as its current PyPI wheels build them on this processor (detail::numpy_fuses_products, detail::numpy_fuses_scalar_products, detail::numpy_fuses_quotients).
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_multiply (const tensor< C > &a, const tensor< C > &b, const numpy_loops &loops=numpy_wheel_loops())
 a · b of complex tensors, broadcast, each product as NumPy's array loop computes it (detail::numpy_multiply, fused as loops says): the binding's *, where std::complex's operator (this library's *) recovers infinities NumPy's makes NaN.
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_divide (const tensor< C > &a, const tensor< C > &b, const numpy_loops &loops=numpy_wheel_loops())
 a / b of complex tensors, broadcast, each quotient as NumPy's array loop computes it (detail::numpy_divide, fused as loops says): the binding's /.
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_prod (const tensor< C > &t, std::size_t axis, const numpy_loops &loops=numpy_wheel_loops())
 The products of a complex t along axis, each line in order from 1 (NumPy's identity, which turns an infinite element alone into a NaN part), each product as NumPy's loops compute it (detail::numpy_multiply): its reduction loop's where nothing follows the axis, its vector loop's (between whole slices) where something does; fused as loops says. The binding's prod, where std::complex's operator (prod) recovers infinities NumPy's makes NaN.
 
template<detail::split_complex C>
C numerical::detail::numpy_prod (const tensor< C > &t, const numpy_loops &loops=numpy_wheel_loops())
 The product of every element of a complex t, in order from 1 (NumPy's identity), as NumPy's reduction loop computes it (detail::numpy_multiply, fused as loops says).
 
template<detail::split_complex C>
tensor< typename C::value_type > numerical::detail::numpy_absolute (const tensor< C > &a, const numpy_loops &loops=numpy_wheel_loops())
 |x| of every element of a complex tensor as NumPy's absolute computes it (detail::numpy_absolute), fused as loops says of its vector loops.
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_square (const tensor< C > &a, const numpy_loops &loops=numpy_wheel_loops())
 x ** 2 of a complex tensor as NumPy's square computes it (the loop of an array to the Python integer 2): its product's vector loop, fused as loops says of it.
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_reciprocal (const tensor< C > &a, const numpy_loops &loops=numpy_wheel_loops())
 x ** -1 of a complex tensor as NumPy's reciprocal computes it (the loop of an array to the Python integer -1, detail::numpy_reciprocal), fused as loops says of its scalar loops.
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_power (const tensor< C > &a, const tensor< C > &b, const numpy_loops &loops=numpy_wheel_loops())
 a ** b of complex tensors, broadcast, as NumPy's power computes each (detail::numpy_power), fused as loops says of its scalar loops.
 
template<typename T >
tensor< T > numerical::detail::exp_of (const T *data, const std::vector< std::size_t > &shape)
 exp of every one of the row-major elements at data, of shape shape: elementary_exp of each.
 
tensor< double > numerical::detail::exp_of (const double *data, const std::vector< std::size_t > &shape)
 exp of every one of the row-major doubles at data, of shape shape: exp_into by chunks of elementwise_chunk among the threads.
 
template<typename T , typename Kernel >
tensor< T > numerical::detail::runs_into (const tensor< T > &t, const Kernel &kernel)
 kernel (kernel(in, out, count), of contiguous elements) of every element of t: a contiguous tensor at once by chunks of elementwise_chunk among the threads; a view lying close (lies_close; a slice, its rows apart) read where it lies, each run of its last axis passed where it is contiguous and gathered by pieces of 512 on the stack where its elements are apart; any other copied first. Each element alone: the bits of the same elements laid row-major (a slice of 200 000 doubles' exp copied first took a third more time).
 
tensor< double > numerical::detail::by_runs (const tensor< double > &a, doubles_into into)
 A function of every element of a, its kernel of doubles (runs_into).
 
tensor< float > numerical::detail::by_runs (const tensor< float > &a, doubles_into into)
 A function of every element of a, its kernel of doubles through doubles (through_doubles_piece).
 
template<typename T >
tensor< T > numerical::detail::exp_viewed (const tensor< T > &a)
 exp of every element of a: of a contiguous copy (exp_of).
 
tensor< double > numerical::detail::exp_viewed (const tensor< double > &a)
 exp of every double of a, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::expm1_viewed (const tensor< T > &a)
 expm1 of every element of a: of a contiguous copy (expm1_of).
 
tensor< double > numerical::detail::expm1_viewed (const tensor< double > &a)
 expm1 of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::expm1_viewed (const tensor< float > &a)
 expm1 of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::atan_viewed (const tensor< T > &a)
 atan of every element of a: of a contiguous copy (atan_of).
 
tensor< double > numerical::detail::atan_viewed (const tensor< double > &a)
 atan of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::atan_viewed (const tensor< float > &a)
 atan of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::sinh_viewed (const tensor< T > &a)
 sinh of every element of a: of a contiguous copy (sinh_of).
 
tensor< double > numerical::detail::sinh_viewed (const tensor< double > &a)
 sinh of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::sinh_viewed (const tensor< float > &a)
 sinh of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::cosh_viewed (const tensor< T > &a)
 cosh of every element of a: of a contiguous copy (cosh_of).
 
tensor< double > numerical::detail::cosh_viewed (const tensor< double > &a)
 cosh of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::cosh_viewed (const tensor< float > &a)
 cosh of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::exp2_viewed (const tensor< T > &a)
 exp2 of every element of a: of a contiguous copy (exp2_of).
 
tensor< double > numerical::detail::exp2_viewed (const tensor< double > &a)
 exp2 of every double of a, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::log1p_viewed (const tensor< T > &a)
 log1p of every element of a: of a contiguous copy (log1p_of).
 
tensor< double > numerical::detail::log1p_viewed (const tensor< double > &a)
 log1p of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::log1p_viewed (const tensor< float > &a)
 log1p of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::sin_viewed (const tensor< T > &a)
 sin of every element of a: of a contiguous copy (sin_of).
 
tensor< double > numerical::detail::sin_viewed (const tensor< double > &a)
 sin of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::sin_viewed (const tensor< float > &a)
 sin of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::cos_viewed (const tensor< T > &a)
 cos of every element of a: of a contiguous copy (cos_of).
 
tensor< double > numerical::detail::cos_viewed (const tensor< double > &a)
 cos of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::cos_viewed (const tensor< float > &a)
 cos of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::detail::tanh_viewed (const tensor< T > &a)
 tanh of every element of a: of a contiguous copy (tanh_of).
 
tensor< double > numerical::detail::tanh_viewed (const tensor< double > &a)
 tanh of every double of a, a view read where it lies (by_runs).
 
tensor< float > numerical::detail::tanh_viewed (const tensor< float > &a)
 tanh of every float of a through doubles, a view read where it lies (by_runs).
 
template<typename T >
tensor< T > numerical::exp (const tensor< T > &a)
 exp of every element: detail::exp_into of a double one (within 0.506 ulp of the exact value), std::exp of a float one, detail::complex_exp of a complex one.
 
template<std::floating_point T>
tensor< T > numerical::expm1 (const tensor< T > &a)
 exp(x) − 1 of every real element, exact near 0: detail::expm1_into of a double one (within 0.503 ulp of the exact value), the double's rounded once of a float one (detail::through_doubles, within half an ulp and 2⁻²⁸).
 
template<std::floating_point T>
tensor< T > numerical::atan (const tensor< T > &a)
 atan of every real element: detail::atan_into of a double one (within 0.501 ulp of the exact value), the double's rounded once of a float one (detail::through_doubles, within half an ulp and 2⁻²⁸).
 
template<std::floating_point T>
tensor< T > numerical::sinh (const tensor< T > &a)
 sinh of every real element: detail::sinh_into of a double one (within 0.503 ulp of the exact value, finite up to the overflow past 710.47), the double's rounded once of a float one (detail::through_doubles, within half an ulp and 2⁻²⁸).
 
template<std::floating_point T>
tensor< T > numerical::cosh (const tensor< T > &a)
 cosh of every real element: detail::cosh_into of a double one (within 0.501 ulp of the exact value, finite up to the overflow past 710.47), the double's rounded once of a float one (detail::through_doubles, within half an ulp and 2⁻²⁸).
 
template<std::floating_point T>
tensor< T > numerical::exp2 (const tensor< T > &a)
 2^x of every real element: detail::exp2_into of a double one (within 0.506 ulp of the exact value where it is normal, exact at every integer), std::exp2 of a float one.
 
template<typename T >
tensor< T > numerical::log (const tensor< T > &a)
 log of every element: std::log of a real one, detail::complex_log of a complex one.
 
template<std::floating_point T>
tensor< T > numerical::log1p (const tensor< T > &a)
 log(1 + x) of every real element, exact near 0: detail::log1p_into of a double one (within 0.501 ulp of the exact value), the double's rounded once of a float one (detail::through_doubles, within half an ulp and 2⁻²⁸).
 
template<typename T >
tensor< T > numerical::sin (const tensor< T > &a)
 sin of every element: std::sin of a real one (a float the double's, detail::sin_of), detail::complex_sin of a complex one.
 
template<typename T >
tensor< T > numerical::cos (const tensor< T > &a)
 cos of every element: std::cos of a real one (a float the double's, detail::cos_of), detail::complex_cos of a complex one.
 
template<typename T >
tensor< T > numerical::tan (const tensor< T > &a)
 tan of every element: std::tan of a real one, detail::complex_tan of a complex one.
 
template<typename T >
tensor< T > numerical::tanh (const tensor< T > &a)
 tanh of every element: detail::tanh_into of a double one (within 0.502 ulp of the exact value), the double's rounded once of a float one (detail::through_doubles, within half an ulp and 2⁻²⁸), detail::complex_tanh of a complex one.
 
template<typename T >
tensor< T > numerical::sqrt (const tensor< T > &a)
 The square root of every element: std::sqrt of a real one, detail::complex_sqrt of a complex one.
 
template<typename T >
tensor< T > numerical::operator+ (const tensor< T > &a, const tensor< T > &b)
 Element-wise sum, broadcast (a signed integer's wrapped, detail::arithmetic).
 
template<std::floating_point T>
T numerical::detail::ieee_maximum (T a, T b) noexcept
 The larger of a and b as IEEE 754-2019's maximum orders them: a NaN wins (a's where both are), and +0 is over −0 whatever their order (a + b of two zeros: −0 only where both are).
 
template<std::floating_point T>
T numerical::detail::ieee_minimum (T a, T b) noexcept
 The smaller of a and b as IEEE 754-2019's minimum orders them: a NaN wins (a's where both are), and −0 is under +0 whatever their order (−(−a − b) of two zeros: +0 only where both are).
 
template<std::floating_point T>
T numerical::detail::masked_select (bool condition, T if_true, T if_false) noexcept
 if_true where condition holds, else if_false, by a mask of their bits: no branch, so a loop of it vectorises (GCC 13 makes a ?: of reals a branch).
 
template<std::floating_point T>
T numerical::detail::clipped (T x, T low, T high) noexcept
 ieee_minimum of ieee_maximum of x and low, and high, the bounds no NaN: the same bits (a NaN x kept, zeros of the two signs ordered alike), by selections, so that clip's loop vectorises.
 
template<std::floating_point T>
tensor< T > numerical::maximum (const tensor< T > &a, const tensor< T > &b)
 The larger element of a and b, broadcast, as numpy.maximum (detail::ieee_maximum; a NaN wins, +0 over −0).
 
template<std::floating_point T>
tensor< T > numerical::minimum (const tensor< T > &a, const tensor< T > &b)
 The smaller element of a and b, broadcast, as numpy.minimum (detail::ieee_minimum; a NaN wins, −0 under +0).
 
template<std::floating_point R, typename F >
requires (!std::same_as<R, float>)
auto numerical::detail::with_decimal_rounding (int decimals, const F &f)
 f(rounding) with the decimal_rounding of R to decimals places: in R, as NumPy's, but in double for a float whose 10ᵈ overflows it (a float under 2²⁴ · 10⁻ᵈ still has places to round there).
 
template<std::same_as< float > R, typename F >
auto numerical::detail::with_decimal_rounding (int decimals, const F &f)
 with_decimal_rounding of a float: in double where its 10ᵈ overflows a float.
 
template<std::floating_point T>
tensor< T > numerical::round (const tensor< T > &a, int decimals=0)
 Every element rounded to decimals places (tens, hundreds for a negative one), half to even, as numpy.round; a value already exact to those places is kept (detail::decimal_rounding).
 
template<detail::split_complex C>
tensor< C > numerical::round (const tensor< C > &a, int decimals=0)
 Every element's real and imaginary parts rounded to decimals places apart, as numpy.round of a complex array.
 
template<std::floating_point T>
tensor< T > numerical::clip (const tensor< T > &a, T low, T high)
 a limited to [low, high] element by element, as numpy.clip of two numbers: minimum(maximum(a, low), high) in one pass, the same bits as the two (detail::ieee_maximum, then detail::ieee_minimum; a NaN wins, a NaN bound's too), by detail::clipped where no bound is a NaN.
 
template<typename T >
tensor< T > numerical::operator- (const tensor< T > &a, const tensor< T > &b)
 Element-wise difference, broadcast.
 
template<typename T >
tensor< T > numerical::operator* (const tensor< T > &a, const tensor< T > &b)
 Element-wise product, broadcast.
 
template<typename T >
tensor< T > numerical::operator/ (const tensor< T > &a, const tensor< T > &b)
 Element-wise quotient, broadcast.
 
template<std::floating_point T>
std::pair< T, T > numerical::detail::floor_divmod (T a, T b) noexcept
 a // b and a % b of reals as NumPy's divmod computes them (Python's: the quotient floored, the remainder of b's sign): fmod first, the quotient (a − mod) / b adjusted by the remainder's sign and snapped to the nearest integer, a zero of the right sign. b zero: a / b and fmod(a, b).
 
template<typename T >
tensor< T > numerical::floor_divide (const tensor< T > &a, const tensor< T > &b)
 a // b element by element, broadcast, as numpy.floor_divide (detail::floor_divmod).
 
template<typename T >
tensor< T > numerical::remainder (const tensor< T > &a, const tensor< T > &b)
 a % b element by element, broadcast, as numpy.remainder (Python's: of b's sign; a real by zero fmod's NaN, detail::floor_divmod).
 
template<typename T >
tensor< T > numerical::operator- (const tensor< T > &a)
 Element-wise negation.
 
template<typename T , typename F >
auto numerical::map (const tensor< T > &t, const F &f) -> tensor< std::decay_t< decltype(f(std::declval< const T & >()))> >
 f(x) for every element, in a tensor of the same shape and of f's result type.
 
template<typename T >
tensor< T > numerical::pow (const tensor< T > &a, const tensor< T > &b)
 a to the power b element by element, broadcast, as std::pow; a single exponent of 2, 0.5 or -1 as the library's own x * x, square root (detail::complex_sqrt for a complex one) and 1 / x (exact where pow rounds: a complex square has no stray real part). A complex one keeps C's Annex G, as the operators do: NumPy's loops are detail::numpy_square, detail::numpy_reciprocal and detail::numpy_power.
 
template<std::signed_integral T>
T numerical::detail::integer_power (T base, T exponent) noexcept
 base to the power exponent (not negative) by repeated squaring, each product wrapped modulo 2ⁿ as NumPy's integer power (arithmetic).
 
template<typename T >
T numerical::sum (const tensor< T > &t)
 The sum of every element, by pairwise halving (0 for an empty tensor), on the threads where they are many (detail::parallel_pairwise_sum): the same bits.
 
template<typename T >
tensor< T > numerical::sum (const tensor< T > &t, std::size_t axis)
 The sums along axis, by pairwise halving; a transpose's where its elements lie (detail::as_laid_reduced; each line's sum the same).
 
template<typename T >
T numerical::mean (const tensor< T > &t)
 The mean of every element. An empty tensor's mean is NaN, as NumPy's (which also warns).
 
template<typename T >
tensor< T > numerical::mean (const tensor< T > &t, std::size_t axis)
 The means along axis; a transpose's where its elements lie (detail::as_laid_reduced).
 
template<std::totally_ordered T>
T numerical::max (const tensor< T > &t)
 The largest element (a NaN, if there is one), of a transpose's elements as they lie (in any order the same).
 
template<std::totally_ordered T>
T numerical::min (const tensor< T > &t)
 The smallest element (a NaN, if there is one), of a transpose's elements as they lie (in any order the same).
 
template<typename T >
T numerical::prod (const tensor< T > &t)
 The product of every element, in order (numpy.prod's, whose multiplications are sequential: only its sums pair). An empty tensor's is 1.
 
template<typename T >
tensor< T > numerical::prod (const tensor< T > &t, std::size_t axis)
 The products along axis, each line in order; a transpose's where its elements lie (detail::as_laid_reduced).
 
template<typename T , typename Step >
tensor< T > numerical::detail::scan_along (const tensor< T > &t, std::size_t axis, const Step &step)
 Each line of t along axis replaced by its running step (x₀, step(x₀, x₁), …), in order.
 
template<detail::split_complex C>
tensor< C > numerical::detail::numpy_cumprod (const tensor< C > &t, std::size_t axis, const numpy_loops &loops=numpy_wheel_loops())
 The running products of a complex t along axis, in order, each as NumPy's loops compute it (detail::numpy_multiply): its vector loop's for a line of two (one product), its reduction loop's past it; fused as loops says. The binding's cumprod.
 
template<std::floating_point T>
bool numerical::detail::sorts_before (T a, T b) noexcept
 Whether a sorts before b as NumPy sorts reals: ascending, NaNs last.
 
template<split_complex C>
bool numerical::detail::sorts_before (const C &a, const C &b) noexcept
 Whether a sorts before b as NumPy sorts complex numbers (npysort's CLT): those with no NaN by the real parts then the imaginary parts, then those with a NaN imaginary part alone by the real parts, then a NaN real part alone by the imaginary parts, then both NaN (R + Rj < R + nanj < nan + Rj < nan + nanj).
 
template<typename T >
tensor< T > numerical::cumsum (const tensor< T > &t, std::size_t axis)
 The running sums along axis, in order, as numpy.cumsum.
 
template<typename T >
tensor< T > numerical::cumprod (const tensor< T > &t, std::size_t axis)
 The running products along axis, in order, as numpy.cumprod.
 
template<std::floating_point T>
order_key_t< T > numerical::detail::order_key (T x) noexcept
 x's bits as an unsigned integer ordered as x is by <: the sign bit set for a positive real, every bit flipped for a negative one. A NaN and the zeros' signs are the caller's: −0 orders below +0.
 
template<std::floating_point T>
T numerical::detail::from_order_key (order_key_t< T > key) noexcept
 The real whose order_key is key.
 
template<typename U >
void numerical::detail::count_bytes (byte_counts< U > &counts, U key) noexcept
 Counts key's bytes in counts. Every byte is counted in the one read that makes the keys, where a read per byte cost a fifth of the sort (10⁶ float64: 22.8 → 18.5 ms on x86-64, 12.1 → 9.5 on arm64).
 
std::size_t numerical::detail::part_begin (std::size_t count, std::size_t parts, std::size_t p) noexcept
 Where part p of parts sharing count elements begins: the first count % parts one longer.
 
template<typename U >
bool numerical::detail::radix_passes (U *keys, U *key_spare, std::int64_t *indices, std::int64_t *index_spare, std::size_t count, std::vector< byte_counts< U > > &counts)
 Sorts the count keys at keys stably by their bytes from the least significant, the indices at indices with them unless null, in counts.size() parts among the threads. Each pass places a part's keys of one byte value after the earlier parts' and moves each part's in its order, so equal keys keep their order whatever the parts: the bits of one counting sort. counts holds each part's counts of every byte of the keys as they lie, counted again for a part once a pass has moved them (one part's are the whole's, whatever their order); a byte that every key shares is skipped.
 
template<typename T >
void numerical::detail::radix_sort (T *first, T *last, std::size_t parts=1)
 Sorts [first, last) by order_key, a byte at a time from the least significant (by radix_passes, a stable counting sort each, so equal keys keep their order; a byte that every key shares skipped), in parts among the threads.
 
template<std::floating_point T>
void numerical::detail::sort_run (T *first, T *last, std::size_t parts=1)
 Sorts [first, last) stably as sorts_before orders reals: the NaNs moved behind the rest in their order first, the rest by <, which tests for no NaN. A long run by radix_sort, its zeros then rewritten with their signs in their order (the bits order −0 below +0, where < holds them equal); a short one by std::stable_sort with a lambda of its own, not the default std::less, which libc++ 22 sorts by the bits from 1024 doubles on, −0 first. The radix sort in parts among the threads.
 
template<std::floating_point T>
order_key_t< T > numerical::detail::argsort_key (T x) noexcept
 A real's order_key for an argsort: both zeros the same key, as < holds them equal (their indices then order them).
 
order_key_t< std::int64_t > numerical::detail::argsort_key (std::int64_t x) noexcept
 An integer's order_key for an argsort.
 
template<std::floating_point T>
std::int64_t * numerical::detail::numbers_end (std::int64_t *first, std::int64_t *last, const T *values, std::size_t stride)
 Where a real's NaNs begin among the indices [first, last), moved behind the rest in their order.
 
std::int64_t * numerical::detail::numbers_end (std::int64_t *, std::int64_t *last, const std::int64_t *, std::size_t)
 An integer has no NaN: every index is a number's.
 
template<radix_orderable T, typename Less >
void numerical::detail::order_run (std::int64_t *first, std::int64_t *last, const T *values, std::size_t stride, const Less &less, std::size_t parts=1)
 Sorts the indices [first, last) stably by their elements (values[k · stride]), as sorts_before orders them: comparisons for a run shorter than radix_sort_least, else the NaNs set apart in their order and the rest by argsort_key, a byte at a time (radix_passes, in parts among the threads: a stable counting sort of the keys and their indices together; a byte that every key shares skipped): equal elements by index either way.
 
template<std::integral T>
void numerical::detail::sort_run (T *first, T *last)
 Sorts [first, last) stably as integers sort.
 
void numerical::detail::sort_run (std::int64_t *first, std::int64_t *last, std::size_t parts=1)
 Sorts [first, last) as int64 sort: a long run by radix_sort in parts among the threads (equal integers are the same bits, so any order of them is the stable one).
 
template<split_complex C>
void numerical::detail::sort_run (C *first, C *last)
 Sorts [first, last) stably as sorts_before orders complex numbers.
 
std::size_t numerical::detail::merged_pieces (std::size_t threads) noexcept
 The pieces parallel_sort_run sorts a line in for threads: the least power of four that many.
 
template<typename T , typename SortRun , typename Less >
void numerical::detail::parallel_sort_run (T *data, std::size_t count, std::size_t pieces, const SortRun &sort_piece, const Less &less)
 Sorts the count elements at data stably in pieces, a power of four, among the threads: each piece sorted apart by sort_piece, then the pieces merged in pairs by less (the order sort_piece sorts by), round after round, between data and a scratch buffer (std::merge takes the first piece's element of two equal ones, so the merge keeps their order). The rounds are even in number, so the last writes data.
 
template<radix_orderable T>
void numerical::detail::sort_shared (T *run, std::size_t count, std::size_t threads)
 Sorts one long line of count elements at run among threads: each pass of its radix sort shared out (radix_passes), the one-thread sort's bits with no merge.
 
template<radix_orderable T, typename Less >
void numerical::detail::order_shared (std::int64_t *order, std::size_t count, const T *values, std::size_t stride, const Less &less, std::size_t threads)
 Sorts the indices of one long line among threads: each pass of its radix sort shared out.
 
template<typename T >
tensor< T > numerical::sort (const tensor< T > &t, std::size_t axis)
 The elements sorted along axis, as numpy.sort: ascending, NaNs last, equal elements in their order (NumPy's kind="stable"; its default sort may order equal ones otherwise, which only a sign of zero shows).
 
template<typename T >
tensor< std::int64_t > numerical::argsort (const tensor< T > &t, std::size_t axis)
 The indices that sort the elements along axis, as numpy.argsort (its kind="stable"): of each line, in the order sort puts its elements, equal ones by index. A long line is sorted among the threads (detail::parallel_sort_run).
 
std::vector< std::size_t > numerical::detail::partition_positions (std::vector< std::size_t > kths, std::size_t extent)
 kths ascending and each once, every one checked against extent.
 
template<typename It , typename Less >
void numerical::detail::select_positions (It first, std::size_t count, const std::vector< std::size_t > &kths, const Less &less)
 Rearranges [first, first + count) by less so that each of the ascending kths holds the element a sort puts there: std::nth_element over what lies past the previous one.
 
template<typename T >
tensor< T > numerical::partition (const tensor< T > &t, const std::vector< std::size_t > &kths, std::size_t axis)
 The elements of each line along axis rearranged as numpy.partition: at each of kths the element sort puts there (NaNs last, a complex number by its real part then its imaginary part), none before it that sorts after it, none after it that sorts before; the others in no order specified. By selection, std::nth_element between neighbouring positions, linear in the extent where sort is not.
 
template<typename T >
tensor< std::int64_t > numerical::argpartition (const tensor< T > &t, const std::vector< std::size_t > &kths, std::size_t axis)
 The indices that partition each line along axis, as numpy.argpartition: of the elements partition would put at each of kths and around it, by selection; equal elements in no order specified.
 
template<typename T >
T numerical::detail::conjugated_chunks (const T *a, const T *b, std::size_t count)
 Σ a[i] · b[i] of reals, their own conjugates: chunked_dot.
 
template<split_complex C>
C numerical::detail::conjugated_chunks (const C *a, const C *b, std::size_t count)
 Σ conj(a[i]) · b[i] of complex numbers: chunked_conjugate_dot.
 
template<typename T >
T numerical::vdot (const tensor< T > &a, const tensor< T > &b)
 Σ conj(a[i]) · b[i] over every element of a and b in their row-major order, as numpy.vdot: the plain sum of products for reals, the conjugated one for complex numbers without a conjugated copy of a (detail::chunked_conjugate_dot, BLAS's zdotc), both by chunks among the threads, the same on any number of them.
 
template<typename T >
tensor< std::int64_t > numerical::argsort_by (const tensor< std::int64_t > &order, const tensor< T > &key, std::size_t axis)
 order, indices along axis into key of its shape, each line's sorted stably by the elements of key they index, as numpy.argsort sorts (NaNs last): equal elements keep their places in order. A sort by the keys of numpy.lexsort in turn, from its least significant, each reading the key where it lies (no gather of the key into the order before its sort). The lines on the threads, or one long line among them.
 
template<std::floating_point T>
bool numerical::detail::wins_any_order (T x) noexcept
 A real's NaN, which wins numpy.argmax and numpy.argmin.
 
template<typename T , typename Match >
bool numerical::detail::holds (const T *first, std::size_t count, const Match &match) noexcept
 Whether one of the count contiguous elements at first passes match: tested without a branch, so the loop vectorises.
 
template<typename T , typename Match >
std::size_t numerical::detail::first_held (const T *first, std::size_t count, const Match &match) noexcept
 The index of the first of the contiguous elements at first that passes match, which one of them does: blocks of 64 tested by holds, then the block that holds it walked.
 
template<std::floating_point T, typename Order >
std::int64_t numerical::detail::extreme_index (const T *first, std::size_t count, const Order &larger)
 The index of the first largest (larger std::greater) or smallest of count contiguous elements, as NumPy's: the first NaN where there is one. Reals in two passes without a branch each, as fast as NumPy's vectors: their extreme (contiguous_extreme, a NaN where there is one), then the first element that is it (zeros of both signs equal, so the first of either, as NumPy's).
 
template<typename T , typename Order >
tensor< std::int64_t > numerical::detail::extreme_indices (const tensor< T > &t, std::size_t axis, const Order &larger)
 argmax or argmin along axis by larger.
 
template<typename T , typename Order >
std::int64_t numerical::detail::parallel_extreme_index (const T *first, std::size_t count, const Order &larger)
 extreme_index of the count contiguous elements at first, by chunks on the threads: each chunk's own, then the first of the chunks' (a NaN's chunk wins at once, the earliest of equal ones otherwise).
 
template<typename T >
std::int64_t numerical::argmax (const tensor< T > &t)
 The flat index of the first largest element, as numpy.argmax (the first NaN where there is one).
 
template<typename T >
std::int64_t numerical::argmin (const tensor< T > &t)
 The flat index of the first smallest element, as numpy.argmin (the first NaN where there is one).
 
template<typename T >
tensor< std::int64_t > numerical::argmax (const tensor< T > &t, std::size_t axis)
 The index of the first largest element of every line along axis, as numpy.argmax.
 
template<typename T >
tensor< std::int64_t > numerical::argmin (const tensor< T > &t, std::size_t axis)
 The index of the first smallest element of every line along axis, as numpy.argmin.
 
template<typename T >
std::vector< tensor< std::int64_t > > numerical::nonzero (const tensor< T > &t)
 The indices of the nonzero elements (x != T {}: a NaN is one), one tensor per axis, in C order, as numpy.nonzero: by chunks on the threads, each chunk's counted, then its indices written at its offset. A chunk walks its elements' indices as an odometer (one division per axis at its start, none after) and writes every element's, the place advancing by one where it is nonzero, without a branch; into a buffer of its own, whose last write may run one past its part.
 
template<typename T >
std::size_t numerical::count_nonzero (const tensor< T > &t)
 The number of nonzero elements (x != T {}: a NaN is one), as numpy.count_nonzero without an axis: by chunks on the threads, each chunk's counted without a branch, then the counts summed; a transpose's as its elements lie (tensor::laid_out), none copied.
 
template<typename T >
tensor< std::int64_t > numerical::count_nonzero (const tensor< T > &t, std::size_t axis)
 The number of nonzero elements (x != T {}) along axis, as numpy.count_nonzero.
 
template<typename T >
tensor< T > numerical::take_along_axis (const tensor< T > &t, const tensor< std::int64_t > &indices, std::size_t axis)
 The elements of t at indices along axis, the other axes matched, as numpy.take_along_axis: indices of t's rank and shape but along the axis (any extent there), each in [0, extent); read directly, on the threads.
 
template<typename T >
tensor< T > numerical::take (const tensor< T > &t, const tensor< std::int64_t > &indices, std::size_t axis, take_mode mode)
 The elements of t at indices along axis, as numpy.take: the result's shape t's with the axis replaced by indices' shape. Under take_mode::raise a negative index counts from the axis's end; under wrap an index is taken modulo the extent; under clip, at the nearer end. Each index read once, then the elements copied on the threads.
 
template<std::floating_point T>
T numerical::detail::stable_choice (const T *row, std::size_t count, std::size_t k, T chosen)
 Of the count elements at row, the one a stable sort (sort_run) puts at k, from the one chosen that a selection put there, equal to it under sorts_before. A zero (either sign) or a NaN (any payload) is not alone in its class, and the class's member of the same rank in row's order is the stable sort's.
 
template<typename T >
requires std::floating_point<T> || std::integral<T>
void numerical::detail::select_row (const T *row, std::size_t count, const std::int64_t *at, std::size_t m, T *work, T *out)
 Into out, the elements of the count at row that a stable sort puts at the m positions at, work holding count elements of scratch: std::nth_element at each distinct position, smallest first, each over the part above the one before (the least or the largest of that part by a pass alone), then stable_choice; more positions than select_positions_sorted, the row sorted whole.
 
template<split_complex C>
void numerical::detail::select_row (const C *row, std::size_t count, const std::int64_t *at, std::size_t m, C *work, C *out)
 A complex row is sorted whole: its classes under sorts_before are zeros and NaNs in either part.
 
template<typename T >
tensor< T > numerical::select_along_last (const tensor< T > &t, const tensor< std::int64_t > &positions)
 Of each line along the last axis of t, the elements its stable sort (sort) puts at the positions positions names, as sort(t)[..., positions] reads them, without the sort: each line's positions found by selection (detail::select_row), the lines on the threads. positions has shape (m,), the same for every line, or the lines' shape and m; the result, the lines' shape and m.
 
template<typename T >
T numerical::detail::correlation_step (const T &sum, const T &x, const T &y)
 sum + x · y of a real or an integer (wrapped) correlation's terms.
 
template<split_complex C>
C numerical::detail::correlation_step (const C &sum, const C &x, const C &y)
 sum + x · conj(y) of a complex correlation's terms, NumPy's product rounded.
 
template<typename T >
tensor< T > numerical::correlate (const tensor< T > &a, const tensor< T > &v, std::ptrdiff_t first, std::size_t count)
 The cross-correlation c[k] = Σₙ a[n + k] · conj(v[n]) of two vectors over the lags first … first + count − 1 (from −(len v − 1), the full range's first, to len a − 1), as numpy.correlate's: each lag's sum in the order of v's terms, a complex product NumPy's rounded one (no infinity recovered), an integer's wrapped; the lags on the threads, by blocks (detail::correlation_block lags), those of a block where v lies within a summed together, a term of each per term of v (in vectors, each lag's sum still its own), the others one by one.
 
template<typename T >
tensor< std::int64_t > numerical::searchsorted (const tensor< T > &a, const tensor< T > &v, bool right=false)
 Where each element of v would go in the sorted line a to keep it sorted, as numpy.searchsorted: the first such place (right false) or the last, in detail::sorts_before's order (NaNs last).
 
template<std::totally_ordered T>
tensor< T > numerical::max (const tensor< T > &t, std::size_t axis)
 The largest elements along axis; a transpose's where its elements lie (detail::as_laid_reduced).
 
template<std::totally_ordered T>
tensor< T > numerical::min (const tensor< T > &t, std::size_t axis)
 The smallest elements along axis; a transpose's where its elements lie (detail::as_laid_reduced).
 
template<typename T >
tensor< T > numerical::broadcast_to (const tensor< T > &t, std::vector< std::size_t > shape)
 t repeated along its axes of extent 1 and new leading ones to shape, as numpy.broadcast_to (a copy: a tensor's elements are contiguous): each row of the result copied from its row of t, or filled with its one element where the last axis broadcasts, the rows on the threads.
 
template<typename T >
tensor< T > numerical::reshape (const tensor< T > &t, std::vector< std::size_t > shape)
 The same elements in the same order under another shape, shared with t (no copy).
 
template<typename T >
tensor< T > numerical::diagonal (const tensor< T > &t, std::ptrdiff_t offset)
 The offset diagonal of each matrix of t (its last two axes), as numpy.linalg.diagonal: above the main diagonal for a positive offset, below for a negative one: a view (tensor::diagonal_view), no element copied but a few; the batch's shape, then the diagonal's length (0 where offset passes the matrix).
 
std::size_t numerical::detail::triangle_split (std::size_t i, std::size_t rows, std::size_t cols, std::ptrdiff_t k, bool lower)
 The first column of row i past the part to and below its k diagonal (lower), or the first column of the part to and above it (else), within [0, cols]; k clamped first, so i + k cannot overflow.
 
template<typename T >
tensor< T > numerical::triangle (const tensor< T > &t, std::ptrdiff_t k, bool lower)
 The matrices of t (its last two axes) zero above their k diagonal (lower) or below it (else), as numpy.tril and numpy.triu: each row's kept part copied and the rest zeroed, by rows on the threads.
 
std::array< tensor< std::int64_t >, 2 > numerical::triangle_indices (std::size_t rows, std::size_t cols, std::ptrdiff_t k, bool lower)
 The row and the column indices of the rows × cols matrix's part to and below its k diagonal (lower) or to and above it (else), in row-major order, as numpy.tril_indices and numpy.triu_indices: each row's count, their offsets, then the rows written on the threads.
 
template<typename T >
tensor< T > numerical::full (std::vector< std::size_t > shape, const T &value)
 A tensor of shape with every element value.
 
template<typename T >
tensor< T > numerical::zeros (std::vector< std::size_t > shape)
 A tensor of shape full of zeros (each thread's share zeroed as the C library zeroes).
 
template<typename T >
tensor< T > numerical::diag (const tensor< T > &v, std::ptrdiff_t k)
 The square matrix with the vector v on its k diagonal (above the main one for a positive k) and zeros elsewhere, as numpy.diag of a vector: of order len v + |k|.
 
template<typename T >
tensor< T > numerical::ones (std::vector< std::size_t > shape)
 A tensor of shape full of ones.
 
template<typename T >
tensor< T > numerical::eye (std::size_t n)
 The n × n identity.
 
tensor< double > numerical::arange (double start, double stop, double step, bool fused=true)
 start, start + step, … below stop (above it for a negative step), as NumPy's arange: its length is ceil((stop − start) / step), its first two elements start itself (a zero's sign kept) and start + step, and element i after them start + i·delta with delta = (start + step) − start, the step as the first two elements hold it: rounded once where fused (a fused multiply-add, as NumPy's macOS arm64 wheels compute it), else the product rounded first (NumPy's other wheels), which can differ in the last bits.
 
tensor< double > numerical::linspace (double start, double stop, std::size_t count)
 count values evenly spaced from start to stop, both included, as NumPy's linspace: element i is start + i·step with step = (stop − start) / (count − 1), and the last is stop exactly.
 

Variables

constexpr double numerical::detail::exp2_normal_bound {-1022.0}
 2^x below which exp2 is subnormal, where table_exp2's two scalings would round twice.
 
constexpr std::size_t numerical::detail::band_columns {256}
 The columns of a band that one task of fold_along and scan_along walks row by row.
 
constexpr std::size_t numerical::detail::radix_sort_least {256}
 Runs this long or longer are sorted by their bits (radix_sort); shorter ones by comparisons.
 
template<typename U >
constexpr U numerical::detail::sign_bit {U {1} << ((sizeof(U) * 8) - 1)}
 The highest bit of U, a real's or an integer's sign.
 
constexpr std::size_t numerical::detail::select_positions_sorted {8}
 A line with more positions than these is sorted whole rather than selected position by position.
 
constexpr std::size_t numerical::detail::correlation_block {16}
 The lags numerical::correlate sums together: a block's sums held in registers (four AVX2 vectors of doubles) through the terms of v.
 

Detailed Description

Element-wise arithmetic with NumPy's broadcasting, reductions, reshaping and creation of tensors.

Two shapes broadcast as NumPy's do: aligned on their last axes, each pair of extents equal or one of them 1 (a missing leading axis counts as 1), the result taking the larger. A sum adds by pairwise halving, as NumPy's reference algorithm does (NumPy's vector builds group the terms their own way, so the last bits can differ by platform): its rounding error grows with the logarithm of the count instead of the count. A maximum or a minimum is defined for an ordered scalar only, and an empty reduction without an identity throws.

Definition in file tensor_ops.hpp.