20#include "../../kernel.hpp"
94 *(iter->second) = gsl_vector_get(params, i);
98 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
100 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
104 gsl_vector_set(fvals, n , 0.0);
115 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
117 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
121 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
125 gsl_vector_set(fvals, n , 0.0);
130 gsl_vector_set(fvals, m+n*(_fData->
vy.size()), (
_fitParser->
Eval().real() - _fData->
vz[n][m])/_fData->
vz_w[n][m]);
156 const double dEps = _fData->
dPrecision*1.0e-1;
162 *(iter->second) = gsl_vector_get(params, i);
166 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
168 vFuncRes =
FitMatrix(_fData->
vx.size(),vector<double>(1,0.0));
169 vDiffRes =
FitMatrix(_fData->
vx.size(),vector<double>(1,0.0));
171 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
177 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
179 vFuncRes =
FitMatrix(_fData->
vz.size(),vector<double>(_fData->
vz[0].size(),0.0));
180 vDiffRes =
FitMatrix(_fData->
vz.size(),vector<double>(_fData->
vz[0].size(),0.0));
182 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
186 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
199 *(iter->second) = gsl_vector_get(params, i) + dEps;
201 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
203 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
209 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
213 gsl_matrix_set(Jac, n, i, 0.0);
217 gsl_matrix_set(Jac, n, i, (vDiffRes[n][0]-vFuncRes[n][0])/(dEps*_fData->
vy_w[n]));
220 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
222 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
226 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
233 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
235 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
239 gsl_matrix_set(Jac, m+n*(_fData->
vy.size()), i, 0.0);
243 gsl_matrix_set(Jac, m+n*(_fData->
vy.size()), i, (vDiffRes[n][m]-vFuncRes[n][m])/(dEps*_fData->
vz_w[n][m]));
248 *(iter->second) = gsl_vector_get(params, i);
252 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
254 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
299 *(iter->second) = gsl_vector_get(params, i);
303 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
305 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
309 gsl_vector_set(fvals, n , 0.0);
321 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
323 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
327 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
331 gsl_vector_set(fvals, n , 0.0);
363 const double dEps = _fData->
dPrecision*1.0e-1;
371 *(iter->second) = gsl_vector_get(params, i);
375 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
377 vFuncRes =
FitMatrix(_fData->
vx.size(),vector<double>(1,0.0));
378 vDiffRes =
FitMatrix(_fData->
vx.size(),vector<double>(1,0.0));
380 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
387 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
389 vFuncRes =
FitMatrix(_fData->
vz.size(),vector<double>(_fData->
vz[0].size(),0.0));
390 vDiffRes =
FitMatrix(_fData->
vz.size(),vector<double>(_fData->
vz[0].size(),0.0));
392 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
396 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
410 *(iter->second) = gsl_vector_get(params, i) + dEps;
412 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
414 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
421 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
425 gsl_matrix_set(Jac, n, i, 0.0);
429 gsl_matrix_set(Jac, n, i, (vDiffRes[n][0]-vFuncRes[n][0])/(dEps*_fData->
vy_w[n]));
432 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
434 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
438 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
446 for (
unsigned int n = 0; n < _fData->
vx.size(); n++)
448 for (
unsigned int m = 0; m < _fData->
vy.size(); m++)
452 gsl_matrix_set(Jac, m+n*(_fData->
vy.size()), i, 0.0);
456 gsl_matrix_set(Jac, m+n*(_fData->
vy.size()), i, (vDiffRes[n][m]-vFuncRes[n][m])/(dEps*_fData->
vz_w[n][m]));
461 *(iter->second) = gsl_vector_get(params, i);
465 if (_fData->
vx.size() && _fData->
vy.size() && !_fData->
vz.size())
467 else if (_fData->
vx.size() && _fData->
vy.size() && _fData->
vz.size())
508 for (
int i = 1; i < nVals; i++)
534 for (
unsigned int i = 0; i < nSize; i++)
536 if (!
isnan(gsl_vector_get(fvals, i)) && !
isinf(gsl_vector_get(fvals, i)))
539 dMax = fabs(gsl_vector_get(fvals, i));
540 else if (dMax < fabs(gsl_vector_get(fvals, i)))
541 dMax = fabs(gsl_vector_get(fvals, i));
550 for (
unsigned int i = 0; i < nSize; i++)
552 if (
isnan(gsl_vector_get(fvals, i)) ||
isinf(gsl_vector_get(fvals, i)))
553 gsl_vector_set(fvals, i, dMax);
572 for (
unsigned int i = 0; i < nLines; i++)
574 for (
unsigned int j = 0; j < nCols; j++)
576 if (!
isnan(gsl_matrix_get(Jac, i, j)) && !
isinf(gsl_matrix_get(Jac, i, j)))
579 dMax = fabs(gsl_matrix_get(Jac, i, j));
580 else if (dMax < fabs(gsl_matrix_get(Jac, i, j)))
581 dMax = fabs(gsl_matrix_get(Jac, i, j));
591 for (
unsigned int i = 0; i < nLines; i++)
593 for (
unsigned int j = 0; j < nCols; j++)
595 if (
isnan(gsl_matrix_get(Jac, i, j)) ||
isinf(gsl_matrix_get(Jac, i, j)))
596 gsl_matrix_set(Jac, i, j, dMax);
623 unsigned int nPoints = (_fData.
vz.size()) ? _fData.
vx.size()*_fData.
vy.size() : _fData.
vx.size();
626 if (_fData.
vz.size() && _fData.
vx.size() && _fData.
vy.size())
628 if (_fData.
vz.size() != _fData.
vx.size()
629 || _fData.
vz[0].size() != _fData.
vy.size()
630 || _fData.
vz.size() != _fData.
vz_w.size()
631 || _fData.
vz[0].size() != _fData.
vz_w[0].size())
634 else if (_fData.
vx.size() && _fData.
vy.size())
636 if (_fData.
vx.size() != _fData.
vy.size() || _fData.
vy.size() != _fData.
vy_w.size())
643 if (!
isnan(__dPrecision) && !
isinf(__dPrecision))
648 if (nMaxIterations <= 1)
649 nMaxIterations = 500;
651 if (__sRestrictions.length())
660 if (_fData.
vy_w.size())
662 for (
size_t i = 0; i < _fData.
vy_w.size(); i++)
663 _fData.
vy_w[i] += 1.0;
666 if (_fData.
vz_w.size())
668 for (
size_t i = 0; i < _fData.
vz_w.size(); i++)
670 for (
size_t j = 0; j < _fData.
vz_w[i].size(); j++)
671 _fData.
vz_w[i][j] += 1.0;
677 gsl_vector* params = gsl_vector_alloc(
mParams.size());
683 gsl_vector_set(params, n, (*iter->second).real());
688 const gsl_multifit_fdfsolver_type* solver_type = gsl_multifit_fdfsolver_lmsder;
689 gsl_multifit_fdfsolver* solver = gsl_multifit_fdfsolver_alloc(solver_type, nPoints,
mParams.size());
690 gsl_multifit_function_fdf
func;
694 if (__sRestrictions.length())
709 func.params = &_fData;
711 gsl_multifit_fdfsolver_set(solver, &
func, params);
717 if (gsl_multifit_fdfsolver_iterate(solver))
722 for (
size_t j = 0; j <
mParams.size(); j++)
724 if (!gsl_vector_get(params, j))
725 gsl_vector_set(params, j, 1.0);
729 gsl_multifit_fdfsolver_free(solver);
730 solver = gsl_multifit_fdfsolver_alloc(solver_type, nPoints,
mParams.size());
731 gsl_multifit_fdfsolver_set(solver, &
func, params);
733 nStatus = GSL_CONTINUE;
739 nStatus = GSL_CONTINUE;
754 gsl_multifit_fdfsolver_free(solver);
758 while (nStatus == GSL_CONTINUE &&
nIterations < nMaxIterations);
761 gsl_matrix* mCovar = gsl_matrix_alloc(
mParams.size(),
mParams.size());
762 gsl_multifit_covar(solver->J, 0.0, mCovar);
765 for (
size_t i = 0; i <
mParams.size(); i++)
767 for (
size_t j = 0; j <
mParams.size(); j++)
771 gsl_matrix_free(mCovar);
777 *(iter->second) = gsl_vector_get(solver->x, n);
782 dChiSqr = gsl_blas_dnrm2(solver->f);
786 gsl_multifit_fdfsolver_free(solver);
787 gsl_vector_free(params);
790 if (__sRestrictions.length())
825 return fitctrl(__sExpr, __sRestrictions, _fData, __dPrecision, nMaxIterations);
853 return fitctrl(__sExpr, __sRestrictions, _fData, __dPrecision, nMaxIterations);
874 for (
unsigned int i = 0; i <
sExpr.length(); i++)
876 if (
sExpr.substr(i, (iter->first).length()) == iter->first)
881 sExpr.replace(i, (iter->first).length(),
toString(*(iter->second), 5));
882 i +=
toString(*(iter->second), 5).length();
888 for (
size_t i = 0; i <
sExpr.length()-1; i++)
890 if (
sExpr.find_first_not_of(
' ', i+1) == string::npos)
893 if (
sExpr[i] ==
'+' &&
sExpr[
sExpr.find_first_not_of(
' ', i+1)] ==
'-')
896 if (
sExpr[i] ==
'-' &&
sExpr[
sExpr.find_first_not_of(
' ', i+1)] ==
'+')
897 sExpr.erase(
sExpr.find_first_not_of(
' ', i+1), 1);
899 if (
sExpr[i] ==
'-' &&
sExpr[
sExpr.find_first_not_of(
' ', i+1)] ==
'-')
902 sExpr.erase(
sExpr.find_first_not_of(
' ', i+1), 1);
This class contains the internal fit logic and the interface to the GSL fitting module.
static mu::value_type * xvar
bool fit(FitVector &vx, FitVector &vy, const std::string &__sExpr, const std::string &__sRestrictions, mu::varmap_type &mParamsMap, double __dPrecision=1e-4, int nMaxIterations=500)
Calculate an unweighted 1D-fit.
static int fitfunctionrestricted(const gsl_vector *params, void *data, gsl_vector *fvals)
Fit function respecting additional restrictions.
static mu::value_type * yvar
static mu::value_type * zvar
static mu::Parser * _fitParser
static double evalRestrictions(const mu::value_type *v, int nVals)
Evaluate additional restrictions.
static mu::varmap_type mParams
FitMatrix vCovarianceMatrix
FitMatrix getCovarianceMatrix() const
Returns the covariance matrix of the performed fit.
static void removeNANVals(gsl_vector *fvals, unsigned int nSize)
Replace NaNs with some very large value to avoid converging towards NaNs.
static int fitfuncjac(const gsl_vector *params, void *data, gsl_vector *fvals, gsl_matrix *Jac)
Combination of fit function and the corresponding jacobian.
static int fitjacobian(const gsl_vector *params, void *data, gsl_matrix *Jac)
Create the jacobian matrix without using restrictions.
static int fitfunction(const gsl_vector *params, void *data, gsl_vector *fvals)
Fit function for the usual case without any restrictions.
static int fitjacobianrestricted(const gsl_vector *params, void *data, gsl_matrix *Jac)
Jacobian matrix respecting additional restrictions.
~Fitcontroller()
Destructor.
bool fitctrl(const std::string &__sExpr, const std::string &__sRestrictions, FitData &_fData, double __dPrecision, int nMaxIterations)
This is the central fitting function using the data passed from the interface functions.
Fitcontroller()
Default constructor.
static int fitfuncjacrestricted(const gsl_vector *params, void *data, gsl_vector *fvals, gsl_matrix *Jac)
Combination of fit function and the corresponding jacobian respecting additional restrictions.
std::string getFitFunction()
Returns the fit function, where each parameter is replaced with its value converted to a string.
static bool GetAsyncCancelState()
This function is used by the kernel to get informed, when the user pressed ESC or used other means of...
Common exception class for all exceptions thrown in NumeRe.
@ PROCESS_ABORTED_BY_USER
static size_t invalid_position
void SetExpr(StringView a_sExpr)
Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of...
value_type Eval()
Single-value wrapper around the vectorized overload of this member function.
const varmap_type & GetVar() const
Return a map containing the used variables only.
Mathematical expressions parser.
std::vector< std::vector< double > > FitMatrix
Typedef for readability.
std::vector< double > FitVector
Typedef for readability.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
bool isnan(const value_type &v)
std::vector< double > real(const std::vector< value_type > &vVec)
bool isinf(const value_type &v)
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Resample_Real(* func)(Resample_Real t)
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
Defines the datapoints, which shall be fitted.
std::string toString(int)
Converts an integer to a string without the Settings bloat.