AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
MathUtil.h File Reference
#include <algorithm>
#include <iterator>
#include <numeric>
#include <vector>

Go to the source code of this file.

Functions

template<typename Container , typename T = typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type>
standard_deviation (Container &&c)
 
template<typename Container , typename T = typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type>
mean (Container &&c)
 
template<typename T >
median (std::vector< T > a)
 

Function Documentation

◆ mean()

template<typename Container , typename T = typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type>
T mean ( Container &&  c)
inline
50{
51 auto b = std::begin(c), e = std::end(c);
52 auto size = std::distance(b, e);
53 auto sum = std::accumulate(b, e, T());
54 return sum / size;
55}

Referenced by WorldSession::ComputeNewClockDelta(), and standard_deviation().

◆ median()

template<typename T >
T median ( std::vector< T >  a)
inline
60{
61 std::size_t n = a.size();
62 // If size of the arr[] is even
63 if (n % 2 == 0)
64 {
65
66 // Applying nth_element
67 // on n/2th index
68 std::nth_element(a.begin(),
69 a.begin() + n / 2,
70 a.end());
71
72 // Applying nth_element
73 // on (n-1)/2 th index
74 std::nth_element(a.begin(),
75 a.begin() + (n - 1) / 2,
76 a.end());
77
78 // Find the average of value at
79 // index N/2 and (N-1)/2
80 return (T)(a[(n - 1) / 2]
81 + a[n / 2])
82 / 2.0;
83 }
84
85 // If size of the arr[] is odd
86 else
87 {
88
89 // Applying nth_element
90 // on n/2
91 std::nth_element(a.begin(),
92 a.begin() + n / 2,
93 a.end());
94
95 // Value at index (N/2)th
96 // is the median
97 return (T)a[n / 2];
98 }
99}

Referenced by WorldSession::ComputeNewClockDelta().

◆ standard_deviation()

template<typename Container , typename T = typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type>
T standard_deviation ( Container &&  c)
inline
29{
30 auto b = std::begin(c), e = std::end(c);
31 auto size = std::distance(b, e);
32 auto sum = std::accumulate(b, e, T());
33 auto mean = sum / size;
34
35 if (size == 1)
36 {
37 return (T) 0;
38 }
39
40 T accum = T();
41 for (const auto d : c)
42 {
43 accum += (d - mean) * (d - mean);
44 }
45 return std::sqrt(accum / (size - 1));
46}
T mean(Container &&c)
Definition: MathUtil.h:49

References mean().

Referenced by WorldSession::ComputeNewClockDelta().