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

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.