Logo ROOT   6.10/09
Reference Guide
IFunction.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: L. Moneta 11/2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for function interfaces
12 //
13 // Generic Interfaces for one or multi-dimensional functions
14 //
15 // Created by: Lorenzo Moneta : Wed Nov 13 2006
16 //
17 //
18 #ifndef ROOT_Math_IFunction
19 #define ROOT_Math_IFunction
20 
21 /**
22 @defgroup CppFunctions Function Classes and Interfaces
23 
24  Interfaces (abstract classes) and Base classes used in MathCore and MathMore numerical methods
25  for describing function classes. They define function and gradient evaluation and as well the
26  functionality for dealing with parameters in the case of parametric functions which are used for
27  fitting and data modeling.
28  Included are also adapter classes, such as functors, to wrap generic callable C++ objects
29  in the desired interface.
30 
31 @ingroup MathCore
32 */
33 
34 //typedefs and tags definitions
35 #include "Math/IFunctionfwd.h"
36 
37 
38 namespace ROOT {
39  namespace Math {
40 
41  /**
42  @defgroup GenFunc Generic Function Evaluation Interfaces
43  Interface classes for evaluation of function object classes in one or multi-dimensions.
44  @ingroup CppFunctions
45  */
46 
47 //___________________________________________________________________________________
48  /**
49  Documentation for the abstract class IBaseFunctionMultiDim.
50  Interface (abstract class) for generic functions objects of multi-dimension
51  Provides a method to evaluate the function given a vector of coordinate values,
52  by implementing operator() (const double *).
53  In addition it defines the interface for copying functions via the pure virtual method Clone()
54  and the interface for getting the function dimension via the NDim() method.
55  Derived classes must implement the pure private virtual method DoEval(const double *) for the
56  function evaluation in addition to NDim() and Clone().
57 
58  @ingroup GenFunc
59  */
60 
61  template<class T>
63 
64  public:
65 
67 
68 
70 
71  /**
72  virtual destructor
73  */
75 
76  /**
77  Clone a function.
78  Each derived class must implement their version of the Clone method
79  */
80  virtual IBaseFunctionMultiDimTempl<T> *Clone() const = 0;
81 
82  /**
83  Retrieve the dimension of the function
84  */
85  virtual unsigned int NDim() const = 0;
86 
87  /**
88  Evaluate the function at a point x[].
89  Use the pure virtual private method DoEval which must be implemented by the sub-classes
90  */
91  T operator()(const T *x) const
92  {
93  return DoEval(x);
94  }
95 
96 #ifdef LATER
97  /**
98  Template method to eveluate the function using the begin of an iterator
99  User is responsible to provide correct size for the iterator
100  */
101  template <class Iterator>
102  T operator()(const Iterator it) const
103  {
104  return DoEval(&(*it));
105  }
106 #endif
107 
108 
109  private:
110 
111 
112  /**
113  Implementation of the evaluation function. Must be implemented by derived classes
114  */
115  virtual T DoEval(const T *x) const = 0;
116 
117 
118  };
119 
120 
121 //___________________________________________________________________________________
122  /**
123  Interface (abstract class) for generic functions objects of one-dimension
124  Provides a method to evaluate the function given a value (simple double)
125  by implementing operator() (const double ).
126  In addition it defines the interface for copying functions via the pure virtual method Clone().
127  Derived classes must implement the pure virtual private method DoEval(double ) for the
128  function evaluation in addition to Clone().
129  An interface for evaluating the function passing a vector (like for multidim functions) is also
130  provided
131 
132  @ingroup GenFunc
133  */
135 
136  public:
137 
139 
141 
142  /**
143  virtual destructor
144  */
145  virtual ~IBaseFunctionOneDim() {}
146 
147  /**
148  Clone a function.
149  Each derived class will implement their version of the provate DoClone method
150  */
151  virtual IBaseFunctionOneDim *Clone() const = 0;
152 
153  /**
154  Evaluate the function at a point x
155  Use the a pure virtual private method DoEval which must be implemented by sub-classes
156  */
157  double operator()(double x) const
158  {
159  return DoEval(x);
160  }
161 
162  /**
163  Evaluate the function at a point x[].
164  Compatible method with multi-dimensional functions
165  */
166  double operator()(const double *x) const
167  {
168  return DoEval(*x);
169  }
170 
171 
172 
173  private:
174 
175  // use private virtual inheritance
176 
177  /// implementation of the evaluation function. Must be implemented by derived classes
178  virtual double DoEval(double x) const = 0;
179 
180  };
181 
182 
183 //-------- GRAD functions---------------------------
184 
185 //___________________________________________________________________________________
186  /**
187  Gradient interface (abstract class) defining the signature for calculating the gradient of a
188  multi-dimensional function.
189  Three methods are provided:
190  - Gradient(const double *x, double * grad) evaluate the full gradient vector at the vector value x
191  - Derivative(const double * x, int icoord) evaluate the partial derivative for the icoord coordinate
192  - FdF(const double *x, double &f, double * g) evaluate at the same time gradient and function/
193 
194  Concrete classes should derive from ROOT::Math::IGradientFunctionMultiDim and not from this class.
195 
196  @ingroup GenFunc
197  */
198 
200 
201  public:
202 
203  /// virual destructor
204  virtual ~IGradientMultiDim() {}
205 
206  /**
207  Evaluate all the vector of function derivatives (gradient) at a point x.
208  Derived classes must re-implement if it is more efficient than evaluting one at a time
209  */
210  virtual void Gradient(const double *x, double *grad) const = 0;
211 
212  /**
213  Return the partial derivative with respect to the passed coordinate
214  */
215  double Derivative(const double *x, unsigned int icoord = 0) const
216  {
217  return DoDerivative(x, icoord);
218  }
219 
220 
221  /**
222  Optimized method to evaluate at the same time the function value and derivative at a point x.
223  Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
224  Derived class should implement this method if performances play an important role and if it is faster to
225  evaluate value and derivative at the same time
226 
227  */
228  virtual void FdF(const double *x, double &f, double *df) const = 0;
229 
230 
231  private:
232 
233 
234  /**
235  function to evaluate the derivative with respect each coordinate. To be implemented by the derived class
236  */
237  virtual double DoDerivative(const double *x, unsigned int icoord) const = 0;
238 
239  };
240 
241 //___________________________________________________________________________________
242  /**
243  Specialized Gradient interface(abstract class) for one dimensional functions
244  It provides a method to evaluate the derivative of the function, Derivative and a
245  method to evaluate at the same time the function and the derivative FdF
246 
247  Concrete classes should derive from ROOT::Math::IGradientFunctionOneDim and not from this class.
248 
249  @ingroup GenFunc
250  */
252 
253  public:
254 
255  /// virtual destructor
256  virtual ~IGradientOneDim() {}
257 
258  /**
259  Return the derivative of the function at a point x
260  Use the private method DoDerivative
261  */
262  double Derivative(double x) const
263  {
264  return DoDerivative(x);
265  }
266 
267 
268  /**
269  Optimized method to evaluate at the same time the function value and derivative at a point x.
270  Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
271  Derived class should implement this method if performances play an important role and if it is faster to
272  evaluate value and derivative at the same time
273 
274  */
275  virtual void FdF(double x, double &f, double &df) const = 0;
276 
277 
278  /**
279  Compatibility method with multi-dimensional interface for partial derivative
280  */
281  double Derivative(const double *x) const
282  {
283  return DoDerivative(*x);
284  }
285 
286  /**
287  Compatibility method with multi-dimensional interface for Gradient
288  */
289  void Gradient(const double *x, double *g) const
290  {
291  g[0] = DoDerivative(*x);
292  }
293 
294  /**
295  Compatibility method with multi-dimensional interface for Gradient and function evaluation
296  */
297  void FdF(const double *x, double &f, double *df) const
298  {
299  FdF(*x, f, *df);
300  }
301 
302 
303 
304  private:
305 
306 
307  /**
308  function to evaluate the derivative with respect each coordinate. To be implemented by the derived class
309  */
310  virtual double DoDerivative(double x) const = 0;
311 
312  };
313 
314 //___________________________________________________________________________________
315  /**
316  Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
317  It implements both the ROOT::Math::IBaseFunctionMultiDim and
318  ROOT::Math::IGradientMultiDim interfaces.
319  The method ROOT::Math::IFunction::Gradient calculates the full gradient vector,
320  ROOT::Math::IFunction::Derivative calculates the partial derivative for each coordinate and
321  ROOT::Math::Fdf calculates the gradient and the function value at the same time.
322  The pure private virtual method DoDerivative() must be implemented by the derived classes, while
323  Gradient and FdF are by default implemented using DoDerivative, butthey can be overloaded by the
324  derived classes to improve the efficiency in the derivative calculation.
325 
326  @ingroup GenFunc
327  */
328 
329 
331  virtual public IBaseFunctionMultiDim ,
332  public IGradientMultiDim {
333 
334 
335  public:
336 
339 
340 
341  /**
342  Virtual Destructor (no operations)
343  */
345 
346  /**
347  Evaluate all the vector of function derivatives (gradient) at a point x.
348  Derived classes must re-implement it if more efficient than evaluting one at a time
349  */
350  virtual void Gradient(const double *x, double *grad) const
351  {
352  unsigned int ndim = NDim();
353  for (unsigned int icoord = 0; icoord < ndim; ++icoord)
354  grad[icoord] = BaseGrad::Derivative(x, icoord);
355  }
356 
357  using BaseFunc::NDim;
358 
359 
360  /**
361  Optimized method to evaluate at the same time the function value and derivative at a point x.
362  Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
363  Derived class should implement this method if performances play an important role and if it is faster to
364  evaluate value and derivative at the same time
365 
366  */
367  virtual void FdF(const double *x, double &f, double *df) const
368  {
369  f = BaseFunc::operator()(x);
370  Gradient(x, df);
371  }
372 
373 
374  };
375 
376 
377 //___________________________________________________________________________________
378  /**
379  Interface (abstract class) for one-dimensional functions providing a gradient calculation.
380  It implements both the ROOT::Math::IBaseFunctionOneDim and
381  ROOT::Math::IGradientOneDim interfaces.
382  The method ROOT::Math::IFunction::Derivative calculates the derivative and
383  ROOT::Math::Fdf calculates the derivative and the function values at the same time.
384  The pure private virtual method DoDerivative() must be implemented by the derived classes, while
385  FdF is by default implemented using DoDerivative, but it can be overloaded by the
386  derived classes to improve the efficiency in the derivative calculation.
387 
388 
389  @ingroup GenFunc
390  */
391  //template <>
393  virtual public IBaseFunctionOneDim ,
394  public IGradientOneDim {
395 
396 
397  public:
398 
401 
402 
403  /**
404  Virtual Destructor (no operations)
405  */
407 
408 
409  /**
410  Optimized method to evaluate at the same time the function value and derivative at a point x.
411  Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
412  Derived class should implement this method if performances play an important role and if it is faster to
413  evaluate value and derivative at the same time
414 
415  */
416  virtual void FdF(double x, double &f, double &df) const
417  {
418  f = operator()(x);
419  df = Derivative(x);
420  }
421 
422 
423 
424  };
425 
426 
427 
428  } // namespace Math
429 } // namespace ROOT
430 
431 #endif /* ROOT_Math_IFunction */
Gradient interface (abstract class) defining the signature for calculating the gradient of a multi-di...
Definition: IFunction.h:199
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:330
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:134
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void FdF(const double *x, double &f, double *df) const
Compatibility method with multi-dimensional interface for Gradient and function evaluation.
Definition: IFunction.h:297
double T(double x)
Definition: ChebyshevPol.h:34
IBaseFunctionOneDim BaseFunc
Definition: IFunction.h:138
double Derivative(const double *x) const
Compatibility method with multi-dimensional interface for partial derivative.
Definition: IFunction.h:281
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
virtual void Gradient(const double *x, double *grad) const
Evaluate all the vector of function derivatives (gradient) at a point x.
Definition: IFunction.h:350
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition: IFunction.h:392
Specialized Gradient interface(abstract class) for one dimensional functions It provides a method to ...
Definition: IFunction.h:251
IBaseFunctionMultiDim BaseFunc
Definition: IFunction.h:337
Double_t x[n]
Definition: legend1.C:17
void Gradient(const double *x, double *g) const
Compatibility method with multi-dimensional interface for Gradient.
Definition: IFunction.h:289
virtual ~IBaseFunctionMultiDimTempl()
virtual destructor
Definition: IFunction.h:74
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
virtual void FdF(double x, double &f, double &df) const
Optimized method to evaluate at the same time the function value and derivative at a point x...
Definition: IFunction.h:416
virtual ~IGradientFunctionMultiDim()
Virtual Destructor (no operations)
Definition: IFunction.h:344
IBaseFunctionOneDim BaseFunc
Definition: IFunction.h:399
double operator()(const double *x) const
Evaluate the function at a point x[].
Definition: IFunction.h:166
double Derivative(const double *x, unsigned int icoord=0) const
Return the partial derivative with respect to the passed coordinate.
Definition: IFunction.h:215
virtual ~IGradientOneDim()
virtual destructor
Definition: IFunction.h:256
IBaseFunctionMultiDimTempl< T > BaseFunc
Definition: IFunction.h:66
double f(double x)
virtual void FdF(const double *x, double &f, double *df) const
Optimized method to evaluate at the same time the function value and derivative at a point x...
Definition: IFunction.h:367
virtual T DoEval(const T *x) const =0
Implementation of the evaluation function.
Namespace for new Math classes and functions.
virtual ~IGradientFunctionOneDim()
Virtual Destructor (no operations)
Definition: IFunction.h:406
virtual ~IBaseFunctionOneDim()
virtual destructor
Definition: IFunction.h:145
virtual ~IGradientMultiDim()
virual destructor
Definition: IFunction.h:204
double Derivative(double x) const
Return the derivative of the function at a point x Use the private method DoDerivative.
Definition: IFunction.h:262
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
double operator()(double x) const
Evaluate the function at a point x Use the a pure virtual private method DoEval which must be impleme...
Definition: IFunction.h:157
T operator()(const T *x) const
Evaluate the function at a point x[].
Definition: IFunction.h:91