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 <li>RootFinder::kMODAB
59 </ol>
60 <li>Root Finding Algorithms using Derivatives
61 <ol>
62 <li>RootFinder::kGSL_NEWTON
63 <li>RootFinder::kGSL_SECANT
64 <li>RootFinder::kGSL_STEFFENSON
65 </ol>
66 </ul>
67
68 This class does not cupport copying
69
70 @ingroup RootFinders
71
72 */
73
74 class RootFinder {
75
76 public:
77
78 enum EType {kBRENT, // Methods from MathCore
81 kMODAB // Modified A&B method added in MathCore
82 };
83
84 /**
85 Construct a Root-Finder algorithm
86 */
88 virtual ~RootFinder();
89
90 // usually copying is non trivial, so we delete this
91 RootFinder(const RootFinder & ) = delete;
92 RootFinder & operator = (const RootFinder & rhs) = delete;
93 RootFinder(RootFinder && ) = delete;
95
97
98 /**
99 Provide to the solver the function and the initial search interval [xlow, xup]
100 for algorithms not using derivatives (bracketing algorithms)
101 The templated function f must be of a type implementing the \a operator() method,
102 <em> double operator() ( double x ) </em>
103 Returns non zero if interval is not valid (i.e. does not contains a root)
104 */
105
106 bool SetFunction( const IGenFunction & f, double xlow, double xup) {
107 return fSolver->SetFunction( f, xlow, xup);
108 }
109
110
111 /**
112 Provide to the solver the function and an initial estimate of the root,
113 for algorithms using derivatives.
114 The templated function f must be of a type implementing the \a operator()
115 and the \a Gradient() methods.
116 <em> double operator() ( double x ) </em>
117 Returns non zero if starting point is not valid
118 */
119
120 bool SetFunction( const IGradFunction & f, double xstart) {
121 return fSolver->SetFunction( f, xstart);
122 }
123
124 template<class Function, class Derivative>
125 bool Solve(Function &f, Derivative &d, double start,
126 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
127
128 template<class Function>
129 bool Solve(Function &f, double min, double max,
130 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
131
132 /**
133 Compute the roots iterating until the estimate of the Root is within the required tolerance returning
134 the iteration Status
135 */
136 bool Solve( int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) {
137 return fSolver->Solve( maxIter, absTol, relTol );
138 }
139
140 /**
141 Return the number of iteration performed to find the Root.
142 */
143 int Iterations() const {
144 return fSolver->Iterations();
145 }
146
147 /**
148 Perform a single iteration and return the Status
149 */
150 int Iterate() {
151 return fSolver->Iterate();
152 }
153
154 /**
155 Return the current and latest estimate of the Root
156 */
157 double Root() const {
158 return fSolver->Root();
159 }
160
161 /**
162 Return the status of the last estimate of the Root
163 = 0 OK, not zero failure
164 */
165 int Status() const {
166 return fSolver->Status();
167 }
168
169
170 /**
171 Return the current and latest estimate of the lower value of the Root-finding interval (for bracketing algorithms)
172 */
173/* double XLower() const { */
174/* return fSolver->XLower(); */
175/* } */
176
177 /**
178 Return the current and latest estimate of the upper value of the Root-finding interval (for bracketing algorithms)
179 */
180/* double XUpper() const { */
181/* return fSolver->XUpper(); */
182/* } */
183
184 /**
185 Get Name of the Root-finding solver algorithm
186 */
187 const char * Name() const {
188 return fSolver->Name();
189 }
190
191
192 protected:
193
194
195 private:
196
197 IRootFinderMethod* fSolver; // type of algorithm to be used
198
199
200 };
201
202 } // namespace Math
203} // namespace ROOT
204
205
206#include "Math/WrappedFunction.h"
207
208#include "Math/Functor.h"
209
210/**
211 * Solve `f(x) = 0`, given a derivative `d`.
212 * @param f Function whose root should be found.
213 * @param d Derivative of the function.
214 * @param start Starting point for iteration.
215 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
216 * @param absTol Absolute tolerance, as in Solve(int,double,double)
217 * @param relTol Relative tolerance, passed to Solve(int,double,double)
218 * @return true if a root was found. Retrieve the result using Root().
219 */
220template<class Function, class Derivative>
221bool ROOT::Math::RootFinder::Solve(Function &f, Derivative &d, double start,
222 int maxIter, double absTol, double relTol)
223{
224 if (!fSolver) return false;
226 bool ret = fSolver->SetFunction(wf, start);
227 if (!ret) return false;
228 return Solve(maxIter, absTol, relTol);
229}
230
231/**
232 * Solve `f(x) = 0` numerically.
233 * @param f Function whose root should be found.
234 * @param min Minimum allowed value of `x`.
235 * @param max Maximum allowed value of `x`.
236 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
237 * @param absTol Absolute tolerance, as in Solve(int,double,double)
238 * @param relTol Relative tolerance, passed to Solve(int,double,double)
239 * @return true if a root was found. Retrieve the result using Root().
240 */
241template<class Function>
242bool ROOT::Math::RootFinder::Solve(Function &f, double min, double max,
243 int maxIter, double absTol, double relTol)
244{
245 if (!fSolver) return false;
247 bool ret = fSolver->SetFunction(wf, min, max);
248 if (!ret) return false;
249 return Solve(maxIter, absTol, relTol);
250}
251
252#endif /* ROOT_Math_RootFinder */
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:271
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:157
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:262
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:74
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:136
int Iterations() const
Return the number of iteration performed to find the Root.
Definition RootFinder.h:143
RootFinder(const RootFinder &)=delete
RootFinder(RootFinder::EType type=RootFinder::kBRENT)
Construct a Root-Finder algorithm.
const char * Name() const
Return the current and latest estimate of the lower value of the Root-finding interval (for bracketin...
Definition RootFinder.h:187
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:106
IRootFinderMethod * fSolver
Definition RootFinder.h:197
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:120
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:221
RootFinder & operator=(const RootFinder &rhs)=delete
double Root() const
Return the current and latest estimate of the Root.
Definition RootFinder.h:157
int Iterate()
Perform a single iteration and return the Status.
Definition RootFinder.h:150
RootFinder(RootFinder &&)=delete
int Status() const
Return the status of the last estimate of the Root = 0 OK, not zero failure.
Definition RootFinder.h:165
Template class to wrap any C++ callable object which takes one argument i.e.
Namespace for new Math classes and functions.