Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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) 2006 CERN *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 * *
11 **********************************************************************/
12
13// Header file for class BrentMinimizer1D
14//
15// Created by: Maline at Mon Feb 4 09:32:36 2008
16//
17//
18
20#include "Math/BrentMethods.h"
21#include "Math/IFunction.h"
22#include "Math/IFunctionfwd.h"
23
24#include "Math/Error.h"
25
26namespace ROOT {
27namespace Math {
28
29static int gDefaultNpx = 100; // default nunmber of points used in the grid to bracked the minimum
30static int gDefaultNSearch = 10; // number of time the iteration (bracketing -Brent ) is repeted
31
32
34 fFunction(nullptr),
35 fLogScan(false), fNIter(0),
36 fNpx(0), fStatus(-1),
37 fXMin(0), fXMax(0), fXMinimum(0)
38{
39// Default Constructor.
41}
42
44
46
47
48void BrentMinimizer1D::SetFunction(const ROOT::Math::IGenFunction& f, double xlow, double xup)
49{
50// Sets function to be minimized.
51
52 fFunction = &f;
53 fStatus = -1; // reset the status
54
55 if (xlow >= xup)
56 {
57 double tmp = xlow;
58 xlow = xup;
59 xup = tmp;
60 }
61 fXMin = xlow;
62 fXMax = xup;
63}
64
65
66
68{ return (*fFunction)(fXMinimum); }
69
71{ return (*fFunction)(fXMin); }
72
74{ return (*fFunction)(fXMax); }
75
76bool BrentMinimizer1D::Minimize( int maxIter, double absTol , double relTol)
77{
78// Find minimum position iterating until convergence specified by the
79// absolute and relative tolerance or the maximum number of iteration
80// is reached.
81// repeat search (Bracketing + Brent) until max number of search is reached (default is 10)
82// maxITer refers to the iterations inside the Brent algorithm
83
84 if (!fFunction) {
85 MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "Function has not been set");
86 return false;
87 }
88
89 if (fLogScan && fXMin <= 0) {
90 MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "xmin is < 0 and log scan is set - disable it");
91 fLogScan = false;
92 }
93
94 fNIter = 0;
95 fStatus = -1;
96
97 double xmin = fXMin;
98 double xmax = fXMax;
99
100 int maxIter1 = gDefaultNSearch; // external loop (number of search )
101 int maxIter2 = maxIter; // internal loop inside the Brent algorithm
102
103 int niter1 = 0;
104 int niter2 = 0;
105 bool ok = false;
106 while (!ok){
107 if (niter1 > maxIter1){
108 MATH_ERROR_MSG("BrentMinimizer1D::Minimize", "Search didn't converge");
109 fStatus = -2;
110 return false;
111 }
113 x = BrentMethods::MinimBrent(fFunction, 0, xmin, xmax, x, 0, ok, niter2, absTol, relTol, maxIter2 );
114 fNIter += niter2; // count the total number of iterations
115 niter1++;
116 fXMinimum = x;
117 }
118
119 fStatus = 0;
120 return true;
121}
122
123
124const char * BrentMinimizer1D::Name() const
125{ return "BrentMinimizer1D"; }
126
127} // Namespace Math
128
129} // Namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
float xmin
float xmax
const IGenFunction * fFunction
Pointer to the function.
static void SetDefaultNSearch(int n)
set number of times the bracketing search in combination with is done to find a good interval Default...
double FValLower() const override
Return function value at current lower bound of the minimization interval.
double fXMax
Upper bound of the search interval.
double FValUpper() const override
Return function value at current upper bound of the minimization interval.
int fNpx
Number of points to bracket minimum with grid (def is 100)
double fXMin
Lower bound of the search interval.
void SetFunction(const ROOT::Math::IGenFunction &f, double xlow, double xup)
Sets function to be minimized.
int fNIter
Number of iterations needed for the last estimation.
bool Minimize(int maxIter, double absTol=1.E-8, double relTol=1.E-10) override
Find minimum position iterating until convergence specified by the absolute and relative tolerance or...
static void SetDefaultNpx(int npx)
set number of default Npx used at construction time (when SetNpx is not called) Default value is 100
bool fLogScan
flag to control usage of a log scan
int fStatus
Status of code of the last estimate.
double fXMinimum
Position of the estimated minimum.
BrentMinimizer1D()
Default Constructor.
const char * Name() const override
Return name of minimization algorithm ("BrentMinimizer1D")
double FValMinimum() const override
Return function value at current estimate of the minimum.
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Interface class for numerical methods for one-dimensional minimization.
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...