Logo ROOT  
Reference Guide
BrentRootFinder.cxx
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  * *
9  **********************************************************************/
10 
11 #include "Math/BrentRootFinder.h"
12 #include "Math/BrentMethods.h"
13 #include "Math/IFunctionfwd.h"
14 #include <cmath>
15 
16 #include "Math/Error.h"
17 
18 namespace ROOT {
19 namespace Math {
20 
21 
22 static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the root
23 static int gDefaultNSearch = 10; // number of time the iteration (bracketing -Brent ) is repeted
24 
26  fLogScan(false), fNIter(0),
27  fNpx(0), fStatus(-1),
28  fXMin(0), fXMax(0), fRoot(0)
29 {
30  // default constructor (number of points used to bracket value is set to 100)
31  fNpx = gDefaultNpx;
32 }
33 
35 
37 
38 
39 bool BrentRootFinder::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
40 {
41 // Set function to solve and the interval in where to look for the root.
42 
43  fFunction = &f;
44  // invalid previous status
45  fStatus = -1;
46 
47  if (xlow >= xup)
48  {
49  double tmp = xlow;
50  xlow = xup;
51  xup = tmp;
52  }
53  fXMin = xlow;
54  fXMax = xup;
55 
56  return true;
57 }
58 
59 const char* BrentRootFinder::Name() const
60 { return "BrentRootFinder"; }
61 
62 
63 bool BrentRootFinder::Solve(int maxIter, double absTol, double relTol)
64 {
65  // Returns the X value corresponding to the function value fy for (xmin<x<xmax).
66 
67  if (!fFunction) {
68  MATH_ERROR_MSG("BrentRootFinder::Solve", "Function has not been set");
69  return false;
70  }
71 
72  if (fLogScan && fXMin <= 0) {
73  MATH_ERROR_MSG("BrentRootFinder::Solve", "xmin is <=0 and log scan is set - disable it");
74  fLogScan = false;
75  }
76 
77 
78  const double fy = 0; // To find the root
79  fNIter = 0;
80  fStatus = -1;
81 
82  double xmin = fXMin;
83  double xmax = fXMax;
84  fRoot = 0;
85 
86  int maxIter1 = gDefaultNSearch; // external loop (number of search )
87  int maxIter2 = maxIter; // internal loop inside the Brent algorithm
88 
89  int niter1 = 0;
90  int niter2 = 0;
91  bool ok = false;
92  while (!ok){
93  if (niter1 > maxIter1){
94  MATH_ERROR_MSG("BrentRootFinder::Solve", "Search didn't converge");
95  fStatus = -2;
96  return false;
97  }
99  if (xmin > xmax) {
100  // interval does not contain minimum - return
101  MATH_ERROR_MSG("BrentRootFinder", "Interval does not contain a root");
102  return false;
103  }
104  x = BrentMethods::MinimBrent(fFunction, 4, xmin, xmax, x, fy, ok, niter2, absTol, relTol, maxIter2);
105  fNIter += niter2; // count the total number of iterations
106  niter1++;
107  fRoot = x;
108  }
109 
110  fStatus = 0;
111  return true;
112 }
113 
114 } // namespace Math
115 } // namespace ROOT
n
const Int_t n
Definition: legend1.C:16
f
#define f(i)
Definition: RSha256.hxx:104
xmax
float xmax
Definition: THbookFile.cxx:95
ROOT::Math::BrentRootFinder::fLogScan
bool fLogScan
Definition: BrentRootFinder.h:136
IFunctionfwd.h
ROOT::Math::gDefaultNSearch
static int gDefaultNSearch
Definition: BrentMinimizer1D.cxx:41
x
Double_t x[n]
Definition: legend1.C:17
BrentRootFinder.h
MATH_ERROR_MSG
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:83
ROOT::Math::BrentRootFinder::fFunction
const IGenFunction * fFunction
Definition: BrentRootFinder.h:135
ROOT::Math::BrentMethods::MinimStep
double MinimStep(const IGenFunction *f, int type, double &xmin, double &xmax, double fy, int npx=100, bool useLog=false)
Grid search implementation, used to bracket the minimum and later use Brent's method with the bracket...
Definition: BrentMethods.cxx:28
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:36
xmin
float xmin
Definition: THbookFile.cxx:95
ROOT::Math::gDefaultNpx
static int gDefaultNpx
Definition: BrentMinimizer1D.cxx:40
Error.h
ROOT::Math::BrentRootFinder::fXMax
double fXMax
Definition: BrentRootFinder.h:141
ROOT::Math::BrentRootFinder::fRoot
double fRoot
Definition: BrentRootFinder.h:142
ROOT::Math::BrentRootFinder::SetFunction
virtual bool SetFunction(const ROOT::Math::IGradFunction &, double)
Set function to solve and the interval in where to look for the root.
Definition: IRootFinderMethod.h:45
ROOT::Math::BrentRootFinder::fNpx
int fNpx
Definition: BrentRootFinder.h:138
ROOT::Math::BrentRootFinder::fXMin
double fXMin
Definition: BrentRootFinder.h:140
ROOT::Math::BrentRootFinder::fStatus
int fStatus
Definition: BrentRootFinder.h:139
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:63
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:34
BrentMethods.h
ROOT::Math::BrentRootFinder::Name
const char * Name() const
Return name of root finder algorithm ("BrentRootFinder").
Definition: BrentRootFinder.cxx:59
ROOT::Math::BrentRootFinder::BrentRootFinder
BrentRootFinder()
Default Constructor.
Definition: BrentRootFinder.cxx:25
ROOT
VSD Structures.
Definition: StringConv.hxx:21
Math
Namespace for new Math classes and functions.
ROOT::Math::BrentMethods::MinimBrent
double MinimBrent(const IGenFunction *f, int type, double &xmin, double &xmax, double xmiddle, double fy, bool &ok, int &niter, double epsabs=1.E-8, double epsrel=1.E-10, int maxiter=100)
Finds a minimum of a function, if the function is unimodal between xmin and xmax This method uses a c...
Definition: BrentMethods.cxx:130
ROOT::Math::BrentRootFinder::fNIter
int fNIter
Definition: BrentRootFinder.h:137