Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 CERN *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 * *
12 **********************************************************************/
13
15#include "Math/BrentMethods.h"
16#include "Math/IFunctionfwd.h"
17#include <cmath>
18
19#include "Math/Error.h"
20
21namespace ROOT {
22namespace Math {
23
24
25static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the root
26static int gDefaultNSearch = 10; // number of time the iteration (bracketing -Brent ) is repeted
27
28 BrentRootFinder::BrentRootFinder() : fFunction(nullptr),
29 fLogScan(false), fNIter(0),
30 fNpx(0), fStatus(-1),
31 fXMin(0), fXMax(0), fRoot(0)
32{
33 // default constructor (number of points used to bracket value is set to 100)
35}
36
38
40
41
42bool BrentRootFinder::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
43{
44// Set function to solve and the interval in where to look for the root.
45
46 fFunction = &f;
47 // invalid previous status
48 fStatus = -1;
49
50 if (xlow >= xup)
51 {
52 double tmp = xlow;
53 xlow = xup;
54 xup = tmp;
55 }
56 fXMin = xlow;
57 fXMax = xup;
58
59 return true;
60}
61
62const char* BrentRootFinder::Name() const
63{ return "BrentRootFinder"; }
64
65
66bool BrentRootFinder::Solve(int maxIter, double absTol, double relTol)
67{
68 // Returns the X value corresponding to the function value fy for (xmin<x<xmax).
69
70 if (!fFunction) {
71 MATH_ERROR_MSG("BrentRootFinder::Solve", "Function has not been set");
72 return false;
73 }
74
75 if (fLogScan && fXMin <= 0) {
76 MATH_ERROR_MSG("BrentRootFinder::Solve", "xmin is <=0 and log scan is set - disable it");
77 fLogScan = false;
78 }
79
80
81 const double fy = 0; // To find the root
82 fNIter = 0;
83 fStatus = -1;
84
85 double xmin = fXMin;
86 double xmax = fXMax;
87 fRoot = 0;
88
89 int maxIter1 = gDefaultNSearch; // external loop (number of search )
90 int maxIter2 = maxIter; // internal loop inside the Brent algorithm
91
92 int niter1 = 0;
93 int niter2 = 0;
94 bool ok = false;
95 while (!ok){
96 if (niter1 > maxIter1){
97 MATH_ERROR_MSG("BrentRootFinder::Solve", "Search didn't converge");
98 fStatus = -2;
99 return false;
100 }
102 if (xmin > xmax) {
103 // interval does not contain minimum - return
104 MATH_ERROR_MSG("BrentRootFinder", "Interval does not contain a root");
105 return false;
106 }
107 x = BrentMethods::MinimBrent(fFunction, 4, xmin, xmax, x, fy, ok, niter2, absTol, relTol, maxIter2);
108 fNIter += niter2; // count the total number of iterations
109 niter1++;
110 fRoot = x;
111 }
112
113 fStatus = 0;
114 return true;
115}
116
117} // namespace Math
118} // namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
double fy() const
float xmin
float xmax
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.
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.
bool fLogScan
flag to control usage of a log scan
const IGenFunction * fFunction
Pointer to the function.
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...
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Namespace for new Math classes and functions.
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 gDefaultNpx
static int gDefaultNSearch
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...