![]() |
Object-oriented Scientific Computing Library: Version 0.910
|
00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006-2012, 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/omatrix_tlate.h> 00028 00029 #ifndef DOXYGENP 00030 namespace o2scl { 00031 #endif 00032 00033 /** \brief Fitting function [abstract base] 00034 */ 00035 template<class vec_t=ovector_base> class fit_funct { 00036 public: 00037 00038 fit_funct() {} 00039 virtual ~fit_funct() {} 00040 00041 /** \brief Using parameters in \c p, predict \c y given \c x 00042 */ 00043 virtual double operator()(size_t np, const vec_t &p, double x)=0; 00044 00045 #ifndef DOXYGEN_INTERNAL 00046 00047 private: 00048 00049 fit_funct(const fit_funct &); 00050 fit_funct& operator=(const fit_funct&); 00051 00052 #endif 00053 00054 }; 00055 00056 /** \brief Function pointer fitting function 00057 */ 00058 template<class vec_t=ovector_base> class fit_funct_fptr : 00059 public fit_funct<vec_t> { 00060 00061 public: 00062 00063 /** \brief Specify a fitting function by a function pointer 00064 */ 00065 fit_funct_fptr(double (*fp)(size_t np, const vec_t &p, double x)) { 00066 fptr=fp; 00067 } 00068 00069 virtual ~fit_funct_fptr() {} 00070 00071 /** \brief Using parameters in \c p, predict \c y given \c x 00072 */ 00073 virtual double operator()(size_t np, const vec_t &p, double x) { 00074 return fptr(np,p,x); 00075 } 00076 00077 #ifndef DOXYGEN_INTERNAL 00078 00079 protected: 00080 00081 fit_funct_fptr() {}; 00082 00083 /// Storage for the user-specified function pointer 00084 double (*fptr)(size_t np, const vec_t &p, double x); 00085 00086 fit_funct_fptr(const fit_funct_fptr &); 00087 fit_funct_fptr& operator=(const fit_funct_fptr&); 00088 00089 #endif 00090 00091 }; 00092 00093 /** \brief Member function pointer fitting function 00094 */ 00095 template <class tclass, class vec_t=ovector_base> 00096 class fit_funct_mfptr : public fit_funct<vec_t> { 00097 00098 public: 00099 00100 /** \brief Specify the member function pointer 00101 */ 00102 fit_funct_mfptr 00103 (tclass *tp, double (tclass::*fp)(size_t np, const vec_t &p, double x)) { 00104 00105 tptr=tp; 00106 fptr=fp; 00107 } 00108 00109 virtual ~fit_funct_mfptr() {}; 00110 00111 /** \brief Using parameters in \c p, predict \c y given \c x 00112 */ 00113 virtual double operator()(size_t np, const vec_t &p, double x) { 00114 return (*tptr.*fptr)(np,p,x); 00115 } 00116 00117 #ifndef DOXYGEN_INTERNAL 00118 00119 protected: 00120 00121 /// Storage for the user-specified function pointer 00122 double (tclass::*fptr)(size_t np, const vec_t &p, double x); 00123 00124 /// Storage for the class pointer 00125 tclass *tptr; 00126 00127 private: 00128 00129 fit_funct_mfptr(const fit_funct_mfptr &); 00130 fit_funct_mfptr& operator=(const fit_funct_mfptr&); 00131 00132 #endif 00133 00134 }; 00135 00136 /** \brief Const member function pointer fitting function 00137 */ 00138 template <class tclass, class vec_t=ovector_base> 00139 class fit_funct_cmfptr : public fit_funct<vec_t> { 00140 public: 00141 00142 /** \brief Specify the member function pointer 00143 */ 00144 fit_funct_cmfptr(tclass *tp, 00145 double (tclass::*fp)(size_t np, const vec_t &p, 00146 double x) const) { 00147 tptr=tp; 00148 fptr=fp; 00149 } 00150 00151 virtual ~fit_funct_cmfptr() {}; 00152 00153 /** \brief Using parameters in \c p, predict \c y given \c x 00154 */ 00155 virtual double operator()(size_t np, const vec_t &p, double x) { 00156 return (*tptr.*fptr)(np,p,x); 00157 } 00158 00159 #ifndef DOXYGEN_INTERNAL 00160 00161 protected: 00162 00163 /// Storage for the user-specified function pointer 00164 double (tclass::*fptr)(size_t np, const vec_t &p, double x) const; 00165 00166 /// Storage for the class pointer 00167 tclass *tptr; 00168 00169 private: 00170 00171 fit_funct_cmfptr(const fit_funct_cmfptr &); 00172 fit_funct_cmfptr& operator=(const fit_funct_cmfptr&); 00173 00174 #endif 00175 00176 }; 00177 00178 // ---------------------------------------------------------------- 00179 // Fitter base 00180 // ---------------------------------------------------------------- 00181 00182 /** \brief Non-linear least-squares fitting [abstract base] 00183 */ 00184 template<class func_t=fit_funct<>, class vec_t=ovector_base, 00185 class mat_t=omatrix_base> class fit_base { 00186 00187 public: 00188 00189 fit_base() { 00190 verbose=0; 00191 } 00192 00193 virtual ~fit_base() {} 00194 00195 /** \brief Print out iteration information. 00196 00197 Depending on the value of the variable verbose, this prints out 00198 the iteration information. If verbose=0, then no information is 00199 printed, while if verbose>1, then after each iteration, the 00200 present values of x and y are output to std::cout along with the 00201 iteration number. If verbose>=2 then each iteration waits for a 00202 character. 00203 */ 00204 virtual int print_iter(size_t nv, vec_t &x, double y, int iter, 00205 double value=0.0, double limit=0.0) { 00206 if (verbose<=0) return 0; 00207 00208 size_t i; 00209 char ch; 00210 00211 std::cout << "Iteration: " << iter << std::endl; 00212 std::cout << "x: "; 00213 for(i=0;i<nv;i++) std::cout << x[i] << " "; 00214 std::cout << std::endl; 00215 std::cout << "y: " << y << " Val: " << value << " Lim: " << limit 00216 << std::endl; 00217 if (verbose>1) { 00218 std::cout << "Press a key and type enter to continue. "; 00219 std::cin >> ch; 00220 } 00221 00222 return 0; 00223 } 00224 00225 /** \brief Fit the data specified in (xdat,ydat) to 00226 the function \c fitfun with the parameters in \c par. 00227 00228 The covariance matrix for the parameters is returned in \c covar 00229 and the value of \f$ \chi^2 \f$ is returned in \c chi2. 00230 00231 */ 00232 virtual int fit(size_t ndat, vec_t &xdat, vec_t &ydat, vec_t &yerr, 00233 size_t npar, vec_t &par, mat_t &covar, double &chi2, 00234 func_t &fitfun)=0; 00235 00236 /** \brief An integer describing the verbosity of the output 00237 */ 00238 int verbose; 00239 00240 /// Return string denoting type ("fit_base") 00241 virtual const char *type() { return "fit_base"; } 00242 00243 /// The number of data points 00244 size_t n_dat; 00245 00246 /// The number of parameters 00247 size_t n_par; 00248 00249 00250 }; 00251 00252 #ifndef DOXYGENP 00253 } 00254 #endif 00255 00256 #endif
Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).