GSLMinimizer1D.cxx

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLMinimizer1D.cxx 32583 2010-03-12 09:57:42Z moneta $
00002 // Authors: L. Moneta, A. Zsenei   08/2005
00003  /**********************************************************************
00004   *                                                                    *
00005   * Copyright (c) 2004 moneta,  CERN/PH-SFT                            *
00006   *                                                                    *
00007   * This library is free software; you can redistribute it and/or      *
00008   * modify it under the terms of the GNU General Public License        *
00009   * as published by the Free Software Foundation; either version 2     *
00010   * of the License, or (at your option) any later version.             *
00011   *                                                                    *
00012   * This library is distributed in the hope that it will be useful,    *
00013   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00014   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00015   * General Public License for more details.                           *
00016   *                                                                    *
00017   * You should have received a copy of the GNU General Public License  *
00018   * along with this library (see file COPYING); if not, write          *
00019   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00020   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00021   *                                                                    *
00022   **********************************************************************/
00023 
00024 // Implementation file for class GSLMinimizer1D
00025 // 
00026 // Created by: moneta  at Wed Dec  1 15:04:51 2004
00027 // 
00028 // Last update: Wed Dec  1 15:04:51 2004
00029 // 
00030 
00031 #include <assert.h>
00032 
00033 #include "Math/GSLMinimizer1D.h"
00034 #include "Math/Error.h"
00035 
00036 #include "GSLFunctionWrapper.h"
00037 #include "GSL1DMinimizerWrapper.h"
00038 
00039 
00040 #include "gsl/gsl_min.h"
00041 #include "gsl/gsl_errno.h"
00042 
00043 #include <iostream> 
00044 
00045 namespace ROOT { 
00046 
00047 namespace Math { 
00048 
00049 
00050 GSLMinimizer1D::GSLMinimizer1D(Minim1D::Type type) : 
00051     fXmin(0), fXlow(0), fXup(0), fMin(0), fLow(0), fUp(0), 
00052     fIter(0), fStatus(-1), fIsSet(false), 
00053     fMinimizer(0), fFunction(0)
00054 {
00055    // construct a minimizer passing the algorithm type as an enumeration
00056 
00057    const gsl_min_fminimizer_type* T = 0 ;
00058    switch ( type )
00059    {
00060    case Minim1D::kGOLDENSECTION          : 
00061       T = gsl_min_fminimizer_goldensection; 
00062       break ;
00063    case Minim1D::kBRENT       :
00064       T = gsl_min_fminimizer_brent; 
00065       break ;
00066    default :
00067       // default case is brent 
00068       T = gsl_min_fminimizer_brent; 
00069       break ;
00070    }
00071 
00072    fMinimizer = new GSL1DMinimizerWrapper(T); 
00073    fFunction  = new GSLFunctionWrapper();
00074 
00075 }
00076 
00077 GSLMinimizer1D::~GSLMinimizer1D() 
00078 {
00079    // destructor: clean up minimizer and function pointers 
00080 
00081    if (fMinimizer) delete fMinimizer;
00082    if (fFunction)  delete  fFunction;
00083 }
00084 
00085 GSLMinimizer1D::GSLMinimizer1D(const GSLMinimizer1D &): IMinimizer1D()
00086 {
00087    // dummy copy ctr
00088 }
00089 
00090 GSLMinimizer1D & GSLMinimizer1D::operator = (const GSLMinimizer1D &rhs) 
00091 {
00092    // dummy operator = 
00093    if (this == &rhs) return *this;  // time saving self-test
00094    return *this;
00095 }
00096 
00097 void GSLMinimizer1D::SetFunction(  GSLFuncPointer f, void * p, double xmin, double xlow, double xup) { 
00098    // set the function to be minimized 
00099    assert(fFunction);
00100    assert(fMinimizer);
00101    fXlow = xlow; 
00102    fXup = xup;
00103    fXmin = xmin;
00104    fFunction->SetFuncPointer( f ); 
00105    fFunction->SetParams( p ); 
00106 
00107 #ifdef DEBUG
00108    std::cout << " [ "<< xlow << " , " << xup << " ]" << std::endl;
00109 #endif
00110 
00111    int status = gsl_min_fminimizer_set( fMinimizer->Get(), fFunction->GetFunc(), xmin, xlow, xup);
00112    if (status != GSL_SUCCESS) 
00113       std::cerr <<"GSLMinimizer1D: Error:  Interval [ "<< xlow << " , " << xup << " ] does not contain a minimum" << std::endl; 
00114 
00115 
00116    fIsSet = true; 
00117    fStatus = -1;
00118    return;
00119 }
00120 
00121 int GSLMinimizer1D::Iterate() {
00122    // perform an iteration and update values 
00123    if (!fIsSet) {
00124       std::cerr << "GSLMinimizer1D- Error: Function has not been set in Minimizer" << std::endl;
00125       return -1; 
00126    }
00127  
00128    int status =  gsl_min_fminimizer_iterate(fMinimizer->Get());
00129    // update values
00130    fXmin = gsl_min_fminimizer_x_minimum(fMinimizer->Get() );
00131    fMin = gsl_min_fminimizer_f_minimum(fMinimizer->Get() );
00132    // update interval values
00133    fXlow =  gsl_min_fminimizer_x_lower(fMinimizer->Get() ); 
00134    fXup =  gsl_min_fminimizer_x_upper(fMinimizer->Get() );
00135    fLow =  gsl_min_fminimizer_f_lower(fMinimizer->Get() ); 
00136    fUp =  gsl_min_fminimizer_f_upper(fMinimizer->Get() );
00137    return status;
00138 }
00139 
00140 double GSLMinimizer1D::XMinimum() const { 
00141    // return x value at function minimum
00142    return fXmin;
00143 }
00144 
00145 double GSLMinimizer1D::XLower() const { 
00146    // return lower x value of bracketing interval
00147    return fXlow; 
00148 }
00149 
00150 double GSLMinimizer1D::XUpper() const { 
00151    // return upper x value of bracketing interval
00152    return fXup;
00153 }
00154 
00155 double GSLMinimizer1D::FValMinimum() const { 
00156    // return function value at minimum
00157    return fMin;
00158 }
00159 
00160 double GSLMinimizer1D::FValLower() const { 
00161    // return function value at x lower
00162    return fLow; 
00163 }
00164 
00165 double GSLMinimizer1D::FValUpper() const { 
00166    // return function value at x upper
00167    return fUp;
00168 }
00169 
00170 const char * GSLMinimizer1D::Name() const {
00171    // return name of minimization algorithm
00172    return gsl_min_fminimizer_name(fMinimizer->Get() ); 
00173 }
00174 
00175 bool GSLMinimizer1D::Minimize (int maxIter, double absTol, double relTol) 
00176 { 
00177    // find the minimum via multiple iterations
00178    fStatus = -1; 
00179    int iter = 0; 
00180    int status = 0; 
00181    do { 
00182       iter++;
00183       status = Iterate();
00184       if (status != GSL_SUCCESS) { 
00185          MATH_ERROR_MSG("GSLMinimizer1D::Minimize","error returned when performing an iteration");
00186          fStatus = status; 
00187          return false; 
00188       }
00189 
00190 #ifdef DEBUG
00191       std::cout << "Min1D - iteration " << iter << " interval : [ " << fXlow << "  , " << fXup << " ]  min = " << fXmin
00192                 << " fmin " << fMin << " f(a) " << fLow << " f(b) " << fUp << std::endl;
00193 #endif
00194 
00195   
00196       status =  TestInterval(fXlow, fXup, absTol, relTol); 
00197       if (status == GSL_SUCCESS) { 
00198          fIter = iter;
00199          fStatus = status;
00200          return true; 
00201       }
00202    }
00203    while (status == GSL_CONTINUE && iter < maxIter); 
00204    if (status == GSL_CONTINUE) { 
00205       double tol = std::abs(fXup-fXlow);
00206       MATH_INFO_MSGVAL("GSLMinimizer1D::Minimize","exceeded max iterations, reached tolerance is not sufficient",tol);
00207    }
00208    fStatus = status; 
00209    return false;
00210 }
00211 
00212 
00213 int GSLMinimizer1D::TestInterval( double xlow, double xup, double epsAbs, double epsRel) { 
00214 // static function to test interval 
00215    return gsl_min_test_interval(xlow, xup, epsAbs, epsRel);
00216 }
00217 
00218 } // end namespace Math
00219 
00220 } // end namespace ROOT
00221 

Generated on Tue Jul 5 14:34:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1