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_MULTI_MIN_FIX_H 00024 #define O2SCL_MULTI_MIN_FIX_H 00025 00026 #include <o2scl/multi_min.h> 00027 #include <o2scl/gsl_mmin_simp.h> 00028 00029 #ifndef DOXYGENP 00030 namespace o2scl { 00031 #endif 00032 00033 /** \brief Multidimensional minimizer fixing some variables and 00034 varying others 00035 00036 \todo Generalize to all vector types 00037 */ 00038 template<class param_t, class bool_vec_t> 00039 class multi_min_fix : public multi_min<param_t,multi_funct<param_t> > { 00040 00041 public: 00042 00043 /** \brief Specify the member function pointer 00044 */ 00045 multi_min_fix() { 00046 mmp=&def_mmin; 00047 } 00048 00049 virtual ~multi_min_fix() {} 00050 00051 /** \brief Calculate the minimum \c min of \c func w.r.t. the 00052 array \c x of size \c nvar. 00053 */ 00054 virtual int mmin(size_t nvar, ovector_view &x, double &fmin, 00055 param_t &pa, multi_funct<param_t> &func) { 00056 00057 // Copy arguments for later use 00058 xp=&x; 00059 funcp=&func; 00060 unv=nvar; 00061 nv_new=nvar; 00062 ovector xnew(nv_new); 00063 00064 // Copy initial guess to new format 00065 for(size_t i=0;i<nvar;i++) { 00066 xnew[i]=x[i]; 00067 } 00068 00069 // Perform minimization 00070 multi_funct_mfptr<multi_min_fix,param_t> 00071 mfm(this,&multi_min_fix::min_func); 00072 int ret=mmp->mmin(nv_new,xnew,fmin,pa,mfm); 00073 if (ret!=0) { 00074 add_err("Minimizer failed in multi_min_fix::mmin().",ret); 00075 } 00076 00077 // Copy final result back to original format 00078 for(size_t i=0;i<nvar;i++) { 00079 x[i]=xnew[i]; 00080 } 00081 00082 return ret; 00083 } 00084 00085 /** \brief Calculate the minimum of \c func while fixing 00086 some parameters as specified in \c fix. 00087 00088 If all of entries <tt>fix[0], fix[1], ... fix[nvar-1]</tt> 00089 are true, then this function assumes all of the parameters 00090 are fixed and that there is no minimization to be performed. 00091 In this case, it will return 0 for success without calling 00092 the error handler. 00093 */ 00094 virtual int mmin_fix(size_t nvar, ovector_view &x, double &fmin, 00095 bool_vec_t &fix, param_t &pa, 00096 multi_funct<param_t> &func) { 00097 00098 // Copy arguments for later use 00099 xp=&x; 00100 funcp=&func; 00101 unv=nvar; 00102 fixp=&fix; 00103 nv_new=0; 00104 for(size_t i=0;i<nvar;i++) { 00105 if (fix[i]==false) nv_new++; 00106 } 00107 if (nv_new==0) return 0; 00108 00109 // Copy initial guess to new format 00110 size_t j=0; 00111 ovector xnew(nv_new); 00112 for(size_t i=0;i<nvar;i++) { 00113 if (fix[i]==false) { 00114 xnew[j]=x[i]; 00115 j++; 00116 } 00117 } 00118 00119 // Perform minimization 00120 multi_funct_mfptr<multi_min_fix,param_t> 00121 mfm(this,&multi_min_fix::min_func); 00122 int ret=mmp->mmin(nv_new,xnew,fmin,pa,mfm); 00123 if (ret!=0) { 00124 add_err("Minimizer failed in multi_min_fix::mmin_fix().",ret); 00125 } 00126 00127 // Copy final result back to original format 00128 j=0; 00129 for(size_t i=0;i<nvar;i++) { 00130 if (fix[i]==false) { 00131 x[i]=xnew[j]; 00132 j++; 00133 } 00134 } 00135 00136 return ret; 00137 } 00138 00139 /// Change the base minimizer 00140 int set_mmin(multi_min<param_t,multi_funct_mfptr 00141 <multi_min_fix,param_t> > &min) { 00142 mmp=&min; 00143 return 0; 00144 } 00145 00146 /// The default base minimizer 00147 gsl_mmin_simp<param_t,multi_funct_mfptr<multi_min_fix,param_t> > def_mmin; 00148 00149 #ifndef DOXYGEN_INTERNAL 00150 00151 protected: 00152 00153 /** \brief The new function to send to the minimizer 00154 */ 00155 virtual int min_func(size_t nv, const ovector_view &x, double &y, 00156 param_t &pa) { 00157 ovector tmp(unv); 00158 size_t j=0; 00159 for(size_t i=0;i<unv;i++) { 00160 if (nv_new<unv && (*fixp)[i]==true) { 00161 tmp[i]=(*xp)[i]; 00162 } else { 00163 tmp[i]=x[j]; 00164 j++; 00165 } 00166 } 00167 int ret=(*funcp)(unv,tmp,y,pa); 00168 return ret; 00169 } 00170 00171 /// The minimizer 00172 multi_min<param_t,multi_funct_mfptr<multi_min_fix,param_t> > *mmp; 00173 00174 /// The user-specified function 00175 multi_funct<param_t> *funcp; 00176 00177 /// The user-specified number of variables 00178 size_t unv; 00179 00180 /// The new number of variables 00181 size_t nv_new; 00182 00183 /// Specify which parameters to fix 00184 bool_vec_t *fixp; 00185 00186 /// The user-specified initial vector 00187 ovector_view *xp; 00188 00189 #ifndef DOXYGENP 00190 #endif 00191 00192 private: 00193 00194 multi_min_fix(const multi_min_fix &); 00195 multi_min_fix& operator=(const multi_min_fix&); 00196 00197 #endif 00198 00199 }; 00200 00201 00202 #ifndef DOXYGENP 00203 } 00204 #endif 00205 00206 #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