The Gaudi Framework  v28r3 (cc1cf868)
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 <memory>
7 #include <boost/variant.hpp>
8 // ============================================================================
9 // from CLHEP
10 // ============================================================================
11 #include "CLHEP/GenericFunctions/GenericFunctions.hh"
12 #include "CLHEP/GenericFunctions/Argument.hh"
13 #include "CLHEP/GenericFunctions/AbsFunction.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 
26 namespace detail {
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 {
33  template <typename... T>
34  composer_t(T&&... t) : lambda_ts(std::forward<T>(t))... {}
35 
36  using lambda_ts::operator()...;
37 };
38 
39 #else
40 
41 template <typename... lambda_ts>
42 struct composer_t;
43 
44 template <typename lambda_t>
45 struct composer_t<lambda_t> : lambda_t {
46  composer_t(const lambda_t& lambda): lambda_t{lambda} { }
47  composer_t(lambda_t&& lambda): lambda_t{std::move(lambda)} { }
48 
49  using lambda_t::operator();
50 };
51 
52 template <typename lambda_t, typename... more_lambda_ts>
53 struct composer_t<lambda_t, more_lambda_ts...> : lambda_t, composer_t<more_lambda_ts...> {
54  using super_t = composer_t<more_lambda_ts...>;
55  template <typename... lambda_ts>
56  composer_t(const lambda_t& lambda, lambda_ts&&... more_lambdas): lambda_t{lambda}, super_t{std::forward<lambda_ts>(more_lambdas)...} { }
57  template <typename... lambda_ts>
58  composer_t(lambda_t&& lambda, lambda_ts&&... more_lambdas): lambda_t{std::move(lambda)}, super_t{std::forward<lambda_ts>(more_lambdas)...} { }
59  using lambda_t::operator();
60  using super_t::operator();
61 };
62 #endif
63 
64 template <typename... lambda_ts>
66 compose(lambda_ts&&... lambdas)
67 { return {std::forward<lambda_ts>(lambdas)...}; }
68 
69 
70 auto dispatch_variant = [](auto&& variant, auto&&... lambdas) -> decltype(auto) {
71  return boost::apply_visitor( compose( std::forward<decltype(lambdas)>(lambdas)... ),
72  std::forward<decltype(variant)>(variant) );
73 };
74 
75 }
76 
77 namespace AIDA { class IFunction ; }
78 
79 namespace Genfun
80 {
81  namespace GaudiMathImplementation
82  {
83  // ========================================================================
90  // =========================================================================
91 
92  class GAUDI_API AdapterIFunction : public AbsFunction
93  {
94  public:
96  FUNCTION_OBJECT_DEF( AdapterIFunction )
97  public:
101  AdapterIFunction ( const AIDA::IFunction& fun) ;
103  AdapterIFunction ( const AdapterIFunction& ) = default;
104 
105  double operator() ( double a ) const override;
106 
107  double operator() ( const Argument& x ) const override;
108 
109  unsigned int dimensionality () const override { return m_arg.size() ; }
110 
112  bool hasAnalyticDerivative() const override { return true ; }
114  Genfun::Derivative partial( unsigned int i ) const override;
115 
116  AdapterIFunction& operator=(const AdapterIFunction&) = delete;
117  private:
118  const AIDA::IFunction* m_fun ;
120  };
122  FUNCTION_OBJECT_IMP( AdapterIFunction )
123 
124  // ========================================================================
148  // ========================================================================
150  {
151  public:
153  typedef double (*Function)( const double ,
154  const double ) ;
155  public:
157  FUNCTION_OBJECT_DEF( Adapter2DoubleFunction )
158  public:
163 
164  double operator() ( double x ) const override;
165 
166  double operator() ( const Argument& x ) const override;
167 
168  unsigned int dimensionality() const override { return 2 ; }
170  bool hasAnalyticDerivative() const override { return true ; }
172  Genfun::Derivative partial( unsigned int i ) const override;
173  public:
174  double operator() ( const double x , const double y ) const ;
175 
176  // assigmenet operator is disabled
178  private:
179  Function m_func = nullptr ;
180  };
182  FUNCTION_OBJECT_IMP( Adapter2DoubleFunction )
183 
184  // ========================================================================
209  // ========================================================================
211  {
212  public:
214  typedef double (*Function)( const double ,
215  const double ,
216  const double ) ;
217  public:
219  FUNCTION_OBJECT_DEF( Adapter3DoubleFunction )
220  public:
225 
226  double operator() ( double x ) const override ;
227 
228  double operator() ( const Argument& x ) const override ;
229 
230  unsigned int dimensionality() const override { return 3 ; }
232  bool hasAnalyticDerivative() const override { return true ; }
234  Genfun::Derivative partial( unsigned int i ) const override ;
235  public:
236  double operator() ( const double x ,
237  const double y ,
238  const double z ) const ;
239  private:
240  // assignment operator is disabled
242  private:
243  Function m_func = nullptr ;
244  };
246  FUNCTION_OBJECT_IMP( Adapter3DoubleFunction )
247 
248  // ========================================================================
255  // ========================================================================
256  class GAUDI_API SimpleFunction : public AbsFunction
257  {
258  public:
259  typedef double (*Function1)( const double ) ;
260  typedef double (*Function2)( const double* ) ;
261  typedef double (*Function3)( const std::vector<double>& ) ;
262  public:
264  FUNCTION_OBJECT_DEF( SimpleFunction )
265 
266  public:
267 
271  SimpleFunction ( Function1 func ) ;
272 
277  SimpleFunction ( Function2 func ,
278  const size_t dim ) ;
279 
284  SimpleFunction ( Function3 func ,
285  const size_t dim ) ;
286 
287  public:
288 
290  unsigned int dimensionality () const override {
291  return detail::dispatch_variant( m_func,
292  [](Function1) { return 1ul; },
293  [&](auto) { return m_arg.size(); } );
294  }
296  bool hasAnalyticDerivative () const override { return true ; }
298  double operator() ( double ) const override ;
300  double operator() ( const Argument& ) const override ;
302  Genfun::Derivative partial ( unsigned int i ) const override ;
303 
304  private:
305 
306  boost::variant<Function1,Function2,Function3> m_func;
308  };
310  FUNCTION_OBJECT_IMP( SimpleFunction )
311 
312  } // end of namespace GaudiMathImeplementation
313 } // end of namespace Genfun
314 
315 #if defined(__clang__) || defined(__CLING__)
316 #pragma clang diagnostic pop
317 #elif defined(__GNUC__) && __GNUC__ >= 5
318 #pragma GCC diagnostic pop
319 #endif
320 
321 #endif // GAUDIMATH_FUNADAPTERS_H
322 // ============================================================================
composer_t(lambda_t &&lambda, lambda_ts &&...more_lambdas)
Definition: FunAdapters.h:58
composer_t< std::decay_t< lambda_ts >... > compose(lambda_ts &&...lambdas)
Definition: FunAdapters.h:66
composer_t(const lambda_t &lambda)
Definition: FunAdapters.h:46
auto dispatch_variant
Definition: FunAdapters.h:70
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:296
boost::variant< Function1, Function2, Function3 > m_func
Definition: FunAdapters.h:306
STL namespace.
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:256
Genfun::AbsFunction Function
Definition: GaudiMath.h:24
#define class
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:29
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:232
GaudiKernel.
Definition: Fill.h:10
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:170
PropertyMgr & operator=(const PropertyMgr &)=delete
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:149
TupleObj.h GaudiAlg/TupleObj.h namespace with few technical implementations.
T move(T...args)
mandatory macro from CLHEP/GenericFunctions
Definition: FunAdapters.h:210
composer_t(const lambda_t &lambda, lambda_ts &&...more_lambdas)
Definition: FunAdapters.h:56
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:47
CLHEP.
Definition: IEqSolver.h:13
#define GAUDI_API
Definition: Kernel.h:107
T forward(T...args)
unsigned int dimensionality() const override
Definition: FunAdapters.h:109
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: FunAdapters.h:112
constructor from the IFunction ( see AIDA/IFunction.h)
Definition: FunAdapters.h:92