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