/*
* 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
#include
BEGIN_NAMESPACE
/******************************************************************************
* Array class with statistics *
******************************************************************************/
template
class StatArray: public Array
{
private:
typedef Array Base;
public:
// constructors
StatArray(void);
explicit StatArray(const Index size);
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray), Base,
ArrayBase)
// destructor
virtual ~StatArray(void) = default;
// access
Index size(void) const;
// operators
T & operator[](const Index s);
const T & operator[](const Index s) const;
// statistics
void bin(Index binSize);
T mean(const Index pos, const Index n) const;
T mean(void) const;
T variance(const Index pos, const Index n) const;
T variance(void) const;
};
// 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(offset))
{}
template
StatArray::StatArray(const Index size)
: Base(static_cast(size + offset))
{}
// access //////////////////////////////////////////////////////////////////////
template
Index StatArray::size(void) const
{
return Base::size() - offset;
}
// operators ///////////////////////////////////////////////////////////////////
template
T & StatArray::operator[](const Index s)
{
return Base::operator[](s + offset);
}
template
const T & StatArray::operator[](const Index s) const
{
return Base::operator[](s + offset);
}
// statistics //////////////////////////////////////////////////////////////////
template
void StatArray::bin(Index binSize)
{
Index q = size()/binSize, r = size()%binSize;
for (Index 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 Index pos, const Index n) const
{
T result;
if (n)
{
result = this->segment(pos+offset, n).redux(&ReducOp::sum);
}
return result/static_cast(n);
}
template
T StatArray::mean(void) const
{
return mean(0, size());
}
template
T StatArray::variance(const Index pos, const Index n) const
{
T s, sqs, result;
if (n)
{
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(n - 1);
}
template
T StatArray::variance(void) const
{
return variance(0, size());
}
// reduction operations ////////////////////////////////////////////////////////
template
inline T ReducOp::sum(const T &a, const T &b)
{
return a + b;
}
template
inline T ReducOp::square(const T &a)
{
return a*a;
}
template <>
inline DMat ReducOp::square(const DMat &a)
{
return a.cwiseProduct(a);
}
END_NAMESPACE
#endif // Latan_StatArray_hpp_