00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_FIT_BASE_H 00024 #define O2SCL_FIT_BASE_H 00025 00026 #include <string> 00027 #include <o2scl/collection.h> 00028 #include <o2scl/omatrix_tlate.h> 00029 #include <o2scl/text_file.h> 00030 00031 #ifndef DOXYGENP 00032 namespace o2scl { 00033 #endif 00034 00035 /** 00036 \brief Fitting function [abstract base] 00037 */ 00038 template<class param_t, class vec_t=ovector_base> class fit_funct { 00039 public: 00040 00041 fit_funct() {} 00042 virtual ~fit_funct() {} 00043 00044 /** \brief Using parameters in \c p, predict \c y given \c x 00045 */ 00046 virtual int operator()(size_t np, vec_t &p, double x, double &y, 00047 param_t &pa)=0; 00048 00049 #ifndef DOXYGEN_INTERNAL 00050 00051 private: 00052 00053 fit_funct(const fit_funct &); 00054 fit_funct& operator=(const fit_funct&); 00055 00056 #endif 00057 00058 }; 00059 00060 /** 00061 \brief Function pointer fitting function 00062 */ 00063 template<class param_t, class vec_t=ovector_base> class fit_funct_fptr : 00064 public fit_funct<param_t,vec_t> { 00065 00066 public: 00067 00068 /** \brief Specify a fitting function by a function pointer 00069 */ 00070 fit_funct_fptr(int (*fp)(size_t np, vec_t &p, double x, 00071 double &y, param_t &pa)) { 00072 fptr=fp; 00073 } 00074 00075 virtual ~fit_funct_fptr() {} 00076 00077 /** \brief Using parameters in \c p, predict \c y given \c x 00078 */ 00079 virtual int operator()(size_t np, vec_t &p, double x, double &y, 00080 param_t &pa) { 00081 return fptr(np,p,x,y,pa); 00082 } 00083 00084 #ifndef DOXYGEN_INTERNAL 00085 00086 protected: 00087 00088 fit_funct_fptr() {}; 00089 00090 /// Storage for the user-specified function pointer 00091 int (*fptr)(size_t np, vec_t &p, double x, double &y, param_t &pa); 00092 00093 fit_funct_fptr(const fit_funct_fptr &); 00094 fit_funct_fptr& operator=(const fit_funct_fptr&); 00095 00096 #endif 00097 00098 }; 00099 00100 /** 00101 \brief Member function pointer fitting function 00102 */ 00103 template <class tclass, class param_t, class vec_t=ovector_base> 00104 class fit_funct_mfptr : public fit_funct<param_t,vec_t> { 00105 public: 00106 00107 /** \brief Specify the member function pointer 00108 */ 00109 fit_funct_mfptr(tclass *tp, 00110 int (tclass::*fp)(size_t np, vec_t &p, double x, 00111 double &y, param_t &pa)) { 00112 tptr=tp; 00113 fptr=fp; 00114 } 00115 00116 virtual ~fit_funct_mfptr() {}; 00117 00118 /** \brief Using parameters in \c p, predict \c y given \c x 00119 */ 00120 virtual int operator()(size_t np, vec_t &p, double x, double &y, 00121 param_t &pa) { 00122 return (*tptr.*fptr)(np,p,x,y,pa); 00123 } 00124 00125 #ifndef DOXYGEN_INTERNAL 00126 00127 protected: 00128 00129 /// Storage for the user-specified function pointer 00130 int (tclass::*fptr)(size_t np, vec_t &p, double x, double &y, param_t &pa); 00131 00132 /// Storage for the class pointer 00133 tclass *tptr; 00134 00135 private: 00136 00137 fit_funct_mfptr(const fit_funct_mfptr &); 00138 fit_funct_mfptr& operator=(const fit_funct_mfptr&); 00139 00140 #endif 00141 00142 }; 00143 00144 /** 00145 \brief Const member function pointer fitting function 00146 */ 00147 template <class tclass, class param_t, class vec_t=ovector_base> 00148 class fit_funct_cmfptr : public fit_funct<param_t,vec_t> { 00149 public: 00150 00151 /** \brief Specify the member function pointer 00152 */ 00153 fit_funct_cmfptr(tclass *tp, 00154 int (tclass::*fp)(size_t np, vec_t &p, double x, 00155 double &y, param_t &pa) const) { 00156 tptr=tp; 00157 fptr=fp; 00158 } 00159 00160 virtual ~fit_funct_cmfptr() {}; 00161 00162 /** \brief Using parameters in \c p, predict \c y given \c x 00163 */ 00164 virtual int operator()(size_t np, vec_t &p, double x, double &y, 00165 param_t &pa) { 00166 return (*tptr.*fptr)(np,p,x,y,pa); 00167 } 00168 00169 #ifndef DOXYGEN_INTERNAL 00170 00171 protected: 00172 00173 /// Storage for the user-specified function pointer 00174 int (tclass::*fptr)(size_t np, vec_t &p, double x, double &y, 00175 param_t &pa) const; 00176 00177 /// Storage for the class pointer 00178 tclass *tptr; 00179 00180 private: 00181 00182 fit_funct_cmfptr(const fit_funct_cmfptr &); 00183 fit_funct_cmfptr& operator=(const fit_funct_cmfptr&); 00184 00185 #endif 00186 00187 }; 00188 00189 // ---------------------------------------------------------------- 00190 // Array versions 00191 // ---------------------------------------------------------------- 00192 00193 /** 00194 \brief Fitting function with arrays [abstract base] 00195 */ 00196 template<class param_t, size_t nvar> class fit_vfunct { 00197 public: 00198 00199 fit_vfunct() {} 00200 virtual ~fit_vfunct() {} 00201 00202 /** \brief Using parameters in \c p, predict \c y given \c x 00203 */ 00204 virtual int operator()(size_t np, double p[], double x, double &y, 00205 param_t &pa)=0; 00206 00207 #ifndef DOXYGEN_INTERNAL 00208 00209 private: 00210 00211 fit_vfunct(const fit_vfunct &); 00212 fit_vfunct& operator=(const fit_vfunct&); 00213 00214 #endif 00215 }; 00216 00217 /** 00218 \brief Function pointer fitting function with arrays 00219 */ 00220 template<class param_t, size_t nvar> class fit_vfunct_fptr : 00221 public fit_vfunct<param_t,nvar> { 00222 00223 public: 00224 00225 /** \brief Specify a fitting function by a function pointer 00226 */ 00227 fit_vfunct_fptr(int (*fp)(size_t np, double p[], double x, 00228 double &y, param_t &pa)) { 00229 fptr=fp; 00230 } 00231 00232 virtual ~fit_vfunct_fptr() {} 00233 00234 /** \brief Using parameters in \c p, predict \c y given \c x 00235 */ 00236 virtual int operator()(size_t np, double p[], double x, double &y, 00237 param_t &pa) { 00238 return fptr(np,p,x,y,pa); 00239 } 00240 00241 #ifndef DOXYGEN_INTERNAL 00242 00243 protected: 00244 00245 fit_vfunct_fptr() {}; 00246 00247 /// Storage for the user-specified function pointer 00248 int (*fptr)(size_t np, double p[], double x, double &y, param_t &pa); 00249 00250 private: 00251 00252 fit_vfunct_fptr(const fit_vfunct_fptr &); 00253 fit_vfunct_fptr& operator=(const fit_vfunct_fptr&); 00254 00255 #endif 00256 00257 }; 00258 00259 /** 00260 \brief Member function pointer fitting function with arrays 00261 */ 00262 template <class tclass, class param_t, size_t nvar> 00263 class fit_vfunct_mfptr : public fit_vfunct<param_t,nvar> { 00264 public: 00265 00266 /** \brief Specify the member function pointer 00267 */ 00268 fit_vfunct_mfptr(tclass *tp, 00269 int (tclass::*fp)(size_t np, double p[], double x, 00270 double &y, param_t &pa)) { 00271 tptr=tp; 00272 fptr=fp; 00273 } 00274 00275 virtual ~fit_vfunct_mfptr() {}; 00276 00277 /** \brief Using parameters in \c p, predict \c y given \c x 00278 */ 00279 virtual int operator()(size_t np, double p[], double x, double &y, 00280 param_t &pa) { 00281 return (*tptr.*fptr)(np,p,x,y,pa); 00282 } 00283 00284 #ifndef DOXYGEN_INTERNAL 00285 00286 protected: 00287 00288 /// Storage for the user-specified function pointer 00289 int (tclass::*fptr)(size_t np, double p[], double x, double &y, 00290 param_t &pa); 00291 00292 /// Storage for the class pointer 00293 tclass *tptr; 00294 00295 private: 00296 00297 fit_vfunct_mfptr(const fit_vfunct_mfptr &); 00298 fit_vfunct_mfptr& operator=(const fit_vfunct_mfptr&); 00299 00300 #endif 00301 00302 }; 00303 00304 /** 00305 \brief Const member function pointer fitting function with arrays 00306 */ 00307 template <class tclass, class param_t, size_t nvar> 00308 class fit_vfunct_cmfptr : public fit_vfunct<param_t,nvar> { 00309 public: 00310 00311 /** \brief Specify the member function pointer 00312 */ 00313 fit_vfunct_cmfptr(tclass *tp, 00314 int (tclass::*fp)(size_t np, double p[], double x, 00315 double &y, param_t &pa) const) { 00316 tptr=tp; 00317 fptr=fp; 00318 } 00319 00320 virtual ~fit_vfunct_cmfptr() {}; 00321 00322 /** \brief Using parameters in \c p, predict \c y given \c x 00323 */ 00324 virtual int operator()(size_t np, double p[], double x, double &y, 00325 param_t &pa) { 00326 return (*tptr.*fptr)(np,p,x,y,pa); 00327 } 00328 00329 #ifndef DOXYGEN_INTERNAL 00330 00331 protected: 00332 00333 /// Storage for the user-specified function pointer 00334 int (tclass::*fptr)(size_t np, double p[], double x, double &y, 00335 param_t &pa) const; 00336 00337 /// Storage for the class pointer 00338 tclass *tptr; 00339 00340 private: 00341 00342 fit_vfunct_cmfptr(const fit_vfunct_cmfptr &); 00343 fit_vfunct_cmfptr& operator=(const fit_vfunct_cmfptr&); 00344 00345 #endif 00346 00347 }; 00348 00349 // ---------------------------------------------------------------- 00350 // Fitter base 00351 // ---------------------------------------------------------------- 00352 00353 /** 00354 \brief Non-linear least-squares fitting [abstract base] 00355 */ 00356 template<class param_t, class func_t, 00357 class vec_t=ovector_base, class mat_t=omatrix_base> class fit_base { 00358 public: 00359 00360 fit_base() { 00361 verbose=0; 00362 } 00363 00364 virtual ~fit_base() {} 00365 00366 /** 00367 \brief Print out iteration information. 00368 00369 Depending on the value of the variable verbose, this prints out 00370 the iteration information. If verbose=0, then no information is 00371 printed, while if verbose>1, then after each iteration, the 00372 present values of x and y are output to std::cout along with the 00373 iteration number. If verbose>=2 then each iteration waits for a 00374 character. 00375 */ 00376 virtual int print_iter(size_t nv, vec_t &x, double y, int iter, 00377 double value=0.0, double limit=0.0) { 00378 if (verbose<=0) return 0; 00379 00380 size_t i; 00381 char ch; 00382 00383 std::cout << "Iteration: " << iter << std::endl; 00384 text_out_file outs(&std::cout,79); 00385 outs.word_out("x:"); 00386 for(i=0;i<nv;i++) outs.double_out(x[i]); 00387 outs.end_line(); 00388 std::cout << "y: " << y << " Val: " << value << " Lim: " << limit 00389 << std::endl; 00390 if (verbose>1) { 00391 std::cout << "Press a key and type enter to continue. "; 00392 std::cin >> ch; 00393 } 00394 00395 return 0; 00396 } 00397 00398 /** \brief Fit the data specified in (xdat,ydat) to 00399 the function \c fitfun with the parameters in \c par. 00400 00401 The covariance matrix for the parameters is returned in \c covar 00402 and the value of \f$ \chi^2 \f$ is returned in \c chi2. 00403 00404 */ 00405 virtual int fit(size_t ndat, vec_t &xdat, vec_t &ydat, vec_t &yerr, 00406 size_t npar, vec_t &par, mat_t &covar, double &chi2, 00407 param_t &pa, func_t &fitfun)=0; 00408 00409 /** \brief An integer describing the verbosity of the output 00410 */ 00411 int verbose; 00412 00413 /// Return string denoting type ("fit_base") 00414 virtual const char *type() { return "fit_base"; } 00415 00416 /// The number of data points 00417 size_t n_dat; 00418 00419 /// The number of parameters 00420 size_t n_par; 00421 00422 00423 }; 00424 00425 #ifndef DOXYGENP 00426 } 00427 #endif 00428 00429 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page