Logo ROOT   6.08/07
Reference Guide
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 #ifndef ROOT_Math_IFunctionfwd
36 #include "Math/IFunctionfwd.h"
37 #endif
38 
39 #ifndef ROOT_Math_IRootFinderMethod
40 #include "Math/IRootFinderMethod.h"
41 #endif
42 
43 
44 /**
45  @defgroup RootFinders One-dimensional Root-Finding
46  Classes implementing algorithms for finding the roots of a one-dimensional function.
47  Various implementation esists in MathCore and MathMore
48  The user interacts with a proxy class ROOT::Math::RootFinder which creates behing
49  the chosen algorithms which are implemented using the ROOT::Math::IRootFinderMethod interface
50 
51  @ingroup NumAlgo
52 */
53 
54 
55 namespace ROOT {
56  namespace Math {
57 
58 
59 //_____________________________________________________________________________________
60  /**
61  User Class to find the Root of one dimensional functions.
62  The GSL Methods are implemented in MathMore and they are loaded automatically
63  via the plug-in manager
64 
65  The possible types of Root-finding algorithms are:
66  <ul>
67  <li>Root Bracketing Algorithms which do not require function derivatives
68  <ol>
69  <li>RootFinder::kBRENT (default method implemented in MathCore)
70  <li>RootFinder::kGSL_BISECTION
71  <li>RootFinder::kGSL_FALSE_POS
72  <li>RootFinder::kGSL_BRENT
73  </ol>
74  <li>Root Finding Algorithms using Derivatives
75  <ol>
76  <li>RootFinder::kGSL_NEWTON
77  <li>RootFinder::kGSL_SECANT
78  <li>RootFinder::kGSL_STEFFENSON
79  </ol>
80  </ul>
81 
82  This class does not cupport copying
83 
84  @ingroup RootFinders
85 
86  */
87 
88  class RootFinder {
89 
90  public:
91 
92  enum EType { kBRENT, // Methods from MathCore
95  };
96 
97  /**
98  Construct a Root-Finder algorithm
99  */
101  virtual ~RootFinder();
102 
103  private:
104  // usually copying is non trivial, so we make this unaccessible
105  RootFinder(const RootFinder & ) {}
107  {
108  if (this == &rhs) return *this; // time saving self-test
109  return *this;
110  }
111 
112  public:
113 
115 
116  /**
117  Provide to the solver the function and the initial search interval [xlow, xup]
118  for algorithms not using derivatives (bracketing algorithms)
119  The templated function f must be of a type implementing the \a operator() method,
120  <em> double operator() ( double x ) </em>
121  Returns non zero if interval is not valid (i.e. does not contains a root)
122  */
123 
124  bool SetFunction( const IGenFunction & f, double xlow, double xup) {
125  return fSolver->SetFunction( f, xlow, xup);
126  }
127 
128 
129  /**
130  Provide to the solver the function and an initial estimate of the root,
131  for algorithms using derivatives.
132  The templated function f must be of a type implementing the \a operator()
133  and the \a Gradient() methods.
134  <em> double operator() ( double x ) </em>
135  Returns non zero if starting point is not valid
136  */
137 
138  bool SetFunction( const IGradFunction & f, double xstart) {
139  return fSolver->SetFunction( f, xstart);
140  }
141 
142  template<class Function, class Derivative>
143  bool Solve(Function &f, Derivative &d, double start,
144  int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
145 
146  template<class Function>
147  bool Solve(Function &f, double min, double max,
148  int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10);
149 
150  /**
151  Compute the roots iterating until the estimate of the Root is within the required tolerance returning
152  the iteration Status
153  */
154  bool Solve( int maxIter = 100, double absTol = 1E-8, double relTol = 1E-10) {
155  return fSolver->Solve( maxIter, absTol, relTol );
156  }
157 
158  /**
159  Return the number of iteration performed to find the Root.
160  */
161  int Iterations() const {
162  return fSolver->Iterations();
163  }
164 
165  /**
166  Perform a single iteration and return the Status
167  */
168  int Iterate() {
169  return fSolver->Iterate();
170  }
171 
172  /**
173  Return the current and latest estimate of the Root
174  */
175  double Root() const {
176  return fSolver->Root();
177  }
178 
179  /**
180  Return the status of the last estimate of the Root
181  = 0 OK, not zero failure
182  */
183  int Status() const {
184  return fSolver->Status();
185  }
186 
187 
188  /**
189  Return the current and latest estimate of the lower value of the Root-finding interval (for bracketing algorithms)
190  */
191 /* double XLower() const { */
192 /* return fSolver->XLower(); */
193 /* } */
194 
195  /**
196  Return the current and latest estimate of the upper value of the Root-finding interval (for bracketing algorithms)
197  */
198 /* double XUpper() const { */
199 /* return fSolver->XUpper(); */
200 /* } */
201 
202  /**
203  Get Name of the Root-finding solver algorithm
204  */
205  const char * Name() const {
206  return fSolver->Name();
207  }
208 
209 
210  protected:
211 
212 
213  private:
214 
215  IRootFinderMethod* fSolver; // type of algorithm to be used
216 
217 
218  };
219 
220  } // namespace Math
221 } // namespace ROOT
222 
223 
224 #ifndef ROOT_Math_WrappedFunction
225 #include "Math/WrappedFunction.h"
226 #endif
227 
228 #ifndef ROOT_Math_Functor
229 #include "Math/Functor.h"
230 #endif
231 
232 template<class Function, class Derivative>
233 bool ROOT::Math::RootFinder::Solve(Function &f, Derivative &d, double start,
234  int maxIter, double absTol, double relTol)
235 {
236  if (!fSolver) return false;
237  ROOT::Math::GradFunctor1D wf(f, d);
238  bool ret = fSolver->SetFunction(wf, start);
239  if (!ret) return false;
240  return Solve(maxIter, absTol, relTol);
241 }
242 
243 template<class Function>
244 bool ROOT::Math::RootFinder::Solve(Function &f, double min, double max,
245  int maxIter, double absTol, double relTol)
246 {
247  if (!fSolver) return false;
249  bool ret = fSolver->SetFunction(wf, min, max);
250  if (!ret) return false;
251  return Solve(maxIter, absTol, relTol);
252 }
253 
254 #endif /* ROOT_Math_RootFinder */
User Class to find the Root of one dimensional functions.
Definition: RootFinder.h:88
double Root() const
Return the current and latest estimate of the Root.
Definition: RootFinder.h:175
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
const double absTol
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
GradFunctor1D class for one-dimensional gradient functions.
Definition: Functor.h:709
virtual double Root() const =0
Returns the previously calculated root.
int Iterations() const
Return the number of iteration performed to find the Root.
Definition: RootFinder.h:161
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition: IFunction.h:382
Template class to wrap any C++ callable object which takes one argument i.e.
Interface for finding function roots of one-dimensional functions.
int Iterate()
Perform a single iteration and return the Status.
Definition: RootFinder.h:168
const char * Name() const
Return the current and latest estimate of the lower value of the Root-finding interval (for bracketin...
Definition: RootFinder.h:205
RootFinder(RootFinder::EType type=RootFinder::kBRENT)
Construct a Root-Finder algorithm.
Definition: RootFinder.cxx:39
RootFinder(const RootFinder &)
Definition: RootFinder.h:105
Double_t E()
Definition: TMath.h:54
RootFinder & operator=(const RootFinder &rhs)
Definition: RootFinder.h:106
double f(double x)
bool SetMethod(RootFinder::EType type=RootFinder::kBRENT)
Definition: RootFinder.cxx:46
int type
Definition: TGX11.cxx:120
virtual int Iterations() const
Return number of iterations used to find the root Must be implemented by derived classes.
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:124
virtual bool SetFunction(const ROOT::Math::IGradFunction &, double)
Sets the function for algorithms using derivatives.
virtual bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10)=0
Stimates the root for the function.
IRootFinderMethod * fSolver
Definition: RootFinder.h:215
Namespace for new Math classes and functions.
virtual const char * Name() const =0
Return name of root finder algorithm.
virtual int Status() const =0
Returns the status of the previous estimate.
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:138
virtual int Iterate()
This method is implemented only by the GSLRootFinder and GSLRootFinderDeriv classes and will return a...
bool Solve(Function &f, Derivative &d, double start, int maxIter=100, double absTol=1E-8, double relTol=1E-10)
Definition: RootFinder.h:233
int Status() const
Return the status of the last estimate of the Root = 0 OK, not zero failure.
Definition: RootFinder.h:183
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:154