|
||||
File indexing completed on 2025-01-18 10:10:14
0001 // @(#)root/mathcore:$Id$ 0002 // Authors: David Gonzalez Maline 01/2008 0003 0004 /********************************************************************** 0005 * * 0006 * Copyright (c) 2006 CERN * 0007 * All rights reserved. * 0008 * * 0009 * For the licensing terms see $ROOTSYS/LICENSE. * 0010 * For the list of contributors see $ROOTSYS/README/CREDITS. * 0011 * * 0012 **********************************************************************/ 0013 0014 // Header for the RootFinder 0015 // 0016 // Created by: David Gonzalez Maline : Wed Jan 21 2008 0017 // 0018 0019 #ifndef ROOT_Math_BrentRootFinder 0020 #define ROOT_Math_BrentRootFinder 0021 0022 #include "Math/IFunctionfwd.h" 0023 0024 #include "Math/IRootFinderMethod.h" 0025 0026 namespace ROOT { 0027 namespace Math { 0028 0029 //___________________________________________________________________________________________ 0030 /** 0031 Class for finding the root of a one dimensional function using the Brent algorithm. 0032 It will use the Brent Method for finding function roots in a given interval. 0033 First, a grid search is used to bracket the root value 0034 with the a step size = (xmax-xmin)/npx. The step size 0035 can be controlled via the SetNpx() function. A default value of npx = 100 is used. 0036 The default value con be changed using the static method SetDefaultNpx. 0037 If the function is unimodal or if its extrema are far apart, setting the fNpx to 0038 a small value speeds the algorithm up many times. 0039 Then, Brent's method is applied on the bracketed interval. 0040 It will use the Brent Method for finding function roots in a given interval. 0041 If the Brent method fails to converge the bracketing is repeated on the latest best estimate of the 0042 interval. The procedure is repeated with a maximum value (default =10) which can be set for all 0043 BrentRootFinder classes with the method SetDefaultNSearch 0044 0045 This class is implemented from TF1::GetX() method. 0046 0047 @ingroup RootFinders 0048 0049 */ 0050 0051 class BrentRootFinder: public IRootFinderMethod { 0052 public: 0053 0054 0055 /** Default Constructor. */ 0056 BrentRootFinder(); 0057 0058 0059 /** Default Destructor. */ 0060 ~BrentRootFinder() override {} 0061 0062 0063 /** Set function to solve and the interval in where to look for the root. 0064 0065 \@param f Function to be minimized. 0066 \@param xlow Lower bound of the search interval. 0067 \@param xup Upper bound of the search interval. 0068 */ 0069 using IRootFinderMethod::SetFunction; 0070 bool SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup) override; 0071 0072 0073 /** Returns the X value corresponding to the function value fy for (xmin<x<xmax). 0074 Method: 0075 First, the grid search is used to bracket the maximum 0076 with the step size = (xmax-xmin)/fNpx. This way, the step size 0077 can be controlled via the SetNpx() function. If the function is 0078 unimodal or if its extrema are far apart, setting the fNpx to 0079 a small value speeds the algorithm up many times. 0080 Then, Brent's method is applied on the bracketed interval. 0081 0082 \@param maxIter maximum number of iterations. 0083 \@param absTol desired absolute error in the minimum position. 0084 \@param absTol desired relative error in the minimum position. 0085 */ 0086 bool Solve(int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) override; 0087 0088 /** Set the number of point used to bracket root using a grid */ 0089 void SetNpx(int npx) { fNpx = npx; } 0090 0091 /** 0092 Set a log grid scan (default is equidistant bins) 0093 will work only if xlow > 0 0094 */ 0095 void SetLogScan(bool on) { fLogScan = on; } 0096 0097 /** Returns root value. Need to call first Solve(). */ 0098 double Root() const override { return fRoot; } 0099 0100 /** Returns status of last estimate. If = 0 is OK */ 0101 int Status() const override { return fStatus; } 0102 0103 /** Return number of iteration used to find minimum */ 0104 int Iterations() const override { return fNIter; } 0105 0106 /** Return name of root finder algorithm ("BrentRootFinder"). */ 0107 const char* Name() const override; 0108 0109 // static function used to modify the default parameters 0110 0111 /** set number of default Npx used at construction time (when SetNpx is not called) 0112 Default value is 100 0113 */ 0114 static void SetDefaultNpx(int npx); 0115 0116 /** set number of times the bracketing search in combination with is done to find a good interval 0117 Default value is 10 0118 */ 0119 static void SetDefaultNSearch(int n); 0120 0121 0122 private: 0123 0124 const IGenFunction* fFunction; ///< Pointer to the function. 0125 bool fLogScan; ///< flag to control usage of a log scan 0126 int fNIter; ///< Number of iterations needed for the last estimation. 0127 int fNpx; ///< Number of points to bracket root with initial grid (def is 100) 0128 int fStatus; ///< Status of code of the last estimate 0129 double fXMin; ///< Lower bound of the search interval. 0130 double fXMax; ///< Upper bound of the search interval 0131 double fRoot; ///< Current estimation of the function root. 0132 }; 0133 0134 } // namespace Math 0135 } // namespace ROOT 0136 0137 #endif /* ROOT_Math_BrentRootFinder */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |