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// Heaer 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 @defgroup Functor_int Internal Functor Classes
33 Internal classes for implementing Functor and Functor1D classes
34 @ingroup GenFunc
35 */
36
37/**
38 FunctorImpl is a base class for the functor
39 handler implementation class.
40 It defines the Copy operator used to clone the functor objects
41*/
42
43template<class IBaseFunc>
44class FunctorImpl : public IBaseFunc {
45
46public:
47
49
50
52
53 virtual ~FunctorImpl() {}
54
55 virtual FunctorImpl* Copy() const = 0;
56
57};
58
59/**
60 Functor Handler class is responsible for wrapping any other functor and pointer to
61 free C functions.
62 It can be created from any function implementing the correct signature
63 corresponding to the requested type
64 In the case of one dimension the function evaluation object must implement
65 double operator() (double x). If it implements a method: double Derivative(double x)
66 can be used to create a Gradient function type.
67
68 In the case of multi-dimension the function evaluation object must implement
69 double operator()(const double *x). If it implements a method:
70 double Derivative(const double *x, int icoord)
71 can be used to create a Gradient function type.
72
73 @ingroup Functor_int
74
75*/
76template<class ParentFunctor, class Func >
77class FunctorHandler : public ParentFunctor::Impl {
78
79 typedef typename ParentFunctor::Impl ImplFunc;
80 typedef typename ImplFunc::BaseFunc BaseFunc;
81 //typedef typename ParentFunctor::Dim Dim;
82
83
84public:
85
86 // constructor for 1d functions
87 FunctorHandler(const Func & fun) : fDim(1), fFunc(fun) {}
88
89
90 // constructor for multi-dimensional functions w/0 NDim()
91 FunctorHandler(unsigned int dim, const Func & fun ) :
92 fDim(dim),
93 fFunc(fun)
94 {}
95
96 virtual ~FunctorHandler() {}
97
98 // copy of the function handler (use copy-ctor)
99 ImplFunc * Copy() const {
100 return new FunctorHandler(*this);
101 }
102
103 // clone of the function handler (use copy-ctor)
104 BaseFunc * Clone() const {
105 return Copy();
106 }
107
108
109 // constructor for multi-dimensional functions
110 unsigned int NDim() const {
111 return fDim;
112 }
113
114private :
115
116 inline double DoEval (double x) const {
117 return fFunc(x);
118 }
119
120 inline double DoEval (const double * x) const {
121 return fFunc(x);
122 }
123
124 inline double DoDerivative (double x) const {
125 return fFunc.Derivative(x);
126 }
127
128 inline double DoDerivative (const double * x, unsigned int icoord ) const {
129 return fFunc.Derivative(x,icoord);
130 }
131
132
133 unsigned int fDim;
134 mutable Func fFunc; // should here be a reference and pass a non-const ref in ctor
135
136};
137
138
139/**
140 Functor Handler class for gradient functions where both callable objects are provided for the function
141 evaluation (type Func) and for the derivative (type DerivFunc) .
142 It can be created from any function implementing the correct signature
143 corresponding to the requested type
144 In the case of one dimension the function evaluation object and the derivative function object must implement
145 double operator() (double x).
146 In the case of multi-dimension the function evaluation object must implement
147 double operator() (const double * x) and the derivative function object must implement
148 double operator() (const double * x, int icoord)
149
150 @ingroup Functor_int
151*/
152template<class ParentFunctor, class Func, class DerivFunc >
153class FunctorDerivHandler : public ParentFunctor::Impl {
154
155 typedef typename ParentFunctor::Impl ImplFunc;
156 typedef typename ImplFunc::BaseFunc BaseFunc;
157
158public:
159
160 // constructor for 1d functions
161 FunctorDerivHandler(const Func & fun, const DerivFunc & gfun) :
162 fDim(1),
163 fFunc(fun),
164 fDerivFunc(gfun)
165 {}
166
167
168 // constructor for multi-dimensional functions
169 FunctorDerivHandler(unsigned int dim, const Func & fun, const DerivFunc & gfun) :
170 fDim(dim),
171 fFunc(fun),
172 fDerivFunc( gfun )
173 {}
174
176
177 // clone of the function handler (use copy-ctor)
178 ImplFunc * Copy() const { return new FunctorDerivHandler(*this); }
179
180 // clone of the function handler (use copy-ctor)
181#ifdef _MSC_VER
182 // FIXME: this is a work-around for a a problem with how the compiler
183 // generates the covariant virtual function "Clone". To address the
184 // issue just use the original return type of the virtual base class.
185 // Try to remove this #ifdef when updating Visual Studio
186 typename ParentFunctor::ImplBase* Clone() const { return Copy(); }
187#else
188 BaseFunc * Clone() const { return Copy(); }
189#endif
190
191 // constructor for multi-dimensional functions
192 unsigned int NDim() const {
193 return fDim;
194 }
195
196private :
197
198 inline double DoEval (double x) const {
199 return fFunc(x);
200 }
201
202 inline double DoEval (const double * x) const {
203 return fFunc(x);
204 }
205
206 inline double DoDerivative (double x) const {
207 return fDerivFunc(x);
208 }
209
210 inline double DoDerivative (const double * x, unsigned int icoord ) const {
211 return fDerivFunc(x, icoord);
212 }
213
214
215 unsigned int fDim;
216 mutable Func fFunc;
217 mutable DerivFunc fDerivFunc;
218
219};
220
221/**
222 Functor Handler class for gradient functions where both callable objects are provided for the function
223 evaluation (type Func) and for the gradient (type GradFunc) .
224 It can be created from any function implementing the correct signature
225 corresponding to the requested type
226 The function evaluation (Func)
227 In the case of multi-dimension the function evaluation object must implement
228 double operator() (const double * x) and the gradient function object must implement
229 double operator() (const double * x, int icoord)
230
231 @ingroup Functor_int
232*/
233template<class ParentFunctor >
234class FunctorGradHandler : public ParentFunctor::Impl {
235
236 // we don't need to template this class and just use std::function
237
238 typedef typename ParentFunctor::Impl ImplFunc;
239 typedef typename ImplFunc::BaseFunc BaseFunc;
240
241public:
242
243// constructor for multi-dimensional functions
244 FunctorGradHandler(unsigned int dim, std::function<double(const double *)> fun, std::function<void(const double *, double*)> gfun) :
245 fDim(dim),
246 fFunc(fun),
247 fGradFunc( gfun )
248 {}
249
251
252 // clone of the function handler (use copy-ctor)
253 ImplFunc * Copy() const { return new FunctorGradHandler(*this); }
254
255 // clone of the function handler (use copy-ctor)
256#ifdef _MSC_VER
257 // FIXME: this is a work-around for a a problem with how the compiler
258 // generates the covariant virtual function "Clone". To address the
259 // issue just use the original return type of the virtual base class.
260 // Try to remove this #ifdef when updating Visual Studio
261 typename ParentFunctor::ImplBase* Clone() const { return Copy(); }
262#else
263 BaseFunc * Clone() const { return Copy(); }
264#endif
265
266 // constructor for multi-dimensional functions
267 unsigned int NDim() const {
268 return fDim;
269 }
270
271 void Gradient(const double *x, double *g) const {
272 fGradFunc(x,g);
273 }
274
275private :
276
277 inline double DoEval (const double * x) const {
278 return fFunc(x);
279 }
280
281 inline double DoDerivative (const double * x, unsigned int icoord ) const {
282 std::vector<double> g(fDim);
283 fGradFunc(x, g.data());
284 return g[icoord];
285 }
286
287private:
288
289 unsigned int fDim;
290 mutable std::function<double(const double *)> fFunc;
291 mutable std::function<void(const double *, double*)> fGradFunc;
292};
293
294/**
295 Functor Handler to Wrap pointers to member functions
296 The member function type must be (XXX means any name is allowed) :
297 double XXX ( double x) for 1D functions
298 and
299 double XXXX (const double *x) for multi-dimensional functions
300
301 @ingroup Functor_int
302*/
303template <class ParentFunctor, typename PointerToObj,
304 typename PointerToMemFn>
305class MemFunHandler : public ParentFunctor::Impl
306{
307 //typedef typename ParentFunctor::Dim Dim;
308 typedef typename ParentFunctor::Impl ImplFunc;
309 typedef typename ImplFunc::BaseFunc BaseFunc;
310
311public:
312
313 /// constructor from a pointer to the class and a pointer to the function
314 MemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn)
315 : fDim(1), fObj(pObj), fMemFn(pMemFn) // should pass pointer by value ??
316 {}
317
318 /// constructor from a pointer to the class and a pointer to the function
319 MemFunHandler(unsigned int dim, const PointerToObj& pObj, PointerToMemFn pMemFn)
320 : fDim(dim), fObj(pObj), fMemFn(pMemFn)
321 {}
322
323 virtual ~MemFunHandler() {}
324
325 // clone of the function handler (use copy-ctor)
326 ImplFunc * Copy() const { return new MemFunHandler(*this); }
327
328 // clone of the function handler (use copy-ctor)
329 BaseFunc * Clone() const { return new MemFunHandler(*this); }
330
331 // constructor for multi-dimensional functions
332 unsigned int NDim() const {
333 return fDim;
334 }
335
336private :
337
338 inline double DoEval (double x) const {
339 return ((*fObj).*fMemFn)(x);
340 }
341
342 inline double DoEval (const double * x) const {
343 return ((*fObj).*fMemFn)(x);
344 }
345
346 unsigned int fDim;
347 mutable PointerToObj fObj;
348 PointerToMemFn fMemFn;
349
350};
351
352/**
353 Functor Handler to Wrap pointers to member functions for the evaluation of the function
354 and the gradient.
355 The member function type must be (XXX means any name is allowed) :
356 double XXX ( double x) for 1D function and derivative evaluation
357 double XXX (const double *x) for multi-dimensional function evaluation and
358 double XXX (cost double *x, int icoord) for partial derivatives evaluation
359
360 @ingroup Functor_int
361
362*/
363template <class ParentFunctor, typename PointerToObj,
364 typename PointerToMemFn, typename PointerToGradMemFn>
365class MemGradFunHandler : public ParentFunctor::Impl
366{
367 typedef typename ParentFunctor::Impl ImplFunc;
368 typedef typename ImplFunc::BaseFunc BaseFunc;
369 //typedef typename ParentFunctor::Dim Dim;
370
371public:
372
373 /// constructor from a pointer to the class and a pointer to the function
374 MemGradFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn, PointerToGradMemFn pGradMemFn)
375 : fDim(1),
376 fObj(pObj),
377 fMemFn(pMemFn),
378 fGradMemFn(pGradMemFn)
379 {}
380
381 /// constructor from a pointer to the class and a pointer to the function
382 MemGradFunHandler(unsigned int dim,
383 const PointerToObj& pObj,
384 PointerToMemFn pMemFn,
385 PointerToGradMemFn pGradMemFn )
386 : fDim(dim),
387 fObj(pObj),
388 fMemFn(pMemFn),
389 fGradMemFn(pGradMemFn)
390 {}
391
393
394 // clone of the function handler (use copy-ctor)
395 ImplFunc * Copy() const { return new MemGradFunHandler(*this); }
396
397 // clone of the function handler (use copy-ctor)
398 BaseFunc * Clone() const { return new MemGradFunHandler(*this); }
399
400 // constructor for multi-dimensional functions
401 unsigned int NDim() const {
402 return fDim;
403 }
404
405private :
406
407 inline double DoEval (double x) const {
408 return ((*fObj).*fMemFn)(x);
409 }
410
411 inline double DoEval (const double * x) const {
412 return ((*fObj).*fMemFn)(x);
413 }
414
415 inline double DoDerivative (double x) const {
416 return ((*fObj).*fGradMemFn)(x);
417 }
418
419 inline double DoDerivative (const double * x, unsigned int icoord ) const {
420 return ((*fObj).*fGradMemFn)(x,icoord);
421 }
422
423 unsigned int fDim;
424 mutable PointerToObj fObj;
425 PointerToMemFn fMemFn;
426 PointerToGradMemFn fGradMemFn;
427};
428
429
430//****************************
431// LM 7/2/2014: no needed this : make template ctor of Functor1D and GradFunctor1D not
432// available to CINT s
433//***************************************
434//#if defined(__MAKECINT__) || defined(G__DICTIONARY)
435// needed since CINT initialize it with TRootIOCtor
436//class TRootIOCtor;
437
438// template<class ParentFunctor>
439// class FunctorHandler<ParentFunctor,TRootIOCtor *> : public ParentFunctor::Impl
440// {
441// public:
442// typedef typename ParentFunctor::Impl ImplFunc;
443// typedef typename ImplFunc::BaseFunc BaseFunc;
444
445// FunctorHandler(TRootIOCtor *) {}
446// // function required by interface
447// virtual ~FunctorHandler() {}
448// double DoEval (double ) const { return 0; }
449// double DoDerivative (double ) const { return 0; }
450// ImplFunc * Copy() const { return 0; }
451// BaseFunc * Clone() const { return 0; }
452
453// };
454// #endif
455
456
457/**
458 Documentation for class Functor class.
459 It is used to wrap in a very simple and convenient way multi-dimensional function objects.
460 It can wrap all the following types:
461 <ul>
462 <li> any C++ callable object implementation double operator()( const double * )
463 <li> a free C function of type double ()(const double * )
464 <li> an std::function of type std::function<double (double const *)>
465 <li> a member function with the correct signature like Foo::Eval(const double * ).
466 In this case one pass the object pointer and a pointer to the member function (&Foo::Eval)
467 </ul>
468 The function dimension is required when constructing the functor.
469
470 @ingroup GenFunc
471
472 */
474
475
476public:
477
480
481 /**
482 Default constructor
483 */
485
486
487 /**
488 construct from a pointer to member function (multi-dim type)
489 */
490 template <class PtrObj, typename MemFn>
491 Functor(const PtrObj& p, MemFn memFn, unsigned int dim )
492 : fImpl(new MemFunHandler<Functor, PtrObj, MemFn>(dim, p, memFn))
493 {}
494
495
496
497 /**
498 construct from a callable object of multi-dimension
499 with the right signature (implementing operator()(const double *x)
500 */
501 template <typename Func>
502 Functor( const Func & f, unsigned int dim ) :
503 fImpl(new FunctorHandler<Functor,Func>(dim,f) )
504 {}
505
506 /**
507 specialized constructor from a std::function of multi-dimension
508 with the right signature (double operator()(double const *x)
509 This specialized constructor is introduced in order to use the Functor class in
510 Python passing Python user defined functions
511 */
512 //template <typename Func>
513 Functor(const std::function<double(double const *)> &f, unsigned int dim)
514 : fImpl(new FunctorHandler<Functor, std::function<double(double const *)> >(dim, f) )
515 {}
516
517 /**
518 Destructor (no operations)
519 */
520 virtual ~Functor () {}
521
522 /**
523 Copy constructor for functor based on ROOT::Math::IMultiGenFunction
524 */
525 Functor(const Functor & rhs) :
526 ImplBase()
527 {
528 if (rhs.fImpl)
529 fImpl = std::unique_ptr<Impl>((rhs.fImpl)->Copy());
530 }
531 // need a specialization in order to call base classes and use clone
532
533
534 /**
535 Assignment operator
536 */
537 Functor & operator = (const Functor & rhs) {
538 Functor copy(rhs);
539 fImpl.swap(copy.fImpl);
540 return *this;
541 }
542
543
544 // clone of the function handler (use copy-ctor)
545 ImplBase * Clone() const { return new Functor(*this); }
546
547 // for multi-dimensional functions
548 unsigned int NDim() const { return fImpl->NDim(); }
549
550private :
551
552
553 inline double DoEval (const double * x) const {
554 return (*fImpl)(x);
555 }
556
557
558 std::unique_ptr<Impl> fImpl; // pointer to base functor handler
559
560
561};
562
563/**
564 Functor1D class for one-dimensional functions.
565 It is used to wrap in a very simple and convenient way:
566 <ul>
567 <li> any C++ callable object implementation double operator()( double )
568 <li> a free C function of type double ()(double )
569 <li> a member function with the correct signature like Foo::Eval(double ).
570 In this case one pass the object pointer and a pointer to the member function (&Foo::Eval)
571 </ul>
572
573
574 @ingroup GenFunc
575
576 */
577
579
580
581public:
582
585
586 /**
587 Default constructor
588 */
590
591 /**
592 construct from a callable object with the right signature
593 implementing operator() (double x)
594 */
595 template <typename Func>
596 Functor1D(const Func & f) :
597 fImpl(new FunctorHandler<Functor1D,Func>(f))
598 {}
599
600
601 /**
602 construct from a pointer to member function (1D type)
603 */
604 template <class PtrObj, typename MemFn>
605 Functor1D(const PtrObj& p, MemFn memFn)
606 : fImpl(new MemFunHandler<Functor1D, PtrObj, MemFn>(p, memFn))
607 {}
608
609 /**
610 specialized constructor from a std::function implementing the function evaluation.
611 This specialized constructor is introduced in order to use the Functor class in
612 Python passing Python user defined functions
613 */
614 Functor1D(const std::function<double(double)> &f)
615 : fImpl(new FunctorHandler<Functor1D, std::function<double(double)>>(f))
616 {}
617
618 /**
619 Destructor (no operations)
620 */
621 virtual ~Functor1D () {}
622
623
624 /**
625 Copy constructor for Functor based on ROOT::Math::IGenFunction
626 */
627 Functor1D(const Functor1D & rhs) :
628 // strange that this is required even though ImplBase is an abstract class
629 ImplBase()
630 {
631 if (rhs.fImpl)
632 fImpl = std::unique_ptr<Impl>( (rhs.fImpl)->Copy() );
633 }
634
635
636 /**
637 Assignment operator
638 */
640 Functor1D copy(rhs);
641 fImpl.swap(copy.fImpl);
642 return *this;
643 }
644
645 // clone of the function handler (use copy-ctor)
646 ImplBase * Clone() const { return new Functor1D(*this); }
647
648private :
649
650 inline double DoEval (double x) const {
651 return (*fImpl)(x);
652 }
653
654 std::unique_ptr<Impl> fImpl; // pointer to base functor handler
655};
656
657/**
658 GradFunctor class for Multidimensional gradient functions.
659 It is used to wrap in a very C++ callable object to make gradient functions.
660 It can be constructed in three different way:
661 <ol>
662 <li> from an object implementing both
663 double operator()( const double * ) for the function evaluation and
664 double Derivative(const double *, int icoord) for the partial derivatives
665 <li>from an object implementing any member function like Foo::XXX(const double *) for the function evaluation
666 and any member function like Foo::XXX(const double *, int icoord) for the partial derivatives
667 <li>from two function objects implementing
668 double operator()( const double * ) for the function evaluation and another function object implementing
669 double operator() (const double *, int icoord) for the partial derivatives
670 <li>from two function objects
671 </ol>
672 The function dimension is required when constructing the functor.
673
674 @ingroup GenFunc
675
676 */
678
679
680public:
681
684
685
686 /**
687 Default constructor
688 */
690
691 /**
692 construct from a callable object of multi-dimension
693 implementing operator()(const double *x) and
694 Derivative(const double * x,icoord)
695 */
696 template <typename Func>
697 GradFunctor( const Func & f, unsigned int dim ) :
698 fImpl(new FunctorHandler<GradFunctor,Func>(dim,f) )
699 {}
700
701 /**
702 construct from a pointer to member function and member function types for function and derivative evaluations
703 */
704 template <class PtrObj, typename MemFn, typename GradMemFn>
705 GradFunctor(const PtrObj& p, MemFn memFn, GradMemFn gradFn, unsigned int dim )
706 : fImpl(new MemGradFunHandler<GradFunctor, PtrObj, MemFn, GradMemFn>(dim, p, memFn, gradFn))
707 {}
708
709 /**
710 construct for Gradient Functions of multi-dimension
711 Func gives the function evaluation, GradFunc the partial derivatives
712 The function dimension is required
713 */
714 template <typename Func, typename GradFunc>
715 GradFunctor(const Func & f, const GradFunc & g, int dim ) :
716 fImpl(new FunctorDerivHandler<GradFunctor,Func,GradFunc>(dim, f, g) )
717 { }
718
719 /**
720 specialized constructor from 2 std::functions
721 with the right signature (the first one implementing double operator()(double const *x)
722 for the function evaluation and the second one implementing double operator()(double const *x, unsigned int icoord)
723 for the function partial derivatives.
724 This specialized constructor is introduced in order to use the Functor class in
725 Python passing Python user defined functions
726 */
727 // template <typename Func>
728 GradFunctor(const std::function<double(double const *)> &f,
729 const std::function<double(double const *, unsigned int)> &g, unsigned int dim)
730 : fImpl(new FunctorDerivHandler<GradFunctor, std::function<double(double const *)>,
731 std::function<double(double const *, unsigned int)> >(dim, f, g))
732 {}
733
734 /**
735 * @brief Construct a new GradFunctor object using 2 std::function,
736 * one for the function evaluation and one for the Gradient
737 * Note the difference with the constructor above where partial derivative function
738 * is used as input
739 *
740 * @param f : function object computing the function value
741 * @param dim : number of function dimension
742 * @param g : function object computing the function gradient
743 */
744 GradFunctor(const std::function<double(double const *)> &f, int dim,
745 const std::function<void(double const *, double *)> &g)
746 : fImpl(new FunctorGradHandler<GradFunctor>(dim, f, g))
747 {}
748
749 /**
750 Destructor (no operations)
751 */
752 virtual ~GradFunctor () {}
753
754
755 /**
756 Copy constructor for functor based on ROOT::Math::IMultiGradFunction
757 */
759 ImplBase()
760 {
761 if (rhs.fImpl)
762 fImpl = std::unique_ptr<Impl>(rhs.fImpl->Copy());
763 }
764
765 /**
766 Assignment operator
767 */
769 GradFunctor copy(rhs);
770 fImpl.swap(copy.fImpl);
771 return *this;
772 }
773
774
775 // clone of the function handler (use copy-ctor)
776 ImplBase * Clone() const { return new GradFunctor(*this); }
777
778 // for multi-dimensional functions
779 unsigned int NDim() const { return fImpl->NDim(); }
780
781 void Gradient(const double *x, double *g) const {
782 fImpl->Gradient(x,g);
783 }
784
785private :
786
787
788 inline double DoEval (const double * x) const {
789 return (*fImpl)(x);
790 }
791
792
793 inline double DoDerivative (const double * x, unsigned int icoord ) const {
794 return fImpl->Derivative(x,icoord);
795 }
796
797 std::unique_ptr<Impl> fImpl; // pointer to base grad functor handler
798
799
800};
801
802
803//_______________________________________________________________________________________________
804/**
805 GradFunctor1D class for one-dimensional gradient functions.
806 It is used to wrap in a very C++ callable object to make a 1D gradient functions.
807 It can be constructed in three different way:
808 <ol>
809 <li> from an object implementing both
810 double operator()( double ) for the function evaluation and
811 double Derivative(double ) for the partial derivatives
812 <li>from an object implementing any member function like Foo::XXX(double ) for the function evaluation
813 and any other member function like Foo::YYY(double ) for the derivative.
814 <li>from an 2 function objects implementing
815 double operator()( double ) . One object provides the function evaluation, the other the derivative.
816 </ol>
817
818 @ingroup GenFunc
819
820 */
821
823
824
825public:
826
829
830
831 /**
832 Default constructor
833 */
835
836
837 /**
838 construct from an object with the right signature
839 implementing both operator() (double x) and Derivative(double x)
840 */
841 template <typename Func>
842 GradFunctor1D(const Func & f) :
844 {}
845
846
847 /**
848 construct from a pointer to class and two pointers to member functions, one for
849 the function evaluation and the other for the derivative.
850 The member functions must take a double as argument and return a double
851 */
852 template <class PtrObj, typename MemFn, typename GradMemFn>
853 GradFunctor1D(const PtrObj& p, MemFn memFn, GradMemFn gradFn)
854 : fImpl(new MemGradFunHandler<GradFunctor1D, PtrObj, MemFn, GradMemFn>(p, memFn, gradFn))
855 {}
856
857
858
859 /**
860 construct from two 1D function objects
861 */
862 template <typename Func, typename GradFunc>
863 GradFunctor1D(const Func & f, const GradFunc & g ) :
864 fImpl(new FunctorDerivHandler<GradFunctor1D,Func, GradFunc>(f, g) )
865 {}
866
867 /**
868 specialized constructor from 2 std::function objects
869 implementing double operator()(double x). The first one for the function evaluation
870 and the second one implementing the function derivative.
871 This specialized constructor is introduced in order to use the class in
872 Python passing Python user defined functions
873 */
874 GradFunctor1D(const std::function<double(double)> &f, const std::function<double(double)> &g )
875 : fImpl(new FunctorDerivHandler<GradFunctor1D, std::function<double(double)>, std::function<double(double)> >(f, g))
876 {}
877
878 /**
879 Destructor (no operations)
880 */
881 virtual ~GradFunctor1D () {}
882
883
884 /**
885 Copy constructor for Functor based on ROOT::Math::IGradFunction
886 */
888 // strange that this is required even though Impl is an abstract class
889 ImplBase()
890 {
891 if (rhs.fImpl)
892 fImpl = std::unique_ptr<Impl>( rhs.fImpl->Copy() );
893 }
894
895
896 /**
897 Assignment operator
898 */
900 GradFunctor1D copy(rhs);
901 fImpl.swap(copy.fImpl);
902 return *this;
903 }
904
905
906 // clone of the function handler (use copy-ctor)
907 ImplBase * Clone() const { return new GradFunctor1D(*this); }
908
909
910private :
911
912
913 inline double DoEval (double x) const {
914 return (*fImpl)(x);
915 }
916
917
918 inline double DoDerivative (double x) const {
919 return fImpl->Derivative(x);
920 }
921
922 std::unique_ptr<Impl> fImpl; // pointer to base gradient functor handler
923
924};
925
926
927
928 } // end namespace Math
929
930} // end namespace ROOT
931
932
933#endif /* ROOT_Math_Functor */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
winID h TVirtualViewer3D TVirtualGLPainter p
Functor1D class for one-dimensional functions.
Definition Functor.h:578
double DoEval(double x) const
implementation of the evaluation function. Must be implemented by derived classes
Definition Functor.h:650
IBaseFunctionOneDim::BaseFunc ImplBase
Definition Functor.h:584
virtual ~Functor1D()
Destructor (no operations)
Definition Functor.h:621
Functor1D & operator=(const Functor1D &rhs)
Assignment operator.
Definition Functor.h:639
FunctorImpl< IBaseFunctionOneDim > Impl
Definition Functor.h:583
Functor1D(const Functor1D &rhs)
Copy constructor for Functor based on ROOT::Math::IGenFunction.
Definition Functor.h:627
Functor1D(const std::function< double(double)> &f)
specialized constructor from a std::function implementing the function evaluation.
Definition Functor.h:614
ImplBase * Clone() const
Clone a function.
Definition Functor.h:646
Functor1D(const PtrObj &p, MemFn memFn)
construct from a pointer to member function (1D type)
Definition Functor.h:605
Functor1D(const Func &f)
construct from a callable object with the right signature implementing operator() (double x)
Definition Functor.h:596
std::unique_ptr< Impl > fImpl
Definition Functor.h:654
Functor1D()
Default constructor.
Definition Functor.h:589
Functor Handler class for gradient functions where both callable objects are provided for the functio...
Definition Functor.h:153
double DoDerivative(const double *x, unsigned int icoord) const
Definition Functor.h:210
FunctorDerivHandler(const Func &fun, const DerivFunc &gfun)
Definition Functor.h:161
ImplFunc::BaseFunc BaseFunc
Definition Functor.h:156
FunctorDerivHandler(unsigned int dim, const Func &fun, const DerivFunc &gfun)
Definition Functor.h:169
unsigned int NDim() const
Definition Functor.h:192
ParentFunctor::Impl ImplFunc
Definition Functor.h:155
double DoDerivative(double x) const
Definition Functor.h:206
ImplFunc * Copy() const
Definition Functor.h:178
BaseFunc * Clone() const
Definition Functor.h:188
double DoEval(double x) const
Definition Functor.h:198
double DoEval(const double *x) const
Definition Functor.h:202
Functor Handler class for gradient functions where both callable objects are provided for the functio...
Definition Functor.h:234
std::function< void(const double *, double *)> fGradFunc
Definition Functor.h:291
void Gradient(const double *x, double *g) const
Definition Functor.h:271
ImplFunc::BaseFunc BaseFunc
Definition Functor.h:239
unsigned int NDim() const
Definition Functor.h:267
ParentFunctor::Impl ImplFunc
Definition Functor.h:238
ImplFunc * Copy() const
Definition Functor.h:253
BaseFunc * Clone() const
Definition Functor.h:263
FunctorGradHandler(unsigned int dim, std::function< double(const double *)> fun, std::function< void(const double *, double *)> gfun)
Definition Functor.h:244
double DoDerivative(const double *x, unsigned int icoord) const
Definition Functor.h:281
double DoEval(const double *x) const
Definition Functor.h:277
std::function< double(const double *)> fFunc
Definition Functor.h:290
Functor Handler class is responsible for wrapping any other functor and pointer to free C functions.
Definition Functor.h:77
unsigned int NDim() const
Definition Functor.h:110
FunctorHandler(unsigned int dim, const Func &fun)
Definition Functor.h:91
double DoEval(const double *x) const
Definition Functor.h:120
double DoDerivative(const double *x, unsigned int icoord) const
Definition Functor.h:128
ImplFunc * Copy() const
Definition Functor.h:99
BaseFunc * Clone() const
Definition Functor.h:104
ParentFunctor::Impl ImplFunc
Definition Functor.h:79
double DoDerivative(double x) const
Definition Functor.h:124
double DoEval(double x) const
Definition Functor.h:116
ImplFunc::BaseFunc BaseFunc
Definition Functor.h:80
FunctorHandler(const Func &fun)
Definition Functor.h:87
FunctorImpl is a base class for the functor handler implementation class.
Definition Functor.h:44
virtual FunctorImpl * Copy() const =0
virtual ~FunctorImpl()
Definition Functor.h:53
Documentation for class Functor class.
Definition Functor.h:473
Functor(const Functor &rhs)
Copy constructor for functor based on ROOT::Math::IMultiGenFunction.
Definition Functor.h:525
Functor()
Default constructor.
Definition Functor.h:484
unsigned int NDim() const
Retrieve the dimension of the function.
Definition Functor.h:548
virtual ~Functor()
Destructor (no operations)
Definition Functor.h:520
FunctorImpl< IBaseFunctionMultiDim > Impl
Definition Functor.h:478
ImplBase * Clone() const
Clone a function.
Definition Functor.h:545
Functor & operator=(const Functor &rhs)
Assignment operator.
Definition Functor.h:537
double DoEval(const double *x) const
Implementation of the evaluation function.
Definition Functor.h:553
Functor(const Func &f, unsigned int dim)
construct from a callable object of multi-dimension with the right signature (implementing operator()...
Definition Functor.h:502
Functor(const std::function< double(double const *)> &f, unsigned int dim)
specialized constructor from a std::function of multi-dimension with the right signature (double oper...
Definition Functor.h:513
std::unique_ptr< Impl > fImpl
Definition Functor.h:558
IBaseFunctionMultiDim::BaseFunc ImplBase
Definition Functor.h:479
Functor(const PtrObj &p, MemFn memFn, unsigned int dim)
construct from a pointer to member function (multi-dim type)
Definition Functor.h:491
GradFunctor1D class for one-dimensional gradient functions.
Definition Functor.h:822
IGradientFunctionOneDim::BaseFunc ImplBase
Definition Functor.h:828
GradFunctor1D(const Func &f, const GradFunc &g)
construct from two 1D function objects
Definition Functor.h:863
ImplBase * Clone() const
Clone a function.
Definition Functor.h:907
FunctorImpl< IGradientFunctionOneDim > Impl
Definition Functor.h:827
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:853
GradFunctor1D(const Func &f)
construct from an object with the right signature implementing both operator() (double x) and Derivat...
Definition Functor.h:842
std::unique_ptr< Impl > fImpl
Definition Functor.h:922
GradFunctor1D()
Default constructor.
Definition Functor.h:834
GradFunctor1D & operator=(const GradFunctor1D &rhs)
Assignment operator.
Definition Functor.h:899
double DoEval(double x) const
implementation of the evaluation function. Must be implemented by derived classes
Definition Functor.h:913
virtual ~GradFunctor1D()
Destructor (no operations)
Definition Functor.h:881
GradFunctor1D(const std::function< double(double)> &f, const std::function< double(double)> &g)
specialized constructor from 2 std::function objects implementing double operator()(double x).
Definition Functor.h:874
double DoDerivative(double x) const
function to evaluate the derivative with respect each coordinate.
Definition Functor.h:918
GradFunctor1D(const GradFunctor1D &rhs)
Copy constructor for Functor based on ROOT::Math::IGradFunction.
Definition Functor.h:887
GradFunctor class for Multidimensional gradient functions.
Definition Functor.h:677
GradFunctor & operator=(const GradFunctor &rhs)
Assignment operator.
Definition Functor.h:768
GradFunctor(const Func &f, const GradFunc &g, int dim)
construct for Gradient Functions of multi-dimension Func gives the function evaluation,...
Definition Functor.h:715
virtual ~GradFunctor()
Destructor (no operations)
Definition Functor.h:752
IGradientFunctionMultiDim::BaseFunc ImplBase
Definition Functor.h:683
double DoDerivative(const double *x, unsigned int icoord) const
Definition Functor.h:793
void Gradient(const double *x, double *g) const
Definition Functor.h:781
ImplBase * Clone() const
Clone a function.
Definition Functor.h:776
GradFunctor(const GradFunctor &rhs)
Copy constructor for functor based on ROOT::Math::IMultiGradFunction.
Definition Functor.h:758
GradFunctor()
Default constructor.
Definition Functor.h:689
std::unique_ptr< Impl > fImpl
Definition Functor.h:797
unsigned int NDim() const
Retrieve the dimension of the function.
Definition Functor.h:779
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:697
FunctorImpl< IGradientFunctionMultiDim > Impl
Definition Functor.h:682
double DoEval(const double *x) const
Definition Functor.h:788
GradFunctor(const std::function< double(double const *)> &f, const std::function< double(double const *, unsigned int)> &g, unsigned int dim)
specialized constructor from 2 std::functions with the right signature (the first one implementing do...
Definition Functor.h:728
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 derivative eva...
Definition Functor.h:705
GradFunctor(const std::function< double(double const *)> &f, int dim, const std::function< void(double const *, double *)> &g)
Construct a new GradFunctor object using 2 std::function, one for the function evaluation and one for...
Definition Functor.h:744
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:135
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
Functor Handler to Wrap pointers to member functions The member function type must be (XXX means any ...
Definition Functor.h:306
ImplFunc::BaseFunc BaseFunc
Definition Functor.h:309
BaseFunc * Clone() const
Definition Functor.h:329
ParentFunctor::Impl ImplFunc
Definition Functor.h:308
PointerToMemFn fMemFn
Definition Functor.h:348
unsigned int NDim() const
Definition Functor.h:332
MemFunHandler(const PointerToObj &pObj, PointerToMemFn pMemFn)
constructor from a pointer to the class and a pointer to the function
Definition Functor.h:314
ImplFunc * Copy() const
Definition Functor.h:326
double DoEval(const double *x) const
Definition Functor.h:342
MemFunHandler(unsigned int dim, const PointerToObj &pObj, PointerToMemFn pMemFn)
constructor from a pointer to the class and a pointer to the function
Definition Functor.h:319
double DoEval(double x) const
Definition Functor.h:338
Functor Handler to Wrap pointers to member functions for the evaluation of the function and the gradi...
Definition Functor.h:366
double DoEval(double x) const
Definition Functor.h:407
BaseFunc * Clone() const
Definition Functor.h:398
MemGradFunHandler(unsigned int dim, const PointerToObj &pObj, PointerToMemFn pMemFn, PointerToGradMemFn pGradMemFn)
constructor from a pointer to the class and a pointer to the function
Definition Functor.h:382
double DoDerivative(double x) const
Definition Functor.h:415
double DoDerivative(const double *x, unsigned int icoord) const
Definition Functor.h:419
ImplFunc::BaseFunc BaseFunc
Definition Functor.h:368
ParentFunctor::Impl ImplFunc
Definition Functor.h:367
PointerToGradMemFn fGradMemFn
Definition Functor.h:426
unsigned int NDim() const
Definition Functor.h:401
ImplFunc * Copy() const
Definition Functor.h:395
double DoEval(const double *x) const
Definition Functor.h:411
MemGradFunHandler(const PointerToObj &pObj, PointerToMemFn pMemFn, PointerToGradMemFn pGradMemFn)
constructor from a pointer to the class and a pointer to the function
Definition Functor.h:374
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.