better stats functions
This commit is contained in:
28
src/util.h
28
src/util.h
@ -73,4 +73,32 @@ constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
|
||||
}
|
||||
|
||||
|
||||
template <typename ForwardIt>
|
||||
constexpr typename ForwardIt::value_type average(const ForwardIt first,
|
||||
const ForwardIt last) {
|
||||
typedef typename ForwardIt::value_type value_type;
|
||||
if (first == last) return 0;
|
||||
value_type average = 0;
|
||||
for (ForwardIt it = first; it != last; ++it) {
|
||||
average += *it;
|
||||
}
|
||||
return average / (last - first);
|
||||
}
|
||||
|
||||
|
||||
template <typename ForwardIt>
|
||||
constexpr typename ForwardIt::value_type
|
||||
standard_deviation(const ForwardIt first,
|
||||
const ForwardIt last,
|
||||
typename ForwardIt::value_type average) {
|
||||
typedef typename ForwardIt::value_type value_type;
|
||||
if (first == last) return 0;
|
||||
value_type accumulator = 0;
|
||||
for (ForwardIt it = first; it != last; ++it) {
|
||||
accumulator += (*it - average) * (*it - average);
|
||||
}
|
||||
return sqrt(accumulator / (last - first));
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user