From: Alex Schmidt Date: Thu, 24 May 2012 13:18:55 +0000 (+0200) Subject: ... X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/commitdiff_plain/0c708ebeb7aec487326d89207d96b9e61bd541ad ... --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 117984c..be72077 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,5 @@ project(latlib) -add_library(obs obs.cpp) add_library(configcache configcache.cpp) -target_link_libraries(configcache boost_iostreams) \ No newline at end of file +target_link_libraries(configcache boost_iostreams) diff --git a/obs.cpp b/obs.cpp deleted file mode 100644 index 75c1d93..0000000 --- a/obs.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "obs.h" - -#include - -using namespace std; - -template -void obs::addMeas(obstype val[], int valsize) -{ - for(int i=0; i -void obs::addMeas(const obstype& val) -{ - measurements.push_back(val); -} - -template -void obs::mean(const string& compid, const list& meas) -{ - computations[compid][0] = 0; - computations[compid][1] = 0; - int nmeas = meas.size(); - - for(list::iterator measIt = meas.begin(); measIt != meas.end(); ++measIt) - computations[compid][0] += *measIt; - computations[compid][0] /= nmeas; - - for(list::iterator measIt = meas.begin(); measIt != meas.end(); ++measIt) - computations[compid][1] += pow( *measIt - computations[compid][0], 2 ); - computations[compid][1] /= nmeas*(nmeas-1); - computations[compid][1] = sqrt(computations[compid][1]); -} - -template -void obs::computeJack(const string& compid, double (*func)(const list& vals, void *para), void *para) -{ - int nmeas=measurements.size(); - double manymeans[nmeas]; - - computations[compid][0] = 0; - computations[compid][1] = 0; - - int imeas=0; - for(typename list::iterator removedIt = measurements.begin(); removedIt != measurements.end(); imeas++) - { - obstype removed = *removedIt; - - typename list::iterator nextAfterIt = removedIt; - ++nextAfterIt; - - measurements.erase(removedIt); - - manymeans[imeas] = func(measurements, para); - computations[compid][0] += manymeans[imeas]; - - measurements.insert(nextAfterIt, removed); - } - computations[compid][0] /= nmeas; - - for(int imean=0; imean -#include -#include - -using namespace std; - -template -class obs -{ - public: - void addMeas(obstype val[], int valsize); - void addMeas(const obstype& val); - - void computeMean(const string& compid) { mean(compid, measurements); } - void computeJack(const string& compid, double (*func)(const list& vals, void *para), void *para=NULL); - - private: - list measurements; - map computations; - - void mean(const string& compid, const list& meas); -}; - -#endif diff --git a/obs.hpp b/obs.hpp new file mode 100644 index 0000000..ae544e4 --- /dev/null +++ b/obs.hpp @@ -0,0 +1,100 @@ +#ifndef OBS_HPP +#define OBS_HPP + +#include +#include +#include +#include + +using namespace std; + +template +class obs +{ + struct result{ + double val; + double err; + }; + +public: + void addMeas(obstype val[], int valsize); + void addMeas(const obstype& val); + + void computeMean(const string& compid) { mean(compid, &measurements); } + void computeJack(const string& compid, double (*func)(list *vals, void *para), void *para=NULL); + + double getMean(const string& compid) { return computations[compid].val; } + double getErr(const string& compid) { return computations[compid].err; } + +private: + list measurements; + map computations; + + void mean(const string& compid, list *meas); +}; + +template +void obs::addMeas(obstype val[], int valsize) +{ + for(int i=0; i +void obs::addMeas(const obstype& val) +{ + measurements.push_back(val); +} + +template +void obs::mean(const string& compid, list *meas) +{ + computations[compid].val = 0; + computations[compid].err = 0; + + int nmeas = meas->size(); + + for(list::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + computations[compid].val += *measIt; + computations[compid].val /= nmeas; + + for(list::iterator measIt = meas->begin(); measIt != meas->end(); ++measIt) + computations[compid].err += pow( *measIt - computations[compid].val, 2 ); + computations[compid].err /= nmeas*(nmeas-1); + computations[compid].err = sqrt( computations[compid].err ); +} + +template +void obs::computeJack(const string& compid, double (*func)(list *vals, void *para), void *para) +{ + int nmeas=measurements.size(); + double manymeans[nmeas]; + + computations[compid].val = 0; + computations[compid].err = 0; + + int imeas=0; + for(typename list::iterator removedIt = measurements.begin(); removedIt != measurements.end(); imeas++) + { + obstype removed = *removedIt; + + typename list::iterator nextAfterIt = removedIt; + ++nextAfterIt; + + measurements.erase(removedIt); + + manymeans[imeas] = func(&measurements, para); + computations[compid].val += manymeans[imeas]; + + measurements.insert(nextAfterIt, removed); + + removedIt = nextAfterIt; + } + computations[compid].val /= nmeas; + + for(int imean=0; imean