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 ROOT Foundation, CERN/PH-SFT *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License *
10 * as published by the Free Software Foundation; either version 2 *
11 * of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this library (see file COPYING); if not, write *
20 * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21 * 330, Boston, MA 02111-1307 USA, or contact the author. *
22 * *
23 **********************************************************************/
24
25// Header file for class RootFinder
26//
27// Created by: moneta at Sun Nov 14 16:59:55 2004
28//
29// Last update: Sun Nov 14 16:59:55 2004
30//
31#ifndef ROOT_Math_RootFinder
32#define ROOT_Math_RootFinder
33
34
35#include "Math/IFunctionfwd.h"
36
38
39
40/**
41 @defgroup RootFinders One-dimensional Root-Finding
42 Classes implementing algorithms for finding the roots of a one-dimensional function.
43 Various implementations exist in MathCore and MathMore
44 The user interacts with a proxy class ROOT::Math::RootFinder which creates behing
45 the chosen algorithms which are implemented using the ROOT::Math::IRootFinderMethod interface
46
47 @ingroup NumAlgo
48*/
49
50
51namespace ROOT {
52 namespace Math {
53
54
55//_____________________________________________________________________________________
56 /**
57 User Class to find the Root of one dimensional functions.
58 The GSL Methods are implemented in MathMore and they are loaded automatically
59 via the plug-in manager
60
61 The possible types of Root-finding algorithms are:
62 <ul>
63 <li>Root Bracketing Algorithms which do not require function derivatives
64 <ol>
65 <li>RootFinder::kBRENT (default method implemented in MathCore)
66 <li>RootFinder::kGSL_BISECTION
67 <li>RootFinder::kGSL_FALSE_POS
68 <li>RootFinder::kGSL_BRENT
69 </ol>
70 <li>Root Finding Algorithms using Derivatives
71 <ol>
72 <li>RootFinder::kGSL_NEWTON
73 <li>RootFinder::kGSL_SECANT
74 <li>RootFinder::kGSL_STEFFENSON
75 </ol>
76 </ul>
77
78 This class does not cupport copying
79
80 @ingroup RootFinders
81
82 */
83
84 class RootFinder {
85
86 public:
87
88 enum EType { kBRENT, // Methods from MathCore
91 };
92
93 /**
94 Construct a Root-Finder algorithm
95 */
97 virtual ~RootFinder();
98
99 private:
100 // usually copying is non trivial, so we make this unaccessible
103 {
104 if (this == &rhs) return *this; // time saving self-test
105 return *this;
106 }
107
108 public:
109
111
112 /**
113 Provide to the solver the function and the initial search interval [xlow, xup]
114 for algorithms not using derivatives (bracketing algorithms)
115 The templated function f must be of a type implementing the \a operator() method,
116 <em> double operator() ( double x ) </em>
117 Returns non zero if interval is not valid (i.e. does not contains a root)
118 */
119
120 bool SetFunction( const IGenFunction & f, double xlow, double xup) {
121 return fSolver->SetFunction( f, xlow, xup);
122 }
123
124
125 /**
126 Provide to the solver the function and an initial estimate of the root,
127 for algorithms using derivatives.
128 The templated function f must be of a type implementing the \a operator()
129 and the \a Gradient() methods.
130 <em> double operator() ( double x ) </em>
131 Returns non zero if starting point is not valid
132 */
133
134 bool SetFunction( const IGradFunction & f, double xstart) {
135 return fSolver->SetFunction( f, xstart);
136 }
137
138 template<class Function, class Derivative>
139 bool Solve(Function &f, Derivative &d, double start,
140 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
141
142 template<class Function>
143 bool Solve(Function &f, double min, double max,
144 int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
145
146 /**
147 Compute the roots iterating until the estimate of the Root is within the required tolerance returning
148 the iteration Status
149 */
150 bool Solve( int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) {
151 return fSolver->Solve( maxIter, absTol, relTol );
152 }
153
154 /**
155 Return the number of iteration performed to find the Root.
156 */
157 int Iterations() const {
158 return fSolver->Iterations();
159 }
160
161 /**
162 Perform a single iteration and return the Status
163 */
164 int Iterate() {
165 return fSolver->Iterate();
166 }
167
168 /**
169 Return the current and latest estimate of the Root
170 */
171 double Root() const {
172 return fSolver->Root();
173 }
174
175 /**
176 Return the status of the last estimate of the Root
177 = 0 OK, not zero failure
178 */
179 int Status() const {
180 return fSolver->Status();
181 }
182
183
184 /**
185 Return the current and latest estimate of the lower value of the Root-finding interval (for bracketing algorithms)
186 */
187/* double XLower() const { */
188/* return fSolver->XLower(); */
189/* } */
190
191 /**
192 Return the current and latest estimate of the upper value of the Root-finding interval (for bracketing algorithms)
193 */
194/* double XUpper() const { */
195/* return fSolver->XUpper(); */
196/* } */
197
198 /**
199 Get Name of the Root-finding solver algorithm
200 */
201 const char * Name() const {
202 return fSolver->Name();
203 }
204
205
206 protected:
207
208
209 private:
210
211 IRootFinderMethod* fSolver; // type of algorithm to be used
212
213
214 };
215
216 } // namespace Math
217} // namespace ROOT
218
219
220#include "Math/WrappedFunction.h"
221
222#include "Math/Functor.h"
223
224/**
225 * Solve `f(x) = 0`, given a derivative `d`.
226 * @param f Function whose root should be found.
227 * @param d Derivative of the function.
228 * @param start Starting point for iteration.
229 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
230 * @param absTol Absolute tolerance, as in Solve(int,double,double)
231 * @param relTol Relative tolerance, passed to Solve(int,double,double)
232 * @return true if a root was found. Retrieve the result using Root().
233 */
234template<class Function, class Derivative>
235bool ROOT::Math::RootFinder::Solve(Function &f, Derivative &d, double start,
236 int maxIter, double absTol, double relTol)
237{
238 if (!fSolver) return false;
240 bool ret = fSolver->SetFunction(wf, start);
241 if (!ret) return false;
242 return Solve(maxIter, absTol, relTol);
243}
244
245/**
246 * Solve `f(x) = 0` numerically.
247 * @param f Function whose root should be found.
248 * @param min Minimum allowed value of `x`.
249 * @param max Maximum allowed value of `x`.
250 * @param maxIter Maximum number of iterations, passed to Solve(int,double,double)
251 * @param absTol Absolute tolerance, as in Solve(int,double,double)
252 * @param relTol Relative tolerance, passed to Solve(int,double,double)
253 * @return true if a root was found. Retrieve the result using Root().
254 */
255template<class Function>
256bool ROOT::Math::RootFinder::Solve(Function &f, double min, double max,
257 int maxIter, double absTol, double relTol)
258{
259 if (!fSolver) return false;
261 bool ret = fSolver->SetFunction(wf, min, max);
262 if (!ret) return false;
263 return Solve(maxIter, absTol, relTol);
264}
265
266#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:822
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:135
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:409
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:84
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:150
RootFinder & operator=(const RootFinder &rhs)
Definition RootFinder.h:102
int Iterations() const
Return the number of iteration performed to find the Root.
Definition RootFinder.h:157
const char * Name() const
Return the current and latest estimate of the lower value of the Root-finding interval (for bracketin...
Definition RootFinder.h:201
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:120
IRootFinderMethod * fSolver
Definition RootFinder.h:211
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:134
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:235
RootFinder(const RootFinder &)
Definition RootFinder.h:101
double Root() const
Return the current and latest estimate of the Root.
Definition RootFinder.h:171
int Iterate()
Perform a single iteration and return the Status.
Definition RootFinder.h:164
int Status() const
Return the status of the last estimate of the Root = 0 OK, not zero failure.
Definition RootFinder.h:179
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.