fit_fix.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_FIX_H
00024 #define O2SCL_FIT_FIX_H
00025 
00026 #include <o2scl/fit_base.h>
00027 #include <o2scl/gsl_fit.h>
00028 
00029 #ifndef DOXYGENP
00030 namespace o2scl {
00031 #endif
00032 
00033   /** \brief Multidimensional fitting fixing some parameters and 
00034       varying others
00035   */
00036   template<class param_t, class bool_vec_t>
00037     class fit_fix_pars : public fit_base<param_t,fit_funct<param_t> > {
00038     
00039   public:
00040     
00041     /** \brief Specify the member function pointer
00042      */
00043     fit_fix_pars() {
00044       fitp=&def_fit;
00045     }
00046     
00047     virtual ~fit_fix_pars() {}
00048     
00049     /** \brief Fit the data specified in (xdat,ydat) to
00050         the function \c fitfun with the parameters in \c par.
00051 
00052         The covariance matrix for the parameters is returned in \c covar
00053         and the value of \f$ \chi^2 \f$ is returned in \c chi2.
00054 
00055     */
00056     virtual int fit(size_t ndat, ovector_view &xdat, ovector_view &ydat, 
00057                     ovector_view &yerr, size_t npar, ovector_view &par, 
00058                     omatrix_view &covar, double &chi2, param_t &pa, 
00059                     fit_funct<param_t> &fitfun) {
00060       
00061       xp=&par;
00062       funcp=&fitfun;
00063       unv=npar;
00064       nv_new=npar;
00065       ovector xnew(nv_new);
00066       for(size_t i=0;i<npar;i++) {
00067         xnew[i]=par[i];
00068       }
00069       fit_funct_mfptr<fit_fix_pars,param_t> 
00070         mfm(this,&fit_fix_pars::fit_func);
00071       fitp->fit(ndat,xdat,ydat,yerr,nv_new,xnew,covar,chi2,pa,mfm);
00072       for(size_t i=0;i<npar;i++) {
00073         par[i]=xnew[i];
00074       }
00075       return 0;
00076     }
00077 
00078     /** \brief Fit function \c func while fixing some parameters as
00079         specified in \c fix.
00080     */
00081     virtual int fit_fix(size_t ndat, ovector_view &xdat, ovector_view &ydat, 
00082                         ovector_view &yerr, size_t npar, ovector_view &par, 
00083                         bool_vec_t &fix, omatrix_view &covar, double &chi2, 
00084                         param_t &pa, fit_funct<param_t> &fitfun) {
00085       xp=&par;
00086       funcp=&fitfun;
00087       unv=npar;
00088       fixp=&fix;
00089       nv_new=0;
00090       for(size_t i=0;i<npar;i++) {
00091         if (fix[i]==false) nv_new++;
00092       }
00093       if (nv_new==0) return 0;
00094       size_t j=0;
00095       ovector xnew(nv_new);
00096       for(size_t i=0;i<npar;i++) {
00097         if (fix[i]==false) {
00098           xnew[j]=par[i];
00099           j++;
00100         }
00101       }
00102       fit_funct_mfptr<fit_fix_pars,param_t> 
00103         mfm(this,&fit_fix_pars::fit_func);
00104       fitp->fit(ndat,xdat,ydat,yerr,nv_new,xnew,covar,chi2,pa,mfm);
00105       j=0;
00106       for(size_t i=0;i<npar;i++) {
00107         if (fix[i]==false) {
00108           par[i]=xnew[j];
00109           j++;
00110         }
00111       }
00112       return 0;
00113     }
00114 
00115     /// Change the base minimizer
00116     int set_fit(fit_base<param_t,fit_funct_mfptr
00117                 <fit_fix_pars,param_t> > &fitter) {
00118       fitp=&fitter;
00119       return 0;
00120     }
00121 
00122     /// The default base minimizer
00123     gsl_fit<param_t,fit_funct_mfptr<fit_fix_pars,param_t> > def_fit;
00124     
00125 #ifndef DOXYGEN_INTERNAL
00126     
00127   protected:
00128     
00129     /** \brief The new function to send to the minimizer
00130      */
00131     virtual int fit_func(size_t nv, ovector_view &x, double xx,
00132                          double &y, param_t &pa) {
00133       ovector tmp(unv);
00134       size_t j=0;
00135       for(size_t i=0;i<unv;i++) {
00136         if (nv_new<unv && (*fixp)[i]==true) {
00137           tmp[i]=(*xp)[i];
00138         } else {
00139           tmp[i]=x[j];
00140           j++;
00141         }
00142       }
00143       int ret=(*funcp)(unv,tmp,xx,y,pa);
00144       return ret;
00145     }
00146     
00147     /// The minimizer
00148     fit_base<param_t,fit_funct_mfptr<fit_fix_pars,param_t> > *fitp;
00149 
00150     /// The user-specified function
00151     fit_funct<param_t> *funcp;
00152 
00153     /// The user-specified number of variables
00154     size_t unv;
00155 
00156     /// The new number of variables
00157     size_t nv_new;
00158     
00159     /// Specify which parameters to fix
00160     bool_vec_t *fixp;
00161     
00162     /// The user-specified initial vector
00163     ovector_view *xp;
00164 
00165 #ifndef DOXYGENP
00166 #endif
00167 
00168   private:
00169  
00170     fit_fix_pars(const fit_fix_pars &);
00171     fit_fix_pars& operator=(const fit_fix_pars&);
00172 
00173 #endif
00174 
00175   };
00176 
00177 
00178 #ifndef DOXYGENP
00179 }
00180 #endif
00181 
00182 #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