|
Stratax 0.4.0rc1
|
Global and axis reductions in logical row-major order. More...
#include <stratax/core/dtypes/Concepts.hpp>#include <stratax/exceptions/Exceptions.hpp>#include <stratax/core/Shape.hpp>#include <stratax/containers/Tensor.hpp>#include <stratax/containers/Matrix.hpp>#include <stratax/containers/Vector.hpp>#include <stratax/core/Slice.hpp>#include <stratax/indexing/Slicing.hpp>#include <stratax/algorithms/Conversion.hpp>#include <stratax/core/ReductionTraits.hpp>#include <numeric>#include <functional>#include <cmath>#include <type_traits>#include <utility>

Go to the source code of this file.
Typedefs | |
| template<Array A, typename Func > | |
| using | reduction::detail::axis_reduce_value_t = decltype(std::declval< Func >()(std::declval< const stratax::container::Tensor< typename A::value_type > & >())) |
| Result type produced by invoking an axis-reduction callback. | |
Functions | |
| template<bool FindMaximum, Array A> requires Ordered<typename A::value_type> | |
| std::pair< typename A::value_type, std::size_t > | reduction::detail::ordered_extreme (const A &arr) |
| Selects an extreme value and its logical index with an ordered scan. | |
| bool | reduction::detail::advance (const stratax::core::Shape &shape, std::vector< std::size_t > &indices) |
| Advances a multidimensional index in row-major order. | |
| template<Array A> | |
| int | reduction::detail::normalize_axis (const A &arr, int axis) |
| Converts a possibly negative axis to its non-negative equivalent. | |
| template<Array A> | |
| std::vector< std::size_t > | reduction::detail::result_shape (const A &arr, int axis, bool keepdims) |
| Computes the dimensions produced by reducing one normalized axis. | |
| template<Array A, typename Func > | |
| stratax::container::Tensor< detail::axis_reduce_value_t< A, Func > > | reduction::axis_reduce (const A &array, int axis, Func func, bool keepdims=false) |
| Applies a scalar reduction callback independently along one axis. | |
| template<Array A> requires Numeric<typename A::value_type> | |
| auto | reduction::sum (const A &arr) |
| Returns the sum of all elements in flat iterator order. | |
| template<Array A> requires Numeric<typename A::value_type> | |
| auto | reduction::prod (const A &arr) |
| Returns the product of all elements in flat iterator order. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::max (const A &arr) |
| Returns the largest element. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::min (const A &arr) |
| Returns the smallest element. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::argmax (const A &arr) |
| Returns the flat index of the largest element. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::argmin (const A &arr) |
| Returns the flat index of the smallest element. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| double | reduction::mean (const A &arr) |
| Returns the arithmetic mean of all elements as double. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| double | reduction::var (const A &arr) |
| Returns the population variance using Welford's online algorithm. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| double | reduction::std (const A &arr) |
| Returns the population standard deviation of all elements. | |
| template<Array A> requires Numeric<typename A::value_type> | |
| auto | reduction::sum (const A &arr, int axis) |
| Sums values along an axis and removes that dimension. | |
| template<Array A> requires Numeric<typename A::value_type> | |
| auto | reduction::sum (const A &arr, int axis, bool keepdims) |
| Sums values along an axis with optional dimension retention. | |
| template<Array A> requires Numeric<typename A::value_type> | |
| auto | reduction::prod (const A &arr, int axis) |
| Multiplies values along an axis and removes that dimension. | |
| template<Array A> requires Numeric<typename A::value_type> | |
| auto | reduction::prod (const A &arr, int axis, bool keepdims) |
| Multiplies values along an axis with optional dimension retention. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::max (const A &arr, int axis) |
| Finds maximum values along an axis and removes that dimension. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::max (const A &arr, int axis, bool keepdims) |
| Finds maximum values along an axis with optional dimension retention. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::min (const A &arr, int axis) |
| Finds minimum values along an axis and removes that dimension. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::min (const A &arr, int axis, bool keepdims) |
| Finds minimum values along an axis with optional dimension retention. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::argmax (const A &arr, int axis) |
| Finds indices of maximum values along an axis. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::argmax (const A &arr, int axis, bool keepdims) |
| Finds indices of maximum values with optional dimension retention. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::argmin (const A &arr, int axis) |
| Finds indices of minimum values along an axis. | |
| template<Array A> requires Ordered<typename A::value_type> | |
| auto | reduction::argmin (const A &arr, int axis, bool keepdims) |
| Finds indices of minimum values with optional dimension retention. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| stratax::container::Tensor< double > | reduction::mean (const A &arr, int axis, bool keepdims) |
| Computes arithmetic means along an axis with optional dimension retention. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| stratax::container::Tensor< double > | reduction::mean (const A &arr, int axis) |
| Computes arithmetic means along an axis and removes that dimension. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| stratax::container::Tensor< double > | reduction::var (const A &arr, int axis, bool keepdims) |
| Computes population variances with optional dimension retention. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| stratax::container::Tensor< double > | reduction::var (const A &arr, int axis) |
| Computes population variances along an axis and removes that dimension. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| stratax::container::Tensor< double > | reduction::std (const A &arr, int axis, bool keepdims) |
| Computes population standard deviations with optional dimension retention. | |
| template<Array A> requires ( Numeric<typename A::value_type> && Ordered<typename A::value_type> ) | |
| stratax::container::Tensor< double > | reduction::std (const A &arr, int axis) |
| Computes population standard deviations and removes the reduced dimension. | |
Global and axis reductions in logical row-major order.
Definition in file Reductions.hpp.
| using reduction::detail::axis_reduce_value_t = typedef decltype(std::declval<Func>()( std::declval<const stratax::container::Tensor<typename A::value_type>&>())) |
Result type produced by invoking an axis-reduction callback.
| A | Source array type satisfying Array. |
| Func | Callable accepting a const Tensor of A::value_type. |
Definition at line 178 of file Reductions.hpp.
|
inline |
Advances a multidimensional index in row-major order.
| shape | Extents that bound each component of indices. |
| indices | Index updated in place, beginning with the final dimension. |
true when another index exists; false after wrapping to all zeros. indices.size() == shape.rank() and every component is in range. Definition at line 82 of file Reductions.hpp.
Returns the flat index of the largest element.
| A | Stratax array type satisfying Array whose values are ordered. |
| arr | Non-empty array to search. |
stratax::dtype::int64; ties choose the first largest value. | Exceptions::IndexError | If arr is empty. |
Definition at line 385 of file Reductions.hpp.
| auto reduction::argmax | ( | const A & | arr, |
| int | axis | ||
| ) |
Finds indices of maximum values along an axis.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
Tensor<stratax::dtype::int64> containing the first maximum's zero-based index within each axis slice. | Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 692 of file Reductions.hpp.
| auto reduction::argmax | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Finds indices of maximum values with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
Tensor<stratax::dtype::int64> containing the first maximum's zero-based index within each axis slice. | Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 710 of file Reductions.hpp.
Returns the flat index of the smallest element.
| A | Stratax array type satisfying Array whose values are ordered. |
| arr | Non-empty array to search. |
stratax::dtype::int64; ties choose the first smallest value. | Exceptions::IndexError | If arr is empty. |
Definition at line 406 of file Reductions.hpp.
| auto reduction::argmin | ( | const A & | arr, |
| int | axis | ||
| ) |
Finds indices of minimum values along an axis.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
Tensor<stratax::dtype::int64> containing the first minimum's zero-based index within each axis slice. | Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 730 of file Reductions.hpp.
| auto reduction::argmin | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Finds indices of minimum values with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
Tensor<stratax::dtype::int64> containing the first minimum's zero-based index within each axis slice. | Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 748 of file Reductions.hpp.
| stratax::container::Tensor< detail::axis_reduce_value_t< A, Func > > reduction::axis_reduce | ( | const A & | array, |
| int | axis, | ||
| Func | func, | ||
| bool | keepdims = false |
||
| ) |
Applies a scalar reduction callback independently along one axis.
Negative axes count backward from the final dimension. Each output position is computed from a strided view into an initial owning copy of the input. With keepdims false the axis is removed; with it true the axis has extent one. Because rank-zero tensors cannot store a scalar in the current container model, reducing a rank-one array without keepdims returns shape {1}. Empty output domains return immediately without invoking func. The callback result type determines the returned tensor's value type.
| A | Source array or view satisfying Array. |
| Func | Callable accepting both Tensor and ArrayView inputs and returning a supported dtype. A generic lambda is appropriate. |
| array | Source array. |
| axis | Axis in [-array.rank(), array.rank() - 1]. |
| func | Scalar reduction applied to every axis slice. |
| keepdims | Whether the reduced dimension remains with extent one. |
Tensor containing one callback result per output position. Its value type is inferred from invoking func on a const Tensor<A::value_type>. Actual calls receive a Tensor for the rank-one, keepdims-false case and an ArrayView otherwise. | Exceptions::AxisError | If axis is outside the valid range. |
| Any | exception propagated by conversion, slicing, allocation, or func. |
func is not invoked when the result shape has zero elements. Definition at line 215 of file Reductions.hpp.
Returns the largest element.
| A | Stratax array type satisfying Array whose values are ordered. |
| arr | Non-empty array to search. |
| Exceptions::IndexError | If arr is empty. |
Definition at line 341 of file Reductions.hpp.
| auto reduction::max | ( | const A & | arr, |
| int | axis | ||
| ) |
Finds maximum values along an axis and removes that dimension.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 616 of file Reductions.hpp.
| auto reduction::max | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Finds maximum values along an axis with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 634 of file Reductions.hpp.
| double reduction::mean | ( | const A & | arr | ) |
Returns the arithmetic mean of all elements as double.
| A | Array whose value_type is Numeric and Ordered (real numeric values). |
| arr | Non-empty array to reduce. |
| Exceptions::ZeroDivisionError | If arr is empty. |
Definition at line 435 of file Reductions.hpp.
| stratax::container::Tensor< double > reduction::mean | ( | const A & | arr, |
| int | axis | ||
| ) |
Computes arithmetic means along an axis and removes that dimension.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::ZeroDivisionError | If a produced output has an empty slice. |
Definition at line 797 of file Reductions.hpp.
| stratax::container::Tensor< double > reduction::mean | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Computes arithmetic means along an axis with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::ZeroDivisionError | If a produced output has an empty slice. |
Definition at line 773 of file Reductions.hpp.
Returns the smallest element.
| A | Stratax array type satisfying Array whose values are ordered. |
| arr | Non-empty array to search. |
| Exceptions::IndexError | If arr is empty. |
Definition at line 364 of file Reductions.hpp.
| auto reduction::min | ( | const A & | arr, |
| int | axis | ||
| ) |
Finds minimum values along an axis and removes that dimension.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 654 of file Reductions.hpp.
| auto reduction::min | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Finds minimum values along an axis with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::IndexError | If a produced output requires reducing an empty slice. |
Definition at line 672 of file Reductions.hpp.
|
inline |
Converts a possibly negative axis to its non-negative equivalent.
| A | Stratax array type satisfying Array. |
| arr | Array whose rank defines the negative-axis offset. |
| axis | Axis in the range [-arr.rank(), arr.rank() - 1] when valid. |
axis unchanged when non-negative, otherwise axis + arr.rank(). Definition at line 112 of file Reductions.hpp.
| std::pair< typename A::value_type, std::size_t > reduction::detail::ordered_extreme | ( | const A & | arr | ) |
Selects an extreme value and its logical index with an ordered scan.
The first value initializes the candidate. Each later value replaces it only when a strict comparison succeeds. This preserves the first occurrence on ties, including signed zeros. With IEEE-style NaNs, an initial NaN remains selected and later NaNs never replace the candidate. The explicit scan does not depend on standard-library extrema algorithms' treatment of NaNs.
| FindMaximum | Selects the largest value when true, otherwise the smallest. |
| A | Array or view with an Ordered value_type. |
| arr | Non-empty input in logical row-major iterator order. |
arr is non-empty. Definition at line 49 of file Reductions.hpp.
Returns the product of all elements in flat iterator order.
| A | Stratax array type satisfying Array. |
| arr | Array to reduce. |
reduction_prod_t<A::value_type>, or that type's multiplicative identity when arr is empty. Definition at line 316 of file Reductions.hpp.
| auto reduction::prod | ( | const A & | arr, |
| int | axis | ||
| ) |
Multiplies values along an axis and removes that dimension.
| A | Numeric Stratax array type. |
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
Tensor<reduction_prod_t<A::value_type>> of per-slice products; empty slices produce the selected type's multiplicative identity. | Exceptions::AxisError | If axis is invalid. |
Definition at line 571 of file Reductions.hpp.
| auto reduction::prod | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Multiplies values along an axis with optional dimension retention.
| A | Numeric Stratax array type. |
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
Tensor<reduction_prod_t<A::value_type>> of per-slice products; empty slices produce the selected type's multiplicative identity. | Exceptions::AxisError | If axis is invalid. |
Definition at line 594 of file Reductions.hpp.
|
inline |
Computes the dimensions produced by reducing one normalized axis.
| A | Stratax array type satisfying Array. |
| arr | Array providing the input shape. |
| axis | Valid, non-negative axis to reduce. |
| keepdims | Whether to retain the reduced axis with extent one. |
axis removed or replaced by one. | std::bad_alloc | If result-dimension allocation fails. |
Definition at line 135 of file Reductions.hpp.
| double reduction::std | ( | const A & | arr | ) |
Returns the population standard deviation of all elements.
| A | Array whose value_type is Numeric and Ordered (real numeric values). |
| arr | Non-empty array to reduce. |
var(arr). | Exceptions::ZeroDivisionError | If arr is empty. |
Definition at line 504 of file Reductions.hpp.
| stratax::container::Tensor< double > reduction::std | ( | const A & | arr, |
| int | axis | ||
| ) |
Computes population standard deviations and removes the reduced dimension.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::ZeroDivisionError | If a produced output has an empty slice. |
Definition at line 889 of file Reductions.hpp.
| stratax::container::Tensor< double > reduction::std | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Computes population standard deviations with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::ZeroDivisionError | If a produced output has an empty slice. |
Definition at line 865 of file Reductions.hpp.
Returns the sum of all elements in flat iterator order.
| A | Stratax array type satisfying Array. |
| arr | Array to reduce. |
reduction_sum_t<A::value_type>, or that type's additive identity when arr is empty. Definition at line 295 of file Reductions.hpp.
| auto reduction::sum | ( | const A & | arr, |
| int | axis | ||
| ) |
Sums values along an axis and removes that dimension.
| A | Numeric Stratax array type. |
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
Tensor<reduction_sum_t<A::value_type>> with the axis removed (shape {1} for rank-one input); empty slices produce the selected type's additive identity. | Exceptions::AxisError | If axis is invalid. |
Definition at line 525 of file Reductions.hpp.
| auto reduction::sum | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Sums values along an axis with optional dimension retention.
| A | Numeric Stratax array type. |
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
Tensor<reduction_sum_t<A::value_type>> of per-slice sums; empty slices produce the selected type's additive identity. | Exceptions::AxisError | If axis is invalid. |
Definition at line 548 of file Reductions.hpp.
| double reduction::var | ( | const A & | arr | ) |
Returns the population variance using Welford's online algorithm.
| A | Array whose value_type is Numeric and Ordered (real numeric values). |
| arr | Non-empty array to reduce. |
arr.size(). mean(x*x) - mean(x)*mean(x), but rounding, conversion loss, and overflow remain possible. NaNs and infinite inputs generally produce NaN; this is not a NaN-skipping reduction. | Exceptions::ZeroDivisionError | If arr is empty. |
Definition at line 468 of file Reductions.hpp.
| stratax::container::Tensor< double > reduction::var | ( | const A & | arr, |
| int | axis | ||
| ) |
Computes population variances along an axis and removes that dimension.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::ZeroDivisionError | If a produced output has an empty slice. |
Definition at line 843 of file Reductions.hpp.
| stratax::container::Tensor< double > reduction::var | ( | const A & | arr, |
| int | axis, | ||
| bool | keepdims | ||
| ) |
Computes population variances with optional dimension retention.
| arr | Source array. |
| axis | Axis to reduce; negative values count from the end. |
| keepdims | Retains the reduced axis with extent one when true. |
| Exceptions::AxisError | If axis is invalid. |
| Exceptions::ZeroDivisionError | If a produced output has an empty slice. |
Definition at line 819 of file Reductions.hpp.