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 CERN *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 * *
12 **********************************************************************/
13
14// Header file for class IParamFunction
15
16#ifndef ROOT_Math_IParamFunction
17#define ROOT_Math_IParamFunction
18
19#include "Math/IFunction.h"
20
22
23#include "Math/Util.h"
24
25#include <cassert>
26#include <string>
27
28/**
29 @defgroup ParamFunc Parametric Function Evaluation Interfaces.
30 Interfaces classes for evaluation of parametric functions
31 @ingroup CppFunctions
32*/
33
34
35namespace ROOT {
36
37 namespace Math {
38
39
40//___________________________________________________________________
41 /**
42 Documentation for the abstract class IBaseParam.
43 It defines the interface for dealing with the function parameters
44 This is used only for internal convenience, to avoid redefining the Parameter API
45 for the one and the multi-dim functions.
46 Concrete class should derive from ROOT::Math::IParamFunction and not from this class.
47
48 @ingroup ParamFunc
49 */
50
51 class IBaseParam {
52
53 public:
54
55
56 /**
57 Virtual Destructor (no operations)
58 */
59 virtual ~IBaseParam() {}
60
61
62 /**
63 Access the parameter values
64 */
65 virtual const double *Parameters() const = 0;
66
67 /**
68 Set the parameter values
69 @param p vector of doubles containing the parameter values.
70
71 to be defined: can user change number of params ? At the moment no.
72
73 */
74 virtual void SetParameters(const double *p) = 0;
75
76
77 /**
78 Return the number of Parameters
79 */
80 virtual unsigned int NPar() const = 0;
81
82 /**
83 Return the name of the i-th parameter (starting from zero)
84 Overwrite if want to avoid the default name ("Par_0, Par_1, ...")
85 */
86 virtual std::string ParameterName(unsigned int i) const
87 {
88 assert(i < NPar());
89 return "Par_" + Util::ToString(i);
90 }
91
92
93 };
94
95//___________________________________________________________________
96 /**
97 IParamFunction interface (abstract class) describing multi-dimensional parametric functions
98 It is a derived class from ROOT::Math::IBaseFunctionMultiDim and
99 ROOT::Math::IBaseParam
100
101 Provides the interface for evaluating a function passing a coordinate vector and a parameter vector.
102
103 @ingroup ParamFunc
104 */
105
106 template<class T>
108 virtual public IBaseParam {
109 public:
110
112
113 /**
114 Evaluate function at a point x and for given parameters p.
115 This method does not change the internal status of the function (internal parameter values).
116 If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be
117 called.
118 Use the pure virtual function DoEvalPar to implement it
119 */
120
121 /* Reimplementation instead of using BaseParamFunc::operator();
122 until the bug in VS is fixed */
123 T operator()(const T *x, const double *p) const
124 {
125 return DoEvalPar(x, p);
126 }
127
128 T operator()(const T *x) const
129 {
130 return DoEval(x);
131 }
132
133 private:
134 /**
135 Implementation of the evaluation function using the x values and the parameters.
136 Must be implemented by derived classes
137 */
138 virtual T DoEvalPar(const T *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 T DoEval(const T *x) const
144 {
145 return DoEvalPar(x, Parameters());
146 }
147 };
148
149
150//___________________________________________________________________
151 /**
152 Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions
153 It is a derived class from ROOT::Math::IBaseFunctionOneDim and
154 ROOT::Math::IBaseParam
155
156 @ingroup ParamFunc
157 */
158
160 virtual public IBaseFunctionOneDim,
161 public IBaseParam {
162
163
164 public:
165
167
168
169 using BaseFunc::operator();
170
171 /**
172 Evaluate function at a point x and for given parameters p.
173 This method does not change the internal status of the function (internal parameter values).
174 If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be
175 called.
176 Use the pure virtual function DoEvalPar to implement it
177 */
178 double operator()(double x, const double *p) const
179 {
180 return DoEvalPar(x, p);
181 }
182
183
184 /**
185 multidim-like interface
186 */
187 double operator()(const double *x, const double *p) const
188 {
189 return DoEvalPar(*x, p);
190 }
191
192 private:
193
194 /**
195 Implementation of the evaluation function using the x value and the parameters.
196 Must be implemented by derived classes
197 */
198 virtual double DoEvalPar(double x, const double *p) const = 0;
199
200 /**
201 Implement the ROOT::Math::IBaseFunctionOneDim interface DoEval(x) using the cached parameter values
202 */
203 double DoEval(double x) const override
204 {
205 return DoEvalPar(x, Parameters());
206 }
207
208 };
209
210
211
212//_______________________________________________________________________________
213 /**
214 Interface (abstract class) for parametric gradient multi-dimensional functions providing
215 in addition to function evaluation with respect to the coordinates
216 also the gradient with respect to the parameters, via the method ParameterGradient.
217
218 It is a derived class from ROOT::Math::IParametricFunctionMultiDim.
219
220 The pure private virtual method DoParameterGradient must be implemented by the derived classes
221 in addition to those inherited by the base abstract classes.
222
223 @ingroup ParamFunc
224 */
225
226 template<class T>
228 public:
229
233
234
235 /**
236 Virtual Destructor (no operations)
237 */
239
240
241 /* Reimplementation instead of using BaseParamFunc::operator();
242 until the bug in VS is fixed */
243 T operator()(const T *x, const double *p) const
244 {
245 return DoEvalPar(x, p);
246 }
247
248 T operator()(const T *x) const
249 {
250 return DoEval(x);
251 }
252
253 /**
254 Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at a point x.
255 It is optional to be implemented by the derived classes for better efficiency
256 */
257 virtual void ParameterGradient(const T *x, const double *p, T *grad) const
258 {
259 unsigned int npar = NPar();
260 for (unsigned int ipar = 0; ipar < npar; ++ipar)
261 grad[ipar] = DoParameterDerivative(x, p, ipar);
262 }
263
264 // Return true if this function provides computation of the Hessian matrix with respect to the parameters
265 virtual bool HasParameterHessian() const { return false;}
266
267 /**
268 Evaluate the all the Hessian (second derivatives matrix) of the function with respect to the parameters at a point x.
269 It is optional to be implemented by the derived classes if needed. If it is not implemented return a false.
270 h must be dimensioned as a n x (n+1)/2 matrix (since it is a symmetric matrix)
271 */
272 virtual bool ParameterHessian(const T * /* x */, const double * /* p */, T * /* h */) const { return false;}
273
274 /**
275 Evaluate all the second derivatives (diagonal ones) of the function with respect to the parameters at a point x.
276 g2 is a vector of dimension npar
277 */
278 virtual bool ParameterG2(const T * /* x */, const double * /* p */, T * /* g2 */) const { return false;}
279
280 /**
281 Evaluate the partial derivative w.r.t a parameter ipar from values and parameters
282 */
283 T ParameterDerivative(const T *x, const double *p, unsigned int ipar = 0) const
284 {
285 return DoParameterDerivative(x, p, ipar);
286 }
287
288 /**
289 Evaluate all derivatives using cached parameter values
290 */
291 void ParameterGradient(const T *x, T *grad) const { return ParameterGradient(x, Parameters(), grad); }
292 /**
293 Evaluate partial derivative using cached parameter values
294 */
295 T ParameterDerivative(const T *x, unsigned int ipar = 0) const
296 {
297 return DoParameterDerivative(x, Parameters() , ipar);
298 }
299
300 private:
301
302 /**
303 Evaluate the partial derivative w.r.t a parameter ipar , to be implemented by the derived classes
304 */
305 virtual T DoParameterDerivative(const T *x, const double *p, unsigned int ipar) const = 0;
306 T DoEvalPar(const T *x, const double *p) const override = 0;
307 T DoEval(const T *x) const override
308 {
309 return DoEvalPar(x, Parameters());
310 }
311 };
312
313//_______________________________________________________________________________
314 /**
315 Interface (abstract class) for parametric one-dimensional gradient functions providing
316 in addition to function evaluation with respect the coordinates
317 also the gradient with respect to the parameters, via the method ParameterGradient.
318
319 It is a derived class from ROOT::Math::IParametricFunctionOneDim.
320
321 The pure private virtual method DoParameterGradient must be implemented by the derived classes
322 in addition to those inherited by the base abstract classes.
323
324 @ingroup ParamFunc
325 */
326
329// ,public IGradientFunctionOneDim
330 {
331
332 public:
333
337
338
339 /**
340 Virtual Destructor (no operations)
341 */
343
344
345 using BaseParamFunc::operator();
346
347 /**
348 Evaluate the derivatives of the function with respect to the parameters at a point x.
349 It is optional to be implemented by the derived classes for better efficiency if needed
350 */
351 virtual void ParameterGradient(double x , const double *p, double *grad) const
352 {
353 unsigned int npar = NPar();
354 for (unsigned int ipar = 0; ipar < npar; ++ipar)
355 grad[ipar] = DoParameterDerivative(x, p, ipar);
356 }
357
358 /**
359 Evaluate all derivatives using cached parameter values
360 */
361 void ParameterGradient(double x , double *grad) const
362 {
363 return ParameterGradient(x, Parameters(), grad);
364 }
365
366 /**
367 Compatibility interface with multi-dimensional functions
368 */
369 void ParameterGradient(const double *x , const double *p, double *grad) const
370 {
371 ParameterGradient(*x, p, grad);
372 }
373
374 /**
375 Evaluate all derivatives using cached parameter values (multi-dim like interface)
376 */
377 void ParameterGradient(const double *x , double *grad) const
378 {
379 return ParameterGradient(*x, Parameters(), grad);
380 }
381
382
383 /**
384 Partial derivative with respect a parameter
385 */
386 double ParameterDerivative(double x, const double *p, unsigned int ipar = 0) const
387 {
388 return DoParameterDerivative(x, p, ipar);
389 }
390
391 /**
392 Evaluate partial derivative using cached parameter values
393 */
394 double ParameterDerivative(double x, unsigned int ipar = 0) const
395 {
396 return DoParameterDerivative(x, Parameters() , ipar);
397 }
398
399 /**
400 Partial derivative with respect a parameter
401 Compatibility interface with multi-dimensional functions
402 */
403 double ParameterDerivative(const double *x, const double *p, unsigned int ipar = 0) const
404 {
405 return DoParameterDerivative(*x, p, ipar);
406 }
407
408
409 /**
410 Evaluate partial derivative using cached parameter values (multi-dim like interface)
411 */
412 double ParameterDerivative(const double *x, unsigned int ipar = 0) const
413 {
414 return DoParameterDerivative(*x, Parameters() , ipar);
415 }
416
417
418
419 private:
420
421
422 /**
423 Evaluate the gradient, to be implemented by the derived classes
424 */
425 virtual double DoParameterDerivative(double x, const double *p, unsigned int ipar) const = 0;
426
427
428 };
429
430
431
432
433 } // end namespace Math
434
435} // end namespace ROOT
436
437
438
439#endif /* ROOT_Math_IParamFunction */
winID h TVirtualViewer3D TVirtualGLPainter p
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:61
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
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:168
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:254
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 ...
~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.
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.
T DoEvalPar(const T *x, const double *p) const override=0
Implementation of the evaluation function using the x values and the parameters.
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...