2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* Function.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2016-04-06 20:11:23 +01:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-02-06 18:52:13 +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/Function.hpp>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/includes.hpp>
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
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-20 22:54:11 +00:00
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleFunction::DoubleFunction(const vecFunc &f, const Index nArg)
|
2014-03-03 12:41:48 +00:00
|
|
|
: buffer_(new DVec)
|
2016-03-31 12:12:30 +01:00
|
|
|
, varName_("x")
|
2014-03-03 12:41:48 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-31 12:12:30 +01:00
|
|
|
VarName & DoubleFunction::varName(void)
|
|
|
|
{
|
|
|
|
return varName_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VarName & DoubleFunction::varName(void) const
|
|
|
|
{
|
|
|
|
return varName_;
|
|
|
|
}
|
|
|
|
|
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-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < getNArg(); ++i)
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-03-13 18:20:40 +00:00
|
|
|
if (arg.empty())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "function argument stack is empty (expected "
|
|
|
|
+ strFrom(getNArg()) + "arguments, got " + strFrom(i)
|
|
|
|
+ ")");
|
|
|
|
}
|
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-03-03 13:04:53 +00:00
|
|
|
double DoubleFunction::operator()(void) const
|
2014-01-30 19:28:30 +00:00
|
|
|
{
|
2014-03-03 13:04:53 +00:00
|
|
|
checkSize(0);
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-03-03 13:04:53 +00:00
|
|
|
return (*this)(nullptr);
|
2014-01-30 19:28:30 +00:00
|
|
|
}
|
2014-03-12 19:23:49 +00:00
|
|
|
|
2019-03-09 22:44:57 +00:00
|
|
|
std::map<double, double> DoubleFunction::operator()(const std::map<double, double> &m) const
|
|
|
|
{
|
|
|
|
checkSize(1);
|
|
|
|
|
|
|
|
std::map<double, double> res;
|
|
|
|
|
|
|
|
for (auto &val: m)
|
|
|
|
{
|
|
|
|
res[val.first] = (*this)(val.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:00:19 +00:00
|
|
|
// bind ////////////////////////////////////////////////////////////////////////
|
2015-07-07 18:49:22 +01:00
|
|
|
DoubleFunction DoubleFunction::bind(const Index argIndex,
|
|
|
|
const double val) const
|
2015-02-24 17:00:19 +00:00
|
|
|
{
|
|
|
|
Index nArg = getNArg();
|
|
|
|
shared_ptr<DVec> buf(new DVec(nArg));
|
|
|
|
DoubleFunction copy(*this), bindFunc;
|
|
|
|
|
|
|
|
auto func = [copy, buf, argIndex, val](const double *arg)
|
|
|
|
{
|
2015-02-26 18:50:56 +00:00
|
|
|
FOR_VEC(*buf, i)
|
|
|
|
{
|
|
|
|
if (i < argIndex)
|
|
|
|
{
|
|
|
|
(*buf)(i) = arg[i];
|
|
|
|
}
|
|
|
|
else if (i == argIndex)
|
|
|
|
{
|
|
|
|
(*buf)(i) = val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*buf)(i) = arg[i - 1];
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 17:00:19 +00:00
|
|
|
|
|
|
|
return copy(*buf);
|
|
|
|
};
|
|
|
|
|
|
|
|
bindFunc.setFunction(func, nArg - 1);
|
|
|
|
|
|
|
|
return bindFunc;
|
|
|
|
}
|
|
|
|
|
2015-07-07 18:49:22 +01:00
|
|
|
DoubleFunction DoubleFunction::bind(const Index argIndex,
|
|
|
|
const DVec &x) const
|
|
|
|
{
|
|
|
|
Index nArg = getNArg();
|
|
|
|
shared_ptr<DVec> buf(new DVec(nArg));
|
|
|
|
DoubleFunction copy(*this), bindFunc;
|
|
|
|
|
|
|
|
auto func = [copy, buf, argIndex, x](const double *arg)
|
|
|
|
{
|
|
|
|
*buf = x;
|
|
|
|
(*buf)(argIndex) = arg[0];
|
|
|
|
|
|
|
|
return copy(*buf);
|
|
|
|
};
|
|
|
|
|
|
|
|
bindFunc.setFunction(func, 1);
|
|
|
|
|
|
|
|
return bindFunc;
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:40:02 +00:00
|
|
|
// sample //////////////////////////////////////////////////////////////////////
|
2019-04-04 20:04:56 +01:00
|
|
|
DVec DoubleFunction::sample(const DMat &x) const
|
2019-03-21 17:40:02 +00:00
|
|
|
{
|
|
|
|
if (x.cols() != getNArg())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "sampling point matrix and number of arguments "
|
|
|
|
"mismatch (matrix has " + strFrom(x.cols())
|
|
|
|
+ ", number of arguments is " + strFrom(getNArg()) + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
DVec res(x.rows());
|
|
|
|
|
|
|
|
for (Index i = 0; i < res.size(); ++i)
|
|
|
|
{
|
|
|
|
res(i) = (*this)(x.row(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-03 12:32:29 +01:00
|
|
|
// arithmetic operators ////////////////////////////////////////////////////////
|
|
|
|
DoubleFunction DoubleFunction::operator-(void) const
|
|
|
|
{
|
2015-02-26 18:50:56 +00:00
|
|
|
DoubleFunction copy(*this), resFunc;
|
2014-06-03 12:32:29 +01:00
|
|
|
|
2015-02-26 18:50:56 +00:00
|
|
|
return DoubleFunction([copy](const double *arg){return -copy(arg);},
|
|
|
|
getNArg());
|
2014-06-03 12:32:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAKE_SELF_FUNC_OP(op)\
|
|
|
|
DoubleFunction & DoubleFunction::operator op##=(const DoubleFunction &f)\
|
|
|
|
{\
|
|
|
|
DoubleFunction copy(*this);\
|
|
|
|
checkSize(f.getNArg());\
|
2015-02-26 18:50:56 +00:00
|
|
|
auto res = [f, copy](const double *arg){return copy(arg) op f(arg);};\
|
2014-06-03 12:32:29 +01:00
|
|
|
setFunction(res, getNArg());\
|
|
|
|
return *this;\
|
|
|
|
}\
|
2016-04-20 03:21:05 +01:00
|
|
|
DoubleFunction & DoubleFunction::operator op##=(const DoubleFunction and f)\
|
2014-06-03 12:32:29 +01:00
|
|
|
{\
|
|
|
|
*this op##= f;\
|
|
|
|
return *this;\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAKE_SELF_SCALAR_OP(op)\
|
|
|
|
DoubleFunction & DoubleFunction::operator op##=(const double x)\
|
|
|
|
{\
|
|
|
|
DoubleFunction copy(*this);\
|
|
|
|
auto res = [x, copy](const double *arg){return copy(arg) op x;};\
|
|
|
|
setFunction(res, getNArg());\
|
|
|
|
return *this;\
|
|
|
|
}\
|
|
|
|
|
|
|
|
MAKE_SELF_FUNC_OP(+)
|
|
|
|
MAKE_SELF_FUNC_OP(-)
|
|
|
|
MAKE_SELF_FUNC_OP(*)
|
|
|
|
MAKE_SELF_FUNC_OP(/)
|
|
|
|
MAKE_SELF_SCALAR_OP(+)
|
|
|
|
MAKE_SELF_SCALAR_OP(-)
|
|
|
|
MAKE_SELF_SCALAR_OP(*)
|
|
|
|
MAKE_SELF_SCALAR_OP(/)
|
|
|
|
|
2014-03-12 19:23:49 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* DoubleFunctionSample implementation *
|
|
|
|
******************************************************************************/
|
2014-09-18 17:16:49 +01:00
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
|
|
|
DoubleFunctionSample::DoubleFunctionSample(void)
|
|
|
|
: Sample<DoubleFunction>()
|
|
|
|
{}
|
|
|
|
|
|
|
|
DoubleFunctionSample::DoubleFunctionSample(const Index nSample)
|
|
|
|
: Sample<DoubleFunction>(nSample)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// function call ///////////////////////////////////////////////////////////////
|
2014-03-12 19:23:49 +00:00
|
|
|
DSample DoubleFunctionSample::operator()(const DMatSample &arg) const
|
|
|
|
{
|
2014-04-18 11:28:51 +01:00
|
|
|
DSample result(size());
|
2014-03-12 19:23:49 +00:00
|
|
|
|
2014-04-18 11:28:51 +01:00
|
|
|
FOR_STAT_ARRAY((*this), s)
|
2014-03-12 19:23:49 +00:00
|
|
|
{
|
|
|
|
result[s] = (*this)[s](arg[s]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-18 11:28:51 +01:00
|
|
|
|
|
|
|
DSample DoubleFunctionSample::operator()(const double *arg) const
|
|
|
|
{
|
|
|
|
DSample result(size());
|
|
|
|
|
|
|
|
FOR_STAT_ARRAY((*this), s)
|
|
|
|
{
|
|
|
|
result[s] = (*this)[s](arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
DSample DoubleFunctionSample::operator()(const DVec &arg) const
|
|
|
|
{
|
|
|
|
return (*this)(arg.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
DSample DoubleFunctionSample::operator()(const vector<double> &arg) const
|
|
|
|
{
|
|
|
|
return (*this)(arg.data());
|
|
|
|
}
|
|
|
|
|
2015-07-07 18:49:22 +01:00
|
|
|
// bind ////////////////////////////////////////////////////////////////////////
|
|
|
|
DoubleFunctionSample DoubleFunctionSample::bind(const Index argIndex,
|
|
|
|
const double val) const
|
|
|
|
{
|
|
|
|
DoubleFunctionSample bindFunc(size());
|
|
|
|
|
|
|
|
FOR_STAT_ARRAY(bindFunc, s)
|
|
|
|
{
|
|
|
|
bindFunc[s] = (*this)[s].bind(argIndex, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
DoubleFunctionSample DoubleFunctionSample::bind(const Index argIndex,
|
|
|
|
const DVec &x) const
|
|
|
|
{
|
|
|
|
DoubleFunctionSample bindFunc(size());
|
|
|
|
|
|
|
|
FOR_STAT_ARRAY(bindFunc, s)
|
|
|
|
{
|
|
|
|
bindFunc[s] = (*this)[s].bind(argIndex, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindFunc;
|
|
|
|
}
|