Jpp  master_rocky
the software that should make you happy
JQuantile_t.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JQUANTILE_T__
2 #define __JMATH__JQUANTILE_T__
3 
4 #include <vector>
5 #include <algorithm>
6 #include <numeric>
7 
8 #include "JLang/JException.hh"
9 
10 /**
11  * \author mdejong
12  */
13 namespace JMATH {}
14 namespace JPP { using namespace JMATH; }
15 
16 namespace JMATH {
17 
19 
20  /**
21  * Auxiliary data structure for average.
22  *
23  * The determination of the average should be independent of the order of the input values.
24  */
25  struct JQuantile_t {
26  /**
27  * Default constructor.
28  */
30  {
31  reset();
32  }
33 
34 
35  /**
36  * Reset.
37  */
38  void reset()
39  {
40  buffer.clear();
41  }
42 
43 
44  /**
45  * Put value.
46  *
47  * \param x value
48  */
49  void put(const double x)
50  {
51  buffer.push_back(x);
52  }
53 
54 
55  /**
56  * Get mean value.
57  *
58  * \return mean value
59  */
60  double getMean() const
61  {
62  using namespace std;
63 
64  if (!buffer.empty()) {
65 
66  sort(buffer.begin(), buffer.end());
67 
68  return accumulate(buffer.begin(), buffer.end(), 0.0) / buffer.size();
69  }
70 
71  THROW(JDivisionByZero, "JQuantile_t::getMean()");
72  }
73 
74 
75  /**
76  * Get mean value.
77  *
78  * \param value default value
79  * \return mean value
80  */
81  double getMean(const double value) const
82  {
83  if (!buffer.empty())
84  return getMean();
85  else
86  return value;
87  }
88 
89  private:
91  };
92 }
93 
94 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Exception for division by zero.
Definition: JException.hh:288
Auxiliary classes and methods for mathematical operations.
Definition: JEigen3D.hh:88
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void accumulate(T &value, JBool< false > option)
Accumulation of value.
Definition: JSTDTypes.hh:14
Auxiliary data structure for average.
Definition: JQuantile_t.hh:25
void put(const double x)
Put value.
Definition: JQuantile_t.hh:49
JQuantile_t()
Default constructor.
Definition: JQuantile_t.hh:29
double getMean(const double value) const
Get mean value.
Definition: JQuantile_t.hh:81
std::vector< double > buffer
Definition: JQuantile_t.hh:90
void reset()
Reset.
Definition: JQuantile_t.hh:38
double getMean() const
Get mean value.
Definition: JQuantile_t.hh:60