2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* Function.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-01-30 19:28:30 +00:00
|
|
|
#include <latan/Function.hpp>
|
|
|
|
#include <latan/includes.hpp>
|
|
|
|
|
|
|
|
#define DEF_STD_FUNC_1ARG(name, funcName) \
|
|
|
|
name##Function::name##Function(void): DoubleFunction(1) {}\
|
|
|
|
name##Function::~name##Function(void) {}\
|
|
|
|
double name##Function::operator()(std::vector<double> &arg)\
|
|
|
|
{\
|
|
|
|
return funcName(arg[0]);\
|
|
|
|
}\
|
|
|
|
name##Function STDMATH_NAMESPACE::funcName;
|
|
|
|
|
|
|
|
#define DEF_STD_FUNC_2ARG(name, funcName) \
|
|
|
|
name##Function::name##Function(void): DoubleFunction(2) {}\
|
|
|
|
name##Function::~name##Function(void) {}\
|
|
|
|
double name##Function::operator()(std::vector<double> &arg)\
|
|
|
|
{\
|
|
|
|
return funcName(arg[0], arg[1]);\
|
|
|
|
}\
|
|
|
|
name##Function STDMATH_NAMESPACE::funcName;
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
2014-02-26 18:36:29 +00:00
|
|
|
* DoubleFunction implementation *
|
2014-01-30 19:28:30 +00:00
|
|
|
******************************************************************************/
|
2014-02-26 18:36:29 +00:00
|
|
|
const DoubleFunction::vecFunc DoubleFunction::nullFunction_ = nullptr;
|
|
|
|
|
2014-02-20 22:54:11 +00:00
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2014-02-26 18:36:29 +00:00
|
|
|
DoubleFunction::DoubleFunction(const Index nArg, const vecFunc &f)
|
2014-03-03 12:41:48 +00:00
|
|
|
: buffer_(new DVec)
|
|
|
|
{
|
|
|
|
setFunction(f, nArg);
|
|
|
|
}
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2014-02-26 18:36:29 +00:00
|
|
|
Index DoubleFunction::getNArg(void) const
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-02-26 18:36:29 +00:00
|
|
|
return buffer_->size();
|
2014-01-30 19:28:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
void DoubleFunction::setFunction(const vecFunc &f, const Index nArg)
|
2014-02-26 18:36:29 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
buffer_->resize(nArg);
|
2014-02-26 18:36:29 +00:00
|
|
|
f_ = f;
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
// error checking //////////////////////////////////////////////////////////////
|
|
|
|
void DoubleFunction::checkSize(const Index nPar) const
|
2014-02-26 18:36:29 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
if (nPar != getNArg())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "function argument vector has a wrong size (expected "
|
|
|
|
+ strFrom(getNArg()) + ", got " + strFrom(nPar)
|
|
|
|
+ ")");
|
|
|
|
}
|
2014-02-26 18:36:29 +00:00
|
|
|
}
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-02-20 22:54:11 +00:00
|
|
|
// function call ///////////////////////////////////////////////////////////////
|
2014-02-21 01:18:22 +00:00
|
|
|
double DoubleFunction::operator()(const double *arg) const
|
2014-02-12 18:32:08 +00:00
|
|
|
{
|
|
|
|
return f_(arg);
|
|
|
|
}
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-02-21 01:18:22 +00:00
|
|
|
double DoubleFunction::operator()(const DVec &arg) const
|
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
checkSize(arg.size());
|
2014-02-21 01:18:22 +00:00
|
|
|
|
|
|
|
return (*this)(arg.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
double DoubleFunction::operator()(const std::vector<double> &arg) const
|
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
checkSize(static_cast<Index>(arg.size()));
|
2014-02-21 01:18:22 +00:00
|
|
|
|
|
|
|
return (*this)(arg.data());
|
|
|
|
}
|
|
|
|
|
2014-02-12 18:32:08 +00:00
|
|
|
double DoubleFunction::operator()(std::stack<double> &arg) const
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-03-03 12:41:48 +00:00
|
|
|
checkSize(static_cast<Index>(arg.size()));
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < getNArg(); ++i)
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-02-21 01:18:22 +00:00
|
|
|
(*buffer_)(getNArg() - i - 1) = arg.top();
|
2014-01-30 19:28:30 +00:00
|
|
|
arg.pop();
|
|
|
|
}
|
|
|
|
|
2014-02-21 01:18:22 +00:00
|
|
|
return (*this)(*buffer_);
|
2014-01-30 19:28:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 18:32:08 +00:00
|
|
|
double DoubleFunction::operator()(const double x0, ...) const
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-02-12 18:32:08 +00:00
|
|
|
(*buffer_)[0] = x0;
|
2014-01-30 19:28:30 +00:00
|
|
|
if (getNArg() > 1)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, x0);
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 1; i < getNArg(); ++i)
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-02-21 01:18:22 +00:00
|
|
|
(*buffer_)(i) = va_arg(va, double);
|
2014-01-30 19:28:30 +00:00
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2014-02-21 01:18:22 +00:00
|
|
|
return (*this)(*buffer_);
|
2014-01-30 19:28:30 +00:00
|
|
|
}
|