Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
IFunction.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Authors: L. Moneta 11/2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 , LCG ROOT MathLib Team *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for function interfaces
12//
13// Generic Interfaces for one or multi-dimensional functions
14//
15// Created by: Lorenzo Moneta : Wed Nov 13 2006
16//
17//
18#ifndef ROOT_Math_IFunction
19#define ROOT_Math_IFunction
20
21/**
22@defgroup CppFunctions Function Classes and Interfaces
23
24 Interfaces (abstract classes) and Base classes used in MathCore and MathMore numerical methods
25 for describing function classes. They define function and gradient evaluation and as well the
26 functionality for dealing with parameters in the case of parametric functions which are used for
27 fitting and data modeling.
28 Included are also adapter classes, such as functors, to wrap generic callable C++ objects
29 in the desired interface.
30
31@ingroup MathCore
32*/
33
34#include "Math/IFunctionfwd.h"
35
36
37namespace ROOT {
38 namespace Math {
39
40 /**
41 @defgroup GenFunc Generic Function Evaluation Interfaces
42 Interface classes for evaluation of function object classes in one or multi-dimensions.
43 @ingroup CppFunctions
44 */
45
46//___________________________________________________________________________________
47 /**
48 Documentation for the abstract class IBaseFunctionMultiDim.
49 Interface (abstract class) for generic functions objects of multi-dimension
50 Provides a method to evaluate the function given a vector of coordinate values,
51 by implementing operator() (const double *).
52 In addition it defines the interface for copying functions via the pure virtual method Clone()
53 and the interface for getting the function dimension via the NDim() method.
54 Derived classes must implement the pure private virtual method DoEval(const double *) for the
55 function evaluation in addition to NDim() and Clone().
56
57 @note ROOT::Math::Functor is the recommended way to use one's own function with the fitting framework.
58
59 @ingroup GenFunc
60 */
61
62 template<class T>
64
65 public:
66
67 typedef T BackendType;
69
70 virtual ~IBaseFunctionMultiDimTempl() = default;
71
72 /// Clone a function.
73 /// Each derived class must implement their version of the Clone method.
75
76 /// Retrieve the dimension of the function.
77 virtual unsigned int NDim() const = 0;
78
79 /// Evaluate the function at a point x[].
80 /// Use the pure virtual private method DoEval which must be implemented by the sub-classes.
81 T operator()(const T *x) const { return DoEval(x); }
82
83#ifdef LATER
84 /// Template method to evaluate the function using the begin of an iterator.
85 /// User is responsible to provide correct size for the iterator.
86 template <class Iterator>
87 T operator()(const Iterator it) const { return DoEval(&(*it)); }
88#endif
89
90 // Indicate whether this class supports gradient calculations, i.e.,
91 // if it inherits from ROOT::Math::IGradientFunctionMultiDim.
92 virtual bool HasGradient() const { return false; }
93
94 /// Evaluate all the vector of function derivatives (gradient) at a point x.
95 /// Derived classes must re-implement it if more efficient than evaluating one at a time
96 virtual void Gradient(const T *x, T *grad) const
97 {
98 unsigned int ndim = NDim();
99 for (unsigned int icoord = 0; icoord < ndim; ++icoord) {
100 grad[icoord] = Derivative(x, icoord);
101 }
102 }
103
104 /// Optimized method to evaluate at the same time the function value and derivative at a point x.
105 /// Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
106 /// Derived class should implement this method if performances play an important role and if it is faster to
107 /// evaluate value and derivative at the same time
108 virtual void FdF(const T *x, T &f, T *df) const
109 {
110 f = operator()(x);
111 Gradient(x, df);
112 }
113
114 /// Return the partial derivative with respect to the passed coordinate.
115 T Derivative(const T *x, unsigned int icoord = 0) const { return DoDerivative(x, icoord); }
116
117 /// In some cases, the derivative algorithm will use information from the previous step, these can be passed
118 /// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
119 /// so that these can be passed forward again as well at the call site, if necessary.
120 T Derivative(const T *x, unsigned int icoord, T *previous_grad, T *previous_g2,
121 T *previous_gstep) const
122 {
124 }
125
126 private:
127 /// Implementation of the evaluation function. Must be implemented by derived classes.
128 virtual T DoEval(const T *x) const = 0;
129
130 /// Function to evaluate the derivative with respect each coordinate. To be implemented by the derived class.
131 virtual T DoDerivative(const T * /*x*/, unsigned int /*icoord*/) const { return {}; }
132
133 /// In some cases, the derivative algorithm will use information from the previous step, these can be passed
134 /// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
135 /// so that these can be passed forward again as well at the call site, if necessary.
136 virtual T DoDerivativeWithPrevResult(const T *x, unsigned int icoord, T * /*previous_grad*/,
137 T * /*previous_g2*/, T * /*previous_gstep*/) const
138 {
139 return DoDerivative(x, icoord);
140 }
141 };
142
143
144//___________________________________________________________________________________
145 /**
146 Interface (abstract class) for generic functions objects of one-dimension
147 Provides a method to evaluate the function given a value (simple double)
148 by implementing operator() (const double ).
149 In addition it defines the interface for copying functions via the pure virtual method Clone().
150 Derived classes must implement the pure virtual private method DoEval(double ) for the
151 function evaluation in addition to Clone().
152 An interface for evaluating the function passing a vector (like for multidim functions) is also
153 provided
154
155 @ingroup GenFunc
156 */
158
159 public:
160
162
163 virtual ~IBaseFunctionOneDim() = default;
164
165 /// Clone a function.
166 /// Each derived class will implement their version of the private DoClone method.
167 virtual IBaseFunctionOneDim *Clone() const = 0;
168
169 /// Evaluate the function at a point x.
170 /// Use the a pure virtual private method DoEval which must be implemented by sub-classes.
171 double operator()(double x) const { return DoEval(x); }
172
173 /// Evaluate the function at a point x[].
174 /// Compatible method with multi-dimensional functions.
175 double operator()(const double *x) const { return DoEval(*x); }
176
177 // Indicate whether this class supports gradient calculations, i.e.,
178 // if it inherits from ROOT::Math::IGradientFunctionOneDim.
179 virtual bool HasGradient() const { return false; }
180
181 /// Return the derivative of the function at a point x
182 /// Use the private method DoDerivative
183 double Derivative(double x) const { return DoDerivative(x); }
184
185 /// Compatibility method with multi-dimensional interface for partial derivative.
186 double Derivative(const double *x) const { return DoDerivative(*x); }
187
188 /// Compatibility method with multi-dimensional interface for Gradient.
189 void Gradient(const double *x, double *g) const { g[0] = DoDerivative(*x); }
190
191 /// Optimized method to evaluate at the same time the function value and derivative at a point x.
192 /// Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
193 /// Derived class should implement this method if performances play an important role and if it is faster to
194 /// evaluate value and derivative at the same time.
195 virtual void FdF(double x, double &f, double &df) const
196 {
197 f = operator()(x);
198 df = Derivative(x);
199 }
200
201 /// Compatibility method with multi-dimensional interface for Gradient and function evaluation.
202 void FdF(const double *x, double &f, double *df) const { FdF(*x, f, *df); }
203
204 private:
205
206 /// implementation of the evaluation function. Must be implemented by derived classes
207 virtual double DoEval(double x) const = 0;
208
209 /// Function to evaluate the derivative with respect each coordinate. To be implemented by the derived class.
210 virtual double DoDerivative(double) const { return 0.; }
211 };
212
213
214//-------- GRAD functions---------------------------
215
216
217
218//___________________________________________________________________________________
219 /**
220 Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
221 The method ROOT::Math::IFunction::Gradient calculates the full gradient vector,
222 ROOT::Math::IFunction::Derivative calculates the partial derivative for each coordinate and
223 ROOT::Math::Fdf calculates the gradient and the function value at the same time.
224 The pure private virtual method DoDerivative() must be implemented by the derived classes, while
225 Gradient and FdF are by default implemented using DoDerivative, butthey can be overloaded by the
226 derived classes to improve the efficiency in the derivative calculation.
227
228 Gradient interface (abstract class) defining the signature for calculating the gradient of a
229 multi-dimensional function.
230 Three methods are provided:
231 - Gradient(const double *x, double * grad) evaluate the full gradient vector at the vector value x
232 - Derivative(const double * x, int icoord) evaluate the partial derivative for the icoord coordinate
233 - FdF(const double *x, double &f, double * g) evaluate at the same time gradient and function/
234
235 @ingroup GenFunc
236 */
237
238 template <class T>
240
241 public:
242
243 bool HasGradient() const override { return true; }
244 };
245
246
247//___________________________________________________________________________________
248 /**
249 Interface (abstract class) for one-dimensional functions providing a gradient calculation.
250 The method ROOT::Math::IFunction::Derivative calculates the derivative and
251 ROOT::Math::Fdf calculates the derivative and the function values at the same time.
252 The pure private virtual method DoDerivative() must be implemented by the derived classes, while
253 FdF is by default implemented using DoDerivative, but it can be overloaded by the
254 derived classes to improve the efficiency in the derivative calculation.
255
256 Specialized Gradient interface(abstract class) for one dimensional functions
257 It provides a method to evaluate the derivative of the function, Derivative and a
258 method to evaluate at the same time the function and the derivative FdF
259
260 @ingroup GenFunc
261 */
263
264 public:
265
266 bool HasGradient() const override { return true; }
267 };
268
269
270
271 } // namespace Math
272} // namespace ROOT
273
274#endif /* ROOT_Math_IFunction */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:63
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
virtual ~IBaseFunctionMultiDimTempl()=default
virtual void Gradient(const T *x, T *grad) const
Evaluate all the vector of function derivatives (gradient) at a point x.
Definition IFunction.h:96
T operator()(const T *x) const
Evaluate the function at a point x[].
Definition IFunction.h:81
virtual T DoDerivative(const T *, unsigned int) const
Function to evaluate the derivative with respect each coordinate. To be implemented by the derived cl...
Definition IFunction.h:131
virtual T DoDerivativeWithPrevResult(const T *x, unsigned int icoord, T *, T *, T *) const
In some cases, the derivative algorithm will use information from the previous step,...
Definition IFunction.h:136
T Derivative(const T *x, unsigned int icoord=0) const
Return the partial derivative with respect to the passed coordinate.
Definition IFunction.h:115
virtual void FdF(const T *x, T &f, T *df) const
Optimized method to evaluate at the same time the function value and derivative at a point x.
Definition IFunction.h:108
IBaseFunctionMultiDimTempl< T > BaseFunc
Definition IFunction.h:68
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
virtual T DoEval(const T *x) const =0
Implementation of the evaluation function. Must be implemented by derived classes.
T Derivative(const T *x, unsigned int icoord, T *previous_grad, T *previous_g2, T *previous_gstep) const
In some cases, the derivative algorithm will use information from the previous step,...
Definition IFunction.h:120
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:157
virtual void FdF(double x, double &f, double &df) const
Optimized method to evaluate at the same time the function value and derivative at a point x.
Definition IFunction.h:195
double Derivative(const double *x) const
Compatibility method with multi-dimensional interface for partial derivative.
Definition IFunction.h:186
virtual bool HasGradient() const
Definition IFunction.h:179
virtual ~IBaseFunctionOneDim()=default
virtual double DoDerivative(double) const
Function to evaluate the derivative with respect each coordinate. To be implemented by the derived cl...
Definition IFunction.h:210
void FdF(const double *x, double &f, double *df) const
Compatibility method with multi-dimensional interface for Gradient and function evaluation.
Definition IFunction.h:202
IBaseFunctionOneDim BaseFunc
Definition IFunction.h:161
virtual double DoEval(double x) const =0
implementation of the evaluation function. Must be implemented by derived classes
double Derivative(double x) const
Return the derivative of the function at a point x Use the private method DoDerivative.
Definition IFunction.h:183
double operator()(const double *x) const
Evaluate the function at a point x[].
Definition IFunction.h:175
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.
double operator()(double x) const
Evaluate the function at a point x.
Definition IFunction.h:171
void Gradient(const double *x, double *g) const
Compatibility method with multi-dimensional interface for Gradient.
Definition IFunction.h:189
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition IFunction.h:239
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:262
bool HasGradient() const override
Definition IFunction.h:266
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.