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