Last change
on this file since 95 was
95,
checked in by Peter, 19 years ago
|
some functions added to WeightedAverager?
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
File size:
1.3 KB
|
Line | |
---|
1 | // $Id: Averager.cc 95 2004-06-09 16:01:43Z peter $ |
---|
2 | |
---|
3 | #include <sys/types.h> |
---|
4 | |
---|
5 | #include <ostream> |
---|
6 | |
---|
7 | #include "Averager.h" |
---|
8 | |
---|
9 | namespace theplu { |
---|
10 | namespace cpptools{ |
---|
11 | |
---|
12 | |
---|
13 | Averager::Averager(void) |
---|
14 | : n_(0), x_(0), xx_(0) |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | Averager::Averager(double x) |
---|
19 | : n_(1), x_(x), xx_(x*x) |
---|
20 | { |
---|
21 | } |
---|
22 | |
---|
23 | Averager::Averager(double x,double xx,long n) |
---|
24 | : n_(n), x_(x), xx_(xx) |
---|
25 | { |
---|
26 | } |
---|
27 | |
---|
28 | Averager::Averager(const Averager& a) |
---|
29 | : n_(a.n_), x_(a.x_), xx_(a.xx_) |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | const Averager& Averager::operator+=(const Averager& a) |
---|
34 | { |
---|
35 | n_+=a.n_; |
---|
36 | x_+=a.x_; |
---|
37 | xx_+=a.xx_; |
---|
38 | return *this; |
---|
39 | } |
---|
40 | |
---|
41 | const Averager& Averager::operator*=(const Averager& N) |
---|
42 | { |
---|
43 | long Rn=10; |
---|
44 | double NM=N.mean(); |
---|
45 | double M=mean(); |
---|
46 | double x=M*NM; |
---|
47 | double var=(variance()*NM*NM+N.variance()*M*M); |
---|
48 | |
---|
49 | *this=Averager(Rn ,Rn*x ,var*Rn*(Rn-1)+x*x*Rn); |
---|
50 | return *this; |
---|
51 | } |
---|
52 | |
---|
53 | const Averager& Averager::operator/=(const Averager& N) |
---|
54 | { |
---|
55 | long Rn=10; |
---|
56 | double NM=N.mean(); |
---|
57 | |
---|
58 | if(NM) { |
---|
59 | double x=mean()/NM; |
---|
60 | double var=(variance()+N.variance()*x*x)/(NM*NM); |
---|
61 | *this=Averager(Rn ,Rn*x ,var*Rn*(Rn-1)+x*x*Rn); |
---|
62 | } |
---|
63 | else |
---|
64 | reset(); |
---|
65 | |
---|
66 | return *this; |
---|
67 | } |
---|
68 | |
---|
69 | Averager operator*(double d,const Averager& a) { |
---|
70 | Averager tmp=a; |
---|
71 | return tmp*=d; |
---|
72 | } |
---|
73 | |
---|
74 | std::ostream& theplu::cpptools::operator<<(std::ostream& o, const Averager& a) |
---|
75 | { |
---|
76 | return o << a.mean() << ' ' << a.standard_error(); |
---|
77 | } |
---|
78 | |
---|
79 | }} // of namespace cpptools and namespace theplu |
---|
Note: See
TracBrowser
for help on using the repository browser.