Skip to content

Reductions

Many values into one: of a whole expression, or along axes.

Concepts

Name Description
reducer_for A reducer R that can reduce values of type T.

Types

Name Description
All Whether every value of a mask is true.
Any Whether any value of a mask is true.
Count How many values of a mask are true, as a std::size_t.
Max The largest value, by < alone, so that any ordered type will do.
Mean The sum divided by the count.
Min The smallest value, by < alone, so that any ordered type will do.
Product The product, in the type T * T gives, which must be T's own.
Reduce E reduced along Axes: an expression of the axes left.
Sum The sum, in Acc if one is given, else in the type T + T gives.

Variables

sum

constexpr detail::accumulating_fn<Sum> sum {}

The sum of the values of an expression.

Reductions are function objects, as the element-wise functions are (see map).

  • sum(x): the sum of every value, a value at once. It computes into buffers first the parts of x that are cheaper read from one, as assign does.
  • sum.along<1, 2>(x): the sum along axes, in any order given, an expression of the axes left. Lazy: it computes nothing until assigned or read.
  • sum.as<double>(x): the sum in an accumulator of one's choosing, sum.as<double>.along<1>(x) both.

along<1> is itself a value, which algorithms and pipelines take as it is. Unless told, the sum is in the type T + T gives: int for std::int8_t, float for float. The sum of no values is 0. The other reductions below are called in the same ways, as only on sum and mean.

const auto total = strata::sum(x);
const Tensor<double, Shape<days>> per_day = strata::sum.along<1, 2>(readings);

prod

constexpr detail::reduction_fn<Product> prod {}

The product of the values of an expression, in the type T * T gives; of no values, 1.

min

constexpr detail::reduction_fn<Min> min {}

The smallest value; of no values at all a precondition violation.

A NaN is the result wherever there is one. Element-wise, of two operands, the function is minimum, as in NumPy.

max

constexpr detail::reduction_fn<Max> max {}

The largest value; of no values at all a precondition violation.

A NaN is the result wherever there is one. Element-wise, of two operands, the function is maximum, as in NumPy.

mean

constexpr detail::accumulating_fn<Mean> mean {}

The sum divided by the count.

A mean of integers is a double, as in NumPy.

The mean of no values is a NaN in floating point, and a precondition violation in integers. Takes as<Acc> as sum does.

all

constexpr detail::reduction_fn<All> all {}

Whether every value of a mask is true; of no values, true.

Reads the whole mask, without stopping at the first value that decides: a loop without a branch, which vectorises.

any

constexpr detail::reduction_fn<Any> any {}

Whether any value of a mask is true; of no values, false.

Reads the whole mask, without stopping at the first value that decides.

count

constexpr detail::reduction_fn<Count> count {}

How many values of a mask are true, as a std::size_t; of no values, 0.

Reads the whole mask, without stopping at the first value that decides.