/* * StatArray.hpp, part of LatAnalyze 3 * * Copyright (C) 2013 - 2014 Antonin Portelli * * LatAnalyze 3 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * LatAnalyze 3 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with LatAnalyze 3. If not, see . */ #ifndef Latan_StatArray_hpp_ #define Latan_StatArray_hpp_ #include #include BEGIN_NAMESPACE /****************************************************************************** * Array class with statistics * ******************************************************************************/ template class StatArray: public Eigen::Array { private: typedef Eigen::Array Base; public: // constructors StatArray(void); StatArray(const unsigned int size); template StatArray(const Eigen::EigenBase &s); // destructor virtual ~StatArray(void); // 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); }; template <> inline DMat StatArray::square(const DMat &a); /****************************************************************************** * StatArray class template implementation * ******************************************************************************/ // constructors //////////////////////////////////////////////////////////////// template StatArray::StatArray(void) : Base(static_cast(1)) {} template StatArray::StatArray(const unsigned int size) : Base(static_cast(size)) {} template template StatArray::StatArray(const Eigen::EigenBase &s) : Base(s) {} // destructor ////////////////////////////////////////////////////////////////// template StatArray::~StatArray(void) {} // statistics ////////////////////////////////////////////////////////////////// template T StatArray::mean(void) const { T result; unsigned int size = this->size() - getOffset(); if (size) { result = this->tail(size).redux(&StatArray::sum); } return result/static_cast(size); } template T StatArray::variance(void) const { T s, sqs, result; unsigned int size = this->size() - getOffset(); if (size) { s = this->tail(size).redux(&StatArray::sum); sqs = this->tail(size).unaryExpr(&StatArray::square) .redux(&StatArray::sum); result = sqs - square(s)/static_cast(size); } return result/static_cast(size - 1); } template inline T StatArray::sum(const T &a, const T &b) { return a + b; } template inline T StatArray::square(const T &a) { return a*a; } template <> inline DMat StatArray::square(const DMat &a) { return a.cwiseProduct(a); } template unsigned int StatArray::getOffset(void) const { return 0u; } END_NAMESPACE #endif // Latan_StatArray_hpp_