1 | // $Id: Averager.h 137 2004-08-19 09:36:44Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_cpptools_averager_ |
---|
4 | #define _theplu_cpptools_averager_ |
---|
5 | |
---|
6 | #include <cmath> |
---|
7 | #include "vector.h" |
---|
8 | |
---|
9 | namespace theplu{ |
---|
10 | namespace cpptools{ |
---|
11 | class ostream; |
---|
12 | /// |
---|
13 | /// Modified version of the Averager class written by Patrik Eden, |
---|
14 | /// Dept. Theoretical Physic, Lund University, Sweden |
---|
15 | /// |
---|
16 | class Averager |
---|
17 | { |
---|
18 | public: |
---|
19 | |
---|
20 | /// |
---|
21 | /// Default constructor |
---|
22 | /// |
---|
23 | Averager(void); |
---|
24 | |
---|
25 | /// |
---|
26 | /// Constructor taking the value of first data point. |
---|
27 | /// |
---|
28 | Averager(const double); |
---|
29 | |
---|
30 | /// |
---|
31 | /// Constructor taking sum of x, sum of squared x and number of |
---|
32 | /// samples. |
---|
33 | /// |
---|
34 | Averager(const double, const double, const long); |
---|
35 | |
---|
36 | /// |
---|
37 | /// Constructor taking gslapi::vector. |
---|
38 | /// |
---|
39 | Averager(const gslapi::vector&); |
---|
40 | |
---|
41 | /// |
---|
42 | /// Copy constructor |
---|
43 | /// |
---|
44 | Averager(const Averager&); |
---|
45 | |
---|
46 | /// |
---|
47 | /// adding n(default=1) number of data point(s) with value d. |
---|
48 | /// |
---|
49 | inline void add(const double d,const long n=1) |
---|
50 | {n_+=n; x_+=n*d; xx_+=n*d*d;} |
---|
51 | |
---|
52 | /// |
---|
53 | /// @return average |
---|
54 | /// |
---|
55 | inline double average(void) const {return mean();} |
---|
56 | |
---|
57 | /// |
---|
58 | /// @return sum of squared values \f$ \sum x_i^2 \f$ |
---|
59 | /// |
---|
60 | inline double average_sqr(void) const {return mean_sqr();} |
---|
61 | |
---|
62 | /// |
---|
63 | /// @return average \f$ \frac{\sum x_i}{n} \f$ |
---|
64 | /// |
---|
65 | inline double mean(void) const { return n_ ? x_/n_ : 0; } |
---|
66 | |
---|
67 | /// |
---|
68 | /// @return @return sum of squared values \f$ \sum x_i^2 \f$. |
---|
69 | /// |
---|
70 | inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; } |
---|
71 | |
---|
72 | /// |
---|
73 | /// @return number of data points |
---|
74 | /// |
---|
75 | inline double n(void) const { return n_; } |
---|
76 | |
---|
77 | /// |
---|
78 | /// resets everything to zero |
---|
79 | /// |
---|
80 | inline void reset(void) { n_=0; x_=xx_=0.0;} |
---|
81 | |
---|
82 | /// |
---|
83 | /// @return standard deviation |
---|
84 | /// |
---|
85 | inline double standard_deviation(void) const { return sqrt(variance()); } |
---|
86 | |
---|
87 | /// |
---|
88 | /// @return standard error, i.e. standard deviation of the mean |
---|
89 | /// |
---|
90 | inline double standard_error(void) const { return sqrt(variance()/n_); } |
---|
91 | |
---|
92 | /// |
---|
93 | /// @return the sum of x |
---|
94 | /// |
---|
95 | inline double sum_x(void) const { return x_; } |
---|
96 | |
---|
97 | /// |
---|
98 | /// @return the sum of squares |
---|
99 | /// |
---|
100 | inline double sum_xsqr(void) const { return xx_; } |
---|
101 | |
---|
102 | /// |
---|
103 | ///The variance is calculated using the (n-1) correction, so the |
---|
104 | ///expectation value is unbiased @return variance |
---|
105 | /// |
---|
106 | inline double variance(void) const |
---|
107 | { return (n_>1) ? (xx_-x_*x_/n_)/(n_-1) : 0; } |
---|
108 | |
---|
109 | /// |
---|
110 | /// equality operator |
---|
111 | /// |
---|
112 | inline Averager operator=(const Averager& a) |
---|
113 | { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; } |
---|
114 | |
---|
115 | /// |
---|
116 | /// operator to add a double to the object |
---|
117 | /// |
---|
118 | inline Averager operator+=(double d) |
---|
119 | { add(d); return *this; } |
---|
120 | |
---|
121 | /// |
---|
122 | /// operator to add a "one" |
---|
123 | /// |
---|
124 | inline Averager operator++(void) |
---|
125 | { add(1); return *this; } |
---|
126 | |
---|
127 | /// |
---|
128 | /// Operator to rescale the object |
---|
129 | /// |
---|
130 | inline Averager operator*=(double d) |
---|
131 | { x_*=d; xx_*=d*d; return *this; } |
---|
132 | |
---|
133 | /// |
---|
134 | /// Operator to rescale the object |
---|
135 | /// |
---|
136 | inline Averager operator/=(double d) |
---|
137 | { return (*this)*=(1/d); } |
---|
138 | |
---|
139 | /// |
---|
140 | /// operator to add another Averager |
---|
141 | /// |
---|
142 | const Averager& operator+=(const Averager&); |
---|
143 | |
---|
144 | /// |
---|
145 | /// operator to multiply with an Averager. See code for details. |
---|
146 | /// |
---|
147 | const Averager& operator*=(const Averager&); |
---|
148 | |
---|
149 | /// |
---|
150 | /// operator to multiply with an Averager. See code for details. |
---|
151 | /// |
---|
152 | const Averager& operator/=(const Averager&); |
---|
153 | |
---|
154 | private: |
---|
155 | long n_; |
---|
156 | double x_, xx_; |
---|
157 | }; |
---|
158 | |
---|
159 | Averager operator*(double,const Averager&); |
---|
160 | |
---|
161 | /// |
---|
162 | /// The output operator |
---|
163 | /// |
---|
164 | std::ostream& operator<<( std::ostream&, const Averager&); |
---|
165 | |
---|
166 | |
---|
167 | }} // of namespace cpptools and namespace theplu |
---|
168 | |
---|
169 | #endif |
---|