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
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
36
37namespace ROOT {
38namespace 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 repeated on the latest best estimate of the
53 interval. The procedure is repeated 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
63 public:
64
65
66 /** Default Constructor. */
68
69
70 /** Default Destructor. */
71 ~BrentRootFinder() override {}
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) override;
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) override;
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 override { return fRoot; }
110
111 /** Returns status of last estimate. If = 0 is OK */
112 int Status() const override { return fStatus; }
113
114 /** Return number of iteration used to find minimum */
115 int Iterations() const override { return fNIter; }
116
117 /** Return name of root finder algorithm ("BrentRootFinder"). */
118 const char* Name() const override;
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 estimation of the function root.
143 };
144
145} // namespace Math
146} // namespace ROOT
147
148#endif /* ROOT_Math_BrentRootFinder */
#define f(i)
Definition: RSha256.hxx:104
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
Class for finding the root of a one dimensional function using the Brent algorithm.
bool SetFunction(const ROOT::Math::IGenFunction &f, double xlow, double xup) override
Sets the function for the rest of the algorithms.
bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10) override
Returns the X value corresponding to the function value fy for (xmin<x<xmax).
double fXMin
Lower bound of the search interval.
int Iterations() const override
Return number of iteration used to find minimum.
double fXMax
Upper bound of the search interval.
BrentRootFinder()
Default Constructor.
int fNpx
Number of points to bracket root with initial grid (def is 100)
int fStatus
Status of code of the last estimate.
int fNIter
Number of iterations needed for the last estimation.
double Root() const override
Returns root value.
bool fLogScan
flag to control usage of a log scan
const IGenFunction * fFunction
Pointer to the function.
int Status() const override
Returns status of last estimate.
double fRoot
Current estimation of the function root.
static void SetDefaultNpx(int npx)
set number of default Npx used at construction time (when SetNpx is not called) Default value is 100
const char * Name() const override
Return name of root finder algorithm ("BrentRootFinder").
static void SetDefaultNSearch(int n)
set number of times the bracketing search in combination with is done to find a good interval Default...
void SetNpx(int npx)
Set the number of point used to bracket root using a grid.
void SetLogScan(bool on)
Set a log grid scan (default is equidistant bins) will work only if xlow > 0.
~BrentRootFinder() override
Default Destructor.
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:135
Interface for finding function roots of one-dimensional functions.
virtual bool SetFunction(const ROOT::Math::IGradFunction &, double)
Sets the function for algorithms using derivatives.
const Int_t n
Definition: legend1.C:16
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
constexpr Double_t E()
Base of natural log: .
Definition: TMath.h:93