00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 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_view> 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_view> 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_view> 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 // Array versions 00146 // ---------------------------------------------------------------- 00147 00148 /** 00149 \brief Fitting function [abstract base] 00150 */ 00151 template<class param_t, size_t nvar> class fit_vfunct { 00152 public: 00153 00154 fit_vfunct() {} 00155 virtual ~fit_vfunct() {} 00156 00157 /** \brief Using parameters in \c p, predict \c y given \c x 00158 */ 00159 virtual int operator()(size_t np, double p[], double x, double &y, 00160 param_t &pa)=0; 00161 00162 #ifndef DOXYGEN_INTERNAL 00163 00164 private: 00165 00166 fit_vfunct(const fit_vfunct &); 00167 fit_vfunct& operator=(const fit_vfunct&); 00168 00169 #endif 00170 }; 00171 00172 /** 00173 \brief Function pointer fitting function 00174 */ 00175 template<class param_t, size_t nvar> class fit_vfunct_fptr : 00176 public fit_vfunct<param_t,nvar> { 00177 00178 public: 00179 00180 /** \brief Specify a fitting function by a function pointer 00181 */ 00182 fit_vfunct_fptr(int (*fp)(size_t np, double p[], double x, 00183 double &y, param_t &pa)) { 00184 fptr=fp; 00185 } 00186 00187 virtual ~fit_vfunct_fptr() {} 00188 00189 /** \brief Using parameters in \c p, predict \c y given \c x 00190 */ 00191 virtual int operator()(size_t np, double p[], double x, double &y, 00192 param_t &pa) { 00193 return fptr(np,p,x,y,pa); 00194 } 00195 00196 #ifndef DOXYGEN_INTERNAL 00197 00198 protected: 00199 00200 fit_vfunct_fptr() {}; 00201 00202 /// Storage for the user-specified function pointer 00203 int (*fptr)(size_t np, double p[], double x, double &y, param_t &pa); 00204 00205 private: 00206 00207 fit_vfunct_fptr(const fit_vfunct_fptr &); 00208 fit_vfunct_fptr& operator=(const fit_vfunct_fptr&); 00209 00210 #endif 00211 00212 }; 00213 00214 /** 00215 \brief Member function pointer fitting function 00216 */ 00217 template <class tclass, class param_t, size_t nvar> 00218 class fit_vfunct_mfptr : public fit_vfunct<param_t,nvar> { 00219 public: 00220 00221 /** \brief Specify the member function pointer 00222 */ 00223 fit_vfunct_mfptr(tclass *tp, 00224 int (tclass::*fp)(size_t np, double p[], double x, 00225 double &y, param_t &pa)) { 00226 tptr=tp; 00227 fptr=fp; 00228 } 00229 00230 virtual ~fit_vfunct_mfptr() {}; 00231 00232 /** \brief Using parameters in \c p, predict \c y given \c x 00233 */ 00234 virtual int operator()(size_t np, double p[], double x, double &y, 00235 param_t &pa) { 00236 return (*tptr.*fptr)(np,p,x,y,pa); 00237 } 00238 00239 #ifndef DOXYGEN_INTERNAL 00240 00241 protected: 00242 00243 /// Storage for the user-specified function pointer 00244 int (tclass::*fptr)(size_t np, double p[], double x, double &y, 00245 param_t &pa); 00246 00247 /// Storage for the class pointer 00248 tclass *tptr; 00249 00250 private: 00251 00252 fit_vfunct_mfptr(const fit_vfunct_mfptr &); 00253 fit_vfunct_mfptr& operator=(const fit_vfunct_mfptr&); 00254 00255 #endif 00256 00257 }; 00258 00259 // ---------------------------------------------------------------- 00260 // Fitter base 00261 // ---------------------------------------------------------------- 00262 00263 /** 00264 \brief Non-linear least-squares fitting [abstract base] 00265 00266 */ 00267 template<class param_t, class func_t, 00268 class vec_t=ovector_view, class mat_t=omatrix_view> class fit_base { 00269 public: 00270 00271 fit_base() { 00272 verbose=0; 00273 } 00274 00275 virtual ~fit_base() {} 00276 00277 /** 00278 \brief Print out iteration information. 00279 00280 Depending on the value of the variable verbose, this prints out 00281 the iteration information. If verbose=0, then no information is 00282 printed, while if verbose>1, then after each iteration, the 00283 present values of x and y are output to std::cout along with the 00284 iteration number. If verbose>=2 then each iteration waits for a 00285 character. 00286 */ 00287 virtual int print_iter(size_t nv, vec_t &x, double y, int iter, 00288 double value=0.0, double limit=0.0) { 00289 if (verbose<=0) return 0; 00290 00291 size_t i; 00292 char ch; 00293 00294 std::cout << "Iteration: " << iter << std::endl; 00295 text_out_file outs(&std::cout,79); 00296 outs.word_out("x:"); 00297 for(i=0;i<nv;i++) outs.double_out(x[i]); 00298 outs.end_line(); 00299 std::cout << "y: " << y << " Val: " << value << " Lim: " << limit 00300 << std::endl; 00301 if (verbose>1) { 00302 std::cout << "Press a key and type enter to continue. "; 00303 std::cin >> ch; 00304 } 00305 00306 return 0; 00307 } 00308 00309 /** \brief Fit the data specified in (xdat,ydat) to 00310 the function \c fitfun with the parameters in \c par. 00311 00312 The covariance matrix for the parameters is returned in \c covar 00313 and the value of \f$ \chi^2 \f$ is returned in \c chi2. 00314 00315 */ 00316 virtual int fit(size_t ndat, vec_t &xdat, vec_t &ydat, vec_t &yerr, 00317 size_t npar, vec_t &par, mat_t &covar, double &chi2, 00318 param_t &pa, func_t &fitfun)=0; 00319 00320 /** \brief An integer describing the verbosity of the output 00321 */ 00322 int verbose; 00323 00324 /// Return string denoting type ("fit_base") 00325 virtual const char *type() { return "fit_base"; } 00326 00327 /// The number of data points 00328 size_t n_dat; 00329 00330 /// The number of parameters 00331 size_t n_par; 00332 00333 00334 }; 00335 00336 #ifndef DOXYGENP 00337 } 00338 #endif 00339 00340 #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