Logo ROOT   6.08/07
Reference Guide
Derivator.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 Derivator
26 //
27 // class for calculating Derivative of functions
28 //
29 // Created by: moneta at Sat Nov 13 14:46:00 2004
30 //
31 // Last update: Sat Nov 13 14:46:00 2004
32 //
33 #ifndef ROOT_Math_Derivator
34 #define ROOT_Math_Derivator
35 
36 
37 #ifndef ROOT_Math_IFunctionfwd
38 #include "Math/IFunctionfwd.h"
39 #endif
40 
41 #ifndef ROOT_Math_IParamFunctionfwd
42 #include "Math/IParamFunctionfwd.h"
43 #endif
44 
45 
46 namespace ROOT {
47 namespace Math {
48 
49 
50 
51 class GSLDerivator;
52 
53 //_______________________________________________________________________
54 /**
55  Class for computing numerical derivative of a function.
56  Presently this class is implemented only using the numerical derivatives
57  algorithms provided by GSL
58  using the implementation class ROOT::Math::GSLDerivator
59 
60  This class does not support copying
61 
62  @ingroup Deriv
63 */
64 
65 class Derivator {
66 
67 public:
68 
69  /**
70  signature for function pointers used by GSL
71  */
72  typedef double ( * GSLFuncPointer ) ( double, void * );
73 
74  /**
75  Empty Construct for a Derivator class
76  Need to set the function afterwards with Derivator::SetFunction
77  */
78  Derivator();
79  /**
80  Construct using a ROOT::Math::IGenFunction interface
81  */
82  explicit Derivator(const IGenFunction &f);
83  /**
84  Construct using a GSL function pointer type
85  @param f : free function pointer of the GSL required type
86  @param p : pointer to the object carrying the function state
87  (for example the function object itself)
88  */
89  explicit Derivator(const GSLFuncPointer &f, void * p = 0);
90 
91  /// destructor
92  virtual ~Derivator();
93 
94  // disable copying
95 private:
96 
97  Derivator(const Derivator &);
98  Derivator & operator = (const Derivator &);
99 
100 public:
101 
102 
103 #ifdef LATER
104  /**
105  Template methods for generic functions
106  Set the function f for evaluating the derivative.
107  The function type must implement the assigment operator,
108  <em> double operator() ( double x ) </em>
109  */
110  template <class UserFunc>
111  inline void SetFunction(const UserFunc &f) {
112  const void * p = &f;
113  SetFunction( &GSLFunctionAdapter<UserFunc>::F, const_cast<void *>(p) );
114  }
115 #endif
116 
117  /**
118  Set the function for calculating the derivatives.
119  The function must implement the ROOT::Math::IGenFunction signature
120  */
121  void SetFunction(const IGenFunction &f);
122 
123 
124  /**
125  Set the function f for evaluating the derivative using a GSL function pointer type
126  @param f : free function pointer of the GSL required type
127  @param p : pointer to the object carrying the function state
128  (for example the function object itself)
129  */
130  void SetFunction( const GSLFuncPointer &f, void * p = 0);
131 
132 
133 
134  /**
135  Computes the numerical derivative of a function f at a point x.
136  It uses Derivator::EvalCentral to compute the derivative using an
137  adaptive central difference algorithm with a step size h
138  */
139 
140  double Eval(double x, double h = 1E-8) const;
141 
142 
143 
144  /**
145  Computes the numerical derivative at a point x using an adaptive central
146  difference algorithm with a step size h.
147  */
148  double EvalCentral( double x, double h = 1E-8) const;
149 
150  /**
151  Computes the numerical derivative at a point x using an adaptive forward
152  difference algorithm with a step size h.
153  The function is evaluated only at points greater than x and at x itself.
154  */
155  double EvalForward( double x, double h = 1E-8) const;
156 
157  /**
158  Computes the numerical derivative at a point x using an adaptive backward
159  difference algorithm with a step size h.
160  The function is evaluated only at points less than x and at x itself.
161  */
162  double EvalBackward( double x, double h = 1E-8) const;
163 
164  /** @name --- Static methods ---
165  This methods don't require to use a Derivator object, and are designed to be used in
166  fast calculation. Error and status code cannot be retrieved in this case
167  */
168 
169  /**
170  Computes the numerical derivative of a function f at a point x.
171  It uses Derivator::EvalCentral to compute the derivative using an
172  adaptive central difference algorithm with a step size h
173  */
174  static double Eval(const IGenFunction & f, double x, double h = 1E-8);
175 
176  /**
177  Computes the numerical derivative of a function f at a point x using an adaptive central
178  difference algorithm with a step size h
179  */
180  static double EvalCentral(const IGenFunction & f, double x, double h = 1E-8);
181 
182 
183  /**
184  Computes the numerical derivative of a function f at a point x using an adaptive forward
185  difference algorithm with a step size h.
186  The function is evaluated only at points greater than x and at x itself
187  */
188  static double EvalForward(const IGenFunction & f, double x, double h = 1E-8);
189 
190  /**
191  Computes the numerical derivative of a function f at a point x using an adaptive backward
192  difference algorithm with a step size h.
193  The function is evaluated only at points less than x and at x itself
194  */
195  static double EvalBackward(const IGenFunction & f, double x, double h = 1E-8);
196 
197  // Derivatives for multi-dimension functions
198  /**
199  Evaluate the partial derivative of a multi-dim function
200  with respect coordinate x_icoord at the point x[]
201  */
202  static double Eval(const IMultiGenFunction & f, const double * x, unsigned int icoord = 0, double h = 1E-8);
203 
204  /**
205  Evaluate the derivative with respect a parameter for one-dim parameteric function
206  at the point ( x,p[]) with respect the parameter p_ipar
207  */
208  static double Eval(IParamFunction & f, double x, const double * p, unsigned int ipar = 0, double h = 1E-8);
209 
210  /**
211  Evaluate the derivative with respect a parameter for a multi-dim parameteric function
212  at the point ( x[],p[]) with respect the parameter p_ipar
213  */
214  static double Eval(IParamMultiFunction & f, const double * x, const double * p, unsigned int ipar = 0, double h = 1E-8);
215 
216 
217  /**
218  return the error status of the last derivative calculation
219  */
220  int Status() const;
221 
222  /**
223  return the result of the last derivative calculation
224  */
225  double Result() const;
226 
227  /**
228  return the estimate of the absolute error of the last derivative calculation
229  */
230  double Error() const;
231 
232 
233 private:
234 
235 
237 
238 };
239 
240 
241 
242 
243 } // namespace Math
244 } // namespace ROOT
245 
246 
247 #endif /* ROOT_Math_Derivator */
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
Class for computing numerical derivative of a function.
Definition: Derivator.h:65
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
GSLDerivator * fDerivator
Definition: Derivator.h:236
Derivator & operator=(const Derivator &)
Definition: Derivator.cxx:76
TH1 * h
Definition: legend2.C:5
Class for computing numerical derivative of a function based on the GSL numerical algorithm This clas...
Definition: GSLDerivator.h:62
Double_t x[n]
Definition: legend1.C:17
double Result() const
return the result of the last derivative calculation
Definition: Derivator.cxx:152
virtual ~Derivator()
destructor
Definition: Derivator.cxx:66
int Status() const
return the error status of the last derivative calculation
Definition: Derivator.cxx:156
double EvalCentral(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive central difference algorithm with a ...
Definition: Derivator.cxx:97
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
void SetFunction(const IGenFunction &f)
Set the function for calculating the derivatives.
Definition: Derivator.cxx:84
Class for adapting any C++ functor class to C function pointers used by GSL.
double Eval(double x, double h=1E-8) const
Computes the numerical derivative of a function f at a point x.
Definition: Derivator.cxx:93
Double_t E()
Definition: TMath.h:54
double f(double x)
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
Namespace for new Math classes and functions.
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
double EvalForward(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive forward difference algorithm with a ...
Definition: Derivator.cxx:101
Derivator()
Empty Construct for a Derivator class Need to set the function afterwards with Derivator::SetFunction...
Definition: Derivator.cxx:47
double EvalBackward(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive backward difference algorithm with a...
Definition: Derivator.cxx:105
double Error() const
return the estimate of the absolute error of the last derivative calculation
Definition: Derivator.cxx:154
double(* GSLFuncPointer)(double, void *)
signature for function pointers used by GSL
Definition: Derivator.h:72