/** * MinVarFunction.hh * * Created on: 09 nov. 2014 * Author: AKKA */ #ifndef MINVARFUNCTION_HH_ #define MINVARFUNCTION_HH_ #include #include namespace AMDA { namespace Statistic { namespace MinVar { template class MinVarFunction { public: MinVarFunction(void) : _dim(0), _nbDataProcessed(0) { } bool pushVector(std::vector v) { if (_nbDataProcessed == 0) { //initialize _dim = v.size(); reset(); } //check dimension if (v.size() != _dim) return false; //update sum of components for (unsigned int i = 0; i < _dim; ++i) _v[i] += v[i]; //update working matrix for (unsigned int i = 0; i < _dim; ++i) for (unsigned int j = 0; j < _dim; ++j) _a[i][j] += v[i]*v[j]; ++_nbDataProcessed; return true; } bool computeMinVar(std::vector>& result) { if (_nbDataProcessed == 0) { reset(); return false; } result.clear(); //compute min var for (unsigned int i = 0; i < _dim; ++i) { result.push_back(_a[i]); for (unsigned int j = 0; j < _dim; ++j) result[i][j] = result[i][j] / _nbDataProcessed - (_v[i] / _nbDataProcessed) * (_v[j] / _nbDataProcessed); } //re-init for next computation reset(); return true; } void reset(void) { _nbDataProcessed = 0; //init working matrix _a.clear(); std::vector An; for (unsigned int i = 0; i < _dim; ++i) An.push_back(0); for (unsigned int i = 0; i < _dim; ++i) _a.push_back(An); //init working vector _v.clear(); for (unsigned int i = 0; i < _dim; ++i) _v.push_back(0); } private: unsigned int _dim; int _nbDataProcessed; std::vector> _a; std::vector _v; }; } /* namespace MinVar */ } /* namespace Statistic */ } /* AMDA */ #endif