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