The Gaudi Framework  v29r0 (ff2e7097)
Splines.h
Go to the documentation of this file.
1 #ifndef GAUDIMATH_SPLINES_H
2 #define GAUDIMATH_SPLINES_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD/STL
7 // ============================================================================
8 #include <algorithm>
9 #include <memory>
10 #include <utility>
11 #include <vector>
12 // ============================================================================
13 // from CLHEP
14 // ============================================================================
15 #include "CLHEP/GenericFunctions/AbsFunction.hh"
16 #include "CLHEP/GenericFunctions/Argument.hh"
17 #include "CLHEP/GenericFunctions/GenericFunctions.hh"
18 // ============================================================================
19 // GaudiGSL/GaudiMath
20 // ============================================================================
22 // ============================================================================
23 // GSL
24 // ============================================================================
25 #include "gsl/gsl_interp.h"
26 #include "gsl/gsl_spline.h"
27 // ============================================================================
28 #include "GaudiKernel/Kernel.h"
29 
30 #ifdef __clang__
31 #pragma clang diagnostic push
32 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
33 #endif
34 
35 namespace Genfun
36 {
37  namespace GaudiMathImplementation
38  {
39  namespace details
40  {
41  struct gsl_deleter {
42  void operator()( gsl_spline* p )
43  {
44  if ( p ) gsl_spline_free( p );
45  }
46  void operator()( gsl_interp_accel* p )
47  {
48  if ( p ) gsl_interp_accel_free( p );
49  }
50  };
51  }
52 
54  {
55  public:
58 
59  public:
65  SplineBase( const Data1D& x, const Data1D& y, const GaudiMath::Interpolation::Type type );
70  SplineBase( const Data2D& data, const GaudiMath::Interpolation::Type type );
77  template <class DATAX, class DATAY>
78  SplineBase( const GaudiMath::Interpolation::Type type, DATAX begin_x, DATAX end_x, DATAY begin_y )
79  : m_type( type )
80  {
81  auto size = std::distance( begin_x, end_x );
82  m_x.reserve( size );
83  std::copy_n( begin_x, size, std::back_inserter( m_x ) );
84  m_y.reserve( size );
85  std::copy_n( begin_y, size, std::back_inserter( m_y ) );
86  }
94  template <class DATA>
95  SplineBase( const GaudiMath::Interpolation::Type type, DATA begin, DATA end ) : m_type( type )
96  {
97  m_x.reserve( end - begin );
98  m_y.reserve( end - begin );
99  for ( ; begin != end; ++begin ) {
100  m_x.push_back( begin->first );
101  m_y.push_back( begin->second );
102  };
103  }
104 
106  SplineBase( SplineBase&& ) = default;
108  SplineBase& operator=( SplineBase&& rhs ) = default;
110  SplineBase( const SplineBase& rhs ) : m_x( rhs.m_x ), m_y( rhs.m_y ), m_type( rhs.m_type )
111  // note that we do NOT copy m_spline, m_accel!
112  {
113  }
116  {
117  *this = SplineBase( rhs );
118  return *this;
119  }
120 
121  public:
123  double eval( const double x ) const;
125  double deriv( const double x ) const;
127  double deriv2( const double x ) const;
129  double integ( const double a, const double b ) const;
130 
131  protected:
132  // initialize
133  void initialize() const;
134 
135  private:
141  };
142 
143  class GAUDI_API GSLSpline : public AbsFunction
144  {
145  public:
148 
149  public:
150  // mandatory macro from CLHEP/GenericFunctions
151  FUNCTION_OBJECT_DEF( GSLSpline )
152  public:
173  GSLSpline( const Data1D& x, const Data1D& y, const GaudiMath::Interpolation::Type type );
192  GSLSpline( const Data2D& data, const GaudiMath::Interpolation::Type type );
219  template <class DATAX, class DATAY>
220  GSLSpline( const GaudiMath::Interpolation::Type type, DATAX begin_x, DATAX end_x, DATAY begin_y )
221  : m_spline( type, begin_x, end_x, begin_y )
222  {
223  }
235  template <class DATA>
236  GSLSpline( const GaudiMath::Interpolation::Type type, DATA begin, DATA end ) : m_spline( type, begin, end )
237  {
238  }
240  GSLSpline( const SplineBase& );
241 
242  public:
244  double operator()( double a ) const override;
246  double operator()( const Argument& x ) const override;
247  unsigned int dimensionality() const override { return 1; }
249  bool hasAnalyticDerivative() const override { return true; }
251  Genfun::Derivative partial( unsigned int i ) const override;
252 
253  public:
255  inline const SplineBase& spline() const { return m_spline; }
257  operator const SplineBase&() const { return spline(); }
259  GSLSpline& operator=( const GSLSpline& ) = delete;
260 
261  private:
262  // the actual spline function
264  };
265  // mandatory macro from CLHEP/GenericFunctions
266  FUNCTION_OBJECT_IMP( GSLSpline )
267 
268  class GAUDI_API GSLSplineDeriv : public AbsFunction
269  {
270  public:
273 
274  public:
275  // mandatory macro from CLHEP/GenericFunctions
276  FUNCTION_OBJECT_DEF( GSLSplineDeriv )
277  public:
298  GSLSplineDeriv( const Data1D& x, const Data1D& y, const GaudiMath::Interpolation::Type type );
317  GSLSplineDeriv( const Data2D& data, const GaudiMath::Interpolation::Type type );
344  template <class DATAX, class DATAY>
345  GSLSplineDeriv( const GaudiMath::Interpolation::Type type, DATAX begin_x, DATAX end_x, DATAY begin_y )
346  : AbsFunction(), m_spline( type, begin_x, end_x, begin_y )
347  {
348  }
360  template <class DATA>
362  : AbsFunction(), m_spline( type, begin, end )
363  {
364  }
366  GSLSplineDeriv( const SplineBase& );
368  GSLSplineDeriv( const GSLSplineDeriv& );
369 
370  public:
372  double operator()( double a ) const override;
374  double operator()( const Argument& x ) const override;
375  unsigned int dimensionality() const override { return 1; }
377  bool hasAnalyticDerivative() const override { return true; }
379  Genfun::Derivative partial( unsigned int i ) const override;
380 
381  public:
383  inline const SplineBase& spline() const { return m_spline; }
385  operator const SplineBase&() const { return spline(); }
386 
387  private:
389  GSLSplineDeriv();
392 
393  private:
394  // the actual spline function
396  };
397  // mandatory macro from CLHEP/GenericFunctions
398  FUNCTION_OBJECT_IMP( GSLSplineDeriv )
399 
400  class GAUDI_API GSLSplineDeriv2 : public AbsFunction
401  {
402  public:
405 
406  public:
407  // mandatory macro from CLHEP/GenericFunctions
408  FUNCTION_OBJECT_DEF( GSLSplineDeriv2 )
409  public:
430  GSLSplineDeriv2( const Data1D& x, const Data1D& y, const GaudiMath::Interpolation::Type type );
449  GSLSplineDeriv2( const Data2D& data, const GaudiMath::Interpolation::Type type );
476  template <class DATAX, class DATAY>
477  GSLSplineDeriv2( const GaudiMath::Interpolation::Type type, DATAX begin_x, DATAX end_x, DATAY begin_y )
478  : AbsFunction(), m_spline( type, begin_x, end_x, begin_y )
479  {
480  }
492  template <class DATA>
494  : AbsFunction(), m_spline( type, begin, end )
495  {
496  }
498  GSLSplineDeriv2( const SplineBase& );
500  GSLSplineDeriv2( const GSLSplineDeriv2& ) = default;
501 
502  public:
504  double operator()( double a ) const override;
506  double operator()( const Argument& x ) const override;
507  unsigned int dimensionality() const override { return 1; }
509  bool hasAnalyticDerivative() const override { return true; }
511  Genfun::Derivative partial( unsigned int i ) const override;
512 
513  public:
515  inline const SplineBase& spline() const { return m_spline; }
517  operator const SplineBase&() const { return spline(); }
518 
519  private:
521  GSLSplineDeriv2& operator=( const GSLSplineDeriv2& ) = delete;
522 
523  private:
524  // the actual spline function
526  };
527  // mandatory macro from CLHEP/GenericFunctions
528  FUNCTION_OBJECT_IMP( GSLSplineDeriv2 )
529 
530  class GAUDI_API GSLSplineInteg : public AbsFunction
531  {
532  public:
535 
536  public:
537  // mandatory macro from CLHEP/GenericFunctions
538  FUNCTION_OBJECT_DEF( GSLSplineInteg )
539  public:
560  GSLSplineInteg( const Data1D& x, const Data1D& y, const GaudiMath::Interpolation::Type type,
561  const double low = 0 );
581  GSLSplineInteg( const Data2D& data, const GaudiMath::Interpolation::Type type, const double low = 0 );
609  template <class DATAX, class DATAY>
610  GSLSplineInteg( const GaudiMath::Interpolation::Type type, DATAX begin_x, DATAX end_x, DATAY begin_y,
611  const double low )
612  : AbsFunction(), m_spline( type, begin_x, end_x, begin_y ), m_low( low )
613  {
614  }
627  template <class DATA>
628  GSLSplineInteg( const GaudiMath::Interpolation::Type type, DATA&& begin, DATA&& end, const double low )
629  : m_spline( type, std::forward<DATA>( begin ), std::forward<DATA>( end ) ), m_low( low )
630  {
631  }
633  GSLSplineInteg( const SplineBase&, const double low = 0 );
635  GSLSplineInteg( const GSLSplineInteg& ) = default;
636 
637  public:
639  double operator()( double a ) const override;
641  double operator()( const Argument& x ) const override;
642  unsigned int dimensionality() const override { return 1; }
644  bool hasAnalyticDerivative() const override { return true; }
646  Genfun::Derivative partial( unsigned int i ) const override;
647 
648  public:
650  inline const SplineBase& spline() const { return m_spline; }
652  operator const SplineBase&() const { return spline(); }
653 
654  private:
656  GSLSplineInteg& operator=( const GSLSplineInteg& ) = delete;
657 
658  private:
659  // the actual spline function
661  double m_low;
662  };
663  // mandatory macro from CLHEP/GenericFunctions
664  FUNCTION_OBJECT_IMP( GSLSplineInteg )
665  }
666 }
667 
668 #ifdef __clang__
669 #pragma clang diagnostic pop
670 #endif
671 
672 #endif // GAUDIMATH_SPLINES_H
673 // ============================================================================
SplineBase(const GaudiMath::Interpolation::Type type, DATA begin, DATA end)
templated constructor from the sequence of (x,y(x)) pairs as sequence of pairs the class TabulatedPro...
Definition: Splines.h:95
SplineBase(const SplineBase &rhs)
copy constructor
Definition: Splines.h:110
const SplineBase & spline() const
acess to the spline function
Definition: Splines.h:650
T distance(T...args)
std::vector< std::pair< double, double > > Data2D
Definition: Splines.h:57
GSLSplineDeriv2(const GaudiMath::Interpolation::Type type, DATA begin, DATA end)
templated constructor from the sequence of pairs as sequence of pairs the class TabulatedProperty can...
Definition: Splines.h:493
unsigned int dimensionality() const override
Definition: Splines.h:375
const SplineBase & spline() const
acess to the spline function
Definition: Splines.h:255
const SplineBase & spline() const
acess to the spline function
Definition: Splines.h:383
GSLSplineDeriv(const GaudiMath::Interpolation::Type type, DATA begin, DATA end)
templated constructor from the sequence of pairs as sequence of pairs the class TabulatedProperty can...
Definition: Splines.h:361
STL namespace.
std::unique_ptr< gsl_spline, details::gsl_deleter > m_spline
Definition: Splines.h:138
T copy_n(T...args)
#define class
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:27
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
PropertyMgr & operator=(const PropertyMgr &)=delete
GSLSplineInteg(const GaudiMath::Interpolation::Type type, DATA &&begin, DATA &&end, const double low)
templated constructor from the sequence of pairs as sequence of pairs the class TabulatedProperty can...
Definition: Splines.h:628
unsigned int dimensionality() const override
Definition: Splines.h:642
GSLSpline(const GaudiMath::Interpolation::Type type, DATA begin, DATA end)
templated constructor from the sequence of pairs as sequence of pairs the class TabulatedProperty can...
Definition: Splines.h:236
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: Splines.h:509
GaudiMath::Interpolation::Type m_type
transient
Definition: Splines.h:140
SplineBase & operator=(const SplineBase &rhs)
assignment
Definition: Splines.h:115
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
SplineBase(const GaudiMath::Interpolation::Type type, DATAX begin_x, DATAX end_x, DATAY begin_y)
templated constructor in the spirit of STL-algorithms
Definition: Splines.h:78
unsigned int dimensionality() const override
Definition: Splines.h:247
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: Splines.h:249
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: Splines.h:644
T back_inserter(T...args)
bool hasAnalyticDerivative() const override
Does this function have an analytic derivative?
Definition: Splines.h:377
GaudiMath.h GaudiMath/GaudiMath.h.
Definition: Adapters.h:13
std::unique_ptr< gsl_interp_accel, details::gsl_deleter > m_accel
transient
Definition: Splines.h:139
CLHEP.
Definition: IEqSolver.h:13
const SplineBase & spline() const
acess to the spline function
Definition: Splines.h:515
#define GAUDI_API
Definition: Kernel.h:110
Type
the list of available types for ntuples
Definition: TupleObj.h:84
unsigned int dimensionality() const override
Definition: Splines.h:507