2014-03-13 18:20:40 +00:00
|
|
|
/*
|
|
|
|
* CompiledModel.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2016-04-06 20:11:23 +01:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-03-13 18:20:40 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-02-10 00:23:36 +00:00
|
|
|
#include <LatAnalyze/Functional/CompiledModel.hpp>
|
|
|
|
#include <LatAnalyze/Core/Math.hpp>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/includes.hpp>
|
2014-03-13 18:20:40 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* CompiledDoubleModel implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2015-02-24 17:00:19 +00:00
|
|
|
CompiledDoubleModel::CompiledDoubleModel(const Index nArg, const Index nPar)
|
|
|
|
: nArg_(nArg)
|
|
|
|
, nPar_(nPar)
|
2014-03-13 18:20:40 +00:00
|
|
|
{}
|
|
|
|
|
2015-02-24 17:00:19 +00:00
|
|
|
CompiledDoubleModel::CompiledDoubleModel(const string &code, const Index nArg,
|
|
|
|
const Index nPar)
|
2014-03-13 18:20:40 +00:00
|
|
|
: CompiledDoubleModel(nArg, nPar)
|
|
|
|
{
|
|
|
|
setCode(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2014-09-29 15:10:38 +01:00
|
|
|
string CompiledDoubleModel::getCode(void)
|
|
|
|
{
|
|
|
|
return code_;
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:20:40 +00:00
|
|
|
void CompiledDoubleModel::setCode(const std::string &code)
|
|
|
|
{
|
2014-09-29 15:10:38 +01:00
|
|
|
code_ = code;
|
|
|
|
interpreter_.reset(new MathInterpreter(code_));
|
2014-03-13 18:20:40 +00:00
|
|
|
context_.reset(new RunContext);
|
2014-04-08 19:48:52 +01:00
|
|
|
varAddress_.reset(new std::vector<unsigned int>);
|
|
|
|
parAddress_.reset(new std::vector<unsigned int>);
|
|
|
|
isCompiled_.reset(new bool(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile /////////////////////////////////////////////////////////////////////
|
|
|
|
void CompiledDoubleModel::compile(void) const
|
|
|
|
{
|
|
|
|
if (!*isCompiled_)
|
|
|
|
{
|
|
|
|
varAddress_->clear();
|
|
|
|
parAddress_->clear();
|
2015-02-24 17:00:19 +00:00
|
|
|
for (Index i = 0; i < nArg_; ++i)
|
2014-04-08 19:48:52 +01:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
varAddress_->push_back(context_->addVariable("x_" + strFrom(i)));
|
2014-04-08 19:48:52 +01:00
|
|
|
}
|
2015-02-24 17:00:19 +00:00
|
|
|
for (Index j = 0; j < nPar_; ++j)
|
2014-04-08 19:48:52 +01:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
parAddress_->push_back(context_->addVariable("p_" + strFrom(j)));
|
2014-04-08 19:48:52 +01:00
|
|
|
}
|
2014-09-26 18:41:30 +01:00
|
|
|
interpreter_->compile(*(context_));
|
2014-04-08 19:48:52 +01:00
|
|
|
*isCompiled_ = true;
|
|
|
|
}
|
2014-03-13 18:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// function call ///////////////////////////////////////////////////////////////
|
|
|
|
double CompiledDoubleModel::operator()(const double *arg,
|
|
|
|
const double *par) const
|
|
|
|
{
|
|
|
|
double result;
|
|
|
|
|
2014-04-08 19:48:52 +01:00
|
|
|
compile();
|
2015-02-24 17:00:19 +00:00
|
|
|
for (unsigned int i = 0; i < nArg_; ++i)
|
2014-03-13 18:20:40 +00:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
context_->setVariable((*varAddress_)[i], arg[i]);
|
2014-03-13 18:20:40 +00:00
|
|
|
}
|
2015-02-24 17:00:19 +00:00
|
|
|
for (unsigned int j = 0; j < nPar_; ++j)
|
2014-03-13 18:20:40 +00:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
context_->setVariable((*parAddress_)[j], par[j]);
|
2014-03-13 18:20:40 +00:00
|
|
|
}
|
|
|
|
(*interpreter_)(*context_);
|
2014-09-26 18:41:30 +01:00
|
|
|
if (!context_->stack().empty())
|
2014-03-13 18:20:40 +00:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
result = context_->stack().top();
|
|
|
|
context_->stack().pop();
|
2014-03-13 18:20:40 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2014-04-08 19:48:52 +01:00
|
|
|
m.compile();
|
2014-03-13 18:20:40 +00:00
|
|
|
out << *(m.interpreter_);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2015-02-24 17:00:19 +00:00
|
|
|
|
|
|
|
// DoubleModel factory /////////////////////////////////////////////////////////
|
|
|
|
DoubleModel CompiledDoubleModel::makeModel(const bool makeHardCopy) const
|
|
|
|
{
|
|
|
|
DoubleModel res;
|
|
|
|
|
|
|
|
if (makeHardCopy)
|
|
|
|
{
|
|
|
|
CompiledDoubleModel copy(*this);
|
|
|
|
|
|
|
|
res.setFunction([copy](const double *x, const double *p)
|
|
|
|
{return copy(x, p);}, nArg_, nPar_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.setFunction([this](const double *x, const double *p)
|
|
|
|
{return (*this)(x, p);}, nArg_, nPar_);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
DoubleModel Latan::compile(const std::string &code, const Index nArg,
|
|
|
|
const Index nPar)
|
|
|
|
{
|
|
|
|
CompiledDoubleModel compiledModel(code, nArg, nPar);
|
|
|
|
|
|
|
|
return compiledModel.makeModel();
|
|
|
|
}
|