X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/a5f05e337d18c193ac57b4da0013d1cdc69f8faa..578c48ada63a6ea4f090728cf2b267fb3a731b78:/obstat.hpp?ds=inline diff --git a/obstat.hpp b/obstat.hpp index d66467b..57a3431 100644 --- a/obstat.hpp +++ b/obstat.hpp @@ -8,121 +8,150 @@ 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), void *para=NULL); + int computeJack(void (*preMeasFunc)(vector< vector > *allVals, vector *preCalculated, void *para), + restype (*measFunc)(vector *preCalculated, vector *excludedmeas, int nmeas, void *para), 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 = new restype[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 +int obstat::computeJack(void (*preMeasFunc)(vector< vector > *allVals, vector *preCalculated, void *para), + restype (*measFunc)(vector *preCalculated, vector *excludedmeas, int nmeas, void *para), + void *para) { + int nmeas=measurements.size(); + restype *manymeans = new restype[nmeas]; + result jackres; + vector preCalculated; + + jackres.val = 0; + jackres.err = 0; + + preMeasFunc(&measurements, &preCalculated, para); + + int imeas=0; + for(typename vector< vector >::iterator removedIt = measurements.begin(); removedIt != measurements.end(); ++removedIt, imeas++) { + manymeans[imeas] = measFunc(&preCalculated, &(*removedIt), measurements.size(), para); + jackres.val += manymeans[imeas]; + } + jackres.val /= nmeas; + for(int imean=0; imean