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

compiled model class

This commit is contained in:
Antonin Portelli 2014-03-13 18:20:40 +00:00
parent 82abf7872a
commit 0109379dc3
8 changed files with 168 additions and 20 deletions

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <latan/CompiledModel.hpp>
#include <latan/MinuitMinimizer.hpp> #include <latan/MinuitMinimizer.hpp>
#include <latan/Plot.hpp> #include <latan/Plot.hpp>
#include <latan/RandGen.hpp> #include <latan/RandGen.hpp>
@ -17,8 +18,7 @@ int main(void)
XYStatData data(nPoint, 1, 1); XYStatData data(nPoint, 1, 1);
RandGen rg; RandGen rg;
double x_k; double x_k;
auto f = [](const double x[1], const double p[2]) CompiledDoubleModel f(1, 2, "return p_1*exp(-x_0*p_0);");
{return p[1]*exp(-x[0]*p[0]);};
for (Index k = 0; k < nPoint; ++k) for (Index k = 0; k < nPoint; ++k)
{ {
@ -31,13 +31,11 @@ int main(void)
// fit // fit
DVec init = DVec::Constant(2, 0.5); DVec init = DVec::Constant(2, 0.5);
DoubleModel model(1, 2, f);
FitResult p; FitResult p;
MinuitMinimizer minimizer; MinuitMinimizer minimizer;
data.fitAllPoints(); data.fitAllPoints();
p = data.fit(model, minimizer, init, true, Minimizer::Verbosity::Normal); p = data.fit(f, minimizer, init, true, Minimizer::Verbosity::Normal);
cout << "a= " << p(0) << " b= " << p(1) cout << "a= " << p(0) << " b= " << p(1)
<< " chi^2/ndof= " << p.getChi2PerDof() << endl; << " chi^2/ndof= " << p.getChi2PerDof() << endl;

View File

@ -34,7 +34,7 @@ CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg)
CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg, CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg,
const string &code) const string &code)
: DoubleFunction(nArg, nullptr) : CompiledDoubleFunction(nArg)
{ {
setCode(code); setCode(code);
} }

View File

