cumulative_statistics.hpp
Go to the documentation of this file.
1 
4 /*****************************************************************************
5 ** Ifdefs
6 *****************************************************************************/
7 
8 #ifndef ecl_statistics_CUMULATIVE_STATISTICS_HPP_
9 #define ecl_statistics_CUMULATIVE_STATISTICS_HPP_
10 
11 /*****************************************************************************
12 ** Includes
13 *****************************************************************************/
14 
15 #include <ecl/mpl.hpp>
16 #include <ecl/type_traits.hpp>
17 
18 /*****************************************************************************
19 ** Namespaces
20 *****************************************************************************/
21 
22 namespace ecl {
23 
24 /*****************************************************************************
25 ** Interfaces
26 *****************************************************************************/
27 
34 template <typename T, typename Enable = void>
36 
40 template <typename T>
41 class CumulativeStatistics<T, typename ecl::enable_if< ecl::is_float<T> >::type> {
42 public:
43  CumulativeStatistics(): number_of_data(0.0) {}
44 
45  void clear() { number_of_data = 0.0; }
46 
55  void push_back(const T & x)
56  {
57  number_of_data++;
58  if(number_of_data == 1.0)
59  {
60  old_mean = new_mean = x;
61  old_variance = 0.0;
62  }
63  else
64  {
65  new_mean = old_mean + static_cast<T>(x - old_mean) / number_of_data;
66  new_variance = old_variance + static_cast<T>(x - old_mean) * static_cast<T>(x - new_mean);
67 
68  old_mean = new_mean;
69  old_variance = new_variance;
70  }
71  }
72 
77  T size() const { return number_of_data; }
78 
83  T mean() const { return (number_of_data > 0) ? new_mean : 0.0; }
84 
89  T variance() const { return ((number_of_data > 1) ? new_variance / (number_of_data - 1) : 0.0); }
90 
91 private:
93  T old_mean, old_variance;
94  T new_mean, new_variance;
95 };
96 
97 /*****************************************************************************
98 ** Trailers
99 *****************************************************************************/
100 
101 } // namespace ecl
102 
103 #endif /* ecl_statistics_CUMULATIVE_STATISTICS_HPP_ */
Dummy parent class for Homogenous points.


ecl_statistics
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:40