Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RootFinder.h
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Authors: L. Moneta, A. Zsenei 08/2005
3
4 /**********************************************************************
5 * *
6 * Copyright (c) 2004 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
14// Header file for class RootFinder
15//
16// Created by: moneta at Sun Nov 14 16:59:55 2004
17//
18// Last update: Sun Nov 14 16:59:55 2004
19//
20#ifndef ROOT_Math_RootFinder
21#define ROOT_Math_RootFinder
22
23
24#include "Math/IFunctionfwd.h"
25
27
28
29/**
30 @defgroup RootFinders One-dimensional Root-Finding
31 Classes implementing algorithms for finding the roots of a one-dimensional function.
32 Various implementations exist in MathCore and MathMore
33 The user interacts with a proxy class ROOT::Math::RootFinder which creates behind
34 the chosen algorithms which are implemented using the ROOT::Math::IRootFinderMethod interface
35
36 @ingroup NumAlgo
37*/
38
39
40namespace ROOT {
41 namespace Math {
42
43
44//_____________________________________________________________________________________
45 /**
46 User Class to find the Root of one dimensional functions.
47 The GSL Methods are implemented in MathMore and they are loaded automatically
48 via the plug-in manager
49
50 The possible types of Root-finding algorithms are:
51 <ul>
52 <li>Root Bracketing Algorithms which do not require function derivatives
53 <ol>
54 <li>RootFinder::kBRENT (default method implemented in MathCore)
55 <li>RootFinder::kGSL_BISECTION
56 <li>RootFinder::kGSL_FALSE_POS
57 <li>RootFinder::kGSL_BRENT
58 </ol>
59 <li>Root Finding Algorithms using Derivatives
60 <ol>
61 <li>RootFinder::kGSL_NEWTON
62 <li>RootFinder::kGSL_SECANT
63 <li>RootFinder::kGSL_STEFFENSON
64 </ol>
65 </ul>
66
67 This class does not cupport copying
68
69 @ingroup RootFinders
70
71 */
72
73 class RootFinder {
74
75 public:
76
77 enum EType { kBRENT, // Methods from MathCore
80 };
81
82 /**
83 Construct a Root-Finder algorithm
84 */
86 virtual ~RootFinder();
87
88 private:
89 // usually copying is non trivial, so we make this unaccessible
92 {
93 if (this == &rhs) return *this; // time saving self-test
94 return *this;
95 }
96
97 public:
98
100
101 /**
102 Provide to the solver the function and the initial search interval [xlow, xup]
103 for algorithms not using derivatives (bracketing algorithms)
104 The templated function f must be of a type implementing the \a operator() method,
105 <em> double operator() ( double x ) </em>
106 Returns non zero if interval is not valid (i.e. does not contains a root)
107 */
108
109 bool SetFunction( const IGenFunction & f, double xlow, double xup) {
110 return fSolver->SetFunction( f, xlow, xup);
111 }
112
113
114 /**
115 Provide to the solver the function and an initial estimate of the root,
116 for algorithms using derivatives.
117 The templated function f must be of a type implementing the \a operator()
118 and the \a Gradient() methods.
119 <em> double operator() ( double x ) </em>
120 Returns non zero if starting point is not valid
121 */
122
123 bool SetFunction( const IGradFunction & f, double xstart) {
124 return fSolver->SetFunction( f, xstart);
125 }
126
127 template<class Function, class Derivative>
128 bool Solve(Function &f, Derivative &d, double start,
129 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
130
131 template<class Function>
132 bool Solve(Function &f, double min, double max,
133 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
134
135 /**
136 Compute the roots iterating until the estimate of the Root is within the required tolerance returning
137 the iteration Status
138 */
139 bool Solve( int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) {
140 return fSolver->Solve( maxIter, absTol, relTol );
141 }
142
143 /**
144 Return the number of iteration performed to find the Root.
145 */
146 int Iterations() const {
147 return fSolver->Iterations();
148 }
149
150 /**
151 Perform a single iteration and return the Status
152 */
153 int Iterate() {
154 return fSolver->Iterate();
155 }
156
157 /**
158 Return the current and latest estimate of the Root
159 */
160 double Root() const {
161 return fSolver->Root();
162 }
163
164 /**
165 Return the status of the last estimate of the Root
166 = 0 OK, not zero failure
167 */
168 int Status() const {
169 return fSolver->Status();
170 }
171
172
173 /**
174 Return the current and latest estimate of the lower value of the Root-finding interval (for bracketing algorithms)
175 */
176/* double XLower() const { */
177/* return fSolver->XLower(); */
178/* } */
179
180 /**
181 Return the current and latest estimate of the upper value of the Root-finding interval (for bracketing algorithms)
182 */
183/* double XUpper() const { */
184/* return fSolver->XUpper(); */
185/* } */
186
187 /**
188 Get Name of the Root-finding solver algorithm
189 */
190 const char * Name() const {
191 return fSolver->Name();
192 }
193
194
195 protected:
196
197
198 private:
199
200 IRootFinderMethod* fSolver; // type of algorithm to be used
201
202
203 };
204
205 } // namespace Math
206} // namespace ROOT
207
208
209#include "Math/WrappedFunction.h"
210
211#include "Math/Functor.h"
212
213/**
214 * Solve `f(x) = 0`, given a derivative `d`.
215 * @param f Function whose root should be found.
216 * @param d Derivative of the function.
217 * @param start Starting point for iteration.
218 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
219 * @param absTol Absolute tolerance, as in Solve(int,double,double)
220 * @param relTol Relative tolerance, passed to Solve(int,double,double)
221 * @return true if a root was found. Retrieve the result using Root().
222 */
223template<class Function, class Derivative>
224bool ROOT::Math::RootFinder::Solve(Function &f, Derivative &d, double start,
225 int maxIter, double absTol, double relTol)
226{
227 if (!fSolver) return false;
229 bool ret = fSolver->SetFunction(wf, start);
230 if (!ret) return false;
231 return Solve(maxIter, absTol, relTol);
232}
233
234/**
235 * Solve `f(x) = 0` numerically.
236 * @param f Function whose root should be found.
237 * @param min Minimum allowed value of `x`.
238 * @param max Maximum allowed value of `x`.
239 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
240 * @param absTol Absolute tolerance, as in Solve(int,double,double)
241 * @param relTol Relative tolerance, passed to Solve(int,double,double)
242 * @return true if a root was found. Retrieve the result using Root().
243 */
244template<class Function>
245bool ROOT::Math::RootFinder::Solve(Function &f, double min, double max,
246 int maxIter, double absTol, double relTol)
247{
248 if (!fSolver) return false;
250 bool ret = fSolver->SetFunction(wf, min, max);
251 if (!ret) return false;
252 return Solve(maxIter, absTol, relTol);
253}
254
255#endif /* ROOT_Math_RootFinder */
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Double_t(* Function)(Double_t)
Definition Functor.C:4
GradFunctor1D class for one-dimensional gradient functions.
Definition Functor.h:269
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:254
Interface for finding function roots of one-dimensional functions.
virtual int Iterations() const
Return number of iterations used to find the root Must be implemented by derived classes.
virtual bool SetFunction(const ROOT::Math::IGradFunction &, double)
Sets the function for algorithms using derivatives.
virtual int Status() const =0
Returns the status of the previous estimate.
virtual double Root() const =0
Returns the previously calculated root.
virtual int Iterate()
This method is implemented only by the GSLRootFinder and GSLRootFinderDeriv classes and will return a...
virtual bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10)=0
Stimates the root for the function.
virtual const char * Name() const =0
Return name of root finder algorithm.
User Class to find the Root of one dimensional functions.
Definition RootFinder.h:73
bool SetMethod(RootFinder::EType type=RootFinder::kBRENT)
bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10)
Compute the roots iterating until the estimate of the Root is within the required tolerance returning...
Definition RootFinder.h:139
RootFinder & operator=(const RootFinder &rhs)
Definition RootFinder.h:91
int Iterations() const
Return the number of iteration performed to find the Root.
Definition RootFinder.h:146
const char * Name() const
Return the current and latest estimate of the lower value of the Root-finding interval (for bracketin...
Definition RootFinder.h:190
bool SetFunction(const IGenFunction &f, double xlow, double xup)
Provide to the solver the function and the initial search interval [xlow, xup] for algorithms not usi...
Definition RootFinder.h:109
IRootFinderMethod * fSolver
Definition RootFinder.h:200
bool SetFunction(const IGradFunction &f, double xstart)
Provide to the solver the function and an initial estimate of the root, for algorithms using derivati...
Definition RootFinder.h:123
bool Solve(Function &f, Derivative &d, double start, int maxIter=100, double absTol=1E-8, double relTol=1E-10)
Solve f(x) = 0, given a derivative d.
Definition RootFinder.h:224
RootFinder(const RootFinder &)
Definition RootFinder.h:90
double Root() const
Return the current and latest estimate of the Root.
Definition RootFinder.h:160
int Iterate()
Perform a single iteration and return the Status.
Definition RootFinder.h:153
int Status() const
Return the status of the last estimate of the Root = 0 OK, not zero failure.
Definition RootFinder.h:168
Template class to wrap any C++ callable object which takes one argument i.e.
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.