1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-10 19:20:44 +01:00

improvements of StatArray and derived classes

This commit is contained in:
Antonin Portelli 2014-02-17 18:48:44 +00:00
parent 34b3b36de4
commit f7345ea717
4 changed files with 151 additions and 117 deletions

View File

@ -39,6 +39,7 @@ private:
typedef StatArray<T> Base; typedef StatArray<T> Base;
public: public:
// constructor // constructor
using Base::Base;
Dataset(void); Dataset(void);
Dataset(const std::string &listFileName, const std::string &dataName); Dataset(const std::string &listFileName, const std::string &dataName);
template <typename Derived> template <typename Derived>
@ -71,12 +72,6 @@ Dataset<T, FileType>::Dataset(const std::string &listFileName,
load(listFileName, dataName); load(listFileName, dataName);
} }
template <typename T, typename FileType>
template <typename Derived>
Dataset<T, FileType>::Dataset(const Eigen::EigenBase<Derived> &dataset)
: Base(dataset)
{}
// destructor ////////////////////////////////////////////////////////////////// // destructor //////////////////////////////////////////////////////////////////
template <typename T, typename FileType> template <typename T, typename FileType>
Dataset<T, FileType>::~Dataset(void) Dataset<T, FileType>::~Dataset(void)

View File

@ -23,8 +23,33 @@
using namespace Latan; using namespace Latan;
using namespace std; using namespace std;
template <> /******************************************************************************
IoObject::IoType Sample<DMat>::getType(void) const * DMatSample implementation *
******************************************************************************/
// constructors ////////////////////////////////////////////////////////////////
DMatSample::DMatSample(void)
: Sample<DMat>()
{}
DMatSample::DMatSample(const unsigned int nSample, const unsigned int nRow,
const unsigned int nCol)
: Sample<DMat>(nSample)
{
resizeMat(nRow, nCol);
}
// destructor //////////////////////////////////////////////////////////////////
DMatSample::~DMatSample(void)
{}
// resize all matrices /////////////////////////////////////////////////////////
void DMatSample::resizeMat(const unsigned int nRow, const unsigned int nCol)
{
this->unaryExpr([nRow, nCol](DMat &m){m.resize(nRow, nCol);});
}
// IO type /////////////////////////////////////////////////////////////////////
IoObject::IoType DMatSample::getType(void) const
{ {
return IoType::dMatSample; return IoType::dMatSample;
} }

View File

@ -27,16 +27,18 @@
BEGIN_NAMESPACE BEGIN_NAMESPACE
const int central = -1; #define SAMPLE_OFFSET 1u
const int central = -static_cast<int>(SAMPLE_OFFSET);
/****************************************************************************** /******************************************************************************
* Sample class * * Sample class *
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T>
class Sample: public StatArray<T>, public IoObject class Sample: public StatArray<T, SAMPLE_OFFSET>
{ {
private: private:
typedef StatArray<T> Base; typedef StatArray<T, SAMPLE_OFFSET> Base;
public: public:
// constructors // constructors
Sample(void); Sample(void);
@ -45,74 +47,35 @@ public:
Sample(const Eigen::EigenBase<Derived> &s); Sample(const Eigen::EigenBase<Derived> &s);
// destructor // destructor
virtual ~Sample(void); virtual ~Sample(void);
// operators
T& operator[](const int s);
// IO type
virtual IoType getType(void) const;
private:
// index of the first element to take into account for statistics
virtual unsigned int getOffset(void) const;
}; };
template <> /******************************************************************************
IoObject::IoType Sample<DMat>::getType(void) const; * DMatSample class *
******************************************************************************/
// specialization aliases class DMatSample: public Sample<DMat>, public IoObject
typedef Sample<DMat> DMatSample; {
public:
// constructors
DMatSample(void);
DMatSample(const unsigned int nSample, const unsigned int nRow,
const unsigned int nCol);
using Sample<DMat>::Sample;
// destructor
virtual ~DMatSample(void);
// resize all matrices
void resizeMat(const unsigned int nRow, const unsigned int nCol);
// IO type
virtual IoType getType(void) const;
};
/****************************************************************************** /******************************************************************************
* Sample class template implementation * * Sample class template implementation *
******************************************************************************/ ******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
template <typename T>
Sample<T>::Sample(void)
: Base(static_cast<typename Base::Index>(getOffset()))
{}
template <typename T>
Sample<T>::Sample(const unsigned int nSample)
: Base(static_cast<typename Base::Index>(nSample + getOffset()))
{}
template <typename T>
template <typename Derived>
Sample<T>::Sample(const Eigen::EigenBase<Derived> &s)
: Base(s)
{}
// destructor ////////////////////////////////////////////////////////////////// // destructor //////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
Sample<T>::~Sample(void) Sample<T>::~Sample(void)
{} {}
// operators ///////////////////////////////////////////////////////////////////
template <typename T>
T& Sample<T>::operator[](const int s)
{
if (s >= 0)
{
return Base::operator[](s + 1);
}
else
{
return Base::operator[](0);
}
}
// IO type /////////////////////////////////////////////////////////////////////
template <typename T>
IoObject::IoType Sample<T>::getType(void) const
{
return IoType::noType;
}
// statistics //////////////////////////////////////////////////////////////////
template <typename T>
unsigned int Sample<T>::getOffset(void) const
{
return 1u;
}
END_NAMESPACE END_NAMESPACE
#endif // Latan_Sample_hpp_ #endif // Latan_Sample_hpp_

View File

