00001 // @(#)root/mathcore:$Id: BrentRootFinder.h 36905 2010-11-24 15:44:34Z moneta $ 00002 // Authors: David Gonzalez Maline 01/2008 00003 00004 /********************************************************************** 00005 * * 00006 * Copyright (c) 2006 , LCG ROOT MathLib Team * 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU General Public License * 00010 * as published by the Free Software Foundation; either version 2 * 00011 * of the License, or (at your option) any later version. * 00012 * * 00013 * This library 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 GNU * 00016 * General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this library (see file COPYING); if not, write * 00020 * to the Free Software Foundation, Inc., 59 Temple Place, Suite * 00021 * 330, Boston, MA 02111-1307 USA, or contact the author. * 00022 * * 00023 **********************************************************************/ 00024 00025 // Header for the RootFinder 00026 // 00027 // Created by: David Gonzalez Maline : Wed Jan 21 2008 00028 // 00029 00030 #ifndef ROOT_Math_BrentRootFinder 00031 #define ROOT_Math_BrentRootFinder 00032 00033 #ifndef ROOT_Math_IFunctionfwd 00034 #include "Math/IFunctionfwd.h" 00035 #endif 00036 00037 #ifndef ROOT_Math_IRootFinderMethod 00038 #include "Math/IRootFinderMethod.h" 00039 #endif 00040 00041 namespace ROOT { 00042 namespace Math { 00043 00044 //___________________________________________________________________________________________ 00045 /** 00046 Class for finding the root of a one dimensional function using the Brent algorithm. 00047 It will use the Brent Method for finding function roots in a given interval. 00048 First, a grid search is used to bracket the root value 00049 with the a step size = (xmax-xmin)/npx. The step size 00050 can be controlled via the SetNpx() function. A default value of npx = 100 is used. 00051 The default value con be changed using the static method SetDefaultNpx. 00052 If the function is unimodal or if its extrema are far apart, setting the fNpx to 00053 a small value speeds the algorithm up many times. 00054 Then, Brent's method is applied on the bracketed interval. 00055 It will use the Brent Method for finding function roots in a given interval. 00056 If the Brent method fails to converge the bracketing is repeted on the latest best estimate of the 00057 interval. The procedure is repeted with a maximum value (default =10) which can be set for all 00058 BrentRootFinder classes with the method SetDefaultNSearch 00059 00060 This class is implemented from TF1::GetX() method. 00061 00062 @ingroup RootFinders 00063 00064 */ 00065 00066 class BrentRootFinder: public IRootFinderMethod { 00067 public: 00068 00069 00070 /** Default Constructor. */ 00071 BrentRootFinder(); 00072 00073 00074 /** Default Destructor. */ 00075 virtual ~BrentRootFinder() {} 00076 00077 00078 /** Set function to solve and the interval in where to look for the root. 00079 00080 \@param f Function to be minimized. 00081 \@param xlow Lower bound of the search interval. 00082 \@param xup Upper bound of the search interval. 00083 */ 00084 using IRootFinderMethod::SetFunction; 00085 bool SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup); 00086 00087 00088 /** Returns the X value corresponding to the function value fy for (xmin<x<xmax). 00089 Method: 00090 First, the grid search is used to bracket the maximum 00091 with the step size = (xmax-xmin)/fNpx. This way, the step size 00092 can be controlled via the SetNpx() function. If the function is 00093 unimodal or if its extrema are far apart, setting the fNpx to 00094 a small value speeds the algorithm up many times. 00095 Then, Brent's method is applied on the bracketed interval. 00096 00097 \@param maxIter maximum number of iterations. 00098 \@param absTol desired absolute error in the minimum position. 00099 \@param absTol desired relative error in the minimum position. 00100 */ 00101 bool Solve(int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10); 00102 00103 /** Set the number of point used to bracket root using a grid */ 00104 void SetNpx(int npx) { fNpx = npx; } 00105 00106 /** 00107 Set a log grid scan (default is equidistant bins) 00108 will work only if xlow > 0 00109 */ 00110 void SetLogScan(bool on) { fLogScan = on; } 00111 00112 /** Returns root value. Need to call first Solve(). */ 00113 double Root() const { return fRoot; } 00114 00115 /** Returns status of last estimate. If = 0 is OK */ 00116 int Status() const { return fStatus; } 00117 00118 /** Return number of iteration used to find minimum */ 00119 int Iterations() const { return fNIter; } 00120 00121 /** Return name of root finder algorithm ("BrentRootFinder"). */ 00122 const char* Name() const; 00123 00124 // static function used to modify the default parameters 00125 00126 /** set number of default Npx used at construction time (when SetNpx is not called) 00127 Default value is 100 00128 */ 00129 static void SetDefaultNpx(int npx); 00130 00131 /** set number of times the bracketing search in combination with is done to find a good interval 00132 Default value is 10 00133 */ 00134 static void SetDefaultNSearch(int n); 00135 00136 00137 private: 00138 00139 const IGenFunction* fFunction; // Pointer to the function. 00140 bool fLogScan; // flag to control usage of a log scan 00141 int fNIter; // Number of iterations needed for the last estimation. 00142 int fNpx; // Number of points to bracket root with initial grid (def is 100) 00143 int fStatus; // Status of code of the last estimate 00144 double fXMin; // Lower bound of the search interval. 00145 double fXMax; // Upper bound of the search interval 00146 double fRoot; // Current stimation of the function root. 00147 }; 00148 00149 } // namespace Math 00150 } // namespace ROOT 00151 00152 #endif /* ROOT_Math_BrentRootFinder */