Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Functor.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: L. Moneta Mon Nov 13 15:58:13 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for Functor classes.
12// designed is inspired by the Loki Functor
13
14#ifndef ROOT_Math_Functor
15#define ROOT_Math_Functor
16
17#include "Math/IFunction.h"
18
19// #ifndef Root_Math_StaticCheck
20// #include "Math/StaticCheck.h"
21// #endif
22
23#include <memory>
24#include <functional>
25#include <vector>
26
27namespace ROOT {
28
29namespace Math {
30
31/**
32 Documentation for class Functor class.
33 It is used to wrap in a very simple and convenient way multi-dimensional function objects.
34 It can wrap all the following types:
35 <ul>
36 <li> any C++ callable object implementation double operator()( const double * )
37 <li> a free C function of type double ()(const double * )
38 <li> an std::function of type std::function<double (double const *)>
39 <li> a member function with the correct signature like Foo::Eval(const double * ).
40 In this case one pass the object pointer and a pointer to the member function (&Foo::Eval)
41 </ul>
42 The function dimension is required when constructing the functor.
43
44 @ingroup GenFunc
45
46 */
48
49public:
50
51 /// Default constructor.
52 Functor () {}
53
54 /// Construct from a pointer to member function (multi-dim type).
55 template <class PtrObj, typename MemFn>
56 Functor(const PtrObj& p, MemFn memFn, unsigned int dim )
57 : fDim{dim}, fFunc{std::bind(memFn, p, std::placeholders::_1)}
58 {}
59
60 /// Construct from a callable object of multi-dimension
61 /// with the right signature (implementing `double operator()(const double *x)`).
62 Functor(std::function<double(double const *)> const& f, unsigned int dim ) : fDim{dim}, fFunc{f} {}
63
64 // clone of the function handler (use copy-ctor)
65 Functor * Clone() const override { return new Functor(*this); }
66
67 // for multi-dimensional functions
68 unsigned int NDim() const override { return fDim; }
69
70private :
71
72 inline double DoEval (const double * x) const override {
73 return fFunc(x);
74 }
75
76 unsigned int fDim;
77 std::function<double(double const *)> fFunc;
78};
79
80/**
81 Functor1D class for one-dimensional functions.
82 It is used to wrap in a very simple and convenient way:
83 <ul>
84 <li> any C++ callable object implementation double operator()( double )
85 <li> a free C function of type double ()(double )
86 <li> a member function with the correct signature like Foo::Eval(double ).
87 In this case one pass the object pointer and a pointer to the member function (&Foo::Eval)
88 </ul>
89
90
91 @ingroup GenFunc
92
93 */
94
96
97public:
98
99 /// Default constructor.
100 Functor1D() = default;
101
102 /// Construct from a callable object with the right signature
103 /// implementing `double operator() (double x)`.
104 Functor1D(std::function<double(double)> const& f) : fFunc{f} {}
105
106 // Construct from a pointer to member function (1D type).
107 template <class PtrObj, typename MemFn>
108 Functor1D(const PtrObj& p, MemFn memFn) : fFunc{std::bind(memFn, p, std::placeholders::_1)} {}
109
110 // Clone of the function handler (use copy-ctor).
111 Functor1D * Clone() const override { return new Functor1D(*this); }
112
113private :
114
115 inline double DoEval (double x) const override {
116 return fFunc(x);
117 }
118
119 std::function<double(double)> fFunc;
120};
121
122/**
123 GradFunctor class for Multidimensional gradient functions.
124 It is used to wrap in a very C++ callable object to make gradient functions.
125 It can be constructed in three different way:
126 <ol>
127 <li> from an object implementing both
128 double operator()( const double * ) for the function evaluation and
129 double Derivative(const double *, int icoord) for the partial derivatives
130 <li>from an object implementing any member function like Foo::XXX(const double *) for the function evaluation
131 and any member function like Foo::XXX(const double *, int icoord) for the partial derivatives
132 <li>from two function objects implementing
133 double operator()( const double * ) for the function evaluation and another function object implementing
134 double operator() (const double *, int icoord) for the partial derivatives
135 <li>from two function objects
136 </ol>
137 The function dimension is required when constructing the functor.
138
139 @ingroup GenFunc
140
141 */
143
144
145public:
146
147 /// Default constructor.
148 GradFunctor() = default;
149
150 /**
151 construct from a callable object of multi-dimension
152 implementing operator()(const double *x) and
153 Derivative(const double * x,icoord)
154 */
155 template <typename Func>
156 GradFunctor( const Func & f, unsigned int dim ) :
157 fDim{dim}, fFunc{f}, fDerivFunc{std::bind(&Func::Derivative, f, std::placeholders::_1, std::placeholders::_2)}
158 {}
159
160 /// Construct from a pointer to member function and member function types for function and derivative evaluations.
161 template <class PtrObj, typename MemFn, typename DerivMemFn,
162 std::enable_if_t<std::is_floating_point<decltype((std::declval<std::remove_pointer_t<PtrObj>>().*
163 std::declval<DerivMemFn>())(
164 std::declval<const double *>(), std::declval<int>()))>::value,
165 bool> = true>
166 GradFunctor(const PtrObj &p, MemFn memFn, DerivMemFn gradFn, unsigned int dim)
167 : fDim{dim},
168 fFunc{std::bind(memFn, p, std::placeholders::_1)},
169 fDerivFunc{std::bind(gradFn, p, std::placeholders::_1, std::placeholders::_2)}
170 {}
171
172 /// Construct from a pointer to member function and member function, types for function and full derivative
173 /// evaluations.
174 template <
175 class PtrObj, typename MemFn, typename GradMemFn,
176 std::enable_if_t<std::is_void<decltype((std::declval<std::remove_pointer_t<PtrObj>>().*std::declval<GradMemFn>())(
177 std::declval<const double *>(), std::declval<double *>()))>::value,
178 bool> = true>
179 GradFunctor(const PtrObj &p, MemFn memFn, GradMemFn gradFn, unsigned int dim)
180 : fDim{dim},
181 fFunc{std::bind(memFn, p, std::placeholders::_1)},
182 fGradFunc{std::bind(gradFn, p, std::placeholders::_1, std::placeholders::_2)}
183 {
184 }
185
186 /// Construct for Gradient Functions of multi-dimension Func gives the
187 /// function evaluation, GradFunc the partial derivatives The function
188 /// dimension is required.
189 GradFunctor(std::function<double(double const *)> const& f,
190 std::function<double(double const *, unsigned int)> const& g, unsigned int dim)
191 : fDim{dim}, fFunc{f}, fDerivFunc{g}
192 {}
193
194 /**
195 * @brief Construct a new GradFunctor object using 2 std::function,
196 * one for the function evaluation and one for the Gradient
197 * Note the difference with the constructor above where partial derivative function
198 * is used as input
199 *
200 * @param f : function object computing the function value
201 * @param dim : number of function dimension
202 * @param g : function object computing the function gradient
203 */
204 GradFunctor(std::function<double(double const *)> const&f, unsigned int dim,
205 std::function<void(double const *, double *)> const& g)
206 : fDim{dim}, fFunc{f}, fGradFunc{g}
207 {}
208
209 // Clone of the function handler (use copy-ctor).
210 GradFunctor * Clone() const override { return new GradFunctor(*this); }
211
212 // for multi-dimensional functions
213 unsigned int NDim() const override { return fDim; }
214
215 void Gradient(const double *x, double *g) const override {
216 // Fall back to base implementation if no gradient function is provided
217 // (it will fill the gradient calling DoDerivative() for each component).
218 if(!fGradFunc) {
220 return;
221 }
222 fGradFunc(x, g);
223 }
224
225private :
226
227 inline double DoEval (const double * x) const override {
228 return fFunc(x);
229 }
230
231 inline double DoDerivative (const double * x, unsigned int icoord ) const override {
232 if(fDerivFunc) {
233 return fDerivFunc(x, icoord);
234 }
235 // Get the component from the gradient if not derivative function is
236 // specified.
237 std::vector<double> gradBuffer(fDim);
238 std::fill(gradBuffer.begin(), gradBuffer.end(), 0.0);
239 fGradFunc(x, gradBuffer.data());
240 return gradBuffer[icoord];
241 }
242
243 unsigned int fDim;
244 std::function<double(const double *)> fFunc;
245 std::function<double(double const *, unsigned int)> fDerivFunc;
246 std::function<void(const double *, double*)> fGradFunc;
247};
248
249
250//_______________________________________________________________________________________________
251/**
252 GradFunctor1D class for one-dimensional gradient functions.
253 It is used to wrap in a very C++ callable object to make a 1D gradient functions.
254 It can be constructed in three different way:
255 <ol>
256 <li> from an object implementing both
257 double operator()( double ) for the function evaluation and
258 double Derivative(double ) for the partial derivatives
259 <li>from an object implementing any member function like Foo::XXX(double ) for the function evaluation
260 and any other member function like Foo::YYY(double ) for the derivative.
261 <li>from an 2 function objects implementing
262 double operator()( double ) . One object provides the function evaluation, the other the derivative.
263 </ol>
264
265 @ingroup GenFunc
266
267 */
268
270
271public:
272
273 /// Default constructor.
274 GradFunctor1D() = default;
275
276 /// Construct from an object with the right signature,
277 /// implementing both `operator() (double x)` and `Derivative(double x)`.
278 template <typename Func>
279 GradFunctor1D(const Func & f) : fFunc{f}, fDerivFunc{std::bind(&Func::Derivative, f, std::placeholders::_1)} {}
280
281 /**
282 construct from a pointer to class and two pointers to member functions, one for
283 the function evaluation and the other for the derivative.
284 The member functions must take a double as argument and return a double
285 */
286 template <class PtrObj, typename MemFn, typename GradMemFn>
287 GradFunctor1D(const PtrObj& p, MemFn memFn, GradMemFn gradFn)
288 : fFunc{std::bind(memFn, p, std::placeholders::_1)}, fDerivFunc{std::bind(gradFn, p, std::placeholders::_1)}
289 {}
290
291
292 /// Specialized constructor from 2 function objects implementing double
293 /// operator()(double x). The first one for the function evaluation and the
294 /// second one implementing the function derivative.
295 GradFunctor1D(std::function<double(double)> const& f, std::function<double(double)> const& g)
296 : fFunc{f}, fDerivFunc{g}
297 {}
298
299 // clone of the function handler (use copy-ctor)
300 GradFunctor1D * Clone() const override { return new GradFunctor1D(*this); }
301
302private :
303
304 inline double DoEval (double x) const override { return fFunc(x); }
305 inline double DoDerivative (double x) const override { return fDerivFunc(x); }
306
307 std::function<double(double)> fFunc;
308 std::function<double(double)> fDerivFunc;
309};
310
311
312} // end namespace Math
313
314} // end namespace ROOT
315
316
317#endif /* ROOT_Math_Functor */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define _2(A, B)
Definition cfortran.h:109
Functor1D class for one-dimensional functions.
Definition Functor.h:95
Functor1D()=default
Default constructor.
std::function< double(double)> fFunc
Definition Functor.h:119
double DoEval(double x) const override
implementation of the evaluation function. Must be implemented by derived classes
Definition Functor.h:115
Functor1D * Clone() const override
Clone a function.
Definition Functor.h:111
Functor1D(const PtrObj &p, MemFn memFn)
Definition Functor.h:108
Functor1D(std::function< double(double)> const &f)
Construct from a callable object with the right signature implementing double operator() (double x).
Definition Functor.h:104
Documentation for class Functor class.
Definition Functor.h:47
Functor(std::function< double(double const *)> const &f, unsigned int dim)
Construct from a callable object of multi-dimension with the right signature (implementing double ope...
Definition Functor.h:62
Functor()
Default constructor.
Definition Functor.h:52
std::function< double(double const *)> fFunc
Definition Functor.h:77
double DoEval(const double *x) const override
Implementation of the evaluation function. Must be implemented by derived classes.
Definition Functor.h:72
Functor * Clone() const override
Clone a function.
Definition Functor.h:65
unsigned int NDim() const override
Retrieve the dimension of the function.
Definition Functor.h:68
Functor(const PtrObj &p, MemFn memFn, unsigned int dim)
Construct from a pointer to member function (multi-dim type).
Definition Functor.h:56
unsigned int fDim
Definition Functor.h:76
GradFunctor1D class for one-dimensional gradient functions.
Definition Functor.h:269
GradFunctor1D(std::function< double(double)> const &f, std::function< double(double)> const &g)
Specialized constructor from 2 function objects implementing double operator()(double x).
Definition Functor.h:295
GradFunctor1D * Clone() const override
Clone a function.
Definition Functor.h:300
std::function< double(double)> fFunc
Definition Functor.h:307
GradFunctor1D(const PtrObj &p, MemFn memFn, GradMemFn gradFn)
construct from a pointer to class and two pointers to member functions, one for the function evaluati...
Definition Functor.h:287
GradFunctor1D(const Func &f)
Construct from an object with the right signature, implementing both operator() (double x) and Deriva...
Definition Functor.h:279
double DoDerivative(double x) const override
Function to evaluate the derivative with respect each coordinate. To be implemented by the derived cl...
Definition Functor.h:305
std::function< double(double)> fDerivFunc
Definition Functor.h:308
double DoEval(double x) const override
implementation of the evaluation function. Must be implemented by derived classes
Definition Functor.h:304
GradFunctor1D()=default
Default constructor.
GradFunctor class for Multidimensional gradient functions.
Definition Functor.h:142
void Gradient(const double *x, double *g) const override
Definition Functor.h:215
std::function< double(double const *, unsigned int)> fDerivFunc
Definition Functor.h:245
GradFunctor(const PtrObj &p, MemFn memFn, DerivMemFn gradFn, unsigned int dim)
Construct from a pointer to member function and member function types for function and derivative eva...
Definition Functor.h:166
unsigned int NDim() const override
Retrieve the dimension of the function.
Definition Functor.h:213
GradFunctor * Clone() const override
Clone a function.
Definition Functor.h:210
double DoEval(const double *x) const override
Definition Functor.h:227
GradFunctor()=default
Default constructor.
GradFunctor(std::function< double(double const *)> const &f, std::function< double(double const *, unsigned int)> const &g, unsigned int dim)
Construct for Gradient Functions of multi-dimension Func gives the function evaluation,...
Definition Functor.h:189
GradFunctor(const PtrObj &p, MemFn memFn, GradMemFn gradFn, unsigned int dim)
Construct from a pointer to member function and member function, types for function and full derivati...
Definition Functor.h:179
GradFunctor(const Func &f, unsigned int dim)
construct from a callable object of multi-dimension implementing operator()(const double *x) and Deri...
Definition Functor.h:156
unsigned int fDim
Definition Functor.h:243
GradFunctor(std::function< double(double const *)> const &f, unsigned int dim, std::function< void(double const *, double *)> const &g)
Construct a new GradFunctor object using 2 std::function, one for the function evaluation and one for...
Definition Functor.h:204
std::function< void(const double *, double *)> fGradFunc
Definition Functor.h:246
double DoDerivative(const double *x, unsigned int icoord) const override
Definition Functor.h:231
std::function< double(const double *)> fFunc
Definition Functor.h:244
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition IFunction.h:168
virtual void Gradient(const T *x, T *grad) const
Evaluate all the vector of function derivatives (gradient) at a point x.
Definition IFunction.h:177
T Derivative(const T *x, unsigned int icoord=0) const
Return the partial derivative with respect to the passed coordinate.
Definition IFunction.h:209
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:254
double Derivative(double x) const
Return the derivative of the function at a point x Use the private method DoDerivative.
Definition IFunction.h:263
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...