@ -23,13 +23,11 @@
#include <latan/Global.hpp> #include <latan/Global.hpp>
#include <latan/Function.hpp> #include <latan/Function.hpp>
#include <latan/MathInterpreter.hpp> #include <latan/MathInterpreter.hpp>
#include <stack>
#include <vector>
BEGIN_NAMESPACE BEGIN_NAMESPACE
/****************************************************************************** /******************************************************************************
* Compiled double function class * * compiled double function class *
******************************************************************************/ ******************************************************************************/
class CompiledDoubleFunction: public DoubleFunction class CompiledDoubleFunction: public DoubleFunction
{ {
@ -43,12 +41,10 @@ public:
void setCode(const std::string &code); void setCode(const std::string &code);
// function call // function call
using DoubleFunction::operator(); using DoubleFunction::operator();
virtual double operator()(const double *arg) const;
// IO // IO
friend std::ostream & operator<<(std::ostream &out, friend std::ostream & operator<<(std::ostream &out,
CompiledDoubleFunction &f); CompiledDoubleFunction &f);
private:
// function call
virtual double operator()(const double *arg) const;
private: private:
std::shared_ptr<MathInterpreter> interpreter_; std::shared_ptr<MathInterpreter> interpreter_;
std::shared_ptr<RunContext> context_; std::shared_ptr<RunContext> context_;

88
latan/CompiledModel.cpp Normal file
View File

@ -0,0 +1,88 @@
/*
* CompiledModel.cpp, 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/>.
*/
#include <latan/CompiledModel.hpp>
#include <latan/Math.hpp>
#include <latan/includes.hpp>
using namespace std;
using namespace Latan;
/******************************************************************************
* CompiledDoubleModel implementation *
******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
CompiledDoubleModel::CompiledDoubleModel(const unsigned nArg,
const unsigned nPar)
: DoubleModel(nArg, nPar, nullptr)
{}
CompiledDoubleModel::CompiledDoubleModel(const unsigned nArg,
const unsigned nPar,
const string &code)
: CompiledDoubleModel(nArg, nPar)
{
setCode(code);
}
// access //////////////////////////////////////////////////////////////////////
void CompiledDoubleModel::setCode(const std::string &code)
{
interpreter_.reset(new MathInterpreter(code));
context_.reset(new RunContext);
StdMath::addStdMathFunc(context_->fTable);
}
// function call ///////////////////////////////////////////////////////////////
double CompiledDoubleModel::operator()(const double *arg,
const double *par) const
{
double result;
for (Index i = 0; i < getNArg(); ++i)
{
context_->vTable["x_" + strFrom(i)] = arg[i];
}
for (Index j = 0; j < getNPar(); ++j)
{
context_->vTable["p_" + strFrom(j)] = par[j];
}
(*interpreter_)(*context_);
if (!context_->dStack.empty())
{
result = context_->dStack.top();
context_->dStack.pop();
}
else
{
result = 0.0;
LATAN_ERROR(Program, "program execution resulted in an empty stack");
}
return result;
}
// IO //////////////////////////////////////////////////////////////////////////
ostream & Latan::operator<<(std::ostream &out, CompiledDoubleModel &m)
{
m.interpreter_->compile();
out << *(m.interpreter_);
return out;
}

59
latan/CompiledModel.hpp Normal file
View File

@ -0,0 +1,59 @@
/*
* CompiledModel.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_CompiledModel_hpp_
#define Latan_CompiledModel_hpp_
#include <latan/Global.hpp>
#include <latan/Model.hpp>
#include <latan/MathInterpreter.hpp>
BEGIN_NAMESPACE
/******************************************************************************
* compiled double model class *
******************************************************************************/
class CompiledDoubleModel: public DoubleModel
{
public:
// constructor
explicit CompiledDoubleModel(const unsigned nArg, const unsigned nPar);
CompiledDoubleModel(const unsigned nArg, const unsigned nPar,
const std::string &code);
// destructor
virtual ~CompiledDoubleModel(void) = default;
// access
void setCode(const std::string &code);
// function call
using DoubleModel::operator();
virtual double operator()(const double *arg, const double *par) const;
// IO
friend std::ostream & operator<<(std::ostream &out,
CompiledDoubleModel &f);
private:
std::shared_ptr<MathInterpreter> interpreter_;
std::shared_ptr<RunContext> context_;
};
std::ostream & operator<<(std::ostream &out, CompiledDoubleModel &f);
END_NAMESPACE
#endif // Latan_CompiledModel_hpp_

View File

@ -80,9 +80,14 @@ double DoubleFunction::operator()(const std::vector<double> &arg) const
double DoubleFunction::operator()(std::stack<double> &arg) const double DoubleFunction::operator()(std::stack<double> &arg) const
{ {
checkSize(static_cast<Index>(arg.size()));
for (Index i = 0; i < getNArg(); ++i) for (Index i = 0; i < getNArg(); ++i)
{ {
if (arg.empty())
{
LATAN_ERROR(Size, "function argument stack is empty (expected "
+ strFrom(getNArg()) + "arguments, got " + strFrom(i)
+ ")");
}
(*buffer_)(getNArg() - i - 1) = arg.top(); (*buffer_)(getNArg() - i - 1) = arg.top();
arg.pop(); arg.pop();
} }

View File

@ -30,6 +30,7 @@ liblatan_la_SOURCES = \
AsciiLexer.lpp \ AsciiLexer.lpp \
Chi2Function.cpp \ Chi2Function.cpp \
CompiledFunction.cpp\ CompiledFunction.cpp\
CompiledModel.cpp \
Exceptions.cpp \ Exceptions.cpp \
Function.cpp \ Function.cpp \
Global.cpp \ Global.cpp \
@ -54,6 +55,7 @@ liblatan_la_HEADERS = \
AsciiFile.hpp \ AsciiFile.hpp \
Chi2Function.hpp \ Chi2Function.hpp \
CompiledFunction.hpp\ CompiledFunction.hpp\
CompiledModel.hpp \
Dataset.hpp \ Dataset.hpp \
FitInterface.hpp \ FitInterface.hpp \
Function.hpp \ Function.hpp \

View File

@ -76,7 +76,7 @@ double DoubleModel::operator()(const DVec &arg, const DVec &par) const
{ {
checkSize(arg.size(), par.size()); checkSize(arg.size(), par.size());
return f_(arg.data(), par.data()); return (*this)(arg.data(), par.data());
} }
double DoubleModel::operator()(const vector<double> &arg, double DoubleModel::operator()(const vector<double> &arg,
@ -84,12 +84,12 @@ double DoubleModel::operator()(const vector<double> &arg,
{ {
checkSize(static_cast<Index>(arg.size()), static_cast<Index>(par.size())); checkSize(static_cast<Index>(arg.size()), static_cast<Index>(par.size()));
return f_(arg.data(), par.data()); return (*this)(arg.data(), par.data());
} }
double DoubleModel::operator()(const double *data, const double *par) const double DoubleModel::operator()(const double *data, const double *par) const
{ {
return f_(data, par); return (*this)(data, par);
} }
// model bind ////////////////////////////////////////////////////////////////// // model bind //////////////////////////////////////////////////////////////////