From: Alex Schmidt Date: Fri, 21 Jun 2013 17:24:33 +0000 (+0200) Subject: ... X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/commitdiff_plain/79dc42fe36820965127efc5755be1450521b0a26 ... --- diff --git a/obstat.hpp b/obstat.hpp index d66467b..2996043 100644 --- a/obstat.hpp +++ b/obstat.hpp @@ -8,121 +8,113 @@ using namespace std; -template +template class obstat { public: - void addMeas(const obstype& val); - void addMeas(obstype val[], int valsize); + void addMeas(const meastype& val); + void addMeas(meastype val[], int valsize); - void computeEasy(const string& compid="one", const int& ival=0) { mean(compid, &measurements, ival); } + int computeEasy(const int& ival=0) { return mean(&measurements, ival); } - void computeJack(const string& compid, double (*func)(vector< vector > *vals, void *para)=NULL, void *para=NULL); - void computeJack(double (*func)(vector< vector > *vals, void *para)=NULL, void *para=NULL) { computeJack("one",func,para); } + int computeJack(restype (*func)(vector< vector > *vals, void *para)=NULL, void *para=NULL); - double getMean(const string& compid="one") { return computations[compid].val; } - double getErr(const string& compid="one") { return computations[compid].err; } + restype getMean(int compid) { return computations[compid].val; } + restype getErr(int compid) { return computations[compid].err; } void reset(); private: struct result{ - double val; - double err; + restype val; + restype err; }; - vector< vector > measurements; - map computations; + vector< vector > measurements; + vector computations; - void mean(const string& compid, vector< vector > *meas, const int& ival); - void mean(const string& compid, vector< vector > *meas, const int& ival); + int mean(vector< vector > *meas, const int& ival); }; - -template -void obstat::reset() +template +void obstat::reset() { computations.clear(); measurements.clear(); } -template -void obstat::addMeas(const obstype& val) +template +void obstat::addMeas(const meastype& val) { - measurements.push_back( vector(1,val) ); + measurements.push_back( vector(1,val) ); } -template -void obstat::addMeas(obstype val[], int valsize) +template +void obstat::addMeas(meastype val[], int valsize) { - vector tmpvec; - for(int i=0; i tmpvec; + for (int i=0; i -void obstat::mean(const string& compid, vector< vector > *meas, const int& ival) +template +int obstat::mean(vector< vector > *meas, const int& ival) { - computations[compid].val = 0; - computations[compid].err = 0; + result meanres; + + meanres.val = 0; + meanres.err = 0; int nmeas = meas->size(); - for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) - computations[compid].val += (*measIt)[ival]; - computations[compid].val /= nmeas; + for (typename vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + meanres.val += (*measIt)[ival]; + meanres.val /= nmeas; - for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) - computations[compid].err += pow( (*measIt)[ival] - computations[compid].val, 2 ); - computations[compid].err = sqrt( computations[compid].err ) / nmeas; -} + for (typename vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + meanres.err += pow( (*measIt)[ival] - meanres.val, 2 ); + meanres.err = sqrt( meanres.err ) / (restype)nmeas; -template -void obstat::mean(const string& compid, vector< vector > *meas, const int& ival) -{ - computations[compid].val = 0; - computations[compid].err = 0; - - int nmeas = meas->size(); + computations.push_back(meanres); - for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) - computations[compid].val += (*measIt)[ival]; - computations[compid].val /= nmeas; - - for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) - computations[compid].err += pow( (*measIt)[ival] - computations[compid].val, 2 ); - computations[compid].err = sqrt( computations[compid].err ) / nmeas; -} - -template -void obstat::computeJack(const string& compid, double (*func)(vector< vector > *vals, void *para), void *para) + return computations.size()-1; +} + +template +int obstat::computeJack(restype (*func)(vector< vector > *vals, void *para), void *para) { int nmeas=measurements.size(); - double manymeans[nmeas]; + restype manymeans[nmeas]; + result jackres; - computations[compid].val = 0; - computations[compid].err = 0; + jackres.val = 0; + jackres.err = 0; int imeas=0; - for(typename vector< vector >::iterator removedIt = measurements.begin(); removedIt != measurements.end(); ++removedIt, imeas++) + for(typename vector< vector >::iterator removedIt = measurements.begin(); removedIt != measurements.end(); ++removedIt, imeas++) { - vector removed = *removedIt; + vector removed = *removedIt; *removedIt = measurements.back(); measurements.pop_back(); manymeans[imeas] = func(&measurements, para); - computations[compid].val += manymeans[imeas]; + jackres.val += manymeans[imeas]; measurements.push_back( *removedIt ); *removedIt = removed; } - computations[compid].val /= nmeas; + jackres.val /= nmeas; for(int imean=0; imean