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 // usually copying is non trivial, so we delete this
89 RootFinder(const RootFinder & ) = delete;
90 RootFinder & operator = (const RootFinder & rhs) = delete;
91 RootFinder(RootFinder && ) = delete;
92 RootFinder & operator = (RootFinder && rhs) = delete;
93
95
96 /**
97 Provide to the solver the function and the initial search interval [xlow, xup]
98 for algorithms not using derivatives (bracketing algorithms)
99 The templated function f must be of a type implementing the \a operator() method,
100 <em> double operator() ( double x ) </em>
101 Returns non zero if interval is not valid (i.e. does not contains a root)
102 */
103
104 bool SetFunction( const IGenFunction & f, double xlow, double xup) {
105 return fSolver->SetFunction( f, xlow, xup);
106 }
107
108
109 /**
110 Provide to the solver the function and an initial estimate of the root,
111 for algorithms using derivatives.
112 The templated function f must be of a type implementing the \a operator()
113 and the \a Gradient() methods.
114 <em> double operator() ( double x ) </em>
115 Returns non zero if starting point is not valid
116 */
117
118 bool SetFunction( const IGradFunction & f, double xstart) {
119 return fSolver->SetFunction( f, xstart);
120 }
121
122 template<class Function, class Derivative>
123 bool Solve(Function &f, Derivative &d, double start,
124 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
125
126 template<class Function>
127 bool Solve(Function &f, double min, double max,
128 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
129
130 /**
131 Compute the roots iterating until the estimate of the Root is within the required tolerance returning
132 the iteration Status
133 */
134 bool Solve( int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) {
135 return fSolver->Solve( maxIter, absTol, relTol );
136 }
137
138 /**
139 Return the number of iteration performed to find the Root.
140 */
141 int Iterations() const {
142 return fSolver->Iterations();
143 }
144
145 /**
146 Perform a single iteration and return the Status
147 */
148 int Iterate() {
149 return fSolver->Iterate();
150 }
151
152 /**
153 Return the current and latest estimate of the Root
154 */
155 double Root() const {
156 return fSolver->Root();
157 }
158
159 /**
160 Return the status of the last estimate of the Root
161 = 0 OK, not zero failure
162 */
163 int Status() const {
164 return fSolver->Status();
165 }
166
167
168 /**
169 Return the current and latest estimate of the lower value of the Root-finding interval (for bracketing algorithms)
170 */
171/* double XLower() const { */
172/* return fSolver->XLower(); */
173/* } */
174
175 /**
176 Return the current and latest estimate of the upper value of the Root-finding interval (for bracketing algorithms)
177 */
178/* double XUpper() const { */
179/* return fSolver->XUpper(); */
180/* } */
181
182 /**
183 Get Name of the Root-finding solver algorithm
184 */
185 const char * Name() const {
186 return fSolver->Name();
187 }
188
189
190 protected:
191
192
193 private:
194
195 IRootFinderMethod* fSolver; // type of algorithm to be used
196
197
198 };
199
200 } // namespace Math
201} // namespace ROOT
202
203
204#include "Math/WrappedFunction.h"
205
206#include "Math/Functor.h"
207
208/**
209 * Solve `f(x) = 0`, given a derivative `d`.
210 * @param f Function whose root should be found.
211 * @param d Derivative of the function.
212 * @param start Starting point for iteration.
213 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
214 * @param absTol Absolute tolerance, as in Solve(int,double,double)
215 * @param relTol Relative tolerance, passed to Solve(int,double,double)
216 * @return true if a root was found. Retrieve the result using Root().
217 */
218template<class Function, class Derivative>
219bool ROOT::Math::RootFinder::Solve(Function &f, Derivative &d, double start,
220 int maxIter, double absTol, double relTol)
221{
222 if (!fSolver) return false;
224 bool ret = fSolver->SetFunction(wf, start);
225 if (!ret) return false;
226 return Solve(maxIter, absTol, relTol);
227}
228
229/**
230 * Solve `f(x) = 0` numerically.
231 * @param f Function whose root should be found.
232 * @param min Minimum allowed value of `x`.
233 * @param max Maximum allowed value of `x`.
234 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
235 * @param absTol Absolute tolerance, as in Solve(int,double,double)
236 * @param relTol Relative tolerance, passed to Solve(int,double,double)
237 * @return true if a root was found. Retrieve the result using Root().
238 */
239template<class Function>
240bool ROOT::Math::RootFinder::Solve(Function &f, double min, double max,
241 int maxIter, double absTol, double relTol)
242{
243 if (!fSolver) return false;
245 bool ret = fSolver->SetFunction(wf, min, max);
246 if (!ret) return false;
247 return Solve(maxIter, absTol, relTol);
248}
249
250#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:134
int Iterations() const
Return the number of iteration performed to find the Root.
Definition RootFinder.h:141
RootFinder(const RootFinder &)=delete
const char * Name() const
Return the current and latest estimate of the lower value of the Root-finding interval (for bracketin...
Definition RootFinder.h:185
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:104
IRootFinderMethod * fSolver
Definition RootFinder.h:195
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:118
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:219
RootFinder & operator=(const RootFinder &rhs)=delete
double Root() const
Return the current and latest estimate of the Root.
Definition RootFinder.h:155
int Iterate()
Perform a single iteration and return the Status.
Definition RootFinder.h:148
RootFinder(RootFinder &&)=delete
int Status() const
Return the status of the last estimate of the Root = 0 OK, not zero failure.
Definition RootFinder.h:163
Template class to wrap any C++ callable object which takes one argument i.e.
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...