X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/d7a3df98c51fc1660f664835131a6ad349d1f270..d39c90a61334fa4c953842483bb79f3a86d319d0:/obs.hpp diff --git a/obs.hpp b/obs.hpp index bba6687..4d20bd6 100644 --- a/obs.hpp +++ b/obs.hpp @@ -1,7 +1,7 @@ #ifndef OBS_HPP #define OBS_HPP -#include +#include #include #include #include @@ -17,24 +17,35 @@ public: void computeEasy(const string& compid="one", const int& ival=0) { mean(compid, &measurements, ival); } - void computeJack(const string& compid, double (*func)(list< vector > *vals, void *para)=NULL, void *para=NULL); - void computeJack(double (*func)(list< vector > *vals, void *para)=NULL, void *para=NULL) { computeJack("one",func,para); } + 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); } double getMean(const string& compid="one") { return computations[compid].val; } double getErr(const string& compid="one") { return computations[compid].err; } + void reset(); + private: struct result{ double val; double err; }; - list< vector > measurements; + vector< vector > measurements; map computations; - void mean(const string& compid, list< vector > *meas, const int& ival); + void mean(const string& compid, vector< vector > *meas, const int& ival); + void mean(const string& compid, vector< vector > *meas, const int& ival); }; + +template +void obs::reset() +{ + computations.clear(); + measurements.clear(); +} + template void obs::addMeas(const obstype& val) { @@ -50,25 +61,41 @@ void obs::addMeas(obstype val[], int valsize) } template -void obs::mean(const string& compid, list< vector > *meas, const int& ival) +void obs::mean(const string& compid, vector< vector > *meas, const int& ival) +{ + computations[compid].val = 0; + computations[compid].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(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 obs::mean(const string& compid, vector< vector > *meas, const int& ival) { computations[compid].val = 0; computations[compid].err = 0; int nmeas = meas->size(); - for(list< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) computations[compid].val += (*measIt)[ival]; computations[compid].val /= nmeas; - for(list< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + for(vector< vector >::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) computations[compid].err += pow( (*measIt)[ival] - computations[compid].val, 2 ); - computations[compid].err /= nmeas*(nmeas-1); - computations[compid].err = sqrt( computations[compid].err ); + computations[compid].err = sqrt( computations[compid].err ) / nmeas; } template -void obs::computeJack(const string& compid, double (*func)(list< vector > *vals, void *para), void *para) +void obs::computeJack(const string& compid, double (*func)(vector< vector > *vals, void *para), void *para) { int nmeas=measurements.size(); double manymeans[nmeas]; @@ -77,26 +104,23 @@ void obs::computeJack(const string& compid, double (*func)(list< vector computations[compid].err = 0; int imeas=0; - for(typename list< vector >::iterator removedIt = measurements.begin(); removedIt != measurements.end(); imeas++) + for(typename vector< vector >::iterator removedIt = measurements.begin(); removedIt != measurements.end(); ++removedIt, imeas++) { vector removed = *removedIt; - typename list< vector >::iterator nextAfterIt = removedIt; - ++nextAfterIt; + *removedIt = measurements.back(); + measurements.pop_back(); - measurements.erase(removedIt); - manymeans[imeas] = func(&measurements, para); computations[compid].val += manymeans[imeas]; - - measurements.insert(nextAfterIt, removed); - - removedIt = nextAfterIt; + + measurements.push_back( *removedIt ); + *removedIt = removed; } computations[compid].val /= nmeas; for(int imean=0; imean