31 #ifndef HELPER_MATH_HPP_ 32 #define HELPER_MATH_HPP_ 40 #include <type_traits> 58 template<
typename R,
typename T,
template<
typename,
typename>
class Container>
59 R
avg(
const Container<T, std::allocator<T>>& values) {
60 return static_cast<R
>(
61 std::accumulate(values.begin(), values.end(), 0ULL)
74 template<
typename R,
typename T,
template<
typename,
typename>
class Container>
75 R
median(Container<T, std::allocator<T>>& values) {
80 if(values.size() % 2 == 0) {
82 values.begin() + values.size() / 2 - 1
85 values.begin() + values.size() / 2
88 std::nth_element(values.begin(), iterator1 , values.end());
90 const auto value1{*iterator1};
92 std::nth_element(values.begin(), iterator2 , values.end());
94 const auto value2{*iterator2};
96 return static_cast<R
>((value1 + value2) / 2);
100 values.begin() + values.size() / 2
103 std::nth_element(values.begin(), iterator, values.end());
121 template<
typename R,
typename T,
template<
typename,
typename>
class Container>
122 R
variance(R mean, Container<T, std::allocator<T>>& values) {
125 for(
const auto value : values) {
126 sum += (value - mean) * (value - mean);
129 return sum / values.size();
143 template<
typename R,
typename T,
template<
typename,
typename>
class Container>
144 R
variance(Container<T, std::allocator<T>>& values) {
145 const auto mean{
avg(values)};
161 template<
typename T>
typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
165 return std::fabs(x - y) <= std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp
167 || std::fabs(x - y) < std::numeric_limits<T>::min();
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type almostEqual(T x, T y, int ulp)
Compares two floating-point values using epsilon.
Definition: Math.hpp:162
Namespace for global math functions.
Definition: Math.hpp:44
R avg(const Container< T, std::allocator< T >> &values)
Definition: Math.hpp:59
R median(Container< T, std::allocator< T >> &values)
Definition: Math.hpp:75
R variance(R mean, Container< T, std::allocator< T >> &values)
Calculates the variance from the given mean of all elements in the given container.
Definition: Math.hpp:122