1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-09-20 05:25:37 +01:00
LatAnalyze/latan/StatArray.hpp

193 lines
5.0 KiB
C++
Raw Normal View History

/*
* 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>
#include <iostream>
2014-03-04 17:16:36 +00:00
#define FOR_STAT_ARRAY(ar, i) \
for (Latan::Index i = -(ar).offset; i < (ar).size(); ++i)
BEGIN_NAMESPACE
/******************************************************************************
* Array class with statistics *
******************************************************************************/
2014-03-04 17:16:36 +00:00
template <typename T, Index os = 0>
class StatArray: public Array<T, dynamic, 1>
{
private:
typedef Array<T, dynamic, 1> Base;
public:
// constructors
StatArray(void);
2014-03-03 14:21:37 +00:00
explicit StatArray(const Index size);
2014-03-04 17:16:36 +00:00
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray<T, os>), Base,
2014-02-20 20:20:37 +00:00
ArrayBase)
// destructor
2014-02-20 22:54:11 +00:00
virtual ~StatArray(void) = default;
// access
Index size(void) const;
// operators
2014-03-03 12:41:48 +00:00
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;
2014-03-04 17:16:36 +00:00
public:
static const Index offset = os;
};
// 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);
}
/******************************************************************************
* StatArray class template implementation *
******************************************************************************/
// constructors ////////////////////////////////////////////////////////////////
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
StatArray<T, os>::StatArray(void)
: Base(static_cast<typename Base::Index>(os))
{}
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
StatArray<T, os>::StatArray(const Index size)
: Base(static_cast<typename Base::Index>(size + os))
{}
// access //////////////////////////////////////////////////////////////////////
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
Index StatArray<T, os>::size(void) const
{
2014-03-04 17:16:36 +00:00
return Base::size() - os;
}
// operators ///////////////////////////////////////////////////////////////////
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
T & StatArray<T, os>::operator[](const Index s)
{
2014-03-04 17:16:36 +00:00
return Base::operator[](s + os);
}
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
const T & StatArray<T, os>::operator[](const Index s) const
{
2014-03-04 17:16:36 +00:00
return Base::operator[](s + os);
}
// statistics //////////////////////////////////////////////////////////////////
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
void StatArray<T, os>::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);
2014-03-04 17:16:36 +00:00
this->conservativeResize(os + q + 1);
}
else
{
2014-03-04 17:16:36 +00:00
this->conservativeResize(os + q);
}
}
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
T StatArray<T, os>::mean(const Index pos, const Index n) const
{
T result;
if (n)
{
2014-03-04 17:16:36 +00:00
result = this->segment(pos+os, n).redux(&ReducOp::sum<T>);
}
return result/static_cast<double>(n);
}
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
T StatArray<T, os>::mean(void) const
{
return mean(0, size());
}
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
T StatArray<T, os>::variance(const Index pos, const Index n) const
{
T s, sqs, result;
if (n)
{
2014-03-04 17:16:36 +00:00
s = this->segment(pos+os, n).redux(&ReducOp::sum<T>);
sqs = this->segment(pos+os, n).unaryExpr(&ReducOp::square<T>)
.redux(&ReducOp::sum<T>);
result = sqs - ReducOp::square(s)/static_cast<double>(n);
}
return result/static_cast<double>(n - 1);
}
2014-03-04 17:16:36 +00:00
template <typename T, Index os>
T StatArray<T, os>::variance(void) const
{
return variance(0, size());
}
// reduction operations ////////////////////////////////////////////////////////
template <typename T>
inline T ReducOp::sum(const T &a, const T &b)
{
return a + b;
}
template <typename T>
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_