Fix warnings and errors in ampi_noimpl.C
[charm.git] / src / util / ckstatistics.h
blob9b5e1389a911da39c087a19e5967833d0b944f93
1 /*
2 (Copied out of) Orion's Standard Library
3 written by
4 Orion Sky Lawlor, olawlor@acm.org, 11/25/2002
5 */
6 #ifndef __UIUC_OSL_STATISTICS_H
7 #define __UIUC_OSL_STATISTICS_H
9 #include "math.h" //for sqrt
10 #include "stdio.h" //for fprintf
11 #include "pup.h" //for pup
13 /**
14 * CkSampleT represents a statistical "sample" of some data values.
15 * It maintains information it can use to compute means
16 * and variances, max and min.
18 * The REAL template parameter is the datatype of the values.
19 * The RET template parameter is the datatype of the various
20 * accumulators used, and the datatype for the derived parameters.
22 * The CkSample typedef is just CkSampleT<double,double>
23 * Other sensible specializations might include CkSampleT<double, long double>
24 * for higher precision accumulation, or CkSampleT<int, double> for computing
25 * continuous statistics of a discrete distribution.
27 template<class real,class ret>
28 class CkSampleT {
29 real lo,hi; //smallest and largest values
30 ret sum; //sum of values
31 ret sq; //sum of squares of values
32 int n; //number of values
33 public:
34 CkSampleT(void) {
35 lo=(real)1.0e20; hi=(real)-1.0e20;
36 sum=sq=(ret)0;
37 n=0;
39 /**
40 * Add this value to the sample set.
41 * This function updates the max, min, and mean and variance
42 * for this sample.
44 inline void add(real r) {
45 if (r<lo) lo=r;
46 if (r>hi) hi=r;
47 sum+=(ret)r;
48 sq+=(ret)(r*r);
49 n++;
51 /// Shorthand for add function.
52 inline void operator+=(real r) { add(r); }
54 /** Add all these samples to this set */
55 inline void add(const CkSampleT<real,ret> &errs) {
56 if (errs.lo<lo) lo=errs.lo;
57 if (errs.hi>hi) hi=errs.hi;
58 sum+=errs.sum;
59 sq+=errs.sq;
60 n+=errs.n;
63 /**
64 * Return the mean value of this sample--the "average" value, in (value) units.
65 * Computed as the sum of all values divided by the number of values.
67 ret getMean(void) const {
68 return sum/n;
70 /**
71 * Return the variance of this sample, in (value^2) units.
72 * Computed as the sum of the squares of the diffences between
73 * each value and the mean, divided by the number of values minus 1.
75 ret getVariance(void) const {
76 return (sq-sum*sum/n)/(n-1);
78 /**
79 * Return the standard deviation of this sample, in (value) units.
80 * Computed as the square root of the variance.
82 ret getStddev(void) const {
83 return (ret)sqrt(getVariance());
85 /**
86 * Return the smallest value encountered.
88 real getMin(void) const {return lo;}
89 /**
90 * Return the largest value encountered.
92 real getMax(void) const {return hi;}
93 /**
94 * Return the number of values in this sample.
96 int getCount(void) const {return n;}
98 /// Return the RMS value of this variable (square root of sum of squares over number)
99 ret getRMS(void) const {return sqrt(sq/n);}
102 * Print a textual description of this sample to this FILE.
103 * For example, a 1,000,000-value sample of a uniform distribution on [0,1] might give:
104 * ave= 0.500367 stddev= 0.288663 min= 1.27012e-06 max= 0.999999 n= 1000000
106 void print(FILE *dest) {
107 fprintf(dest,"ave= %g stddev= %g min= %g max= %g n= %d\n",
108 (double)getMean(), (double)getStddev(), (double)getMin(), (double)getMax(), (int)getCount());
112 * Print a terse textual description of this sample to this FILE.
114 void printMinAveMax(FILE *dest) {
115 fprintf(dest,"min= %g ave= %g max= %g \n",
116 (double)getMin(), (double)getMean(), (double)getMax());
120 * Print a textual description of this sample to stdout.
122 void print(void) {print(stdout);}
124 /// Pup routine, for migration and parameter marshalling.
125 void pup(PUP::er &p) {
126 p|lo; p|hi;
127 p|sum; p|sq;
128 p|n;
130 friend inline void operator|(PUP::er &p,CkSampleT<real,ret> &s) {s.pup(p);}
132 typedef CkSampleT<double,double> CkSample;
134 #endif