Logo ROOT   6.10/09
Reference Guide
IParamFunction.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Tue Nov 14 14:20:07 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class IParamFunction
12 
13 #ifndef ROOT_Math_IParamFunction
14 #define ROOT_Math_IParamFunction
15 
16 #include "Math/IFunction.h"
17 
18 #include "Math/IParamFunctionfwd.h"
19 
20 #include "Math/Util.h"
21 
22 
23 #include <cassert>
24 
25 /**
26  @defgroup ParamFunc Parameteric Function Evaluation Interfaces.
27  Interfaces classes for evaluation of parametric functions
28  @ingroup CppFunctions
29 */
30 
31 
32 namespace ROOT {
33 
34  namespace Math {
35 
36 
37 //___________________________________________________________________
38  /**
39  Documentation for the abstract class IBaseParam.
40  It defines the interface for dealing with the function parameters
41  This is used only for internal convinience, to avoid redefining the Parameter API
42  for the one and the multi-dim functions.
43  Concrete class should derive from ROOT::Math::IParamFunction and not from this class.
44 
45  @ingroup ParamFunc
46  */
47 
48  class IBaseParam {
49 
50  public:
51 
52 
53  /**
54  Virtual Destructor (no operations)
55  */
56  virtual ~IBaseParam() {}
57 
58 
59  /**
60  Access the parameter values
61  */
62  virtual const double *Parameters() const = 0;
63 
64  /**
65  Set the parameter values
66  @param p vector of doubles containing the parameter values.
67 
68  to be defined: can user change number of params ? At the moment no.
69 
70  */
71  virtual void SetParameters(const double *p) = 0;
72 
73 
74  /**
75  Return the number of Parameters
76  */
77  virtual unsigned int NPar() const = 0;
78 
79  /**
80  Return the name of the i-th parameter (starting from zero)
81  Overwrite if want to avoid the default name ("Par_0, Par_1, ...")
82  */
83  virtual std::string ParameterName(unsigned int i) const
84  {
85  assert(i < NPar());
86  return "Par_" + Util::ToString(i);
87  }
88 
89 
90  };
91 
92 //___________________________________________________________________
93  /**
94  IParamFunction interface (abstract class) describing multi-dimensional parameteric functions
95  It is a derived class from ROOT::Math::IBaseFunctionMultiDim and
96  ROOT::Math::IBaseParam
97 
98  Provides the interface for evaluating a function passing a coordinate vector and a parameter vector.
99 
100  @ingroup ParamFunc
101  */
102 
103  template<class T>
105  virtual public IBaseParam {
106  public:
107 
109 
110  /**
111  Evaluate function at a point x and for given parameters p.
112  This method does not change the internal status of the function (internal parameter values).
113  If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be
114  called.
115  Use the pure virtual function DoEvalPar to implement it
116  */
117 
118  T operator()(const T *x, const double *p) const
119  {
120  return DoEvalPar(x, p);
121  }
122 
123  using BaseFunc::operator();
124 
125  private:
126  /**
127  Implementation of the evaluation function using the x values and the parameters.
128  Must be implemented by derived classes
129  */
130  virtual T DoEvalPar(const T *x, const double *p) const = 0;
131 
132  /**
133  Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values
134  */
135  virtual T DoEval(const T *x) const
136  {
137  return DoEvalPar(x, Parameters());
138  }
139  };
140 
141 
142 //___________________________________________________________________
143  /**
144  Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions
145  It is a derived class from ROOT::Math::IBaseFunctionOneDim and
146  ROOT::Math::IBaseParam
147 
148  @ingroup ParamFunc
149  */
150 
152  virtual public IBaseFunctionOneDim,
153  public IBaseParam {
154 
155 
156  public:
157 
159 
160 
161  using BaseFunc::operator();
162 
163  /**
164  Evaluate function at a point x and for given parameters p.
165  This method does not change the internal status of the function (internal parameter values).
166  If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be
167  called.
168  Use the pure virtual function DoEvalPar to implement it
169  */
170  double operator()(double x, const double *p) const
171  {
172  return DoEvalPar(x, p);
173  }
174 
175 
176  /**
177  multidim-like interface
178  */
179  double operator()(const double *x, const double *p) const
180  {
181  return DoEvalPar(*x, p);
182  }
183 
184  private:
185 
186  /**
187  Implementation of the evaluation function using the x value and the parameters.
188  Must be implemented by derived classes
189  */
190  virtual double DoEvalPar(double x, const double *p) const = 0;
191 
192  /**
193  Implement the ROOT::Math::IBaseFunctionOneDim interface DoEval(x) using the cached parameter values
194  */
195  virtual double DoEval(double x) const
196  {
197  return DoEvalPar(x, Parameters());
198  }
199 
200  };
201 
202 
203 
204 //_______________________________________________________________________________
205  /**
206  Interface (abstract class) for parametric gradient multi-dimensional functions providing
207  in addition to function evaluation with respect to the coordinates
208  also the gradient with respect to the parameters, via the method ParameterGradient.
209 
210  It is a derived class from ROOT::Math::IParametricFunctionMultiDim.
211 
212  The pure private virtual method DoParameterGradient must be implemented by the derived classes
213  in addition to those inherited by the base abstract classes.
214 
215  @ingroup ParamFunc
216  */
217 
218  template<class T>
220  public:
221 
225 
226 
227  /**
228  Virtual Destructor (no operations)
229  */
231 
232 
233 
234  using BaseParamFunc::operator();
235 
236  /**
237  Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at a point x.
238  It is optional to be implemented by the derived classes for better efficiency
239  */
240  virtual void ParameterGradient(const double *x , const double *p, double *grad) const
241  {
242  unsigned int npar = NPar();
243  for (unsigned int ipar = 0; ipar < npar; ++ipar)
244  grad[ipar] = DoParameterDerivative(x, p, ipar);
245  }
246 
247  /**
248  Evaluate the partial derivative w.r.t a parameter ipar from values and parameters
249  */
250  double ParameterDerivative(const double *x, const double *p, unsigned int ipar = 0) const
251  {
252  return DoParameterDerivative(x, p, ipar);
253  }
254 
255  /**
256  Evaluate all derivatives using cached parameter values
257  */
258  void ParameterGradient(const double *x , double *grad) const
259  {
260  return ParameterGradient(x, Parameters(), grad);
261  }
262  /**
263  Evaluate partial derivative using cached parameter values
264  */
265  double ParameterDerivative(const double *x, unsigned int ipar = 0) const
266  {
267  return DoParameterDerivative(x, Parameters() , ipar);
268  }
269 
270  private:
271 
272  /**
273  Evaluate the partial derivative w.r.t a parameter ipar , to be implemented by the derived classes
274  */
275  virtual double DoParameterDerivative(const double *x, const double *p, unsigned int ipar) const = 0;
276 
277  };
278 
279 //_______________________________________________________________________________
280  /**
281  Interface (abstract class) for parametric one-dimensional gradient functions providing
282  in addition to function evaluation with respect the coordinates
283  also the gradient with respect to the parameters, via the method ParameterGradient.
284 
285  It is a derived class from ROOT::Math::IParametricFunctionOneDim.
286 
287  The pure private virtual method DoParameterGradient must be implemented by the derived classes
288  in addition to those inherited by the base abstract classes.
289 
290  @ingroup ParamFunc
291  */
292 
295 // ,public IGradientFunctionOneDim
296  {
297 
298  public:
299 
303 
304 
305  /**
306  Virtual Destructor (no operations)
307  */
309 
310 
311  using BaseParamFunc::operator();
312 
313  /**
314  Evaluate the derivatives of the function with respect to the parameters at a point x.
315  It is optional to be implemented by the derived classes for better efficiency if needed
316  */
317  virtual void ParameterGradient(double x , const double *p, double *grad) const
318  {
319  unsigned int npar = NPar();
320  for (unsigned int ipar = 0; ipar < npar; ++ipar)
321  grad[ipar] = DoParameterDerivative(x, p, ipar);
322  }
323 
324  /**
325  Evaluate all derivatives using cached parameter values
326  */
327  void ParameterGradient(double x , double *grad) const
328  {
329  return ParameterGradient(x, Parameters(), grad);
330  }
331 
332  /**
333  Compatibility interface with multi-dimensional functions
334  */
335  void ParameterGradient(const double *x , const double *p, double *grad) const
336  {
337  ParameterGradient(*x, p, grad);
338  }
339 
340  /**
341  Evaluate all derivatives using cached parameter values (multi-dim like interface)
342  */
343  void ParameterGradient(const double *x , double *grad) const
344  {
345  return ParameterGradient(*x, Parameters(), grad);
346  }
347 
348 
349  /**
350  Partial derivative with respect a parameter
351  */
352  double ParameterDerivative(double x, const double *p, unsigned int ipar = 0) const
353  {
354  return DoParameterDerivative(x, p, ipar);
355  }
356 
357  /**
358  Evaluate partial derivative using cached parameter values
359  */
360  double ParameterDerivative(double x, unsigned int ipar = 0) const
361  {
362  return DoParameterDerivative(x, Parameters() , ipar);
363  }
364 
365  /**
366  Partial derivative with respect a parameter
367  Compatibility interface with multi-dimensional functions
368  */
369  double ParameterDerivative(const double *x, const double *p, unsigned int ipar = 0) const
370  {
371  return DoParameterDerivative(*x, p, ipar);
372  }
373 
374 
375  /**
376  Evaluate partial derivative using cached parameter values (multi-dim like interface)
377  */
378  double ParameterDerivative(const double *x, unsigned int ipar = 0) const
379  {
380  return DoParameterDerivative(*x, Parameters() , ipar);
381  }
382 
383 
384 
385  private:
386 
387 
388  /**
389  Evaluate the gradient, to be implemented by the derived classes
390  */
391  virtual double DoParameterDerivative(double x, const double *p, unsigned int ipar) const = 0;
392 
393 
394  };
395 
396 
397 
398 
399  } // end namespace Math
400 
401 } // end namespace ROOT
402 
403 
404 
405 #endif /* ROOT_Math_IParamFunction */
virtual const double * Parameters() const =0
Access the parameter values.
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:330
double ParameterDerivative(double x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values.
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
virtual void ParameterGradient(const double *x, const double *p, double *grad) const
Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at ...
double operator()(const double *x, const double *p) const
multidim-like interface
double T(double x)
Definition: ChebyshevPol.h:34
double ParameterDerivative(double x, const double *p, unsigned int ipar=0) const
Partial derivative with respect a parameter.
T operator()(const T *x, const double *p) const
Evaluate function at a point x and for given parameters p.
virtual ~IParametricGradFunctionOneDim()
Virtual Destructor (no operations)
virtual ~IParametricGradFunctionMultiDimTempl()
Virtual Destructor (no operations)
IParametricFunctionOneDim BaseParamFunc
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
double ParameterDerivative(const double *x, const double *p, unsigned int ipar=0) const
Evaluate the partial derivative w.r.t a parameter ipar from values and parameters.
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition: IFunction.h:392
virtual T DoEval(const T *x) const
Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values...
Double_t x[n]
Definition: legend1.C:17
void ParameterGradient(double x, double *grad) const
Evaluate all derivatives using cached parameter values.
virtual double DoEval(double x) const
Implement the ROOT::Math::IBaseFunctionOneDim interface DoEval(x) using the cached parameter values...
virtual ~IBaseParam()
Virtual Destructor (no operations)
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
virtual unsigned int NPar() const =0
Return the number of Parameters.
virtual void ParameterGradient(double x, const double *p, double *grad) const
Evaluate the derivatives of the function with respect to the parameters at a point x...
void ParameterGradient(const double *x, double *grad) const
Evaluate all derivatives using cached parameter values (multi-dim like interface) ...
void ParameterGradient(const double *x, const double *p, double *grad) const
Compatibility interface with multi-dimensional functions.
IParametricFunctionOneDim::BaseFunc BaseFunc
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
virtual void SetParameters(const double *p)=0
Set the parameter values.
double ParameterDerivative(const double *x, const double *p, unsigned int ipar=0) const
Partial derivative with respect a parameter Compatibility interface with multi-dimensional functions...
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
double ParameterDerivative(const double *x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values (multi-dim like interface) ...
double operator()(double x, const double *p) const
Evaluate function at a point x and for given parameters p.
Namespace for new Math classes and functions.
IBaseFunctionMultiDimTempl< T > BaseFunc
std::string ToString(const T &val)
Utility function for conversion to strings.
Definition: Util.h:40
Documentation for the abstract class IBaseParam.
virtual std::string ParameterName(unsigned int i) const
Return the name of the i-th parameter (starting from zero) Overwrite if want to avoid the default nam...
void ParameterGradient(const double *x, double *grad) const
Evaluate all derivatives using cached parameter values.
double ParameterDerivative(const double *x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values.