Logo ROOT  
Reference Guide
BrentRootFinder.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: David Gonzalez Maline 01/2008
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 , LCG ROOT MathLib Team *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 
25 // Header for the RootFinder
26 //
27 // Created by: David Gonzalez Maline : Wed Jan 21 2008
28 //
29 
30 #ifndef ROOT_Math_BrentRootFinder
31 #define ROOT_Math_BrentRootFinder
32 
33 #include "Math/IFunctionfwd.h"
34 
35 #include "Math/IRootFinderMethod.h"
36 
37 namespace ROOT {
38 namespace Math {
39 
40 //___________________________________________________________________________________________
41 /**
42  Class for finding the root of a one dimensional function using the Brent algorithm.
43  It will use the Brent Method for finding function roots in a given interval.
44  First, a grid search is used to bracket the root value
45  with the a step size = (xmax-xmin)/npx. The step size
46  can be controlled via the SetNpx() function. A default value of npx = 100 is used.
47  The default value con be changed using the static method SetDefaultNpx.
48  If the function is unimodal or if its extrema are far apart, setting the fNpx to
49  a small value speeds the algorithm up many times.
50  Then, Brent's method is applied on the bracketed interval.
51  It will use the Brent Method for finding function roots in a given interval.
52  If the Brent method fails to converge the bracketing is repeted on the latest best estimate of the
53  interval. The procedure is repeted with a maximum value (default =10) which can be set for all
54  BrentRootFinder classes with the method SetDefaultNSearch
55 
56  This class is implemented from TF1::GetX() method.
57 
58  @ingroup RootFinders
59 
60  */
61 
62  class BrentRootFinder: public IRootFinderMethod {
63  public:
64 
65 
66  /** Default Constructor. */
68 
69 
70  /** Default Destructor. */
71  virtual ~BrentRootFinder() {}
72 
73 
74  /** Set function to solve and the interval in where to look for the root.
75 
76  \@param f Function to be minimized.
77  \@param xlow Lower bound of the search interval.
78  \@param xup Upper bound of the search interval.
79  */
81  bool SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup);
82 
83 
84  /** Returns the X value corresponding to the function value fy for (xmin<x<xmax).
85  Method:
86  First, the grid search is used to bracket the maximum
87  with the step size = (xmax-xmin)/fNpx. This way, the step size
88  can be controlled via the SetNpx() function. If the function is
89  unimodal or if its extrema are far apart, setting the fNpx to
90  a small value speeds the algorithm up many times.
91  Then, Brent's method is applied on the bracketed interval.
92 
93  \@param maxIter maximum number of iterations.
94  \@param absTol desired absolute error in the minimum position.
95  \@param absTol desired relative error in the minimum position.
96  */
97  bool Solve(int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
98 
99  /** Set the number of point used to bracket root using a grid */
100  void SetNpx(int npx) { fNpx = npx; }
101 
102  /**
103  Set a log grid scan (default is equidistant bins)
104  will work only if xlow > 0
105  */
106  void SetLogScan(bool on) { fLogScan = on; }
107 
108  /** Returns root value. Need to call first Solve(). */
109  double Root() const { return fRoot; }
110 
111  /** Returns status of last estimate. If = 0 is OK */
112  int Status() const { return fStatus; }
113 
114  /** Return number of iteration used to find minimum */
115  int Iterations() const { return fNIter; }
116 
117  /** Return name of root finder algorithm ("BrentRootFinder"). */
118  const char* Name() const;
119 
120  // static function used to modify the default parameters
121 
122  /** set number of default Npx used at construction time (when SetNpx is not called)
123  Default value is 100
124  */
125  static void SetDefaultNpx(int npx);
126 
127  /** set number of times the bracketing search in combination with is done to find a good interval
128  Default value is 10
129  */
130  static void SetDefaultNSearch(int n);
131 
132 
133  private:
134 
135  const IGenFunction* fFunction; // Pointer to the function.
136  bool fLogScan; // flag to control usage of a log scan
137  int fNIter; // Number of iterations needed for the last estimation.
138  int fNpx; // Number of points to bracket root with initial grid (def is 100)
139  int fStatus; // Status of code of the last estimate
140  double fXMin; // Lower bound of the search interval.
141  double fXMax; // Upper bound of the search interval
142  double fRoot; // Current stimation of the function root.
143  };
144 
145 } // namespace Math
146 } // namespace ROOT
147 
148 #endif /* ROOT_Math_BrentRootFinder */
n
const Int_t n
Definition: legend1.C:16
f
#define f(i)
Definition: RSha256.hxx:122
ROOT::Math::BrentRootFinder::fLogScan
bool fLogScan
Definition: BrentRootFinder.h:193
IFunctionfwd.h
IRootFinderMethod.h
ROOT::Math::BrentRootFinder::fFunction
const IGenFunction * fFunction
Definition: BrentRootFinder.h:192
ROOT::Math::BrentRootFinder::SetDefaultNSearch
static void SetDefaultNSearch(int n)
set number of times the bracketing search in combination with is done to find a good interval Default...
Definition: BrentRootFinder.cxx:46
ROOT::Math::BrentRootFinder::Status
int Status() const
Returns status of last estimate.
Definition: BrentRootFinder.h:169
ROOT::Math::BrentRootFinder::Iterations
int Iterations() const
Return number of iteration used to find minimum.
Definition: BrentRootFinder.h:172
ROOT::Math::BrentRootFinder::fXMax
double fXMax
Definition: BrentRootFinder.h:198
ROOT::Math::BrentRootFinder::fRoot
double fRoot
Definition: BrentRootFinder.h:199
ROOT::Math::BrentRootFinder::fNpx
int fNpx
Definition: BrentRootFinder.h:195
ROOT::Math::BrentRootFinder::SetLogScan
void SetLogScan(bool on)
Set a log grid scan (default is equidistant bins) will work only if xlow > 0.
Definition: BrentRootFinder.h:163
ROOT::Math::BrentRootFinder::fXMin
double fXMin
Definition: BrentRootFinder.h:197
ROOT::Math::BrentRootFinder::~BrentRootFinder
virtual ~BrentRootFinder()
Default Destructor.
Definition: BrentRootFinder.h:128
ROOT::Math::BrentRootFinder::SetNpx
void SetNpx(int npx)
Set the number of point used to bracket root using a grid.
Definition: BrentRootFinder.h:157
ROOT::Math::BrentRootFinder::fStatus
int fStatus
Definition: BrentRootFinder.h:196
ROOT::Math::IBaseFunctionOneDim
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:135
ROOT::Math::BrentRootFinder::Solve
bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10)
Returns the X value corresponding to the function value fy for (xmin<x<xmax).
Definition: BrentRootFinder.cxx:73
ROOT::Math::BrentRootFinder::SetDefaultNpx
static void SetDefaultNpx(int npx)
set number of default Npx used at construction time (when SetNpx is not called) Default value is 100
Definition: BrentRootFinder.cxx:44
ROOT::Math::BrentRootFinder::Root
double Root() const
Returns root value.
Definition: BrentRootFinder.h:166
ROOT::Math::BrentRootFinder::SetFunction
bool SetFunction(const ROOT::Math::IGenFunction &f, double xlow, double xup)
Sets the function for the rest of the algorithms.
Definition: BrentRootFinder.cxx:49
ROOT::Math::IRootFinderMethod::SetFunction
virtual bool SetFunction(const ROOT::Math::IGradFunction &, double)
Sets the function for algorithms using derivatives.
Definition: IRootFinderMethod.h:60
ROOT::Math::BrentRootFinder::Name
const char * Name() const
Return name of root finder algorithm ("BrentRootFinder").
Definition: BrentRootFinder.cxx:69
ROOT::Math::BrentRootFinder::BrentRootFinder
BrentRootFinder()
Default Constructor.
Definition: BrentRootFinder.cxx:35
ROOT
VSD Structures.
Definition: StringConv.hxx:21
TMath::E
constexpr Double_t E()
Base of natural log:
Definition: TMath.h:102
Math
ROOT::Math::BrentRootFinder::fNIter
int fNIter
Definition: BrentRootFinder.h:194