SPH
Means.h
Go to the documentation of this file.
1 #pragma once
2 
7 
10 
12 
14 template <int Power>
16 private:
17  double sum = 0.; // using double to limit round-off errors in summing
18  Size weight = 0;
19 
20 public:
21  GeneralizedMean() = default;
22 
23  INLINE void accumulate(const Float value) {
24  sum += pow<Power>(value);
25  weight++;
26  }
27 
29  INLINE void reset() {
30  sum = 0.;
31  weight = 0;
32  }
33 
34  INLINE Float compute() const {
35  return Float(pow(sum / weight, 1. / Power));
36  }
37 
38  INLINE Size count() const {
39  return weight;
40  }
41 
42  friend std::ostream& operator<<(std::ostream& stream, const GeneralizedMean& stats) {
43  stream << stats.compute();
44  return stream;
45  }
46 };
47 
49 template <>
50 class GeneralizedMean<0> {
51 private:
52  double sum = 1.;
53  Size weight = 0;
54 
55 public:
56  GeneralizedMean() = default;
57 
58  INLINE void accumulate(const Float value) {
59  sum *= value;
60  weight++;
61  }
62 
63  INLINE void reset() {
64  sum = 1.;
65  weight = 0;
66  }
67 
68  INLINE Float compute() const {
69  return Float(pow(sum, 1. / weight));
70  }
71 
72  INLINE Size count() const {
73  return weight;
74  }
75 
76  friend std::ostream& operator<<(std::ostream& stream, const GeneralizedMean& stats) {
77  stream << stats.compute();
78  return stream;
79  }
80 };
81 
82 
87 
88 
92 class PositiveMean {
93 protected:
94  double sum = 0.;
95  Size weight = 0;
97 
98 public:
100  : power(power) {
101  SPH_ASSERT(power > 0._f);
102  }
103 
104  INLINE void accumulate(const Float value) {
105  sum += powFastest(value, power);
106  weight++;
107  }
108 
109  INLINE void accumulate(const PositiveMean& other) {
110  SPH_ASSERT(power == other.power); // it only makes sense to sum up same means
111  sum += other.sum;
112  weight += other.weight;
113  }
114 
115  INLINE void reset() {
116  sum = 0.;
117  weight = 0;
118  }
119 
120  INLINE Float compute() const {
121  return Float(powFastest(sum / weight, 1._f / power));
122  }
123 
124  INLINE Size count() const {
125  return weight;
126  }
127 
128  friend std::ostream& operator<<(std::ostream& stream, const PositiveMean& stats) {
129  stream << stats.compute();
130  return stream;
131  }
132 };
133 
137 class NegativeMean : public PositiveMean {
138 public:
140  : PositiveMean(-power) {}
141 
142  INLINE void accumulate(const Float value) {
143  SPH_ASSERT(value > 0._f, value);
144  const Float p = pow(value, power);
145  if (p == INFTY) {
146  weight++; // just increase weight
147  } else if (p > 0._f) {
148  sum += 1._f / p;
149  weight++;
150  }
151  }
152 
153  INLINE void accumulate(const NegativeMean& other) {
154  SPH_ASSERT(power == other.power);
155  sum += other.sum;
156  weight += other.weight;
157  }
158 
159  INLINE Float compute() const {
160  Float avg = sum / weight;
161  SPH_ASSERT(isReal(avg), avg, sum, weight);
162  Float avgPow = pow(avg, 1._f / power);
163  if (avgPow == 0._f) {
164  return INFINITY;
165  } else {
166  return 1._f / avgPow;
167  }
168  }
169 };
170 
172 class MinMaxMean {
173 private:
174  Interval minMax;
175  ArithmeticMean avg;
176 
177 public:
178  MinMaxMean() = default;
179 
180  INLINE void accumulate(const Float value) {
181  avg.accumulate(value);
182  minMax.extend(value);
183  }
184 
186  INLINE void reset() {
187  avg.reset();
188  minMax = Interval();
189  }
190 
191  INLINE Float mean() const {
192  return avg.compute();
193  }
194 
195  INLINE Float min() const {
196  return minMax.lower();
197  }
198 
199  INLINE Float max() const {
200  return minMax.upper();
201  }
202 
204  return minMax;
205  }
206 
207  INLINE Size count() const {
208  return avg.count();
209  }
210 
211  friend std::ostream& operator<<(std::ostream& stream, const MinMaxMean& stats) {
212  stream << "average = " << stats.mean() << " (min = " << stats.min() << ", max = " << stats.max()
213  << ")";
214  return stream;
215  }
216 };
217 
INLINE bool isReal(const AntisymmetricTensor &t)
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Tuple< T, T > minMax(const T &t1, const T &t2)
Helper function sorting two values.
Definition: Collision.h:73
Function for generic manipulation with geometric types.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Object representing interval of real values.
INLINE Float weight(const Vector &r1, const Vector &r2)
INLINE Float powFastest(const Float value, const Float power)
Definition: MathUtils.h:229
constexpr INLINE Float pow(const Float v)
Power for floats.
constexpr Float INFTY
Definition: MathUtils.h:38
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Geometric mean has to be specialized.
Definition: Means.h:50
friend std::ostream & operator<<(std::ostream &stream, const GeneralizedMean &stats)
Definition: Means.h:76
INLINE void reset()
Definition: Means.h:63
INLINE Float compute() const
Definition: Means.h:68
INLINE Size count() const
Definition: Means.h:72
GeneralizedMean()=default
INLINE void accumulate(const Float value)
Definition: Means.h:58
Generalized mean with fixed (compile-time) power.
Definition: Means.h:15
friend std::ostream & operator<<(std::ostream &stream, const GeneralizedMean &stats)
Definition: Means.h:42
INLINE Size count() const
Definition: Means.h:38
INLINE void reset()
Removes all values from the set.
Definition: Means.h:29
INLINE Float compute() const
Definition: Means.h:34
INLINE void accumulate(const Float value)
Definition: Means.h:23
GeneralizedMean()=default
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
Helper class for statistics, accumulating minimal, maximal and mean value of a set of numbers.
Definition: Means.h:172
INLINE void reset()
Removes all values from the set.
Definition: Means.h:186
INLINE void accumulate(const Float value)
Definition: Means.h:180
INLINE Size count() const
Definition: Means.h:207
INLINE Float mean() const
Definition: Means.h:191
INLINE Float max() const
Definition: Means.h:199
INLINE Float min() const
Definition: Means.h:195
INLINE Interval range() const
Definition: Means.h:203
friend std::ostream & operator<<(std::ostream &stream, const MinMaxMean &stats)
Definition: Means.h:211
MinMaxMean()=default
Generalized mean with negative (runtime) power.
Definition: Means.h:137
INLINE void accumulate(const Float value)
Definition: Means.h:142
INLINE Float compute() const
Definition: Means.h:159
NegativeMean(const Float power)
Definition: Means.h:139
INLINE void accumulate(const NegativeMean &other)
Definition: Means.h:153
Generalized mean with positive (runtime) power.
Definition: Means.h:92
INLINE Float compute() const
Definition: Means.h:120
double sum
Definition: Means.h:94
INLINE Size count() const
Definition: Means.h:124
Float power
Definition: Means.h:96
Size weight
Definition: Means.h:95
INLINE void accumulate(const Float value)
Definition: Means.h:104
PositiveMean(const Float power)
Definition: Means.h:99
INLINE void accumulate(const PositiveMean &other)
Definition: Means.h:109
friend std::ostream & operator<<(std::ostream &stream, const PositiveMean &stats)
Definition: Means.h:128
INLINE void reset()
Definition: Means.h:115