ROOT   6.08/07 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 #ifndef ROOT_Math_IFunctionfwd
34 #include "Math/IFunctionfwd.h"
35 #endif
36
37 #ifndef ROOT_Math_IRootFinderMethod
38 #include "Math/IRootFinderMethod.h"
39 #endif
40
41 namespace ROOT {
42 namespace Math {
43
44 //___________________________________________________________________________________________
45 /**
46  Class for finding the root of a one dimensional function using the Brent algorithm.
47  It will use the Brent Method for finding function roots in a given interval.
48  First, a grid search is used to bracket the root value
49  with the a step size = (xmax-xmin)/npx. The step size
50  can be controlled via the SetNpx() function. A default value of npx = 100 is used.
51  The default value con be changed using the static method SetDefaultNpx.
52  If the function is unimodal or if its extrema are far apart, setting the fNpx to
53  a small value speeds the algorithm up many times.
54  Then, Brent's method is applied on the bracketed interval.
55  It will use the Brent Method for finding function roots in a given interval.
56  If the Brent method fails to converge the bracketing is repeted on the latest best estimate of the
57  interval. The procedure is repeted with a maximum value (default =10) which can be set for all
58  BrentRootFinder classes with the method SetDefaultNSearch
59
60  This class is implemented from TF1::GetX() method.
61
62  @ingroup RootFinders
63
64  */
65
67  public:
68
69
70  /** Default Constructor. */
72
73
74  /** Default Destructor. */
75  virtual ~BrentRootFinder() {}
76
77
78  /** Set function to solve and the interval in where to look for the root.
79
80  \@param f Function to be minimized.
81  \@param xlow Lower bound of the search interval.
82  \@param xup Upper bound of the search interval.
83  */
85  bool SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup);
86
87
88  /** Returns the X value corresponding to the function value fy for (xmin<x<xmax).
89  Method:
90  First, the grid search is used to bracket the maximum
91  with the step size = (xmax-xmin)/fNpx. This way, the step size
92  can be controlled via the SetNpx() function. If the function is
93  unimodal or if its extrema are far apart, setting the fNpx to
94  a small value speeds the algorithm up many times.
95  Then, Brent's method is applied on the bracketed interval.
96
97  \@param maxIter maximum number of iterations.
98  \@param absTol desired absolute error in the minimum position.
99  \@param absTol desired relative error in the minimum position.
100  */
101  bool Solve(int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
102
103  /** Set the number of point used to bracket root using a grid */
104  void SetNpx(int npx) { fNpx = npx; }
105
106  /**
107  Set a log grid scan (default is equidistant bins)
108  will work only if xlow > 0
109  */
110  void SetLogScan(bool on) { fLogScan = on; }
111
112  /** Returns root value. Need to call first Solve(). */
113  double Root() const { return fRoot; }
114
115  /** Returns status of last estimate. If = 0 is OK */
116  int Status() const { return fStatus; }
117
118  /** Return number of iteration used to find minimum */
119  int Iterations() const { return fNIter; }
120
121  /** Return name of root finder algorithm ("BrentRootFinder"). */
122  const char* Name() const;
123
124  // static function used to modify the default parameters
125
126  /** set number of default Npx used at construction time (when SetNpx is not called)
127  Default value is 100
128  */
129  static void SetDefaultNpx(int npx);
130
131  /** set number of times the bracketing search in combination with is done to find a good interval
132  Default value is 10
133  */
134  static void SetDefaultNSearch(int n);
135
136
137  private:
138
139  const IGenFunction* fFunction; // Pointer to the function.
140  bool fLogScan; // flag to control usage of a log scan
141  int fNIter; // Number of iterations needed for the last estimation.
142  int fNpx; // Number of points to bracket root with initial grid (def is 100)
143  int fStatus; // Status of code of the last estimate
144  double fXMin; // Lower bound of the search interval.
145  double fXMax; // Upper bound of the search interval
146  double fRoot; // Current stimation of the function root.
147  };
148
149 } // namespace Math
150 } // namespace ROOT
151
152 #endif /* ROOT_Math_BrentRootFinder */
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
const double absTol
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).
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
const char * Name() const
Return name of root finder algorithm ("BrentRootFinder").
Interface for finding function roots of one-dimensional functions.
const IGenFunction * fFunction
double Root() const
Returns root value.
void SetLogScan(bool on)
Set a log grid scan (default is equidistant bins) will work only if xlow > 0.
Double_t E()
Definition: TMath.h:54
int Iterations() const
Return number of iteration used to find minimum.
int Status() const
Returns status of last estimate.
Class for finding the root of a one dimensional function using the Brent algorithm.
BrentRootFinder()
Default Constructor.
double f(double x)
virtual bool SetFunction(const ROOT::Math::IGradFunction &, double)
Sets the function for algorithms using derivatives.
Namespace for new Math classes and functions.
static void SetDefaultNSearch(int n)
set number of times the bracketing search in combination with is done to find a good interval Default...
static void SetDefaultNpx(int npx)
set number of default Npx used at construction time (when SetNpx is not called) Default value is 100 ...
bool SetFunction(const ROOT::Math::IGenFunction &f, double xlow, double xup)
Sets the function for the rest of the algorithms.
void SetNpx(int npx)
Set the number of point used to bracket root using a grid.
const Int_t n
Definition: legend1.C:16
virtual ~BrentRootFinder()
Default Destructor.