/*
* StatArray.hpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2020 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
#define FOR_STAT_ARRAY(ar, i) \
for (Latan::Index i = -(ar).offset; i < (ar).size(); ++i)
BEGIN_LATAN_NAMESPACE
/******************************************************************************
* Array class with statistics *
******************************************************************************/
template
class StatArray: public Array, public IoObject
{
protected:
typedef Array Base;
public:
// constructors
StatArray(void);
explicit StatArray(const Index size);
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray), Base, ArrayExpr)
// destructor
virtual ~StatArray(void) = default;
// access
Index size(void) const;
void resize(const Index size);
// operators
T & operator[](const Index s);
const T & operator[](const Index s) const;
// statistics
void bin(Index binSize);
T sum(const Index pos = 0, const Index n = -1) const;
T meanOld(const Index pos = 0, const Index n = -1) const;
T mean(const Index pos = 0, const Index n = -1) const;
T covariance(const StatArray &array) const;
T variance(void) const;
// IO type
virtual IoType getType(void) const;
public:
static constexpr Index offset = os;
};
// reduction operations
namespace StatOp
{
// general templates
template
inline T prod(const T &a, const T &b);
template
inline T tensProd(const T &v1, const T &v2);
template
inline T sum(const T &a, const T &b);
}
// Sample types
const int central = -1;
template
using Sample = StatArray;
typedef Sample DSample;
typedef Sample> CSample;
/******************************************************************************
* StatArray class template implementation *
******************************************************************************/
// constructors ////////////////////////////////////////////////////////////////
template
StatArray::StatArray(void)
: Base(static_cast(os))
{}
template
StatArray::StatArray(const Index size)
: Base(static_cast(size + os))
{}
// access //////////////////////////////////////////////////////////////////////
template
Index StatArray::size(void) const
{
return Base::size() - os;
}
template
void StatArray::resize(const Index size)
{
Base::resize(size + os);
}
// operators ///////////////////////////////////////////////////////////////////
template
T & StatArray::operator[](const Index s)
{
return Base::operator[](s + os);
}
template
const T & StatArray::operator[](const Index s) const
{
return Base::operator[](s + os);
}
// 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(os + q + 1);
}
else
{
this->conservativeResize(os + q);
}
}
template
T StatArray::sum(const Index pos, const Index n) const
{
T result;
const Index m = (n >= 0) ? n : size();
result = (*this)[pos];
for (Index i = pos + 1; i < pos + m; ++i)
{
result += (*this)[i];
}
return result;
}
template
T StatArray::mean(const Index pos, const Index n) const
{
const Index m = (n >= 0) ? n : size();
return sum(pos, n)/static_cast(m);
}
template
T StatArray::covariance(const StatArray &array) const
{
T s1, s2, res;
s1 = array.sum();
s2 = this->sum();
res = StatOp::prod(array[0], (*this)[0]);
for (Index i = 1; i < size(); ++i)
{
res += StatOp::prod(array[i], (*this)[i]);
}
res -= StatOp::prod(s1, s2)/static_cast(size());
res /= static_cast(size() - 1);
return res;
}
template
T StatArray::variance(void) const
{
return covariance(*this);
}
// reduction operations ////////////////////////////////////////////////////////
namespace StatOp
{
template
inline T prod(const T &a, const T &b)
{
return a*b;
}
template <>
inline Mat prod(const Mat &a, const Mat &b)
{
return a.cwiseProduct(b);
}
}
// IO type /////////////////////////////////////////////////////////////////////
template
IoObject::IoType StatArray::getType(void) const
{
return IoType::noType;
}
END_LATAN_NAMESPACE
#endif // Latan_StatArray_hpp_