From f7345ea71779db6bc35a9d5336813ad4d104d21e Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Mon, 17 Feb 2014 18:48:44 +0000 Subject: [PATCH] improvements of StatArray and derived classes --- latan/Dataset.hpp | 7 +-- latan/Sample.cpp | 29 ++++++++- latan/Sample.hpp | 83 +++++++----------------- latan/StatArray.hpp | 149 +++++++++++++++++++++++++++++--------------- 4 files changed, 151 insertions(+), 117 deletions(-) diff --git a/latan/Dataset.hpp b/latan/Dataset.hpp index d712270..14bd062 100644 --- a/latan/Dataset.hpp +++ b/latan/Dataset.hpp @@ -39,6 +39,7 @@ private: typedef StatArray Base; public: // constructor + using Base::Base; Dataset(void); Dataset(const std::string &listFileName, const std::string &dataName); template @@ -71,12 +72,6 @@ Dataset::Dataset(const std::string &listFileName, load(listFileName, dataName); } -template -template -Dataset::Dataset(const Eigen::EigenBase &dataset) -: Base(dataset) -{} - // destructor ////////////////////////////////////////////////////////////////// template Dataset::~Dataset(void) diff --git a/latan/Sample.cpp b/latan/Sample.cpp index b21dad2..58ac376 100644 --- a/latan/Sample.cpp +++ b/latan/Sample.cpp @@ -23,8 +23,33 @@ using namespace Latan; using namespace std; -template <> -IoObject::IoType Sample::getType(void) const +/****************************************************************************** + * DMatSample implementation * + ******************************************************************************/ +// constructors //////////////////////////////////////////////////////////////// +DMatSample::DMatSample(void) +: Sample() +{} + +DMatSample::DMatSample(const unsigned int nSample, const unsigned int nRow, + const unsigned int nCol) +: Sample(nSample) +{ + resizeMat(nRow, nCol); +} + +// destructor ////////////////////////////////////////////////////////////////// +DMatSample::~DMatSample(void) +{} + +// resize all matrices ///////////////////////////////////////////////////////// +void DMatSample::resizeMat(const unsigned int nRow, const unsigned int nCol) +{ + this->unaryExpr([nRow, nCol](DMat &m){m.resize(nRow, nCol);}); +} + +// IO type ///////////////////////////////////////////////////////////////////// +IoObject::IoType DMatSample::getType(void) const { return IoType::dMatSample; } diff --git a/latan/Sample.hpp b/latan/Sample.hpp index 7453da8..739dfdf 100644 --- a/latan/Sample.hpp +++ b/latan/Sample.hpp @@ -27,16 +27,18 @@ BEGIN_NAMESPACE -const int central = -1; +#define SAMPLE_OFFSET 1u + +const int central = -static_cast(SAMPLE_OFFSET); /****************************************************************************** * Sample class * ******************************************************************************/ template -class Sample: public StatArray, public IoObject +class Sample: public StatArray { private: - typedef StatArray Base; + typedef StatArray Base; public: // constructors Sample(void); @@ -45,74 +47,35 @@ public: Sample(const Eigen::EigenBase &s); // destructor virtual ~Sample(void); - // operators - T& operator[](const int s); - // IO type - virtual IoType getType(void) const; -private: - // index of the first element to take into account for statistics - virtual unsigned int getOffset(void) const; }; -template <> -IoObject::IoType Sample::getType(void) const; - -// specialization aliases -typedef Sample DMatSample; +/****************************************************************************** + * DMatSample class * + ******************************************************************************/ +class DMatSample: public Sample, public IoObject +{ +public: + // constructors + DMatSample(void); + DMatSample(const unsigned int nSample, const unsigned int nRow, + const unsigned int nCol); + using Sample::Sample; + // destructor + virtual ~DMatSample(void); + // resize all matrices + void resizeMat(const unsigned int nRow, const unsigned int nCol); + // IO type + virtual IoType getType(void) const; +}; /****************************************************************************** * Sample class template implementation * ******************************************************************************/ -// constructor ///////////////////////////////////////////////////////////////// -template -Sample::Sample(void) -: Base(static_cast(getOffset())) -{} - -template -Sample::Sample(const unsigned int nSample) -: Base(static_cast(nSample + getOffset())) -{} - -template -template -Sample::Sample(const Eigen::EigenBase &s) -: Base(s) -{} - // destructor ////////////////////////////////////////////////////////////////// template Sample::~Sample(void) {} -// operators /////////////////////////////////////////////////////////////////// -template -T& Sample::operator[](const int s) -{ - if (s >= 0) - { - return Base::operator[](s + 1); - } - else - { - return Base::operator[](0); - } -} - -// IO type ///////////////////////////////////////////////////////////////////// -template -IoObject::IoType Sample::getType(void) const -{ - return IoType::noType; -} - -// statistics ////////////////////////////////////////////////////////////////// -template -unsigned int Sample::getOffset(void) const -{ - return 1u; -} - END_NAMESPACE #endif // Latan_Sample_hpp_ diff --git a/latan/StatArray.hpp b/latan/StatArray.hpp index 6dfa08a..291abcf 100644 --- a/latan/StatArray.hpp +++ b/latan/StatArray.hpp @@ -22,13 +22,14 @@ #include #include +#include BEGIN_NAMESPACE /****************************************************************************** * Array class with statistics * ******************************************************************************/ -template +template class StatArray: public Eigen::Array { private: @@ -41,101 +42,151 @@ public: StatArray(const Eigen::EigenBase &s); // destructor virtual ~StatArray(void); + // access + unsigned int size(void) const; + // operators + T & operator[](const int s); + const T & operator[](const int s) const; // statistics - T mean(void) const; - T variance(void) const; -private: - // index of the first element to take into account for statistics - virtual unsigned int getOffset(void) const; - // operations for reduction in statistical computations - static inline T square(const T &a); - static inline T sum(const T &a, const T &b); + void bin(unsigned int binSize); + T mean(const unsigned int pos, const unsigned int n) const; + T mean(void) const; + T variance(const unsigned int pos, const unsigned int n) const; + T variance(void) const; }; -template <> -inline DMat StatArray::square(const DMat &a); +// reduction operations +namespace ReducOp +{ + template + inline T square(const T &a); + template + inline T sum(const T &a, const T &b); + template <> + inline DMat square(const DMat &a); +} /****************************************************************************** * StatArray class template implementation * ******************************************************************************/ // constructors //////////////////////////////////////////////////////////////// -template -StatArray::StatArray(void) -: Base(static_cast(1)) +template +StatArray::StatArray(void) +: Base(static_cast(offset)) {} -template -StatArray::StatArray(const unsigned int size) -: Base(static_cast(size)) -{} - -template -template -StatArray::StatArray(const Eigen::EigenBase &s) -: Base(s) +template +StatArray::StatArray(const unsigned int size) +: Base(static_cast(size + offset)) {} // destructor ////////////////////////////////////////////////////////////////// -template -StatArray::~StatArray(void) +template +StatArray::~StatArray(void) {} +// access ////////////////////////////////////////////////////////////////////// +template +unsigned int StatArray::size(void) const +{ + return Base::size() - offset; +} + +// operators /////////////////////////////////////////////////////////////////// +template +T & StatArray::operator[](const int s) +{ + return Base::operator[](s + offset); +} + +template +const T & StatArray::operator[](const int s) const +{ + return Base::operator[](s + offset); +} + + // statistics ////////////////////////////////////////////////////////////////// -template -T StatArray::mean(void) const +template +void StatArray::bin(unsigned int binSize) +{ + unsigned int q = size()/binSize, r = size()%binSize; + + for (unsigned int i = 0; i < q; ++i) + { + (*this)[i] = mean(i*binSize, binSize); + } + if (r != 0) + { + (*this)[q] = mean(q*binSize, r); + this->conservativeResize(offset + q + 1); + } + else + { + this->conservativeResize(offset + q); + } +} + +template +T StatArray::mean(const unsigned int pos, const unsigned int n) const { T result; - unsigned int size = this->size() - getOffset(); - if (size) + if (n) { - result = this->tail(size).redux(&StatArray::sum); + result = this->segment(pos+offset, n).redux(&ReducOp::sum); } - return result/static_cast(size); + return result/static_cast(n); } -template -T StatArray::variance(void) const +template +T StatArray::mean(void) const +{ + return mean(0, size()); +} + +template +T StatArray::variance(const unsigned int pos, const unsigned int n) const { T s, sqs, result; - unsigned int size = this->size() - getOffset(); - if (size) + if (n) { - s = this->tail(size).redux(&StatArray::sum); - sqs = this->tail(size).unaryExpr(&StatArray::square) - .redux(&StatArray::sum); - result = sqs - square(s)/static_cast(size); + s = this->segment(pos+offset, n).redux(&ReducOp::sum); + sqs = this->segment(pos+offset, n).unaryExpr(&ReducOp::square) + .redux(&ReducOp::sum); + result = sqs - ReducOp::square(s)/static_cast(n); } - return result/static_cast(size - 1); + return result/static_cast(n - 1); } +template +T StatArray::variance(void) const +{ + return variance(0, size()); +} + +// reduction operations //////////////////////////////////////////////////////// template -inline T StatArray::sum(const T &a, const T &b) +inline T ReducOp::sum(const T &a, const T &b) { return a + b; } template -inline T StatArray::square(const T &a) +inline T ReducOp::square(const T &a) { return a*a; } template <> -inline DMat StatArray::square(const DMat &a) +inline DMat ReducOp::square(const DMat &a) { return a.cwiseProduct(a); } -template -unsigned int StatArray::getOffset(void) const -{ - return 0u; -} - END_NAMESPACE #endif // Latan_StatArray_hpp_