ROOT  6.06/09
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 <cmath>
14 
15 #ifndef ROOT_Math_Error
16 #include "Math/Error.h"
17 #endif
18 
19 namespace ROOT {
20 namespace Math {
21 
22 
23 static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the root
24 static int gDefaultNSearch = 10; // nnumber of time the iteration (bracketing -Brent ) is repeted
25 
27  fLogScan(false), fNIter(0),
28  fNpx(0), fStatus(-1),
29  fXMin(0), fXMax(0), fRoot(0)
30 {
31  // default constructor (number of points used to bracket value is set to 100)
32  fNpx = gDefaultNpx;
33 }
34 
35 void BrentRootFinder::SetDefaultNpx(int n) { gDefaultNpx = n; }
36 
37 void BrentRootFinder::SetDefaultNSearch(int n) { gDefaultNSearch = n; }
38 
39 
40 bool BrentRootFinder::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
41 {
42 // Set function to solve and the interval in where to look for the root.
43 
44  fFunction = &f;
45  // invalid previous status
46  fStatus = -1;
47 
48  if (xlow >= xup)
49  {
50  double tmp = xlow;
51  xlow = xup;
52  xup = tmp;
53  }
54  fXMin = xlow;
55  fXMax = xup;
56 
57  return true;
58 }
59 
60 const char* BrentRootFinder::Name() const
61 { return "BrentRootFinder"; }
62 
63 
64 bool BrentRootFinder::Solve(int maxIter, double absTol, double relTol)
65 {
66  // Returns the X value corresponding to the function value fy for (xmin<x<xmax).
67 
68  if (!fFunction) {
69  MATH_ERROR_MSG("BrentRootFinder::Solve", "Function has not been set");
70  return false;
71  }
72 
73  if (fLogScan && fXMin <= 0) {
74  MATH_ERROR_MSG("BrentRootFinder::Solve", "xmin is < 0 and log scan is set - disable it");
75  fLogScan = false;
76  }
77 
78 
79  const double fy = 0; // To find the root
80  fNIter = 0;
81  fStatus = -1;
82 
83  double xmin = fXMin;
84  double xmax = fXMax;
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  }
98  double x = BrentMethods::MinimStep(fFunction, 4, xmin, xmax, fy, fNpx,fLogScan);
99  x = BrentMethods::MinimBrent(fFunction, 4, xmin, xmax, x, fy, ok, niter2, absTol, relTol, maxIter2);
100  fNIter += niter2; // count the total number of iterations
101  niter1++;
102  fRoot = x;
103  }
104 
105  fStatus = 0;
106  return true;
107 }
108 
109 } // namespace Math
110 } // namespace ROOT
float xmin
Definition: THbookFile.cxx:93
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
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
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...
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...
static int gDefaultNSearch
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
Definition: TIterator.cxx:20
Double_t x[n]
Definition: legend1.C:17
const IGenFunction * fFunction
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
const char * Name() const
Return name of root finder algorithm ("BrentRootFinder").
static int gDefaultNpx
float xmax
Definition: THbookFile.cxx:93
BrentRootFinder()
Default Constructor.
double f(double x)
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.
const Int_t n
Definition: legend1.C:16