The Gaudi Framework  v29r0 (ff2e7097)
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;
130  };
132  FUNCTION_OBJECT_IMP( AdapterIFunction )
133 
134  // ========================================================================
158  // ========================================================================
160  {
161  public:
163  typedef double ( *Function )( const double, const double );
164 
165  public:
167  FUNCTION_OBJECT_DEF( Adapter2DoubleFunction )
168  public:
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
189 
190  private:
191  Function m_func = nullptr;
192  };
194  FUNCTION_OBJECT_IMP( Adapter2DoubleFunction )
195 
196  // ========================================================================
221  // ========================================================================
223  {
224  public:
226  typedef double ( *Function )( const double, const double, const double );
227 
228  public:
230  FUNCTION_OBJECT_DEF( Adapter3DoubleFunction )
231  public:
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
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;
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 // ============================================================================
composer_t(lambda_t &&lambda, lambda_ts &&...more_lambdas)
Definition: FunAdapters.h:62
composer_t< std::decay_t< lambda_ts >... > compose(lambda_ts &&...lambdas)
Definition: FunAdapters.h:72
composer_t(const lambda_t &lambda)
Definition: FunAdapters.h:47
auto dispatch_variant
Definition: FunAdapters.h:77
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:305
STL namespace.
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:268
Genfun::AbsFunction Function
Definition: GaudiMath.h:24
#define class
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:27
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:243
GaudiKernel.
Definition: Fill.h:10
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:180
PropertyMgr & operator=(const PropertyMgr &)=delete
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:159
TupleObj.h GaudiAlg/TupleObj.h namespace with few technical implementations.
boost::variant< Function1, Function2, Function3 > m_func
Definition: FunAdapters.h:314
T move(T...args)
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:222
composer_t(const lambda_t &lambda, lambda_ts &&...more_lambdas)
Definition: FunAdapters.h:57
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:26
composer_t(lambda_t &&lambda)
Definition: FunAdapters.h:48
CLHEP.
Definition: IEqSolver.h:13
#define GAUDI_API
Definition: Kernel.h:110
T forward(T...args)
unsigned int dimensionality() const override
Definition: FunAdapters.h:118
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:121
constructor from the IFunction ( see AIDA/IFunction.h)
Definition: FunAdapters.h:101