2014-03-03 12:41:48 +00:00
|
|
|
/*
|
|
|
|
* Model.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2020-01-13 09:57:06 +00:00
|
|
|
* Copyright (C) 2013 - 2020 Antonin Portelli
|
2014-03-03 12:41:48 +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/Model.hpp>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/includes.hpp>
|
2014-03-03 12:41:48 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2014-03-03 17:01:07 +00:00
|
|
|
using namespace std::placeholders;
|
2014-03-03 12:41:48 +00:00
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Model implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleModel::DoubleModel(const vecFunc &f, const Index nArg, const Index nPar)
|
2014-03-03 12:41:48 +00:00
|
|
|
: size_(new ModelSize)
|
2016-03-31 12:12:30 +01:00
|
|
|
, varName_("x")
|
|
|
|
, parName_("p")
|
2014-03-03 12:41:48 +00:00
|
|
|
{
|
2015-02-24 17:00:19 +00:00
|
|
|
setFunction(f, nArg, nPar);
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
Index DoubleModel::getNArg(void) const
|
|
|
|
{
|
|
|
|
return size_->nArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Index DoubleModel::getNPar(void) const
|
|
|
|
{
|
|
|
|
return size_->nPar;
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:00:19 +00:00
|
|
|
void DoubleModel::setFunction(const vecFunc &f, const Index nArg,
|
|
|
|
const Index nPar)
|
2014-03-03 12:41:48 +00:00
|
|
|
{
|
|
|
|
size_->nArg = nArg;
|
|
|
|
size_->nPar = nPar;
|
|
|
|
f_ = f;
|
|
|
|
}
|
|
|
|
|
2016-03-31 12:12:30 +01:00
|
|
|
VarName & DoubleModel::varName(void)
|
|
|
|
{
|
|
|
|
return varName_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VarName & DoubleModel::varName(void) const
|
|
|
|
{
|
|
|
|
return varName_;
|
|
|
|
}
|
|
|
|
|
|
|
|
VarName & DoubleModel::parName(void)
|
|
|
|
{
|
|
|
|
return parName_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VarName & DoubleModel::parName(void) const
|
|
|
|
{
|
|
|
|
return parName_;
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:41:48 +00:00
|
|
|
// error checking //////////////////////////////////////////////////////////////
|
|
|
|
void DoubleModel::checkSize(const Index nArg, const Index nPar) const
|
|
|
|
{
|
|
|
|
if (nArg != getNArg())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "model argument vector has a wrong size (expected "
|
|
|
|
+ strFrom(getNArg()) + ", got " + strFrom(nArg)
|
|
|
|
+ ")");
|
|
|
|
}
|
|
|
|
if (nPar != getNPar())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Size, "model parameter vector has a wrong size (expected "
|
|
|
|
+ strFrom(getNPar()) + ", got " + strFrom(nPar)
|
|
|
|
+ ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function call ///////////////////////////////////////////////////////////////
|
|
|
|
double DoubleModel::operator()(const DVec &arg, const DVec &par) const
|
|
|
|
{
|
|
|
|
checkSize(arg.size(), par.size());
|
|
|
|
|
2014-03-13 18:20:40 +00:00
|
|
|
return (*this)(arg.data(), par.data());
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double DoubleModel::operator()(const vector<double> &arg,
|
|
|
|
const vector<double> &par) const
|
|
|
|
{
|
|
|
|
checkSize(static_cast<Index>(arg.size()), static_cast<Index>(par.size()));
|
|
|
|
|
2014-03-13 18:20:40 +00:00
|
|
|
return (*this)(arg.data(), par.data());
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double DoubleModel::operator()(const double *data, const double *par) const
|
|
|
|
{
|
2014-03-17 14:59:39 +00:00
|
|
|
return f_(data, par);
|
2014-03-03 12:41:48 +00:00
|
|
|
}
|
2014-03-03 17:01:07 +00:00
|
|
|
|
|
|
|
// model bind //////////////////////////////////////////////////////////////////
|
2015-01-28 17:19:57 +00:00
|
|
|
DoubleFunction DoubleModel::fixArg(const DVec &arg) const
|
2014-03-03 17:01:07 +00:00
|
|
|
{
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleModel copy(*this);
|
|
|
|
|
|
|
|
auto modelWithVec = [copy](const DVec &x, const double *p)
|
|
|
|
{
|
|
|
|
return copy(x.data(), p);
|
|
|
|
};
|
2015-01-28 17:19:57 +00:00
|
|
|
auto modelBind = bind(modelWithVec, arg, _1);
|
|
|
|
|
2015-02-24 17:00:19 +00:00
|
|
|
return DoubleFunction(modelBind, getNPar());
|
2015-01-28 17:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DoubleFunction DoubleModel::fixPar(const DVec &par) const
|
|
|
|
{
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleModel copy(*this);
|
|
|
|
|
|
|
|
auto modelWithVec = [copy](const double *x, const DVec &p)
|
|
|
|
{
|
|
|
|
return copy(x, p.data());
|
|
|
|
};
|
2014-03-03 17:01:07 +00:00
|
|
|
auto modelBind = bind(modelWithVec, _1, par);
|
|
|
|
|
2015-02-24 17:00:19 +00:00
|
|
|
return DoubleFunction(modelBind, getNArg());
|
2014-03-03 17:01:07 +00:00
|
|
|
}
|
2015-01-28 17:19:57 +00:00
|
|
|
|
|
|
|
DoubleFunction DoubleModel::toFunction(void) const
|
|
|
|
{
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleModel copy(*this);
|
|
|
|
|
|
|
|
auto func = [copy](const double *x){return copy(x, x + copy.getNArg());};
|
2015-01-28 17:19:57 +00:00
|
|
|
|
2015-02-24 17:00:19 +00:00
|
|
|
return DoubleFunction(func, getNArg() + getNPar());
|
2015-01-28 17:19:57 +00:00
|
|
|
}
|