47template<
bool FindMaximum, Array A>
51 auto current = arr.begin();
52 const auto end = arr.end();
53 typename A::value_type selected = *current;
54 std::size_t selected_index = 0;
55 std::size_t current_index = 1;
59 for (++current; current != end; ++current, ++current_index)
61 const typename A::value_type value = *current;
62 const bool replace = FindMaximum ? selected < value : value < selected;
66 selected_index = current_index;
70 return {selected, selected_index};
84 std::vector<std::size_t>& indices)
86 for (std::size_t d = shape.
rank(); d-- > 0;)
90 if (indices[d] < shape[d])
135inline std::vector<std::size_t>
result_shape(
const A& arr,
int axis,
bool keepdims)
139 std::vector<std::size_t> result_dimensions;
143 for (std::size_t dimension = 0; dimension < input_shape.
rank(); ++dimension)
145 if (dimension ==
static_cast<std::size_t
>(axis))
147 result_dimensions.push_back(1);
152 result_dimensions.push_back(input_shape[dimension]);
159 for (std::size_t dimension = 0; dimension < input_shape.
rank(); ++dimension)
161 if (dimension !=
static_cast<std::size_t
>(axis))
163 result_dimensions.push_back(input_shape[dimension]);
168 return result_dimensions;
177template<Array A,
typename Func>
179 decltype(std::declval<Func>()(
180 std::declval<const stratax::container::Tensor<typename A::value_type>&>()));
213template<Array A,
typename Func>
215axis_reduce(
const A& array,
int axis, Func func,
bool keepdims =
false)
219 int Axis = detail::normalize_axis(array, axis);
221 if (Axis < 0 || Axis >=
static_cast<int>(array.rank()))
227 stratax::conversion::to_tensor(array);
231 std::vector<std::size_t> result_dims =
232 detail::result_shape(array, Axis, keepdims);
236 if (result_dims.empty())
238 ResultType scalar_result = func(arr);
248 std::vector<std::size_t> output_index(result.rank(), 0);
249 std::vector<stratax::core::Slice> slices;
253 std::size_t output_position = 0;
254 for (std::size_t dimension = 0; dimension < input_shape.
rank(); ++dimension)
256 if (
static_cast<int>(dimension) == Axis)
259 static_cast<std::ptrdiff_t
>(input_shape[dimension])});
264 const std::size_t index = keepdims
265 ? output_index[dimension]
266 : output_index[output_position++];
269 static_cast<std::ptrdiff_t
>(index),
270 static_cast<std::ptrdiff_t
>(index + 1)
274 auto s = stratax::indexing::slice(arr, slices);
275 ResultType value = func(s);
276 result(output_index) = value;
278 while (detail::advance(result.shape(), output_index));
300 return std::accumulate(
321 return std::accumulate(
325 std::multiplies<result_type>{});
348 return detail::ordered_extreme<true>(arr).first;
371 return detail::ordered_extreme<false>(arr).first;
442 long double total = 0.0L;
443 for (
const auto& value : arr)
445 total +=
static_cast<long double>(value);
448 return static_cast<double>(total /
static_cast<long double>(arr.size()));
476 double mean_value = 0.0;
479 for (
const auto& value : arr)
482 const double delta =
static_cast<double>(value) - mean_value;
483 mean_value += delta / count;
484 const double delta2 =
static_cast<double>(value) - mean_value;
485 m2 += delta * delta2;
506 auto vars =
var(arr);
507 return std::sqrt(vars);
525auto sum(
const A& arr,
int axis)
548auto sum(
const A& arr,
int axis,
bool keepdims)
571auto prod(
const A& arr,
int axis)
594auto prod(
const A& arr,
int axis,
bool keepdims)
616auto max(
const A& arr,
int axis)
634auto max(
const A& arr,
int axis,
bool keepdims)
654auto min(
const A& arr,
int axis)
672auto min(
const A& arr,
int axis,
bool keepdims)
710auto argmax(
const A& arr,
int axis,
bool keepdims)
748auto argmin(
const A& arr,
int axis,
bool keepdims)
773mean(
const A& arr,
int axis,
bool keepdims)
819var(
const A& arr,
int axis,
bool keepdims)
843var(
const A& arr,
int axis)
865std(
const A& arr,
int axis,
bool keepdims)
889std(
const A& arr,
int axis)
Dtype and array interface concepts.
Copying container conversions and dtype casts.
Stratax exception types derived from std::runtime_error.
Accumulator and result dtype selection for reductions.
typename ReductionTraits< std::remove_cvref_t< T > >::prod_type reduction_prod_t
Accumulator and result type used by reduction::prod.
typename ReductionTraits< std::remove_cvref_t< T > >::sum_type reduction_sum_t
Accumulator and result type used by reduction::sum.
auto min(const A &arr)
Returns the smallest element.
int normalize_axis(const A &arr, int axis)
Converts a possibly negative axis to its non-negative equivalent.
double std(const A &arr)
Returns the population standard deviation of all elements.
auto sum(const A &arr)
Returns the sum of all elements in flat iterator order.
double mean(const A &arr)
Returns the arithmetic mean of all elements as double.
bool advance(const stratax::core::Shape &shape, std::vector< std::size_t > &indices)
Advances a multidimensional index in row-major order.
auto argmin(const A &arr)
Returns the flat index of the smallest element.
double var(const A &arr)
Returns the population variance using Welford's online algorithm.
auto max(const A &arr)
Returns the largest element.
decltype(std::declval< Func >()(std::declval< const stratax::container::Tensor< typename A::value_type > & >())) axis_reduce_value_t
Result type produced by invoking an axis-reduction callback.
std::pair< typename A::value_type, std::size_t > ordered_extreme(const A &arr)
Selects an extreme value and its logical index with an ordered scan.
std::vector< std::size_t > result_shape(const A &arr, int axis, bool keepdims)
Computes the dimensions produced by reducing one normalized axis.
stratax::container::Tensor< detail::axis_reduce_value_t< A, Func > > axis_reduce(const A &array, int axis, Func func, bool keepdims=false)
Applies a scalar reduction callback independently along one axis.
auto argmax(const A &arr)
Returns the flat index of the largest element.
auto prod(const A &arr)
Returns the product of all elements in flat iterator order.
Immutable dimension metadata and checked row-major strides.
Raw signed half-open ranges; applying views currently requires positive steps.
Positive-step non-owning slice views with clamped bounds.
Arbitrary-rank owning arrays; rank zero is empty.
std::int64_t int64
Signed 64-bit integer dtype.
Reduction axis is outside the input rank.
Index or slice cannot be used for the requested access.
An explicitly checked divisor is zero.
Arbitrary-rank owning array of supported dtype values.
const Shape & shape() const noexcept
Returns the logical shape metadata.
Stores the dimensions of a multidimensional array.
size_type rank() const noexcept
Returns the number of dimensions.
Describes a signed, strided half-open index range.
Supported integer, floating-point, or complex dtype, excluding bool.
Supported non-complex dtype, including bool; floating-point NaNs need not compare ordered.