BrentMinimizer1D.cxx

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: BrentMinimizer1D.cxx 36905 2010-11-24 15:44:34Z moneta $
00002 // Author: David Gonzalez Maline 2/2008
00003  /**********************************************************************
00004   *                                                                    *
00005   * Copyright (c) 2004 Maline,  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 // Header file for class BrentMinimizer1D
00025 // 
00026 // Created by: Maline  at Mon Feb  4 09:32:36 2008
00027 // 
00028 //
00029 
00030 #include "Math/BrentMinimizer1D.h"
00031 #include "Math/BrentMethods.h"
00032 #include "Math/IFunction.h"
00033 
00034 #ifndef ROOT_Math_Error
00035 #include "Math/Error.h"
00036 #endif
00037 
00038 namespace ROOT {
00039 namespace Math {
00040 
00041 static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the minimum
00042 static int gDefaultNSearch = 10;  // nnumber of time the iteration (bracketing -Brent ) is repeted
00043 
00044 
00045    BrentMinimizer1D::BrentMinimizer1D(): IMinimizer1D(),                                          
00046                                          fFunction(0), 
00047                                          fLogScan(false), fNIter(0), 
00048                                          fNpx(0), fStatus(-1), 
00049                                          fXMin(0), fXMax(0), fXMinimum(0) 
00050 {
00051 // Default Constructor.
00052    fNpx = gDefaultNpx;
00053 }
00054  
00055 void BrentMinimizer1D::SetDefaultNpx(int n) { gDefaultNpx = n; }
00056 
00057 void BrentMinimizer1D::SetDefaultNSearch(int n) { gDefaultNSearch = n; }
00058 
00059 
00060 void BrentMinimizer1D::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
00061 {
00062 // Sets function to be minimized. 
00063 
00064    fFunction = &f;
00065    fStatus = -1;  // reset the status
00066 
00067    if (xlow >= xup) 
00068    {
00069       double tmp = xlow;
00070       xlow = xup; 
00071       xup = tmp;
00072    }
00073    fXMin = xlow;
00074    fXMax = xup;
00075 }
00076 
00077 
00078  
00079 double BrentMinimizer1D::FValMinimum() const
00080 {   return (*fFunction)(fXMinimum); }
00081 
00082 double BrentMinimizer1D::FValLower() const
00083 {   return (*fFunction)(fXMin);  }
00084 
00085 double BrentMinimizer1D::FValUpper() const
00086 {   return (*fFunction)(fXMax);  }
00087 
00088 bool BrentMinimizer1D::Minimize( int maxIter, double absTol , double relTol)
00089 {
00090 // Find minimum position iterating until convergence specified by the
00091 // absolute and relative tolerance or the maximum number of iteration
00092 // is reached.
00093 // repet search (Bracketing + Brent) until max number of search is reached (default is 10)
00094 // maxITer refers to the iterations inside the Brent algorithm
00095 
00096    if (!fFunction) { 
00097        MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "Function has not been set");
00098        return false;
00099    }
00100 
00101    if (fLogScan && fXMin <= 0) { 
00102       MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "xmin is < 0 and log scan is set - disable it");
00103       fLogScan = false; 
00104    }
00105 
00106    fNIter = 0; 
00107    fStatus = -1;
00108 
00109    double xmin = fXMin;
00110    double xmax = fXMax;
00111    
00112    int maxIter1 = gDefaultNSearch;  // external loop (number of search )
00113    int maxIter2 = maxIter;          // internal loop inside the Brent algorithm 
00114 
00115    int niter1 = 0;
00116    int niter2 = 0;
00117    bool ok = false;
00118    while (!ok){
00119       if (niter1 > maxIter1){
00120          MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "Search didn't converge");
00121          fStatus = -2; 
00122          return false;
00123       }
00124       double x = BrentMethods::MinimStep(fFunction, 0, xmin, xmax, 0, fNpx,fLogScan);
00125       x = BrentMethods::MinimBrent(fFunction, 0, xmin, xmax, x, 0,  ok, niter2, absTol, relTol, maxIter2 );
00126       fNIter += niter2;  // count the total number of iterations
00127       niter1++;
00128       fXMinimum = x;
00129    }
00130 
00131    fStatus = 0; 
00132    return true;
00133 } 
00134 
00135 
00136 const char * BrentMinimizer1D::Name() const
00137 {   return "BrentMinimizer1D";  }
00138 
00139 } // Namespace Math
00140 
00141 } // Namespace ROOT

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