@ -22,13 +22,14 @@
#include <latan/Global.hpp> #include <latan/Global.hpp>
#include <latan/Mat.hpp> #include <latan/Mat.hpp>
#include <iostream>
BEGIN_NAMESPACE BEGIN_NAMESPACE
/****************************************************************************** /******************************************************************************
* Array class with statistics * * Array class with statistics *
******************************************************************************/ ******************************************************************************/
template <typename T> template <typename T, unsigned int offset = 0>
class StatArray: public Eigen::Array<T, Eigen::Dynamic, 1> class StatArray: public Eigen::Array<T, Eigen::Dynamic, 1>
{ {
private: private:
@ -41,101 +42,151 @@ public:
StatArray(const Eigen::EigenBase<Derived> &s); StatArray(const Eigen::EigenBase<Derived> &s);
// destructor // destructor
virtual ~StatArray(void); virtual ~StatArray(void);
// access
unsigned int size(void) const;
// operators
T & operator[](const int s);
const T & operator[](const int s) const;
// statistics // statistics
T mean(void) const; void bin(unsigned int binSize);
T variance(void) const; T mean(const unsigned int pos, const unsigned int n) const;
private: T mean(void) const;
// index of the first element to take into account for statistics T variance(const unsigned int pos, const unsigned int n) const;
virtual unsigned int getOffset(void) const; T variance(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 <> // reduction operations
inline DMat StatArray<DMat>::square(const DMat &a); 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 * * StatArray class template implementation *
******************************************************************************/ ******************************************************************************/
// constructors //////////////////////////////////////////////////////////////// // constructors ////////////////////////////////////////////////////////////////
template <typename T> template <typename T, unsigned int offset>
StatArray<T>::StatArray(void) StatArray<T, offset>::StatArray(void)
: Base(static_cast<typename Base::Index>(1)) : Base(static_cast<typename Base::Index>(offset))
{} {}
template <typename T> template <typename T, unsigned int offset>
StatArray<T>::StatArray(const unsigned int size) StatArray<T, offset>::StatArray(const unsigned int size)
: Base(static_cast<typename Base::Index>(size)) : Base(static_cast<typename Base::Index>(size + offset))
{}
template <typename T>
template <typename Derived>
StatArray<T>::StatArray(const Eigen::EigenBase<Derived> &s)
: Base(s)
{} {}
// destructor ////////////////////////////////////////////////////////////////// // destructor //////////////////////////////////////////////////////////////////
template <typename T> template <typename T, unsigned int offset>
StatArray<T>::~StatArray(void) StatArray<T, offset>::~StatArray(void)
{} {}
// access //////////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset>
unsigned int StatArray<T, offset>::size(void) const
{
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);
}
// statistics ////////////////////////////////////////////////////////////////// // statistics //////////////////////////////////////////////////////////////////
template <typename T> template <typename T, unsigned int offset>
T StatArray<T>::mean(void) const void StatArray<T, offset>::bin(unsigned int binSize)
{
unsigned int q = size()/binSize, r = size()%binSize;
for (unsigned int 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 <typename T, unsigned int offset>
T StatArray<T, offset>::mean(const unsigned int pos, const unsigned int n) const
{ {
T result; T result;
unsigned int size = this->size() - getOffset();
if (size) if (n)
{ {
result = this->tail(size).redux(&StatArray<T>::sum); result = this->segment(pos+offset, n).redux(&ReducOp::sum<T>);
} }
return result/static_cast<double>(size); return result/static_cast<double>(n);
} }
template <typename T> template <typename T, unsigned int offset>
T StatArray<T>::variance(void) const T StatArray<T, offset>::mean(void) const
{
return mean(0, size());
}
template <typename T, unsigned int offset>
T StatArray<T, offset>::variance(const unsigned int pos, const unsigned int n) const
{ {
T s, sqs, result; T s, sqs, result;
unsigned int size = this->size() - getOffset();
if (size) if (n)
{ {
s = this->tail(size).redux(&StatArray<T>::sum); s = this->segment(pos+offset, n).redux(&ReducOp::sum<T>);
sqs = this->tail(size).unaryExpr(&StatArray<T>::square) sqs = this->segment(pos+offset, n).unaryExpr(&ReducOp::square<T>)
.redux(&StatArray<T>::sum); .redux(&ReducOp::sum<T>);
result = sqs - square(s)/static_cast<double>(size); result = sqs - ReducOp::square(s)/static_cast<double>(n);
} }
return result/static_cast<double>(size - 1); return result/static_cast<double>(n - 1);
} }
template <typename T, unsigned int offset>
T StatArray<T, offset>::variance(void) const
{
return variance(0, size());
}
// reduction operations ////////////////////////////////////////////////////////
template <typename T> template <typename T>
inline T StatArray<T>::sum(const T &a, const T &b) inline T ReducOp::sum(const T &a, const T &b)
{ {
return a + b; return a + b;
} }
template <typename T> template <typename T>
inline T StatArray<T>::square(const T &a) inline T ReducOp::square(const T &a)
{ {
return a*a; return a*a;
} }
template <> template <>
inline DMat StatArray<DMat>::square(const DMat &a) inline DMat ReducOp::square(const DMat &a)
{ {
return a.cwiseProduct(a); return a.cwiseProduct(a);
} }
template <typename T>
unsigned int StatArray<T>::getOffset(void) const
{
return 0u;
}
END_NAMESPACE END_NAMESPACE
#endif // Latan_StatArray_hpp_ #endif // Latan_StatArray_hpp_