Corrected a bug in the Distribution class and added useful access bin counting functions members to the Histogram class

This commit is contained in:
Paolo Cignoni 2010-01-11 22:57:32 +00:00
parent f017c8646e
commit c72bfe6f63
1 changed files with 18 additions and 11 deletions

View File

@ -102,8 +102,11 @@ class Distribution
{ {
private: private:
std::vector<ScalarType> vec; std::vector<ScalarType> vec;
bool dirty; bool dirty;
double valSum;
double sqrdValSum;
double avg; double avg;
double sqrdAvg;
double rms; double rms;
double min_v; double min_v;
double max_v; double max_v;
@ -137,25 +140,26 @@ public:
ScalarType RMS(){ DirtyCheck(); return rms;} ScalarType RMS(){ DirtyCheck(); return rms;}
//! Returns the variance of the data. //! Returns the variance of the data.
ScalarType Variance(){ return math::Abs(rms-avg*avg);} // the average of the squares less the square of the average.
ScalarType Variance(){ DirtyCheck(); return sqrdAvg - avg*avg ;}
//! Returns the standard deviation of the data. //! Returns the standard deviation of the data.
ScalarType StandardDeviation(){ return sqrt(Variance());} ScalarType StandardDeviation(){ DirtyCheck(); return sqrt( Variance() );}
void DirtyCheck() void DirtyCheck()
{ {
if(!dirty) return; if(!dirty) return;
std::sort(vec.begin(),vec.end()); std::sort(vec.begin(),vec.end());
avg=0; valSum=0;
rms=0; sqrdValSum=0;
typename std::vector<ScalarType>::iterator vi; typename std::vector<ScalarType>::iterator vi;
for(vi=vec.begin();vi!=vec.end();++vi) for(vi=vec.begin();vi!=vec.end();++vi)
{ {
avg += double(*vi); valSum += double(*vi);
rms += double(*vi)*double(*vi); sqrdValSum += double(*vi)*double(*vi);
} }
rms = math::Sqrt(rms/double(vec.size())); avg = valSum/double(vec.size());
avg = avg/double(vec.size()); sqrdAvg = sqrdValSum/double(vec.size());
rms = math::Sqrt(sqrdAvg);
dirty=false; dirty=false;
} }
@ -232,8 +236,11 @@ public:
int MaxCount() const; int MaxCount() const;
int BinCount(ScalarType v); int BinCount(ScalarType v);
int BinCountInd(int index) {return H[index];}
int BinCount(ScalarType v, ScalarType width); int BinCount(ScalarType v, ScalarType width);
int RangeCount(ScalarType rangeMin, ScalarType rangeMax); ScalarType BinLowerBound(int index) {return R[index];}
ScalarType BinUpperBound(int index) {return R[index+1];};
int RangeCount(ScalarType rangeMin, ScalarType rangeMax);
ScalarType BinWidth(ScalarType v); ScalarType BinWidth(ScalarType v);
/** /**