diff --git a/latan/CompiledFunction.cpp b/latan/CompiledFunction.cpp index 72c635c..c334c09 100644 --- a/latan/CompiledFunction.cpp +++ b/latan/CompiledFunction.cpp @@ -48,16 +48,10 @@ void CompiledDoubleFunction::setCode(const string &code) } // function call /////////////////////////////////////////////////////////////// -double CompiledDoubleFunction::evaluate(const vector &arg) const +double CompiledDoubleFunction::operator()(const double *arg) const { double result; - if (arg.size() != getNArg()) - { - LATAN_ERROR(Size, "function argument vector has a wrong size (got " + - strFrom(arg.size()) + ", expected " + strFrom(getNArg()) + - ")"); - } for (unsigned int i = 0; i < getNArg(); ++i) { context_->vTable["x_" + strFrom(i)] = arg[i]; diff --git a/latan/CompiledFunction.hpp b/latan/CompiledFunction.hpp index afb8f6e..75d1fde 100644 --- a/latan/CompiledFunction.hpp +++ b/latan/CompiledFunction.hpp @@ -45,10 +45,13 @@ public: // access void setCode(const std::string &code); // function call - virtual double evaluate(const std::vector &arg) const; + using DoubleFunction::operator(); // IO friend std::ostream & operator<<(std::ostream &out, CompiledDoubleFunction &f); +private: + // function call + virtual double operator()(const double *arg) const; private: std::shared_ptr interpreter_; std::shared_ptr context_; diff --git a/latan/Function.cpp b/latan/Function.cpp index 4b701e5..737311e 100644 --- a/latan/Function.cpp +++ b/latan/Function.cpp @@ -61,25 +61,55 @@ unsigned int Function::getNArg(void) const // constructor ///////////////////////////////////////////////////////////////// DoubleFunction::DoubleFunction(const unsigned nArg, vecFunc f) : Function(nArg) -, buffer_(new vector(nArg)) +, buffer_(new DVec(nArg)) , f_(f) {} // function call /////////////////////////////////////////////////////////////// -double DoubleFunction::evaluate(const std::vector &arg) const +double DoubleFunction::operator()(const double *arg) const { return f_(arg); } +double DoubleFunction::operator()(const DVec &arg) const +{ + if (arg.size() != getNArg()) + { + LATAN_ERROR(Size, "function argument vector has a wrong size (expected " + + strFrom(getNArg()) + ", got " + strFrom(arg.size()) + + ")"); + } + + return (*this)(arg.data()); +} + +double DoubleFunction::operator()(const std::vector &arg) const +{ + if (arg.size() != getNArg()) + { + LATAN_ERROR(Size, "function argument vector has a wrong size (expected " + + strFrom(getNArg()) + ", got " + strFrom(arg.size()) + + ")"); + } + + return (*this)(arg.data()); +} + double DoubleFunction::operator()(std::stack &arg) const { for (unsigned int i = 0; i < getNArg(); ++i) { - (*buffer_)[getNArg() - i - 1] = arg.top(); + if (arg.empty()) + { + LATAN_ERROR(Size, "function argument stack has a wrong size (expected " + + strFrom(getNArg()) + ", got " + strFrom(i) + + ")"); + } + (*buffer_)(getNArg() - i - 1) = arg.top(); arg.pop(); } - return this->evaluate(*buffer_); + return (*this)(*buffer_); } double DoubleFunction::operator()(const double x0, ...) const @@ -92,10 +122,10 @@ double DoubleFunction::operator()(const double x0, ...) const va_start(va, x0); for (unsigned int i = 1; i < getNArg(); ++i) { - (*buffer_)[i] = va_arg(va, double); + (*buffer_)(i) = va_arg(va, double); } va_end(va); } - return this->evaluate(*buffer_); + return (*this)(*buffer_); } diff --git a/latan/Function.hpp b/latan/Function.hpp index 5d07449..043801b 100644 --- a/latan/Function.hpp +++ b/latan/Function.hpp @@ -25,7 +25,6 @@ #include #include #include -#include BEGIN_NAMESPACE @@ -51,19 +50,23 @@ private: class DoubleFunction: public Function { private: - typedef std::function &)> vecFunc; + typedef std::function vecFunc; public: // constructor explicit DoubleFunction(const unsigned nArg, vecFunc f = nullptr); // destructor virtual ~DoubleFunction(void) = default; // function call - virtual double evaluate(const std::vector &arg) const; + double operator()(const DVec &arg) const; + double operator()(const std::vector &arg) const; double operator()(std::stack &arg) const; double operator()(const double x0, ...) const; +protected: + // function call + virtual double operator()(const double *arg) const; private: - std::shared_ptr> buffer_; - vecFunc f_; + std::shared_ptr buffer_; + vecFunc f_; }; END_NAMESPACE diff --git a/latan/Math.cpp b/latan/Math.cpp index 05e0831..3c65493 100644 --- a/latan/Math.cpp +++ b/latan/Math.cpp @@ -28,14 +28,14 @@ using namespace Latan; ******************************************************************************/ #define DEF_STD_FUNC_1ARG(name) \ -static double name##VecFunc(const vector &arg)\ +static double name##VecFunc(const double *arg)\ {\ return (name)(arg[0]);\ }\ DoubleFunction STDMATH_NAMESPACE::name(1, &name##VecFunc); #define DEF_STD_FUNC_2ARG(name) \ -static double name##VecFunc(const vector &arg)\ +static double name##VecFunc(const double *arg)\ {\ return (name)(arg[0], arg[1]);\ }\ diff --git a/latan/MinuitMinimizer.cpp b/latan/MinuitMinimizer.cpp index 7f9e09c..92d5634 100644 --- a/latan/MinuitMinimizer.cpp +++ b/latan/MinuitMinimizer.cpp @@ -47,7 +47,7 @@ MinuitMinimizer::MinuitFunction::MinuitFunction(const DoubleFunction &f) double MinuitMinimizer::MinuitFunction::operator() (const vector &x) const { - return f_->evaluate(x); + return (*f_)(x); } double MinuitMinimizer::MinuitFunction::Up(void) const