Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
19
20#include "Math/Util.h"
21
22#include <cassert>
23#include <string>
24
25/**
26 @defgroup ParamFunc Parametric Function Evaluation Interfaces.
27 Interfaces classes for evaluation of parametric functions
28 @ingroup CppFunctions
29*/
30
31
32namespace 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 convenience, 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 parametric 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 double DoEval(double x) const override
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 // Return true if this function provides computation of the Hessian matrix with respect to the parameters
262 virtual bool HasParameterHessian() const { return false;}
263
264 /**
265 Evaluate the all the Hessian (second derivatives matrix) of the function with respect to the parameters at a point x.
266 It is optional to be implemented by the derived classes if needed. If it is not implemented return a false.
267 h must be dimensioned as a n x (n+1)/2 matrix (since it is a symmetric matrix)
268 */
269 virtual bool ParameterHessian(const T * /* x */, const double * /* p */, T * /* h */) const { return false;}
270
271 /**
272 Evaluate all the second derivatives (diagonal ones) of the function with respect to the parameters at a point x.
273 g2 is a vector of dimension npar
274 */
275 virtual bool ParameterG2(const T * /* x */, const double * /* p */, T * /* g2 */) const { return false;}
276
277 /**
278 Evaluate the partial derivative w.r.t a parameter ipar from values and parameters
279 */
280 T ParameterDerivative(const T *x, const double *p, unsigned int ipar = 0) const
281 {
282 return DoParameterDerivative(x, p, ipar);
283 }
284
285 /**
286 Evaluate all derivatives using cached parameter values
287 */
288 void ParameterGradient(const T *x, T *grad) const { return ParameterGradient(x, Parameters(), grad); }
289 /**
290 Evaluate partial derivative using cached parameter values
291 */
292 T ParameterDerivative(const T *x, unsigned int ipar = 0) const
293 {
294 return DoParameterDerivative(x, Parameters() , ipar);
295 }
296
297 private:
298
299 /**
300 Evaluate the partial derivative w.r.t a parameter ipar , to be implemented by the derived classes
301 */
302 virtual T DoParameterDerivative(const T *x, const double *p, unsigned int ipar) const = 0;
303 virtual T DoEvalPar(const T *x, const double *p) const override = 0;
304 virtual T DoEval(const T *x) const override
305 {
306 return DoEvalPar(x, Parameters());
307 }
308 };
309
310//_______________________________________________________________________________
311 /**
312 Interface (abstract class) for parametric one-dimensional gradient functions providing
313 in addition to function evaluation with respect the coordinates
314 also the gradient with respect to the parameters, via the method ParameterGradient.
315
316 It is a derived class from ROOT::Math::IParametricFunctionOneDim.
317
318 The pure private virtual method DoParameterGradient must be implemented by the derived classes
319 in addition to those inherited by the base abstract classes.
320
321 @ingroup ParamFunc
322 */
323
326// ,public IGradientFunctionOneDim
327 {
328
329 public:
330
334
335
336 /**
337 Virtual Destructor (no operations)
338 */
340
341
342 using BaseParamFunc::operator();
343
344 /**
345 Evaluate the derivatives of the function with respect to the parameters at a point x.
346 It is optional to be implemented by the derived classes for better efficiency if needed
347 */
348 virtual void ParameterGradient(double x , const double *p, double *grad) const
349 {
350 unsigned int npar = NPar();
351 for (unsigned int ipar = 0; ipar < npar; ++ipar)
352 grad[ipar] = DoParameterDerivative(x, p, ipar);
353 }
354
355 /**
356 Evaluate all derivatives using cached parameter values
357 */
358 void ParameterGradient(double x , double *grad) const
359 {
360 return ParameterGradient(x, Parameters(), grad);
361 }
362
363 /**
364 Compatibility interface with multi-dimensional functions
365 */
366 void ParameterGradient(const double *x , const double *p, double *grad) const
367 {
368 ParameterGradient(*x, p, grad);
369 }
370
371 /**
372 Evaluate all derivatives using cached parameter values (multi-dim like interface)
373 */
374 void ParameterGradient(const double *x , double *grad) const
375 {
376 return ParameterGradient(*x, Parameters(), grad);
377 }
378
379
380 /**
381 Partial derivative with respect a parameter
382 */
383 double ParameterDerivative(double x, const double *p, unsigned int ipar = 0) const
384 {
385 return DoParameterDerivative(x, p, ipar);
386 }
387
388 /**
389 Evaluate partial derivative using cached parameter values
390 */
391 double ParameterDerivative(double x, unsigned int ipar = 0) const
392 {
393 return DoParameterDerivative(x, Parameters() , ipar);
394 }
395
396 /**
397 Partial derivative with respect a parameter
398 Compatibility interface with multi-dimensional functions
399 */
400 double ParameterDerivative(const double *x, const double *p, unsigned int ipar = 0) const
401 {
402 return DoParameterDerivative(*x, p, ipar);
403 }
404
405
406 /**
407 Evaluate partial derivative using cached parameter values (multi-dim like interface)
408 */
409 double ParameterDerivative(const double *x, unsigned int ipar = 0) const
410 {
411 return DoParameterDerivative(*x, Parameters() , ipar);
412 }
413
414
415
416 private:
417
418
419 /**
420 Evaluate the gradient, to be implemented by the derived classes
421 */
422 virtual double DoParameterDerivative(double x, const double *p, unsigned int ipar) const = 0;
423
424
425 };
426
427
428
429
430 } // end namespace Math
431
432} // end namespace ROOT
433
434
435
436#endif /* ROOT_Math_IParamFunction */
winID h TVirtualViewer3D TVirtualGLPainter p
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:62
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:135
Documentation for the abstract class IBaseParam.
virtual void SetParameters(const double *p)=0
Set the parameter values.
virtual const double * Parameters() const =0
Access the parameter values.
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...
virtual ~IBaseParam()
Virtual Destructor (no operations)
virtual unsigned int NPar() const =0
Return the number of Parameters.
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition IFunction.h:343
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:409
IParamFunction interface (abstract class) describing multi-dimensional parametric functions It is a d...
T operator()(const T *x, const double *p) const
Evaluate function at a point x and for given parameters p.
virtual T DoEval(const T *x) const
Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values...
IBaseFunctionMultiDimTempl< T > BaseFunc
virtual T DoEvalPar(const T *x, const double *p) const =0
Implementation of the evaluation function using the x values and the parameters.
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
double DoEval(double x) const override
Implement the ROOT::Math::IBaseFunctionOneDim interface DoEval(x) using the cached parameter values.
virtual double DoEvalPar(double x, const double *p) const =0
Implementation of the evaluation function using the x value and the parameters.
double operator()(double x, const double *p) const
Evaluate function at a point x and for given parameters p.
double operator()(const double *x, const double *p) const
multidim-like interface
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
virtual T DoEvalPar(const T *x, const double *p) const override=0
Implementation of the evaluation function using the x values and the parameters.
~IParametricGradFunctionMultiDimTempl() override
Virtual Destructor (no operations)
T operator()(const T *x, const double *p) const
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 ...
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.
virtual T DoEval(const T *x) const override
Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values...
T ParameterDerivative(const T *x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values.
void ParameterGradient(const T *x, T *grad) const
Evaluate all derivatives using cached parameter values.
virtual T DoParameterDerivative(const T *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 bool ParameterG2(const T *, const double *, T *) const
Evaluate all the second derivatives (diagonal ones) of the function with respect to the parameters at...
virtual bool ParameterHessian(const T *, const double *, T *) const
Evaluate the all the Hessian (second derivatives matrix) of the function with respect to the paramete...
typename IParametricFunctionMultiDimTempl< T >::BaseFunc BaseFunc
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
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.
IParametricFunctionOneDim::BaseFunc BaseFunc
void ParameterGradient(const double *x, const double *p, double *grad) const
Compatibility interface with multi-dimensional functions.
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.
double ParameterDerivative(const double *x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values (multi-dim like interface)
void ParameterGradient(double x, double *grad) const
Evaluate all derivatives using cached parameter values.
double ParameterDerivative(double x, unsigned int ipar=0) const
Evaluate partial derivative using cached parameter values.
virtual double DoParameterDerivative(double x, const double *p, unsigned int ipar) const =0
Evaluate the gradient, to be implemented by the derived classes.
void ParameterGradient(const double *x, double *grad) const
Evaluate all derivatives using cached parameter values (multi-dim like interface)
double ParameterDerivative(double x, const double *p, unsigned int ipar=0) const
Partial derivative with respect a parameter.
~IParametricGradFunctionOneDim() override
Virtual Destructor (no operations)
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.