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