Logo ROOT  
Reference Guide
BrentMinimizer1D.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: David Gonzalez Maline 2/2008
3  /**********************************************************************
4  * *
5  * Copyright (c) 2004 Maline, CERN/PH-SFT *
6  * *
7  * This library is free software; you can redistribute it and/or *
8  * modify it under the terms of the GNU General Public License *
9  * as published by the Free Software Foundation; either version 2 *
10  * of the License, or (at your option) any later version. *
11  * *
12  * This library is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this library (see file COPYING); if not, write *
19  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
20  * 330, Boston, MA 02111-1307 USA, or contact the author. *
21  * *
22  **********************************************************************/
23 
24 // Header file for class BrentMinimizer1D
25 //
26 // Created by: Maline at Mon Feb 4 09:32:36 2008
27 //
28 //
29 
30 #include "Math/BrentMinimizer1D.h"
31 #include "Math/BrentMethods.h"
32 #include "Math/IFunction.h"
33 #include "Math/IFunctionfwd.h"
34 
35 #include "Math/Error.h"
36 
37 namespace ROOT {
38 namespace Math {
39 
40 static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the minimum
41 static int gDefaultNSearch = 10; // nnumber of time the iteration (bracketing -Brent ) is repeted
42 
43 
45  fFunction(0),
46  fLogScan(false), fNIter(0),
47  fNpx(0), fStatus(-1),
48  fXMin(0), fXMax(0), fXMinimum(0)
49 {
50 // Default Constructor.
51  fNpx = gDefaultNpx;
52 }
53 
55 
57 
58 
59 void BrentMinimizer1D::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
60 {
61 // Sets function to be minimized.
62 
63  fFunction = &f;
64  fStatus = -1; // reset the status
65 
66  if (xlow >= xup)
67  {
68  double tmp = xlow;
69  xlow = xup;
70  xup = tmp;
71  }
72  fXMin = xlow;
73  fXMax = xup;
74 }
75 
76 
77 
79 { return (*fFunction)(fXMinimum); }
80 
82 { return (*fFunction)(fXMin); }
83 
85 { return (*fFunction)(fXMax); }
86 
87 bool BrentMinimizer1D::Minimize( int maxIter, double absTol , double relTol)
88 {
89 // Find minimum position iterating until convergence specified by the
90 // absolute and relative tolerance or the maximum number of iteration
91 // is reached.
92 // repet search (Bracketing + Brent) until max number of search is reached (default is 10)
93 // maxITer refers to the iterations inside the Brent algorithm
94 
95  if (!fFunction) {
96  MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "Function has not been set");
97  return false;
98  }
99 
100  if (fLogScan && fXMin <= 0) {
101  MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "xmin is < 0 and log scan is set - disable it");
102  fLogScan = false;
103  }
104 
105  fNIter = 0;
106  fStatus = -1;
107 
108  double xmin = fXMin;
109  double xmax = fXMax;
110 
111  int maxIter1 = gDefaultNSearch; // external loop (number of search )
112  int maxIter2 = maxIter; // internal loop inside the Brent algorithm
113 
114  int niter1 = 0;
115  int niter2 = 0;
116  bool ok = false;
117  while (!ok){
118  if (niter1 > maxIter1){
119  MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "Search didn't converge");
120  fStatus = -2;
121  return false;
122  }
124  x = BrentMethods::MinimBrent(fFunction, 0, xmin, xmax, x, 0, ok, niter2, absTol, relTol, maxIter2 );
125  fNIter += niter2; // count the total number of iterations
126  niter1++;
127  fXMinimum = x;
128  }
129 
130  fStatus = 0;
131  return true;
132 }
133 
134 
135 const char * BrentMinimizer1D::Name() const
136 { return "BrentMinimizer1D"; }
137 
138 } // Namespace Math
139 
140 } // Namespace ROOT
n
const Int_t n
Definition: legend1.C:16
ROOT::Math::BrentMinimizer1D::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: BrentMinimizer1D.cxx:56
IFunction.h
f
#define f(i)
Definition: RSha256.hxx:104
ROOT::Math::BrentMinimizer1D::fNIter
int fNIter
Definition: BrentMinimizer1D.h:155
ROOT::Math::BrentMinimizer1D::BrentMinimizer1D
BrentMinimizer1D()
Default Constructor.
Definition: BrentMinimizer1D.cxx:44
ROOT::Math::BrentMinimizer1D::SetFunction
void SetFunction(const ROOT::Math::IGenFunction &f, double xlow, double xup)
Sets function to be minimized.
Definition: BrentMinimizer1D.cxx:59
xmax
float xmax
Definition: THbookFile.cxx:95
ROOT::Math::BrentMinimizer1D::fXMax
double fXMax
Definition: BrentMinimizer1D.h:159
ROOT::Math::BrentMinimizer1D::fXMin
double fXMin
Definition: BrentMinimizer1D.h:158
IFunctionfwd.h
ROOT::Math::BrentMinimizer1D::fFunction
const IGenFunction * fFunction
Definition: BrentMinimizer1D.h:153
ROOT::Math::gDefaultNSearch
static int gDefaultNSearch
Definition: BrentMinimizer1D.cxx:41
ROOT::Math::BrentMinimizer1D::fStatus
int fStatus
Definition: BrentMinimizer1D.h:157
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Math::IMinimizer1D
Interface class for numerical methods for one-dimensional minimization.
Definition: IMinimizer1D.h:50
MATH_ERROR_MSG
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:83
ROOT::Math::BrentMinimizer1D::Minimize
virtual bool Minimize(int maxIter, double absTol=1.E-8, double relTol=1.E-10)
Find minimum position iterating until convergence specified by the absolute and relative tolerance or...
Definition: BrentMinimizer1D.cxx:87
BrentMinimizer1D.h
ROOT::Math::BrentMinimizer1D::fNpx
int fNpx
Definition: BrentMinimizer1D.h:156
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
xmin
float xmin
Definition: THbookFile.cxx:95
ROOT::Math::gDefaultNpx
static int gDefaultNpx
Definition: BrentMinimizer1D.cxx:40
Error.h
ROOT::Math::BrentMinimizer1D::FValLower
virtual double FValLower() const
Return function value at current lower bound of the minimization interval.
Definition: BrentMinimizer1D.cxx:81
ROOT::Math::BrentMinimizer1D::Name
virtual const char * Name() const
Return name of minimization algorithm ("BrentMinimizer1D")
Definition: BrentMinimizer1D.cxx:135
ROOT::Math::BrentMinimizer1D::FValUpper
virtual double FValUpper() const
Return function value at current upper bound of the minimization interval.
Definition: BrentMinimizer1D.cxx:84
ROOT::Math::IBaseFunctionOneDim
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:135
ROOT::Math::BrentMinimizer1D::fXMinimum
double fXMinimum
Definition: BrentMinimizer1D.h:160
ROOT::Math::BrentMinimizer1D::fLogScan
bool fLogScan
Definition: BrentMinimizer1D.h:154
BrentMethods.h
ROOT::Math::BrentMinimizer1D::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: BrentMinimizer1D.cxx:54
ROOT::Math::BrentMinimizer1D::FValMinimum
virtual double FValMinimum() const
Return function value at current estimate of the minimum.
Definition: BrentMinimizer1D.cxx:78
ROOT
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: EExecutionPolicy.hxx:4
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