2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* CompiledFunction.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/>.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/CompiledFunction.hpp>
|
|
|
|
#include <LatAnalyze/Math.hpp>
|
|
|
|
#include <LatAnalyze/includes.hpp>
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Compiled double function implementation *
|
|
|
|
******************************************************************************/
|
2014-02-20 22:54:11 +00:00
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg)
|
2014-03-03 12:41:48 +00:00
|
|
|
: DoubleFunction(nArg, nullptr)
|
2014-02-13 19:23:39 +00:00
|
|
|
{}
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg,
|
|
|
|
const string &code)
|
2014-03-13 18:20:40 +00:00
|
|
|
: CompiledDoubleFunction(nArg)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
setCode(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2014-09-29 15:10:38 +01:00
|
|
|
string CompiledDoubleFunction::getCode(void)
|
|
|
|
{
|
|
|
|
return code_;
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
void CompiledDoubleFunction::setCode(const string &code)
|
|
|
|
{
|
2014-09-29 15:10:38 +01:00
|
|
|
code_ = code;
|
2014-02-13 19:23:39 +00:00
|
|
|
interpreter_.reset(new MathInterpreter(code));
|
|
|
|
context_.reset(new RunContext);
|
2014-04-08 19:48:52 +01:00
|
|
|
varAddress_.reset(new std::vector<unsigned int>);
|
|
|
|
isCompiled_.reset(new bool(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile /////////////////////////////////////////////////////////////////////
|
|
|
|
void CompiledDoubleFunction::compile(void) const
|
|
|
|
{
|
|
|
|
if (!*isCompiled_)
|
|
|
|
{
|
|
|
|
varAddress_->clear();
|
|
|
|
for (Index i = 0; i < getNArg(); ++i)
|
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
varAddress_->push_back(context_->addVariable("x_" + strFrom(i)));
|
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-02-06 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// function call ///////////////////////////////////////////////////////////////
|
2014-02-21 01:18:22 +00:00
|
|
|
double CompiledDoubleFunction::operator()(const double *arg) const
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
double result;
|
|
|
|
|
2014-04-08 19:48:52 +01:00
|
|
|
compile();
|
|
|
|
for (unsigned int i = 0; i < getNArg(); ++i)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
context_->setVariable((*varAddress_)[i], arg[i]);
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
2014-02-12 18:32:08 +00:00
|
|
|
(*interpreter_)(*context_);
|
2014-09-26 18:41:30 +01:00
|
|
|
if (!context_->stack().empty())
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
result = context_->stack().top();
|
|
|
|
context_->stack().pop();
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = 0.0;
|
|
|
|
LATAN_ERROR(Program, "program execution resulted in an empty stack");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IO //////////////////////////////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
ostream & Latan::operator<<(ostream &out, CompiledDoubleFunction &f)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-04-08 19:48:52 +01:00
|
|
|
f.compile();
|
2014-02-12 18:32:08 +00:00
|
|
|
out << *(f.interpreter_);
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|