23 #ifndef O2SCL_FIT_BASE_H 24 #define O2SCL_FIT_BASE_H 32 #include <o2scl/jacobian.h> 33 #include <o2scl/mm_funct.h> 35 #ifndef DOXYGEN_NO_O2NS 40 typedef std::function<
55 template<
class vec_
string_t=std::vector<std::
string> >
62 for (
int i=0;i<np;i++) {
78 template<
class vec_t=boost::numeric::ublas::vector<
double> >
81 for(
size_t i=0;i<np;i++) {
93 #ifndef DOXYGEN_INTERNAL 101 std::map<std::string,double>
vars;
116 std::string var,
int nauxp=0, std::string auxp=
"");
133 template<
class vec_t=boost::numeric::ublas::vector<
double>,
134 class mat_t=boost::numeric::ublas::matrix<
double> >
146 virtual void operator()(
size_t np,
const vec_t &p,
size_t nd,
152 virtual void jac(
size_t np, vec_t &p,
size_t nd, vec_t &f,
156 virtual size_t get_ndata()=0;
158 #ifndef DOXYGEN_INTERNAL 197 template<
class vec_t=boost::numeric::ublas::vector<
double>,
198 class mat_t=boost::numeric::ublas::matrix<
double>,
208 const vec_t &yerr, fit_func_t &fun) {
215 mfm=std::bind(std::mem_fn<
int(
size_t,
const vec_t &,vec_t &)>
217 std::placeholders::_1,std::placeholders::_2,
218 std::placeholders::_3);
220 auto_jac.set_function(mfm);
227 void set_data(
size_t ndat,
const vec_t &xdat,
const vec_t &ydat,
245 virtual double chi2(
size_t np,
const vec_t &p) {
247 for(
size_t i=0;i<ndat_;i++) {
248 double yi=((*fun_)(np,p,(*xdat_)[i])-(*ydat_)[i])/((*yerr_)[i]);
257 virtual void operator()(
size_t np,
const vec_t &p,
size_t nd, vec_t &f) {
259 for(
size_t i=0;i<nd;i++) {
260 double yi=(*fun_)(np,p,(*xdat_)[i]);
261 f[i]=(yi-(*ydat_)[i])/((*yerr_)[i]);
269 virtual void jac(
size_t np, vec_t &p,
size_t nd, vec_t &f,
272 auto_jac(np,p,nd,f,J);
286 #ifndef DOXYGEN_INTERNAL 297 std::function<int(size_t,const vec_t &,vec_t &)>
mfm;
358 virtual int print_iter(
size_t nv, vec_t &x,
double y,
int iter,
359 double value=0.0,
double limit=0.0) {
360 if (verbose<=0)
return 0;
365 std::cout <<
"Iteration: " << iter << std::endl;
367 for(i=0;i<nv;i++) std::cout << x[i] <<
" ";
368 std::cout << std::endl;
369 std::cout <<
"y: " << y <<
" Val: " << value <<
" Lim: " << limit
372 std::cout <<
"Press a key and type enter to continue. ";
386 virtual int fit(
size_t npar, vec_t &parms, mat_t &covar,
387 double &chi2, func_t &fitfun)=0;
394 virtual const char *
type() {
return "fit_base"; }
405 #ifndef DOXYGEN_NO_O2NS Generalized fitting function [abstract base].
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
calculator calc
The function parser.
virtual void jac(size_t np, vec_t &p, size_t nd, vec_t &f, mat_t &J)
Using parameters in p, compute the Jacobian in J.
void compile(const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
Compile expression expr using variables specified in vars.
void set_func(fit_func_t &fun)
Set the fitting function.
Standard fitting function based on one-dimensional data with a numerical Jacobian.
int set_aux_parm(std::string name, double val)
Set the values of the auxilliary parameters that were specified in auxp in the constructor.
std::string st_form
The expression.
virtual const char * type()
Return string denoting type ("fit_base")
std::vector< std::string > st_parms
The parameters.
size_t n_dat
The number of data points.
fit_funct11_strings(std::string expr, vec_string_t &parms, std::string var)
Specify a fitting function through a string.
void set_data(size_t ndat, const vec_t &xdat, const vec_t &ydat, const vec_t &yerr)
Set the data to be fit.
std::function< double(size_t, const boost::numeric::ublas::vector< double > &, double)> fit_funct11
Array of multi-dimensional functions typedef (C++11 version)
size_t ntrial
Maximum number of iterations (default 500)
int set_function(std::string expr, std::string parms, std::string var, int nauxp=0, std::string auxp="")
Specify the strings which define the fitting function.
fit_func_t * fun_
Fitting function.
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
std::function< int(size_t, const vec_t &, vec_t &)> mfm
Function object for Jacobian object.
double tol_rel
(default 1.0e-4)
virtual int print_iter(size_t nv, vec_t &x, double y, int iter, double value=0.0, double limit=0.0)
Print out iteration information.
int verbose
An integer describing the verbosity of the output.
Evaluate a mathematical expression in a string.
size_t n_par
The number of parameters.
virtual size_t get_ndata()
Return the number of data points.
double operator()(size_t np, const vec_t &p, double x)
Using parameters in p, predict y given x.
virtual void operator()(size_t np, const vec_t &p, size_t nd, vec_t &f)
Using parameters in p, compute the relative deviations in f.
std::map< std::string, double > vars
Desc.
Non-linear least-squares fitting [abstract base].
virtual double chi2(size_t np, const vec_t &p)
Return .
chi_fit_funct(size_t ndat, const vec_t &xdat, const vec_t &ydat, const vec_t &yerr, fit_func_t &fun)
Create an object with specified data and specified fitting function.
jacobian_gsl< std::function< int(size_t, const vec_t &, vec_t &)>, vec_t, mat_t > auto_jac
Automatic Jacobian object.
std::string st_var
The variable.
double tol_abs
Absolute tolerance (default 1.0e-4)
int jac_mm_funct(size_t np, const vec_t &p, vec_t &f)
Reformulate operator() into a mm_funct11 object.
Simple automatic Jacobian.