1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 00:45:36 +00:00

Various Eigen types brought into Latan scope

This commit is contained in:
Antonin Portelli 2014-02-26 18:36:29 +00:00
parent fc18acc4bb
commit b8f8d66418
20 changed files with 144 additions and 104 deletions

View File

@ -76,7 +76,7 @@ void AsciiFile::save(const DMatSample &s, const std::string &name)
fileStream_ << "#L latan_begin rs_sample " << name << endl;
fileStream_ << s.size() << endl;
save(s[central], name + "_C");
for (int i = 0; i < static_cast<int>(s.size()); ++i)
for (Index i = 0; i < s.size(); ++i)
{
save(s[i], name + "_S_" + strFrom(i));
}

View File

@ -111,7 +111,7 @@ mat:
OPEN MAT ID INT floats CLOSE MAT
{
const unsigned int nRow = state->doubleQueue.size()/$INT, nCol = $INT;
int i, j, r = 0;
Index i, j, r = 0;
if (state->doubleQueue.size() != nRow*nCol)
{
@ -161,7 +161,7 @@ rg_state:
LATAN_ERROR(Size, "random generator state '" + *state->streamName
+ ":" + $ID + "' has a wrong size");
}
for (unsigned int i = 0; i < RLXG_STATE_SIZE; ++i)
for (Index i = 0; i < RLXG_STATE_SIZE; ++i)
{
state->stateBuf[i] = state->intQueue.front();
state->intQueue.pop();

View File

@ -52,7 +52,7 @@ double CompiledDoubleFunction::operator()(const double *arg) const
{
double result;
for (unsigned int i = 0; i < getNArg(); ++i)
for (Index i = 0; i < getNArg(); ++i)
{
context_->vTable["x_" + strFrom(i)] = arg[i];
}

View File

@ -49,7 +49,7 @@ public:
template <typename FileType>
void load(const std::string &listFileName, const std::string &dataName);
// resampling
Sample<T> bootstrapMean(const unsigned int nSample, RandGen& generator);
Sample<T> bootstrapMean(const Index nSample, RandGen& generator);
private:
// mean from pointer vector for resampling
void ptVectorMean(T &m, const std::vector<const T *> &v);
@ -93,7 +93,7 @@ void Dataset<T>::load(const std::string &listFileName,
}
listFile.close();
this->resize(dataFileName.size());
for (unsigned int i = 0; i < dataFileName.size(); ++i)
for (Index i = 0; i < static_cast<Index>(dataFileName.size()); ++i)
{
file.open(dataFileName[i], File::Mode::read);
(*this)[i] = file.template read<T>(dataName);
@ -103,21 +103,20 @@ void Dataset<T>::load(const std::string &listFileName,
// resampling //////////////////////////////////////////////////////////////////
template <typename T>
Sample<T> Dataset<T>::bootstrapMean(const unsigned int nSample,
RandGen& generator)
Sample<T> Dataset<T>::bootstrapMean(const Index nSample, RandGen& generator)
{
unsigned int nData = this->size();
Index nData = this->size();
std::vector<const T *> data(nData);
Sample<T> s(nSample);
for (unsigned int j = 0; j < nData; ++j)
for (Index j = 0; j < nData; ++j)
{
data[j] = &((*this)[j]);
}
ptVectorMean(s[central], data);
for (unsigned int i = 0; i < nSample; ++i)
for (Index i = 0; i < nSample; ++i)
{
for (unsigned int j = 0; j < nData; ++j)
for (Index j = 0; j < nData; ++j)
{
data[j] = &((*this)[generator.discreteUniform(nData)]);
}

View File

@ -42,28 +42,31 @@ using namespace std;
using namespace Latan;
/******************************************************************************
* Function implementation *
* DoubleFunction implementation *
******************************************************************************/
const DoubleFunction::vecFunc DoubleFunction::nullFunction_ = nullptr;
// constructor /////////////////////////////////////////////////////////////////
Function::Function(const unsigned nArg)
: nArg_(nArg)
DoubleFunction::DoubleFunction(const Index nArg, const vecFunc &f)
: buffer_(new DVec(nArg))
, f_(f)
{}
// access //////////////////////////////////////////////////////////////////////
unsigned int Function::getNArg(void) const
Index DoubleFunction::getNArg(void) const
{
return nArg_;
return buffer_->size();
}
/******************************************************************************
* DoubleFunction implementation *
******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
DoubleFunction::DoubleFunction(const unsigned nArg, vecFunc f)
: Function(nArg)
, buffer_(new DVec(nArg))
, f_(f)
{}
void DoubleFunction::setFunction(const vecFunc &f)
{
f_ = f;
}
void DoubleFunction::setNArg(const Index nArg)
{
buffer_->resize(nArg);
}
// function call ///////////////////////////////////////////////////////////////
double DoubleFunction::operator()(const double *arg) const
@ -85,7 +88,7 @@ double DoubleFunction::operator()(const DVec &arg) const
double DoubleFunction::operator()(const std::vector<double> &arg) const
{
if (arg.size() != getNArg())
if (arg.size() != static_cast<unsigned int>(getNArg()))
{
LATAN_ERROR(Size, "function argument vector has a wrong size (expected "
+ strFrom(getNArg()) + ", got " + strFrom(arg.size())
@ -97,7 +100,7 @@ double DoubleFunction::operator()(const std::vector<double> &arg) const
double DoubleFunction::operator()(std::stack<double> &arg) const
{
for (unsigned int i = 0; i < getNArg(); ++i)
for (Index i = 0; i < getNArg(); ++i)
{
if (arg.empty())
{
@ -120,7 +123,7 @@ double DoubleFunction::operator()(const double x0, ...) const
va_list va;
va_start(va, x0);
for (unsigned int i = 1; i < getNArg(); ++i)
for (Index i = 1; i < getNArg(); ++i)
{
(*buffer_)(i) = va_arg(va, double);
}

View File

@ -21,6 +21,7 @@
#define Latan_Function_hpp_
#include <latan/Global.hpp>
#include <latan/Mat.hpp>
#include <functional>
#include <memory>
#include <stack>
@ -28,34 +29,22 @@
BEGIN_NAMESPACE
/******************************************************************************
* Base function class *
******************************************************************************/
class Function
{
public:
// constructor
explicit Function(const unsigned nArg);
// destructor
virtual ~Function(void) = default;
// access
unsigned int getNArg(void) const;
private:
const unsigned int nArg_;
};
/******************************************************************************
* Double function class *
******************************************************************************/
class DoubleFunction: public Function
class DoubleFunction
{
private:
typedef std::function<double(const double *)> vecFunc;
public:
// constructor
explicit DoubleFunction(const unsigned nArg, vecFunc f = nullptr);
DoubleFunction(const Index nArg = 0, const vecFunc &f = nullFunction_);
// destructor
virtual ~DoubleFunction(void) = default;
// access
Index getNArg(void) const;
void setFunction(const vecFunc &f);
void setNArg(const Index nArg);
// function call
double operator()(const DVec &arg) const;
double operator()(const std::vector<double> &arg) const;
@ -67,6 +56,7 @@ protected:
private:
std::shared_ptr<DVec> buffer_;
vecFunc f_;
static const vecFunc nullFunction_;
};
END_NAMESPACE

View File

@ -20,9 +20,11 @@
#ifndef Latan_Global_hpp_
#define Latan_Global_hpp_
#include <latan/Eigen/Dense>
#include <map>
#include <string>
#include <latan/Eigen/Dense>
#include <sstream>
#include <cstdlib>
#define BEGIN_NAMESPACE namespace Latan {
#define END_NAMESPACE }
@ -53,7 +55,53 @@ Class & operator=(const Eigen::EigenBase<Derived> &m)\
BEGIN_NAMESPACE
// Environment
// Eigen type aliases //////////////////////////////////////////////////////////
const int dynamic = -1;
// array types
template <typename T, int nRow = dynamic, int nCol = dynamic>
using Array = Eigen::Array<T, nRow, nCol>;
// matrix types
template <typename T, int nRow = dynamic, int nCol = dynamic>
using Mat = Eigen::Matrix<T, nRow, nCol>;
typedef Mat<int> IMat;
typedef Mat<double> DMatBase;
// vector types
template <typename T>
using Vec = Mat<T, dynamic, 1>;
typedef Vec<int> IVec;
typedef Vec<double> DVec;
// block types
template <typename Derived>
using Block = Eigen::Block<Derived>;
template <typename Derived>
using ConstBlock = const Eigen::Block<const Derived>;
template <typename Derived>
using Row = typename Derived::RowXpr;
template <typename Derived>
using ConstRow = typename Derived::ConstRowXpr;
template <typename Derived>
using Col = typename Derived::ColXpr;
template <typename Derived>
using ConstCol = typename Derived::ConstColXpr;
// map type
template <typename Derived>
using Map = Eigen::Map<Derived>;
template <typename Derived>
using ConstMap = Eigen::Map<const Derived>;
// Index type //////////////////////////////////////////////////////////////////
typedef DMatBase::Index Index;
// Environment /////////////////////////////////////////////////////////////////
namespace Env
{
extern const std::string fullName;
@ -62,9 +110,6 @@ namespace Env
extern const std::string msgPrefix;
}
// vector type
typedef Eigen::VectorXd DVec;
// pointer type test
template <typename Derived, typename Base>
inline bool isDerivedFrom(const Base *pt)
@ -72,7 +117,7 @@ inline bool isDerivedFrom(const Base *pt)
return (dynamic_cast<const Derived *>(pt) != nullptr);
}
// string conversions
// string conversions //////////////////////////////////////////////////////////
template <typename T>
inline T strTo(const std::string &str)
{

View File

@ -31,7 +31,7 @@ DMat::DMat(void)
: Base()
{}
DMat::DMat(const unsigned int nRow, const unsigned int nCol)
DMat::DMat(const Index nRow, const Index nCol)
: Base(nRow, nCol)
{}

View File

@ -20,20 +20,23 @@
#ifndef Latan_Mat_hpp_
#define Latan_Mat_hpp_
#include <latan/Eigen/Dense>
#include <latan/Global.hpp>
#include <latan/IOObject.hpp>
#define FOR_MAT(mat, i, j) \
for (Index j = 0; j < mat.cols(); ++j)\
for (Index i = 0; i < mat.rows(); ++i)
BEGIN_NAMESPACE
class DMat: public Eigen::MatrixXd, public IoObject
class DMat: public DMatBase, public IoObject
{
private:
typedef Eigen::MatrixXd Base;
typedef DMatBase Base;
public:
// constructors
DMat(void);
DMat(const unsigned int nRow, const unsigned int nCol);
DMat(const Index nRow, const Index nCol);
EIGEN_EXPR_CTOR(DMat, DMat, Base, MatrixBase)
// destructor
virtual ~DMat(void) = default;

View File

@ -193,9 +193,9 @@ const string &ExprNode::getName(void) const
return name_;
}
unsigned int ExprNode::getNArg(void) const
Index ExprNode::getNArg(void) const
{
return static_cast<unsigned int>(arg_.size());
return static_cast<Index>(arg_.size());
}
const ExprNode * ExprNode::getParent(void) const
@ -203,7 +203,7 @@ const ExprNode * ExprNode::getParent(void) const
return parent_;
}
unsigned int ExprNode::getLevel(void) const
Index ExprNode::getLevel(void) const
{
if (getParent())
{
@ -230,16 +230,16 @@ void ExprNode::pushArg(ExprNode *node)
}
// ExprNode operators //////////////////////////////////////////////////////////
const ExprNode &ExprNode::operator[](const unsigned int i) const
const ExprNode &ExprNode::operator[](const Index i) const
{
return *arg_[i];
return *arg_[static_cast<unsigned int>(i)];
}
ostream &Latan::operator<<(ostream &out, const ExprNode &n)
{
unsigned int level = n.getLevel();
Index level = n.getLevel();
for (unsigned int i = 0; i <= level; ++i)
for (Index i = 0; i <= level; ++i)
{
if (i == level)
{
@ -255,7 +255,7 @@ ostream &Latan::operator<<(ostream &out, const ExprNode &n)
}
}
out << " " << n.getName() << endl;
for (unsigned int i = 0; i < n.getNArg(); ++i)
for (Index i = 0; i < n.getNArg(); ++i)
{
out << n[i];
}
@ -283,7 +283,7 @@ void SemicolonNode::compile(Program &program) const
{
auto &n = *this;
for (unsigned int i = 0; i < getNArg(); ++i)
for (Index i = 0; i < getNArg(); ++i)
{
bool isAssign = isDerivedFrom<AssignNode>(&n[i]);
bool isSemiColumn = isDerivedFrom<SemicolonNode>(&n[i]);
@ -330,7 +330,7 @@ void MathOpNode::compile(Program &program) const
{
auto &n = *this;
for (unsigned int i = 0; i < n.getNArg(); ++i)
for (Index i = 0; i < n.getNArg(); ++i)
{
n[i].compile(program);
}
@ -348,7 +348,7 @@ void FuncNode::compile(Program &program) const
{
auto &n = *this;
for (unsigned int i = 0; i < n.getNArg(); ++i)
for (Index i = 0; i < n.getNArg(); ++i)
{
n[i].compile(program);
}
@ -401,9 +401,9 @@ MathInterpreter::MathInterpreter(const std::string &code)
}
// access //////////////////////////////////////////////////////////////////////
const Instruction * MathInterpreter::operator[](const unsigned int i) const
const Instruction * MathInterpreter::operator[](const Index i) const
{
return program_[i].get();
return program_[static_cast<unsigned int>(i)].get();
}
const ExprNode * MathInterpreter::getAST(void) const

View File

@ -159,13 +159,13 @@ public:
virtual ~ExprNode() = default;
// access
const std::string& getName(void) const;
unsigned int getNArg(void) const;
Index getNArg(void) const;
const ExprNode * getParent(void) const;
unsigned int getLevel(void) const;
Index getLevel(void) const;
void setName(const std::string &name);
void pushArg(ExprNode *node);
// operator
const ExprNode &operator[](const unsigned int i) const;
const ExprNode &operator[](const Index i) const;
// compile
virtual void compile(Program &program) const = 0;
private:
@ -241,7 +241,7 @@ public:
// destructor
~MathInterpreter(void) = default;
// access
const Instruction * operator[](const unsigned int i) const;
const Instruction * operator[](const Index i) const;
const ExprNode * getAST(void) const;
// initialization
void setCode(const std::string &code);

View File

@ -32,9 +32,9 @@ Minimizer::Minimizer(Verbosity verbosity)
{}
// access //////////////////////////////////////////////////////////////////////
unsigned int Minimizer::getDim(void) const
Index Minimizer::getDim(void) const
{
return static_cast<unsigned int>(x_.size());
return x_.size();
}
DVec & Minimizer::getState(void)

View File

@ -45,7 +45,7 @@ public:
// destructor
virtual ~Minimizer(void) = default;
// access
unsigned int getDim(void) const;
Index getDim(void) const;
Verbosity getVerbosity(void) const;
void setInit(const DVec &x0);
void setVerbosity(const Verbosity verbosity);

View File

@ -70,7 +70,7 @@ const DVec & MinuitMinimizer::operator()(const DoubleFunction &f)
MinuitFunction minuitF(f);
MnUserParameters parameters;
for (unsigned int i = 0; i < x.size(); ++i)
for (Index i = 0; i < x.size(); ++i)
{
parameters.Add("x_" + strFrom(i), x(i), 0.1*fabs(x(i)));
}

View File

@ -27,11 +27,11 @@
BEGIN_NAMESPACE
class RandGenState: public Eigen::Array<int, RLXG_STATE_SIZE, 1>,
class RandGenState: public Array<int, RLXG_STATE_SIZE, 1>,
public IoObject
{
private:
typedef Eigen::Array<int, RLXG_STATE_SIZE, 1> Base;
typedef Array<int, RLXG_STATE_SIZE, 1> Base;
public:
// destructor
virtual ~RandGenState(void) = default;

View File

@ -31,15 +31,15 @@ DMatSample::DMatSample(void)
: Sample<DMat>()
{}
DMatSample::DMatSample(const unsigned int nSample, const unsigned int nRow,
const unsigned int nCol)
DMatSample::DMatSample(const Index nSample, const Index nRow,
const Index nCol)
: Sample<DMat>(nSample)
{
resizeMat(nRow, nCol);
}
// resize all matrices /////////////////////////////////////////////////////////
void DMatSample::resizeMat(const unsigned int nRow, const unsigned int nCol)
void DMatSample::resizeMat(const Index nRow, const Index nCol)
{
this->unaryExpr([nRow, nCol](DMat &m){m.resize(nRow, nCol);});
}

View File

@ -54,13 +54,12 @@ class DMatSample: public Sample<DMat>, public IoObject
public:
// constructors
DMatSample(void);
DMatSample(const unsigned int nSample, const unsigned int nRow,
const unsigned int nCol);
DMatSample(const Index nSample, const Index nRow, const Index nCol);
using Sample<DMat>::Sample;
// destructor
virtual ~DMatSample(void) = default;
// resize all matrices
void resizeMat(const unsigned int nRow, const unsigned int nCol);
void resizeMat(const Index nRow, const Index nCol);
// IO type
virtual IoType getType(void) const;
};

View File

@ -30,28 +30,28 @@ BEGIN_NAMESPACE
* Array class with statistics *
******************************************************************************/
template <typename T, unsigned int offset = 0>
class StatArray: public Eigen::Array<T, Eigen::Dynamic, 1>
class StatArray: public Array<T, dynamic, 1>
{
private:
typedef Eigen::Array<T, Eigen::Dynamic, 1> Base;
typedef Array<T, dynamic, 1> Base;
public:
// constructors
StatArray(void);
StatArray(const unsigned int size);
StatArray(const Index size);
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray<T, offset>), Base,
ArrayBase)
// destructor
virtual ~StatArray(void) = default;
// access
unsigned int size(void) const;
Index size(void) const;
// operators
T & operator[](const int s);
const T & operator[](const int s) const;
// statistics
void bin(unsigned int binSize);
T mean(const unsigned int pos, const unsigned int n) const;
void bin(Index binSize);
T mean(const Index pos, const Index n) const;
T mean(void) const;
T variance(const unsigned int pos, const unsigned int n) const;
T variance(const Index pos, const Index n) const;
T variance(void) const;
};
@ -76,13 +76,13 @@ StatArray<T, offset>::StatArray(void)
{}
template <typename T, unsigned int offset>
StatArray<T, offset>::StatArray(const unsigned int size)
StatArray<T, offset>::StatArray(const Index size)
: Base(static_cast<typename Base::Index>(size + offset))
{}
// access //////////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset>
unsigned int StatArray<T, offset>::size(void) const
Index StatArray<T, offset>::size(void) const
{
return Base::size() - offset;
}
@ -103,11 +103,11 @@ const T & StatArray<T, offset>::operator[](const int s) const
// statistics //////////////////////////////////////////////////////////////////
template <typename T, unsigned int offset>
void StatArray<T, offset>::bin(unsigned int binSize)
void StatArray<T, offset>::bin(Index binSize)
{
unsigned int q = size()/binSize, r = size()%binSize;
Index q = size()/binSize, r = size()%binSize;
for (unsigned int i = 0; i < q; ++i)
for (Index i = 0; i < q; ++i)
{
(*this)[i] = mean(i*binSize, binSize);
}
@ -123,7 +123,7 @@ void StatArray<T, offset>::bin(unsigned int binSize)
}
template <typename T, unsigned int offset>
T StatArray<T, offset>::mean(const unsigned int pos, const unsigned int n) const
T StatArray<T, offset>::mean(const Index pos, const Index n) const
{
T result;
@ -142,7 +142,7 @@ T StatArray<T, offset>::mean(void) const
}
template <typename T, unsigned int offset>
T StatArray<T, offset>::variance(const unsigned int pos, const unsigned int n) const
T StatArray<T, offset>::variance(const Index pos, const Index n) const
{
T s, sqs, result;

View File

@ -23,6 +23,7 @@
#include <fstream>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <utility>
#include <cfloat>

View File

@ -5,7 +5,7 @@
#include <latan/Dataset.hpp>
#ifndef DEF_NSAMPLE
#define DEF_NSAMPLE 100u
#define DEF_NSAMPLE 100
#endif
using namespace std;
@ -22,7 +22,7 @@ static void usage(const string &cmdName)
int main(int argc, char *argv[])
{
int c;
unsigned int nSample = DEF_NSAMPLE;
Index nSample = DEF_NSAMPLE;
string manFileName, name, outFileName, stateFileName;
char *cmdName;
@ -33,7 +33,7 @@ int main(int argc, char *argv[])
switch (c)
{
case 'n':
nSample = strTo<unsigned int>(optarg);
nSample = strTo<Index>(optarg);
break;
case 'r':
stateFileName = optarg;