multi_min_fix.h

00001 /*
00002   -------------------------------------------------------------------
00003 
00004   Copyright (C) 2006, 2007, 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   template<class param_t, class bool_vec_t>
00037     class multi_min_fix : public multi_min<param_t,multi_funct<param_t> > {
00038     
00039   public:
00040     
00041     /** \brief Specify the member function pointer
00042      */
00043     multi_min_fix() {
00044       mmp=&def_mmin;
00045     }
00046     
00047     virtual ~multi_min_fix() {}
00048     
00049     /** \brief Calculate the minimum \c min of \c func w.r.t. the
00050         array \c x of size \c nvar.
00051     */
00052     virtual int mmin(size_t nvar, ovector_view &x, double &fmin, 
00053                      param_t &pa, multi_funct<param_t> &func) {
00054       xp=&x;
00055       funcp=&func;
00056       unv=nvar;
00057       nv_new=nvar;
00058       ovector xnew(nv_new);
00059       for(size_t i=0;i<nvar;i++) {
00060         xnew[i]=x[i];
00061       }
00062       multi_funct_mfptr<multi_min_fix,param_t> 
00063         mfm(this,&multi_min_fix::min_func);
00064       mmp->mmin(nv_new,xnew,fmin,pa,mfm);
00065       for(size_t i=0;i<nvar;i++) {
00066         x[i]=xnew[i];
00067       }
00068       return 0;
00069     }
00070 
00071     /** \brief Calculate the minimum of \c func while fixing 
00072         some parameters as specified in \c fix.
00073     */
00074     virtual int mmin_fix(size_t nvar, ovector_view &x, double &fmin, 
00075                          bool_vec_t &fix, param_t &pa, 
00076                          multi_funct<param_t> &func) {
00077       xp=&x;
00078       funcp=&func;
00079       unv=nvar;
00080       fixp=&fix;
00081       nv_new=0;
00082       for(size_t i=0;i<nvar;i++) {
00083         if (fix[i]==false) nv_new++;
00084       }
00085       if (nv_new==0) return 0;
00086       size_t j=0;
00087       ovector xnew(nv_new);
00088       for(size_t i=0;i<nvar;i++) {
00089         if (fix[i]==false) {
00090           xnew[j]=x[i];
00091           j++;
00092         }
00093       }
00094       multi_funct_mfptr<multi_min_fix,param_t> 
00095         mfm(this,&multi_min_fix::min_func);
00096       mmp->mmin(nv_new,xnew,fmin,pa,mfm);
00097       j=0;
00098       for(size_t i=0;i<nvar;i++) {
00099         if (fix[i]==false) {
00100           x[i]=xnew[j];
00101           j++;
00102         }
00103       }
00104       return 0;
00105     }
00106 
00107     /// Change the base minimizer
00108     int set_mmin(multi_min<param_t,multi_funct_mfptr
00109                  <multi_min_fix,param_t> > &min) {
00110       mmp=&min;
00111       return 0;
00112     }
00113 
00114     /// The default base minimizer
00115     gsl_mmin_simp<param_t,multi_funct_mfptr<multi_min_fix,param_t> > def_mmin;
00116     
00117 #ifndef DOXYGEN_INTERNAL
00118     
00119   protected:
00120     
00121     /** \brief The new function to send to the minimizer
00122      */
00123     virtual int min_func(size_t nv, const ovector_view &x, double &y, 
00124                          param_t &pa) {
00125       ovector tmp(unv);
00126       size_t j=0;
00127       for(size_t i=0;i<unv;i++) {
00128         if (nv_new<unv && (*fixp)[i]==true) {
00129           tmp[i]=(*xp)[i];
00130         } else {
00131           tmp[i]=x[j];
00132           j++;
00133         }
00134       }
00135       int ret=(*funcp)(unv,tmp,y,pa);
00136       return ret;
00137     }
00138     
00139     /// The minimizer
00140     multi_min<param_t,multi_funct_mfptr<multi_min_fix,param_t> > *mmp;
00141 
00142     /// The user-specified function
00143     multi_funct<param_t> *funcp;
00144 
00145     /// The user-specified number of variables
00146     size_t unv;
00147 
00148     /// The new number of variables
00149     size_t nv_new;
00150     
00151     /// Specify which parameters to fix
00152     bool_vec_t *fixp;
00153     
00154     /// The user-specified initial vector
00155     ovector_view *xp;
00156 
00157 #ifndef DOXYGENP
00158 #endif
00159 
00160   private:
00161  
00162     multi_min_fix(const multi_min_fix &);
00163     multi_min_fix& operator=(const multi_min_fix&);
00164 
00165 #endif
00166 
00167   };
00168 
00169 
00170 #ifndef DOXYGENP
00171 }
00172 #endif
00173 
00174 #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