E.V.E  0.1-beta

◆ average

eve::average = {}
inlineconstexpr

Callable object computing the average of multiple values.

Required header: #include <eve/function/average.hpp>

Members Functions

Member Effect
operator() the mid-point operation
operator[] Construct a conditional version of current function object

template< value T, value U > auto operator()( T x, U y ) const noexcept requires compatible< T, U >;
template< floating_value T, floating_value ...Ts> auto operator()( T x,Ts... args ) const noexcept
requires (compatiblevalues< T, Ts > && ...);
Definition: value.hpp:83

Parameters

x, y: values.

x, args, ...: floating values.

Return value

For two parameters half the sum of x and y. No overflow occurs.

For more than two parameters only floating entries are allowed and overflow is avoided.

The result type is the common compatible type of the parameters.

Warning
If x and y are integral values and the sum is odd, the result is a rounded value at a distance guaranted to be less than or equal to 0.5 of the average floating value, but may differ by unity from the truncation given by (x+y)/2. Moreover, as some architectures provide simd intrinsics to perform the operation, the scalar results may differ by one unit from simd ones which are system dependent.

auto operator[]( conditional_expression auto cond ) const noexcept;

Higher-order function generating a masked version of eve::average

Parameters

cond : conditional expression

Return value

A Callable object so that the expression average[cond](x, ...) is equivalent to if_else(cond,average(x, ...),x)


Supported decorators

  • eve::diff, eve::diff_1st, eve::diff_2nd, eve::diff_3rd, eve::diff_nth

    Required header: #include <eve/function/diff/average.hpp>

    The expression diff_nth< N >(average)(x,args...) computes the partial derivative of the function relative to its Nth parameter. The returned value is 0 if N is greater that the actual number of parameters, otherwise it is the inverse of the number of parameters.

    This is only available for floating point entries.

  • eve::raw

    when raw(average)(x, args, ...) is used, no provision is made to avoid overflows for more than 2 parameters.

Example

See it live on Compiler Explorer

#include <eve/function/average.hpp>
#include <eve/literals.hpp>
#include <eve/wide.hpp>
#include <vector>
#include <iostream>
float mean(std::vector<float> ary) {
float avg = 0;
int t = 1;
for (float x : ary) {
avg += (x - avg) / t;
++t;
}
return avg;
}
int main()
{
w_t pi = {3, 2, 3, 3}, qi = {4, 1, 1, ~0};
std::cout << "---- simd" << '\n'
<< " <- pi = " << pi << '\n'
<< " <- qi = " << qi << '\n'
<< " -> average(pi, qi) = " << eve::average(pi, qi) << '\n';
std::uint32_t xi = 3, yi = 4;
std::cout << "---- scalar" << '\n'
<< " xi = " << xi << '\n'
<< " yi = " << yi << '\n'
<< " -> average(xi, yi) = " << eve::average(xi, yi) << '\n';
w_ft pf = {3, 4, 3, 10}, qf = {4, 1, 1, 15};;
std::cout << "---- multi" << '\n'
<< " <- pf = " << pf << '\n'
<< " <- qf = " << qf << '\n'
<< " -> average(pf, 0.0f, qf, pf, 11.0f) = " << eve::average(pf, 0.0f, qf, pf, 11.0f) << '\n';
return 0;
}
constexpr callable_average_ average
Callable object computing the average of multiple values.
Definition: average.hpp:100
constexpr callable_pi_ pi
Callable object computing the value.
Definition: pi.hpp:54
Wrapper for SIMD registers.
Definition: wide.hpp:65