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

more loop macros

This commit is contained in:
Antonin Portelli 2014-03-04 17:16:36 +00:00
parent 6b8ba55e47
commit 559a29f92d
2 changed files with 39 additions and 34 deletions

View File

@ -75,8 +75,8 @@ using Vec = Mat<T, dynamic, 1>;
typedef Vec<int> IVec; typedef Vec<int> IVec;
typedef Vec<double> DVec; typedef Vec<double> DVec;
#define FOR_VEC(vec, i) \ #define FOR_VEC(vec, i) for (Latan::Index i = 0; i < (vec).size(); ++i)
for (Latan::Index i = 0; i < (vec).size(); ++i) #define FOR_ARRAY(ar, i) FOR_VEC(ar, i)
// block types // block types
template <typename Derived> template <typename Derived>

View File

@ -24,12 +24,15 @@
#include <latan/Mat.hpp> #include <latan/Mat.hpp>
#include <iostream> #include <iostream>
#define FOR_STAT_ARRAY(ar, i) \
for (Latan::Index i = -(ar).offset; i < (ar).size(); ++i)
BEGIN_NAMESPACE BEGIN_NAMESPACE
/****************************************************************************** /******************************************************************************
* Array class with statistics * * Array class with statistics *
******************************************************************************/ ******************************************************************************/
template <typename T, unsigned int offset = 0> template <typename T, Index os = 0>
class StatArray: public Array<T, dynamic, 1> class StatArray: public Array<T, dynamic, 1>
{ {
private: private:
@ -38,7 +41,7 @@ public:
// constructors // constructors
StatArray(void); StatArray(void);
explicit StatArray(const Index size); explicit StatArray(const Index size);
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray<T, offset>), Base, EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray<T, os>), Base,
ArrayBase) ArrayBase)
// destructor // destructor
virtual ~StatArray(void) = default; virtual ~StatArray(void) = default;
@ -53,6 +56,8 @@ public:
T mean(void) const; T mean(void) const;
T variance(const Index pos, const Index n) const; T variance(const Index pos, const Index n) const;
T variance(void) const; T variance(void) const;
public:
static const Index offset = os;
}; };
// reduction operations // reduction operations
@ -70,40 +75,40 @@ namespace ReducOp
* StatArray class template implementation * * StatArray class template implementation *
******************************************************************************/ ******************************************************************************/
// constructors //////////////////////////////////////////////////////////////// // constructors ////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset> template <typename T, Index os>
StatArray<T, offset>::StatArray(void) StatArray<T, os>::StatArray(void)
: Base(static_cast<typename Base::Index>(offset)) : Base(static_cast<typename Base::Index>(os))
{} {}
template <typename T, unsigned int offset> template <typename T, Index os>
StatArray<T, offset>::StatArray(const Index size) StatArray<T, os>::StatArray(const Index size)
: Base(static_cast<typename Base::Index>(size + offset)) : Base(static_cast<typename Base::Index>(size + os))
{} {}
// access ////////////////////////////////////////////////////////////////////// // access //////////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset> template <typename T, Index os>
Index StatArray<T, offset>::size(void) const Index StatArray<T, os>::size(void) const
{ {
return Base::size() - offset; return Base::size() - os;
} }
// operators /////////////////////////////////////////////////////////////////// // operators ///////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset> template <typename T, Index os>
T & StatArray<T, offset>::operator[](const Index s) T & StatArray<T, os>::operator[](const Index s)
{ {
return Base::operator[](s + offset); return Base::operator[](s + os);
} }
template <typename T, unsigned int offset> template <typename T, Index os>
const T & StatArray<T, offset>::operator[](const Index s) const const T & StatArray<T, os>::operator[](const Index s) const
{ {
return Base::operator[](s + offset); return Base::operator[](s + os);
} }
// statistics ////////////////////////////////////////////////////////////////// // statistics //////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset> template <typename T, Index os>
void StatArray<T, offset>::bin(Index binSize) void StatArray<T, os>::bin(Index binSize)
{ {
Index q = size()/binSize, r = size()%binSize; Index q = size()/binSize, r = size()%binSize;
@ -114,42 +119,42 @@ void StatArray<T, offset>::bin(Index binSize)
if (r != 0) if (r != 0)
{ {
(*this)[q] = mean(q*binSize, r); (*this)[q] = mean(q*binSize, r);
this->conservativeResize(offset + q + 1); this->conservativeResize(os + q + 1);
} }
else else
{ {
this->conservativeResize(offset + q); this->conservativeResize(os + q);
} }
} }
template <typename T, unsigned int offset> template <typename T, Index os>
T StatArray<T, offset>::mean(const Index pos, const Index n) const T StatArray<T, os>::mean(const Index pos, const Index n) const
{ {
T result; T result;
if (n) if (n)
{ {
result = this->segment(pos+offset, n).redux(&ReducOp::sum<T>); result = this->segment(pos+os, n).redux(&ReducOp::sum<T>);
} }
return result/static_cast<double>(n); return result/static_cast<double>(n);
} }
template <typename T, unsigned int offset> template <typename T, Index os>
T StatArray<T, offset>::mean(void) const T StatArray<T, os>::mean(void) const
{ {
return mean(0, size()); return mean(0, size());
} }
template <typename T, unsigned int offset> template <typename T, Index os>
T StatArray<T, offset>::variance(const Index pos, const Index n) const T StatArray<T, os>::variance(const Index pos, const Index n) const
{ {
T s, sqs, result; T s, sqs, result;
if (n) if (n)
{ {
s = this->segment(pos+offset, n).redux(&ReducOp::sum<T>); s = this->segment(pos+os, n).redux(&ReducOp::sum<T>);
sqs = this->segment(pos+offset, n).unaryExpr(&ReducOp::square<T>) sqs = this->segment(pos+os, n).unaryExpr(&ReducOp::square<T>)
.redux(&ReducOp::sum<T>); .redux(&ReducOp::sum<T>);
result = sqs - ReducOp::square(s)/static_cast<double>(n); result = sqs - ReducOp::square(s)/static_cast<double>(n);
} }
@ -157,8 +162,8 @@ T StatArray<T, offset>::variance(const Index pos, const Index n) const
return result/static_cast<double>(n - 1); return result/static_cast<double>(n - 1);
} }
template <typename T, unsigned int offset> template <typename T, Index os>
T StatArray<T, offset>::variance(void) const T StatArray<T, os>::variance(void) const
{ {
return variance(0, size()); return variance(0, size());
} }