ROOT   6.08/07 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 #ifndef ROOT_Math_Error
17 #include "Math/Error.h"
18 #endif
19
20 namespace ROOT {
21 namespace Math {
22
23
24 static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the root
25 static int gDefaultNSearch = 10; // nnumber of time the iteration (bracketing -Brent ) is repeted
26
28  fLogScan(false), fNIter(0),
29  fNpx(0), fStatus(-1),
30  fXMin(0), fXMax(0), fRoot(0)
31 {
32  // default constructor (number of points used to bracket value is set to 100)
33  fNpx = gDefaultNpx;
34 }
35
36 void BrentRootFinder::SetDefaultNpx(int n) { gDefaultNpx = n; }
37
38 void BrentRootFinder::SetDefaultNSearch(int n) { gDefaultNSearch = n; }
39
40
41 bool BrentRootFinder::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
42 {
43 // Set function to solve and the interval in where to look for the root.
44
45  fFunction = &f;
46  // invalid previous status
47  fStatus = -1;
48
49  if (xlow >= xup)
50  {
51  double tmp = xlow;
52  xlow = xup;
53  xup = tmp;
54  }
55  fXMin = xlow;
56  fXMax = xup;
57
58  return true;
59 }
60
61 const char* BrentRootFinder::Name() const
62 { return "BrentRootFinder"; }
63
64
65 bool BrentRootFinder::Solve(int maxIter, double absTol, double relTol)
66 {
67  // Returns the X value corresponding to the function value fy for (xmin<x<xmax).
68
69  if (!fFunction) {
70  MATH_ERROR_MSG("BrentRootFinder::Solve", "Function has not been set");
71  return false;
72  }
73
74  if (fLogScan && fXMin <= 0) {
75  MATH_ERROR_MSG("BrentRootFinder::Solve", "xmin is < 0 and log scan is set - disable it");
76  fLogScan = false;
77  }
78
79
80  const double fy = 0; // To find the root
81  fNIter = 0;
82  fStatus = -1;
83
84  double xmin = fXMin;
85  double xmax = fXMax;
86
87  int maxIter1 = gDefaultNSearch; // external loop (number of search )
88  int maxIter2 = maxIter; // internal loop inside the Brent algorithm
89
90  int niter1 = 0;
91  int niter2 = 0;
92  bool ok = false;
93  while (!ok){
94  if (niter1 > maxIter1){
95  MATH_ERROR_MSG("BrentRootFinder::Solve", "Search didn't converge");
96  fStatus = -2;
97  return false;
98  }
99  double x = BrentMethods::MinimStep(fFunction, 4, xmin, xmax, fy, fNpx,fLogScan);
100  x = BrentMethods::MinimBrent(fFunction, 4, xmin, xmax, x, fy, ok, niter2, absTol, relTol, maxIter2);
101  fNIter += niter2; // count the total number of iterations
102  niter1++;
103  fRoot = x;
104  }
105
106  fStatus = 0;
107  return true;
108 }
109
110 } // namespace Math
111 } // 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<x<xmax).
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
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&#39;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
Double_t x[n]
Definition: legend1.C:17
const char * Name() const
Return name of root finder algorithm ("BrentRootFinder").
const IGenFunction * fFunction
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
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