fit_base.h

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 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) {
00048       set_err_ret("Empty fit_funct::operator()",gsl_nobase);
00049     }
00050 
00051 #ifndef DOXYGEN_INTERNAL
00052 
00053   private:
00054 
00055     fit_funct(const fit_funct &);
00056     fit_funct& operator=(const fit_funct&);
00057 
00058 #endif
00059   };
00060 
00061   /** 
00062       \brief Function pointer fitting function
00063   */
00064   template<class param_t, class vec_t=ovector_view> class fit_funct_fptr : 
00065     public fit_funct<param_t,vec_t> {
00066     
00067     public:
00068     
00069     /** \brief Specify a fitting function by a function pointer
00070      */
00071     fit_funct_fptr(int (*fp)(size_t np, vec_t &p, double x, 
00072                                 double &y, param_t &pa)) {
00073       fptr=fp;
00074     }
00075     
00076     virtual ~fit_funct_fptr() {}
00077 
00078     /** \brief Using parameters in \c p, predict \c y given \c x
00079      */
00080     virtual int operator()(size_t np, vec_t &p, double x, double &y, 
00081                            param_t &pa) {
00082       return fptr(np,p,x,y,pa);
00083     }
00084 
00085 #ifndef DOXYGEN_INTERNAL
00086 
00087     protected:
00088   
00089     fit_funct_fptr() {};
00090 
00091     /// Storage for the user-specified function pointer
00092     int (*fptr)(size_t np, vec_t &p, double x, double &y, param_t &pa);
00093     
00094     private:
00095 
00096     fit_funct_fptr(const fit_funct_fptr &);
00097     fit_funct_fptr& operator=(const fit_funct_fptr&);
00098 
00099 #endif
00100 
00101   };
00102 
00103   /** 
00104       \brief Member function pointer fitting function
00105   */
00106   template <class tclass, class param_t, class vec_t=ovector_view> 
00107     class fit_funct_mfptr : public fit_funct<param_t,vec_t> {
00108     public:
00109     
00110     /** \brief Specify the member function pointer
00111      */
00112     fit_funct_mfptr(tclass *tp, 
00113                        int (tclass::*fp)(size_t np, vec_t &p, double x, 
00114                                          double &y, param_t &pa)) {
00115       tptr=tp;
00116       fptr=fp;
00117     }
00118   
00119     virtual ~fit_funct_mfptr() {};
00120   
00121     /** \brief Using parameters in \c p, predict \c y given \c x
00122      */
00123     virtual int operator()(size_t np, vec_t &p, double x, double &y, 
00124                            param_t &pa) {
00125       return (*tptr.*fptr)(np,p,x,y,pa);
00126     }
00127   
00128 #ifndef DOXYGEN_INTERNAL
00129 
00130     protected:
00131   
00132     /// Storage for the user-specified function pointer
00133     int (tclass::*fptr)(size_t np, vec_t &p, double x, double &y, param_t &pa);
00134 
00135     /// Storage for the class pointer
00136     tclass *tptr;
00137   
00138     private:
00139 
00140     fit_funct_mfptr(const fit_funct_mfptr &);
00141     fit_funct_mfptr& operator=(const fit_funct_mfptr&);
00142 
00143 #endif
00144 
00145   };
00146 
00147   // ----------------------------------------------------------------
00148   // Array versions
00149   // ----------------------------------------------------------------
00150 
00151   /** 
00152       \brief Fitting function base
00153   */
00154   template<class param_t, size_t nvar> class fit_vfunct {
00155   public:  
00156 
00157     fit_vfunct() {}
00158     virtual ~fit_vfunct() {}
00159     
00160     /** \brief Using parameters in \c p, predict \c y given \c x
00161      */
00162     virtual int operator()(size_t np, double p[], double x, double &y, 
00163                            param_t &pa) {
00164       set_err_ret("Empty fit_vfunct::operator()",gsl_nobase);
00165     }
00166 
00167 #ifndef DOXYGEN_INTERNAL
00168 
00169   private:
00170 
00171     fit_vfunct(const fit_vfunct &);
00172     fit_vfunct& operator=(const fit_vfunct&);
00173 
00174 #endif
00175   };
00176 
00177   /** 
00178       \brief Function pointer fitting function
00179   */
00180   template<class param_t, size_t nvar> class fit_vfunct_fptr : 
00181     public fit_vfunct<param_t,nvar> {
00182     
00183     public:
00184     
00185     /** \brief Specify a fitting function by a function pointer
00186      */
00187     fit_vfunct_fptr(int (*fp)(size_t np, double p[], double x, 
00188                                 double &y, param_t &pa)) {
00189       fptr=fp;
00190     }
00191     
00192     virtual ~fit_vfunct_fptr() {}
00193 
00194     /** \brief Using parameters in \c p, predict \c y given \c x
00195      */
00196     virtual int operator()(size_t np, double p[], double x, double &y, 
00197                            param_t &pa) {
00198       return fptr(np,p,x,y,pa);
00199     }
00200 
00201 #ifndef DOXYGEN_INTERNAL
00202 
00203     protected:
00204   
00205     fit_vfunct_fptr() {};
00206 
00207     /// Storage for the user-specified function pointer
00208     int (*fptr)(size_t np, double p[], double x, double &y, param_t &pa);
00209     
00210     private:
00211 
00212     fit_vfunct_fptr(const fit_vfunct_fptr &);
00213     fit_vfunct_fptr& operator=(const fit_vfunct_fptr&);
00214 
00215 #endif
00216 
00217   };
00218 
00219   /** 
00220       \brief Member function pointer fitting function
00221   */
00222   template <class tclass, class param_t, size_t nvar> 
00223     class fit_vfunct_mfptr : public fit_vfunct<param_t,nvar> {
00224     public:
00225     
00226     /** \brief Specify the member function pointer
00227      */
00228     fit_vfunct_mfptr(tclass *tp, 
00229                        int (tclass::*fp)(size_t np, double p[], double x, 
00230                                          double &y, param_t &pa)) {
00231       tptr=tp;
00232       fptr=fp;
00233     }
00234   
00235     virtual ~fit_vfunct_mfptr() {};
00236   
00237     /** \brief Using parameters in \c p, predict \c y given \c x
00238      */
00239     virtual int operator()(size_t np, double p[], double x, double &y, 
00240                            param_t &pa) {
00241       return (*tptr.*fptr)(np,p,x,y,pa);
00242     }
00243   
00244 #ifndef DOXYGEN_INTERNAL
00245 
00246     protected:
00247   
00248     /// Storage for the user-specified function pointer
00249     int (tclass::*fptr)(size_t np, double p[], double x, double &y, 
00250                         param_t &pa);
00251 
00252     /// Storage for the class pointer
00253     tclass *tptr;
00254   
00255     private:
00256 
00257     fit_vfunct_mfptr(const fit_vfunct_mfptr &);
00258     fit_vfunct_mfptr& operator=(const fit_vfunct_mfptr&);
00259 
00260 #endif
00261 
00262   };
00263 
00264   // ----------------------------------------------------------------
00265   // Fitter base
00266   // ----------------------------------------------------------------
00267 
00268   /** 
00269       \brief Non-linear least-squares fitting base class
00270 
00271   */
00272   template<class param_t, class func_t, 
00273     class vec_t=ovector_view, class mat_t=omatrix_view> class fit_base {
00274     public:
00275 
00276     fit_base() {
00277       verbose=0;
00278     }
00279 
00280     virtual ~fit_base() {}
00281     
00282     /** 
00283         \brief Print out iteration information.
00284         
00285         Depending on the value of the variable verbose, this prints out
00286         the iteration information. If verbose=0, then no information is
00287         printed, while if verbose>1, then after each iteration, the
00288         present values of x and y are output to std::cout along with the
00289         iteration number. If verbose>=2 then each iteration waits for a
00290         character.  
00291     */
00292     virtual int print_iter(size_t nv, vec_t &x, double y, int iter,
00293                            double value=0.0, double limit=0.0) {
00294       if (verbose<=0) return 0;
00295   
00296       size_t i;
00297       char ch;
00298 
00299       std::cout << "Iteration: " << iter << std::endl;
00300       text_out_file outs(&std::cout,79);
00301       outs.word_out("x:");
00302       for(i=0;i<nv;i++) outs.double_out(x[i]);
00303       outs.end_line();
00304       std::cout << "y: " << y << " Val: " << value << " Lim: " << limit 
00305                 << std::endl;
00306       if (verbose>1) {
00307         std::cout << "Press a key and type enter to continue. ";
00308         std::cin >> ch;
00309       }
00310 
00311       return 0;
00312     }
00313 
00314     /** \brief Fit the data specified in (xdat,ydat) to
00315         the function \c fitfun with the parameters in \c par.
00316 
00317         The covariance matrix for the parameters is returned in \c covar
00318         and the value of \f$ \chi^2 \f$ is returned in \c chi2.
00319 
00320     */
00321     virtual int fit(size_t ndat, vec_t &xdat, vec_t &ydat, vec_t &yerr,
00322                     size_t npar, vec_t &par, mat_t &covar, double &chi2,
00323                     param_t &pa, func_t &fitfun) {
00324       set_err_ret("Empty fit_base::fit()",gsl_nobase);
00325     }
00326     
00327     /** \brief An integer describing the verbosity of the output 
00328      */
00329     int verbose;
00330 
00331     /// Return string denoting type ("fit_base")
00332     virtual const char *type() { return "fit_base"; }
00333 
00334     /// The number of data points
00335     size_t n_dat;
00336 
00337     /// The number of parameters
00338     size_t n_par;
00339 
00340 
00341   };
00342 
00343 #ifndef DOXYGENP
00344 }
00345 #endif
00346 
00347 #endif

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page