Logo ROOT   6.12/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 #include "Math/IFunctionfwd.h"
36 
37 #include "Math/IRootFinderMethod.h"
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 implementation esists 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 
51 namespace 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
101  RootFinder(const RootFinder & ) {}
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 template<class Function, class Derivative>
225 bool ROOT::Math::RootFinder::Solve(Function &f, Derivative &d, double start,
226  int maxIter, double absTol, double relTol)
227 {
228  if (!fSolver) return false;
229  ROOT::Math::GradFunctor1D wf(f, d);
230  bool ret = fSolver->SetFunction(wf, start);
231  if (!ret) return false;
232  return Solve(maxIter, absTol, relTol);
233 }
234 
235 template<class Function>
236 bool ROOT::Math::RootFinder::Solve(Function &f, double min, double max,
237  int maxIter, double absTol, double relTol)
238 {
239  if (!fSolver) return false;
241  bool ret = fSolver->SetFunction(wf, min, max);
242  if (!ret) return false;
243  return Solve(maxIter, absTol, relTol);
244 }
245 
246 #endif /* ROOT_Math_RootFinder */
User Class to find the Root of one dimensional functions.
Definition: RootFinder.h:84
double Root() const
Return the current and latest estimate of the Root.
Definition: RootFinder.h:171
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:135
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
GradFunctor1D class for one-dimensional gradient functions.
Definition: Functor.h:689
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:157
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition: IFunction.h:381
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:164
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
RootFinder(RootFinder::EType type=RootFinder::kBRENT)
Construct a Root-Finder algorithm.
Definition: RootFinder.cxx:37
RootFinder(const RootFinder &)
Definition: RootFinder.h:101
Double_t(* Function)(Double_t)
Definition: Functor.C:4
constexpr Double_t E()
Definition: TMath.h:74
RootFinder & operator=(const RootFinder &rhs)
Definition: RootFinder.h:102
bool SetMethod(RootFinder::EType type=RootFinder::kBRENT)
Definition: RootFinder.cxx:44
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:120
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:211
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:134
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:225
int Status() const
Return the status of the last estimate of the Root = 0 OK, not zero failure.
Definition: RootFinder.h:179
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