| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

FunAdapters.h
Go to the documentation of this file.
1 #ifndef GAUDIMATH_FUNADAPTERS_H
2 #define GAUDIMATH_FUNADAPTERS_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 #include <boost/variant.hpp>
7 #include <memory>
8 // ============================================================================
9 // from CLHEP
10 // ============================================================================
11 #include "CLHEP/GenericFunctions/AbsFunction.hh"
12 #include "CLHEP/GenericFunctions/Argument.hh"
13 #include "CLHEP/GenericFunctions/GenericFunctions.hh"
14 // ============================================================================
15 #include "GaudiKernel/Kernel.h"
16
17 #if defined( __clang__ ) || defined( __CLING__ )
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
20 #elif defined( __GNUC__ ) && __GNUC__ >= 5
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wsuggest-override"
23 #endif
24
25 namespace detail
26 {
27 #if __cplusplus > 201402L
28
29 // C++17 version: https://godbolt.org/g/2vAZQt
30 template <typename... lambda_ts>
31 struct composer_t : lambda_ts... {
32 template <typename... T>
33 composer_t( T&&... t ) : lambda_ts( std::forward<T>( t ) )...
34 {
35 }
36
37 using lambda_ts::operator()...;
38 };
39
40 #else
41
42 template <typename... lambda_ts>
43 struct composer_t;
44
45 template <typename lambda_t>
46 struct composer_t<lambda_t> : lambda_t {
47 composer_t( const lambda_t& lambda ) : lambda_t{lambda} {}
48 composer_t( lambda_t&& lambda ) : lambda_t{std::move( lambda )} {}
49
50 using lambda_t::operator();
51 };
52
53 template <typename lambda_t, typename... more_lambda_ts>
54 struct composer_t<lambda_t, more_lambda_ts...> : lambda_t, composer_t<more_lambda_ts...> {
55 using super_t = composer_t<more_lambda_ts...>;
56 template <typename... lambda_ts>
57 composer_t( const lambda_t& lambda, lambda_ts&&... more_lambdas )
58 : lambda_t{lambda}, super_t{std::forward<lambda_ts>( more_lambdas )...}
59 {
60 }
61 template <typename... lambda_ts>
62 composer_t( lambda_t&& lambda, lambda_ts&&... more_lambdas )
63 : lambda_t{std::move( lambda )}, super_t{std::forward<lambda_ts>( more_lambdas )...}
64 {
65 }
66 using lambda_t::operator();
67 using super_t::operator();
68 };
69 #endif
70
71 template <typename... lambda_ts>
72 composer_t<std::decay_t<lambda_ts>...> compose( lambda_ts&&... lambdas )
73 {
74 return {std::forward<lambda_ts>( lambdas )...};
75 }
76
77 auto dispatch_variant = []( auto&& variant, auto&&... lambdas ) -> decltype( auto ) {
78 return boost::apply_visitor( compose( std::forward<decltype( lambdas )>( lambdas )... ),
79 std::forward<decltype( variant )>( variant ) );
80 };
81 }
82
83 namespace AIDA
84 {
85 class IFunction;
86 }
87
88 namespace Genfun
89 {
90 namespace GaudiMathImplementation
91 {
92 // ========================================================================
99 // =========================================================================
100
101 class GAUDI_API AdapterIFunction : public AbsFunction
102 {
103 public:
105 FUNCTION_OBJECT_DEF( AdapterIFunction )
106 public:
110 AdapterIFunction( const AIDA::IFunction& fun );
112 AdapterIFunction( const AdapterIFunction& ) = default;
113
114 double operator()( double a ) const override;
115
116 double operator()( const Argument& x ) const override;
117
118 unsigned int dimensionality() const override { return m_arg.size(); }
119
121 bool hasAnalyticDerivative() const override { return true; }
123 Genfun::Derivative partial( unsigned int i ) const override;
124
125 AdapterIFunction& operator=( const AdapterIFunction& ) = delete;
126
127 private:
128 const AIDA::IFunction* m_fun;
129 mutable std::vector<double> m_arg;
130 };
132 FUNCTION_OBJECT_IMP( AdapterIFunction )
133
134 // ========================================================================
158 // ========================================================================
159 class GAUDI_API Adapter2DoubleFunction : public AbsFunction
160 {
161 public:
163 typedef double ( *Function )( const double, const double );
164
165 public:
167 FUNCTION_OBJECT_DEF( Adapter2DoubleFunction )
168 public:
170 Adapter2DoubleFunction( Function func );
172 Adapter2DoubleFunction( const Adapter2DoubleFunction& ) = default;
173
174 double operator()( double x ) const override;
175
176 double operator()( const Argument& x ) const override;
177
178 unsigned int dimensionality() const override { return 2; }
180 bool hasAnalyticDerivative() const override { return true; }
182 Genfun::Derivative partial( unsigned int i ) const override;
183
184 public:
185 double operator()( const double x, const double y ) const;
186
187 // assigmenet operator is disabled
188 Adapter2DoubleFunction& operator=( const Adapter2DoubleFunction& ) = delete;
189
190 private:
191 Function m_func = nullptr;
192 };
194 FUNCTION_OBJECT_IMP( Adapter2DoubleFunction )
195
196 // ========================================================================
221 // ========================================================================
222 class GAUDI_API Adapter3DoubleFunction : public AbsFunction
223 {
224 public:
226 typedef double ( *Function )( const double, const double, const double );
227
228 public:
230 FUNCTION_OBJECT_DEF( Adapter3DoubleFunction )
231 public:
233 Adapter3DoubleFunction( Function func );
235 Adapter3DoubleFunction( const Adapter3DoubleFunction& ) = default;
236
237 double operator()( double x ) const override;
238
239 double operator()( const Argument& x ) const override;
240
241 unsigned int dimensionality() const override { return 3; }
243 bool hasAnalyticDerivative() const override { return true; }
245 Genfun::Derivative partial( unsigned int i ) const override;
246
247 public:
248 double operator()( const double x, const double y, const double z ) const;
249
250 private:
251 // assignment operator is disabled
252 Adapter3DoubleFunction& operator=( const Adapter3DoubleFunction& );
253
254 private:
255 Function m_func = nullptr;
256 };
258 FUNCTION_OBJECT_IMP( Adapter3DoubleFunction )
259
260 // ========================================================================
267 // ========================================================================
268 class GAUDI_API SimpleFunction : public AbsFunction
269 {
270 public:
271 typedef double ( *Function1 )( const double );
272 typedef double ( *Function2 )( const double* );
273 typedef double ( *Function3 )( const std::vector<double>& );
274
275 public:
277 FUNCTION_OBJECT_DEF( SimpleFunction )
278
279 public:
283 SimpleFunction( Function1 func );
284
289 SimpleFunction( Function2 func, const size_t dim );
290
295 SimpleFunction( Function3 func, const size_t dim );
296
297 public:
299 unsigned int dimensionality() const override
300 {
301 return detail::dispatch_variant( m_func, []( Function1 ) { return 1ul; },
302 [&]( auto ) { return m_arg.size(); } );
303 }
305 bool hasAnalyticDerivative() const override { return true; }
307 double operator()( double ) const override;
309 double operator()( const Argument& ) const override;
311 Genfun::Derivative partial( unsigned int i ) const override;
312
313 private:
314 boost::variant<Function1, Function2, Function3> m_func;
315 mutable std::vector<double> m_arg;
316 };
318 FUNCTION_OBJECT_IMP( SimpleFunction )
319
320 } // end of namespace GaudiMathImeplementation
321 } // end of namespace Genfun
322
323 #if defined( __clang__ ) || defined( __CLING__ )
324 #pragma clang diagnostic pop
325 #elif defined( __GNUC__ ) && __GNUC__ >= 5
326 #pragma GCC diagnostic pop
327 #endif
328
329 #endif // GAUDIMATH_FUNADAPTERS_H
330 // ============================================================================
detail::composer_t< lambda_t, more_lambda_ts... >::composer_t
composer_t(lambda_t &&lambda, lambda_ts &&...more_lambdas)
Definition: FunAdapters.h:62
Kernel.h
detail::compose
composer_t< std::decay_t< lambda_ts >... > compose(lambda_ts &&...lambdas)
Definition: FunAdapters.h:72
gaudirun.default
default
Definition: gaudirun.py:149
detail::composer_t< lambda_t >::composer_t
composer_t(const lambda_t &lambda)
Definition: FunAdapters.h:47
detail::dispatch_variant
auto dispatch_variant
Definition: FunAdapters.h:77
Genfun::GaudiMathImplementation::SimpleFunction::hasAnalyticDerivative
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:305
std
STL namespace.
Genfun::GaudiMathImplementation::SimpleFunction
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:268
GaudiMath::Function
Genfun::AbsFunction Function
Definition: GaudiMath.h:24
class
#define class
Definition: HistogramPersistencySvc.cpp:45
GaudiMath::Derivative
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:27
Genfun::GaudiMathImplementation::Adapter3DoubleFunction::hasAnalyticDerivative
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:243
AIDA
GaudiKernel.
Definition: Fill.h:10
Genfun::GaudiMathImplementation::Adapter2DoubleFunction::hasAnalyticDerivative
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:180
operator=
PropertyMgr & operator=(const PropertyMgr &)=delete
Genfun::GaudiMathImplementation::Adapter2DoubleFunction
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:159
detail
TupleObj.h GaudiAlg/TupleObj.h namespace with few technical implementations.
Genfun::GaudiMathImplementation::SimpleFunction::m_arg
std::vector< double > m_arg
Definition: FunAdapters.h:315
GaudiPluginService.cpluginsvc.func
func
Definition: cpluginsvc.py:229
Genfun::GaudiMathImplementation::SimpleFunction::m_func
boost::variant< Function1, Function2, Function3 > m_func
Definition: FunAdapters.h:314
std::move
T move(T...args)
Genfun::GaudiMathImplementation::Adapter3DoubleFunction
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:222
detail::composer_t< lambda_t, more_lambda_ts... >::composer_t
composer_t(const lambda_t &lambda, lambda_ts &&...more_lambdas)
Definition: FunAdapters.h:57
Genfun::GaudiMathImplementation::AdapterIFunction::m_fun
const AIDA::IFunction * m_fun
Definition: FunAdapters.h:128
std::vector< double >
Gaudi::Functional::operator()
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
Genfun::GaudiMathImplementation::AdapterIFunction::m_arg
std::vector< double > m_arg
Definition: FunAdapters.h:129
fun
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:26
detail::composer_t< lambda_t >::composer_t
composer_t(lambda_t &&lambda)
Definition: FunAdapters.h:48
detail::composer_t
Definition: FunAdapters.h:43
Genfun
CLHEP.
Definition: IEqSolver.h:13
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
std::forward
T forward(T...args)
Genfun::GaudiMathImplementation::AdapterIFunction::dimensionality
unsigned int dimensionality() const override
Definition: FunAdapters.h:118
Genfun::GaudiMathImplementation::AdapterIFunction::hasAnalyticDerivative
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:121
Genfun::GaudiMathImplementation::AdapterIFunction
constructor from the IFunction ( see AIDA/IFunction.h)
Definition: FunAdapters.h:101