2014-02-12 18:39:17 +00:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_StatArray_hpp_
|
|
|
|
#define Latan_StatArray_hpp_
|
|
|
|
|
|
|
|
#include <latan/Global.hpp>
|
|
|
|
#include <latan/Mat.hpp>
|
2014-02-17 18:48:44 +00:00
|
|
|
#include <iostream>
|
2014-02-12 18:39:17 +00:00
|
|
|
|
|
|
|
BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Array class with statistics *
|
|
|
|
******************************************************************************/
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T, unsigned int offset = 0>
|
2014-02-26 18:36:29 +00:00
|
|
|
class StatArray: public Array<T, dynamic, 1>
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
private:
|
2014-02-26 18:36:29 +00:00
|
|
|
typedef Array<T, dynamic, 1> Base;
|
2014-02-12 18:39:17 +00:00
|
|
|
public:
|
|
|
|
// constructors
|
|
|
|
StatArray(void);
|
2014-02-26 18:36:29 +00:00
|
|
|
StatArray(const Index size);
|
2014-02-17 18:50:03 +00:00
|
|
|
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray<T, offset>), Base,
|
2014-02-20 20:20:37 +00:00
|
|
|
ArrayBase)
|
2014-02-12 18:39:17 +00:00
|
|
|
// destructor
|
2014-02-20 22:54:11 +00:00
|
|
|
virtual ~StatArray(void) = default;
|
2014-02-17 18:48:44 +00:00
|
|
|
// access
|
2014-02-26 18:36:29 +00:00
|
|
|
Index size(void) const;
|
2014-02-17 18:48:44 +00:00
|
|
|
// operators
|
|
|
|
T & operator[](const int s);
|
|
|
|
const T & operator[](const int s) const;
|
2014-02-12 18:39:17 +00:00
|
|
|
// statistics
|
2014-02-26 18:36:29 +00:00
|
|
|
void bin(Index binSize);
|
|
|
|
T mean(const Index pos, const Index n) const;
|
2014-02-17 18:48:44 +00:00
|
|
|
T mean(void) const;
|
2014-02-26 18:36:29 +00:00
|
|
|
T variance(const Index pos, const Index n) const;
|
2014-02-17 18:48:44 +00:00
|
|
|
T variance(void) const;
|
2014-02-12 18:39:17 +00:00
|
|
|
};
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// reduction operations
|
|
|
|
namespace ReducOp
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
inline T square(const T &a);
|
|
|
|
template <typename T>
|
|
|
|
inline T sum(const T &a, const T &b);
|
|
|
|
template <>
|
|
|
|
inline DMat square(const DMat &a);
|
|
|
|
}
|
2014-02-12 18:39:17 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* StatArray class template implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T, unsigned int offset>
|
|
|
|
StatArray<T, offset>::StatArray(void)
|
|
|
|
: Base(static_cast<typename Base::Index>(offset))
|
2014-02-12 18:39:17 +00:00
|
|
|
{}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T, unsigned int offset>
|
2014-02-26 18:36:29 +00:00
|
|
|
StatArray<T, offset>::StatArray(const Index size)
|
2014-02-17 18:48:44 +00:00
|
|
|
: Base(static_cast<typename Base::Index>(size + offset))
|
2014-02-12 18:39:17 +00:00
|
|
|
{}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T, unsigned int offset>
|
2014-02-26 18:36:29 +00:00
|
|
|
Index StatArray<T, offset>::size(void) const
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
|
|
|
return Base::size() - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// operators ///////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T, unsigned int offset>
|
|
|
|
T & StatArray<T, offset>::operator[](const int s)
|
|
|
|
{
|
|
|
|
return Base::operator[](s + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, unsigned int offset>
|
|
|
|
const T & StatArray<T, offset>::operator[](const int s) const
|
|
|
|
{
|
|
|
|
return Base::operator[](s + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-12 18:39:17 +00:00
|
|
|
// statistics //////////////////////////////////////////////////////////////////
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T, unsigned int offset>
|
2014-02-26 18:36:29 +00:00
|
|
|
void StatArray<T, offset>::bin(Index binSize)
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2014-02-26 18:36:29 +00:00
|
|
|
Index q = size()/binSize, r = size()%binSize;
|
2014-02-17 18:48:44 +00:00
|
|
|
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < q; ++i)
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
|
|
|
(*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 <typename T, unsigned int offset>
|
2014-02-26 18:36:29 +00:00
|
|
|
T StatArray<T, offset>::mean(const Index pos, const Index n) const
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
T result;
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
if (n)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-17 18:48:44 +00:00
|
|
|
result = this->segment(pos+offset, n).redux(&ReducOp::sum<T>);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
return result/static_cast<double>(n);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T, unsigned int offset>
|
|
|
|
T StatArray<T, offset>::mean(void) const
|
|
|
|
{
|
|
|
|
return mean(0, size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, unsigned int offset>
|
2014-02-26 18:36:29 +00:00
|
|
|
T StatArray<T, offset>::variance(const Index pos, const Index n) const
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
T s, sqs, result;
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
if (n)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-17 18:48:44 +00:00
|
|
|
s = this->segment(pos+offset, n).redux(&ReducOp::sum<T>);
|
|
|
|
sqs = this->segment(pos+offset, n).unaryExpr(&ReducOp::square<T>)
|
|
|
|
.redux(&ReducOp::sum<T>);
|
|
|
|
result = sqs - ReducOp::square(s)/static_cast<double>(n);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
return result/static_cast<double>(n - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, unsigned int offset>
|
|
|
|
T StatArray<T, offset>::variance(void) const
|
|
|
|
{
|
|
|
|
return variance(0, size());
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// reduction operations ////////////////////////////////////////////////////////
|
2014-02-12 18:39:17 +00:00
|
|
|
template <typename T>
|
2014-02-17 18:48:44 +00:00
|
|
|
inline T ReducOp::sum(const T &a, const T &b)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2014-02-17 18:48:44 +00:00
|
|
|
inline T ReducOp::square(const T &a)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
return a*a;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2014-02-17 18:48:44 +00:00
|
|
|
inline DMat ReducOp::square(const DMat &a)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
return a.cwiseProduct(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // Latan_StatArray_hpp_
|