Logo ROOT   6.13/01
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  /* Reimplementation instead of using BaseParamFunc::operator();
119  until the bug in VS is fixed */
120  T operator()(const T *x, const double *p) const
121  {
122  return DoEvalPar(x, p);
123  }
124 
125  T operator()(const T *x) const
126  {
127  return DoEval(x);
128  }
129 
130  private:
131  /**
132  Implementation of the evaluation function using the x values and the parameters.
133  Must be implemented by derived classes
134  */
135  virtual T DoEvalPar(const T *x, const double *p) const = 0;
136 
137  /**
138  Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values
139  */
140  virtual T DoEval(const T *x) const
141  {
142  return DoEvalPar(x, Parameters());
143  }
144  };
145 
146 
147 //___________________________________________________________________
148  /**
149  Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions
150  It is a derived class from ROOT::Math::IBaseFunctionOneDim and
151  ROOT::Math::IBaseParam
152 
153  @ingroup ParamFunc
154  */
155 
157  virtual public IBaseFunctionOneDim,
158  public IBaseParam {
159 
160 
161  public:
162 
164 
165 
166  using BaseFunc::operator();
167 
168  /**
169  Evaluate function at a point x and for given parameters p.
170  This method does not change the internal status of the function (internal parameter values).
171  If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be
172  called.
173  Use the pure virtual function DoEvalPar to implement it
174  */
175  double operator()(double x, const double *p) const
176  {
177  return DoEvalPar(x, p);
178  }
179 
180 
181  /**
182  multidim-like interface
183  */
184  double operator()(const double *x, const double *p) const
185  {
186  return DoEvalPar(*x, p);
187  }
188 
189  private:
190 
191  /**
192  Implementation of the evaluation function using the x value and the parameters.
193  Must be implemented by derived classes
194  */
195  virtual double DoEvalPar(double x, const double *p) const = 0;
196 
197  /**
198  Implement the ROOT::Math::IBaseFunctionOneDim interface DoEval(x) using the cached parameter values
199  */
200  virtual double DoEval(double x) const
201  {
202  return DoEvalPar(x, Parameters());
203  }
204 
205  };
206 
207 
208 
209 //_______________________________________________________________________________
210  /**
211  Interface (abstract class) for parametric gradient multi-dimensional functions providing
212  in addition to function evaluation with respect to the coordinates
213  also the gradient with respect to the parameters, via the method ParameterGradient.
214 
215  It is a derived class from ROOT::Math::IParametricFunctionMultiDim.
216 
217  The pure private virtual method DoParameterGradient must be implemented by the derived classes
218  in addition to those inherited by the base abstract classes.
219 
220  @ingroup ParamFunc
221  */
222 
223  template<class T>
225  public:
226 
230 
231 
232  /**
233  Virtual Destructor (no operations)
234  */
236 
237 
238  /* Reimplementation instead of using BaseParamFunc::operator();
239  until the bug in VS is fixed */
240  T operator()(const T *x, const double *p) const
241  {
242  return DoEvalPar(x, p);
243  }
244 
245  T operator()(const T *x) const
246  {
247  return DoEval(x);
248  }
249 
250  /**
251  Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at a point x.
252  It is optional to be implemented by the derived classes for better efficiency
253  */
254  virtual void ParameterGradient(const T *x, const double *p, T *grad) const
255  {
256  unsigned int npar = NPar();
257  for (unsigned int ipar = 0; ipar < npar; ++ipar)
258  grad[ipar] = DoParameterDerivative(x, p, ipar);
259  }
260 
261  /**
262  Evaluate the partial derivative w.r.t a parameter ipar from values and parameters
263  */
264  T ParameterDerivative(const T *x, const double *p, unsigned int ipar = 0) const
265  {
266  return DoParameterDerivative(x, p, ipar);
267  }
268 
269  /**
270  Evaluate all derivatives using cached parameter values
271  */
272  void ParameterGradient(const T *x, T *grad) const { return ParameterGradient(x, Parameters(), grad); }
273  /**
274  Evaluate partial derivative using cached parameter values
275  */
276  T ParameterDerivative(const T *x, unsigned int ipar = 0) const
277  {
278  return DoParameterDerivative(x, Parameters() , ipar);
279  }
280 
281  private:
282 
283  /**
284  Evaluate the partial derivative w.r.t a parameter ipar , to be implemented by the derived classes
285  */
286  virtual T DoParameterDerivative(const T *x, const double *p, unsigned int ipar) const = 0;
287  virtual T DoEvalPar(const T *x, const double *p) const = 0;
288  virtual T DoEval(const T *x) const
289  {
290  return DoEvalPar(x, Parameters());
291  }
292  };
293 
294 //_______________________________________________________________________________
295  /**
296  Interface (abstract class) for parametric one-dimensional gradient functions providing
297  in addition to function evaluation with respect the coordinates
298  also the gradient with respect to the parameters, via the method ParameterGradient.
299 
300  It is a derived class from ROOT::Math::IParametricFunctionOneDim.
301 
302  The pure private virtual method DoParameterGradient must be implemented by the derived classes
303  in addition to those inherited by the base abstract classes.
304 
305  @ingroup ParamFunc
306  */
307 
310 // ,public IGradientFunctionOneDim
311  {
312 
313  public:
314 
318 
319 
320  /**
321  Virtual Destructor (no operations)
322  */
324 
325 
326  using BaseParamFunc::operator();
327 
328  /**
329  Evaluate the derivatives of the function with respect to the parameters at a point x.
330  It is optional to be implemented by the derived classes for better efficiency if needed
331  */
332  virtual void ParameterGradient(double x , const double *p, double *grad) const
333  {
334  unsigned int npar = NPar();
335  for (unsigned int ipar = 0; ipar < npar; ++ipar)
336  grad[ipar] = DoParameterDerivative(x, p, ipar);
337  }
338 
339  /**
340  Evaluate all derivatives using cached parameter values
341  */
342  void ParameterGradient(double x , double *grad) const
343  {
344  return ParameterGradient(x, Parameters(), grad);
345  }
346 
347  /**
348  Compatibility interface with multi-dimensional functions
349  */
350  void ParameterGradient(const double *x , const double *p, double *grad) const
351  {
352  ParameterGradient(*x, p, grad);
353  }
354 
355  /**
356  Evaluate all derivatives using cached parameter values (multi-dim like interface)
357  */
358  void ParameterGradient(const double *x , double *grad) const
359  {
360  return ParameterGradient(*x, Parameters(), grad);
361  }
362 
363 
364  /**
365  Partial derivative with respect a parameter
366  */
367  double ParameterDerivative(double x, const double *p, unsigned int ipar = 0) const
368  {
369  return DoParameterDerivative(x, p, ipar);
370  }
371 
372  /**
373  Evaluate partial derivative using cached parameter values
374  */
375  double ParameterDerivative(double x, unsigned int ipar = 0) const
376  {
377  return DoParameterDerivative(x, Parameters() , ipar);
378  }
379 
380  /**
381  Partial derivative with respect a parameter
382  Compatibility interface with multi-dimensional functions
383  */
384  double ParameterDerivative(const double *x, const double *p, unsigned int ipar = 0) const
385  {
386  return DoParameterDerivative(*x, p, ipar);
387  }
388 
389 
390  /**
391  Evaluate partial derivative using cached parameter values (multi-dim like interface)
392  */
393  double ParameterDerivative(const double *x, unsigned int ipar = 0) const
394  {
395  return DoParameterDerivative(*x, Parameters() , ipar);
396  }
397 
398 
399 
400  private:
401 
402 
403  /**
404  Evaluate the gradient, to be implemented by the derived classes
405  */
406  virtual double DoParameterDerivative(double x, const double *p, unsigned int ipar) const = 0;
407 
408 
409  };
410 
411 
412 
413 
414  } // end namespace Math
415 
416 } // end namespace ROOT
417 
418 
419 
420 #endif /* ROOT_Math_IParamFunction */
virtual const double * Parameters() const =0
Access the parameter values.
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:135
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:326
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
double operator()(const double *x, const double *p) const
multidim-like interface
virtual T DoEval(const T *x) const
Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values...
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)
void ParameterGradient(const T *x, T *grad) const
Evaluate all derivatives using cached parameter values.
IParametricFunctionOneDim BaseParamFunc
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition: IFunction.h:381
virtual T DoEval(const T *x) const
Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values...
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.
T operator()(const T *x, const double *p) const
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.
virtual void ParameterGradient(const T *x, const double *p, T *grad) const
Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at ...
IParametricFunctionOneDim::BaseFunc BaseFunc
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
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:42
T ParameterDerivative(const T *x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values.
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...
T ParameterDerivative(const T *x, const double *p, unsigned int ipar=0) const
Evaluate the partial derivative w.r.t a parameter ipar from values and parameters.