The Gaudi Framework  v28r3 (cc1cf868)
Splines.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // GSL
5 // ============================================================================
6 #include "gsl/gsl_interp.h"
7 #include "gsl/gsl_spline.h"
8 // ============================================================================
9 // GaudiMath
10 // ============================================================================
11 #include "GaudiMath/Splines.h"
12 #include "GaudiMath/GaudiMath.h"
13 // ============================================================================
14 #include <cstring>
15 
16 
23 namespace Genfun
24 {
25  namespace GaudiMathImplementation
26  {
33  ( const SplineBase::Data1D& x ,
34  const SplineBase::Data1D& y ,
36  : m_x ( x )
37  , m_y ( y )
38  , m_type ( type )
39  {
40 
41  }
42  // ========================================================================
43 
44  // ========================================================================
49  // ========================================================================
51  ( const SplineBase::Data2D& data ,
53  : m_type ( type )
54  {
55  m_x.reserve(data.size());
56  m_y.reserve(data.size());
57  for( const auto& i : data ) {
58  m_x.push_back( i.first );
59  m_y.push_back( i.second );
60  }
61  }
62  // ========================================================================
63 
64 
65  // ========================================================================
67  {
68  if ( m_spline ) { return ; } // RETURN
69 
70  const gsl_interp_type* T = nullptr ;
71 
72  switch ( m_type )
73  {
75  T = gsl_interp_linear ; break ;
77  T = gsl_interp_polynomial ; break ;
79  T = gsl_interp_cspline ; break ;
81  T = gsl_interp_cspline_periodic ; break ;
83  T = gsl_interp_akima ; break ;
85  T = gsl_interp_akima_periodic ; break ;
86  default :
87  T = gsl_interp_cspline ; break ;
88  };
89 
90  m_spline.reset( gsl_spline_alloc( T , m_x.size() ) );
91  gsl_spline_init( m_spline.get() , m_x.data() , m_y.data() , m_x.size() ) ;
92  m_accel.reset( gsl_interp_accel_alloc() );
93 
94  if (!m_accel) m_spline.reset();
95 
96 
97  }
98  // ========================================================================
99 
100  // ========================================================================
101  double SplineBase::eval ( const double x ) const
102  {
103  if ( !m_spline ) { initialize() ; }
104  return gsl_spline_eval ( m_spline.get() , x , m_accel.get() );
105  }
106  // ========================================================================
107 
108  // ========================================================================
109  double SplineBase::deriv ( const double x ) const
110  {
111  if ( !m_spline ) { initialize() ; }
112  return gsl_spline_eval_deriv ( m_spline.get() , x , m_accel.get() );
113  }
114  // ========================================================================
115 
116  // ========================================================================
117  double SplineBase::deriv2 ( const double x ) const
118  {
119  if ( !m_spline ) { initialize() ; }
120  return gsl_spline_eval_deriv2 ( m_spline.get() , x , m_accel.get() );
121  }
122  // ========================================================================
123 
124  // ========================================================================
125  double SplineBase::integ ( const double a ,
126  const double b ) const
127  {
128  if ( !m_spline ) { initialize() ; }
129  return gsl_spline_eval_integ ( m_spline.get() , a , b , m_accel.get() ) ;
130  }
131  // ========================================================================
132 
133 
134  // ========================================================================
135  FUNCTION_OBJECT_IMP( GSLSpline )
136  // ========================================================================
137 
138  // ========================================================================
159  // ========================================================================
161  ( const GSLSpline::Data1D& x ,
162  const GSLSpline::Data1D& y ,
163  const GaudiMath::Interpolation::Type type )
164  : AbsFunction ()
165  , m_spline ( x , y , type )
166  {}
167  // ========================================================================
168 
169  // ========================================================================
188  // ========================================================================
190  ( const GSLSpline::Data2D& data ,
191  const GaudiMath::Interpolation::Type type )
192  : AbsFunction ()
193  , m_spline ( data , type )
194  {}
195  // ========================================================================
196 
197  // ========================================================================
199  // ========================================================================
201  ( const SplineBase& right )
202  : AbsFunction ()
203  , m_spline ( right )
204  {}
205  // ========================================================================
206 
207  // ========================================================================
208  double GSLSpline::operator() ( double x ) const
209  { return m_spline.eval( x ) ; }
210  // ========================================================================
211  double GSLSpline::operator() ( const Argument& x ) const
212  { return m_spline.eval( x[0] ) ; }
213  // ========================================================================
214 
215  // ========================================================================
217  // ========================================================================
218  Genfun::Derivative GSLSpline::partial( unsigned int i ) const
219  {
220  if ( i >= 1 )
221  {
222  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
223  return Genfun::FunctionNoop( &aux ) ;
224  }
225  const AbsFunction& aux = GSLSplineDeriv( *this ) ;
226  return Genfun::FunctionNoop( &aux ) ;
227  }
228  // ========================================================================
229 
230  // ========================================================================
231  FUNCTION_OBJECT_IMP( GSLSplineDeriv )
232  // ========================================================================
233 
234  // ========================================================================
255  // ========================================================================
258  const GSLSplineDeriv::Data1D& y ,
259  const GaudiMath::Interpolation::Type type )
260  : AbsFunction ()
261  , m_spline ( x , y , type )
262  {}
263  // ========================================================================
264 
265  // ========================================================================
284  // ========================================================================
286  ( const GSLSplineDeriv::Data2D& data ,
287  const GaudiMath::Interpolation::Type type )
288  : AbsFunction ()
289  , m_spline ( data , type )
290  {}
291  // ========================================================================
292 
293  // ========================================================================
295  // ========================================================================
297  ( const SplineBase& right )
298  : AbsFunction ()
299  , m_spline ( right )
300  {}
301  // ========================================================================
302 
303  // ========================================================================
305  // ========================================================================
307  ( const GSLSplineDeriv& right )
308  : AbsFunction ()
309  , m_spline ( right )
310  {}
311  // ========================================================================
312 
313  // ========================================================================
314  double GSLSplineDeriv::operator() ( double x ) const
315  { return m_spline.deriv ( x ) ; }
316  // ========================================================================
317  double GSLSplineDeriv::operator() ( const Argument& x ) const
318  { return m_spline.deriv ( x[0] ) ; }
319  // ========================================================================
320 
321  // ========================================================================
323  // ========================================================================
325  {
326  if ( i >= 1 )
327  {
328  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
329  return Genfun::FunctionNoop( &aux ) ;
330  }
331  const AbsFunction& aux = GSLSplineDeriv2( *this ) ;
332  return Genfun::FunctionNoop( &aux ) ;
333  }
334  // ========================================================================
335 
336  // ========================================================================
337  FUNCTION_OBJECT_IMP( GSLSplineDeriv2 )
338  // ========================================================================
339 
340  // ========================================================================
361  // ========================================================================
364  const GSLSplineDeriv2::Data1D& y ,
365  const GaudiMath::Interpolation::Type type )
366  : AbsFunction ()
367  , m_spline ( x , y , type )
368  {}
369  // ========================================================================
370 
371  // ========================================================================
390  // ========================================================================
392  ( const GSLSplineDeriv2::Data2D& data ,
393  const GaudiMath::Interpolation::Type type )
394  : AbsFunction ()
395  , m_spline ( data , type )
396  {}
397  // ========================================================================
398 
399  // ========================================================================
401  // ========================================================================
403  ( const SplineBase& right )
404  : AbsFunction ()
405  , m_spline ( right )
406  {}
407  // ========================================================================
408 
409  // ========================================================================
410  double GSLSplineDeriv2::operator() ( double x ) const
411  { return m_spline.deriv2 ( x ) ; }
412  // ========================================================================
413  double GSLSplineDeriv2::operator() ( const Argument& x ) const
414  { return m_spline.deriv2 ( x[0] ) ; }
415  // ========================================================================
416 
417  // ========================================================================
419  // ========================================================================
421  {
422  if ( i >= 1 )
423  {
424  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
425  return Genfun::FunctionNoop( &aux ) ;
426  }
427  const AbsFunction& aux = GaudiMath::Derivative( *this , i ) ;
428  return Genfun::FunctionNoop( &aux ) ;
429  }
430  // ========================================================================
431 
432 
433  // ========================================================================
434  FUNCTION_OBJECT_IMP( GSLSplineInteg )
435  // ========================================================================
436 
437  // ========================================================================
458  // ========================================================================
461  const GSLSplineInteg::Data1D& y ,
462  const GaudiMath::Interpolation::Type type ,
463  const double low )
464  : AbsFunction ( )
465  , m_spline ( x , y , type )
466  , m_low ( low )
467  {}
468  // ========================================================================
469 
470  // ========================================================================
489  // ========================================================================
491  ( const GSLSplineInteg::Data2D& data ,
492  const GaudiMath::Interpolation::Type type ,
493  const double low )
494  : AbsFunction ()
495  , m_spline ( data , type )
496  , m_low ( low )
497  {}
498  // ========================================================================
499 
500  // ========================================================================
502  // ========================================================================
504  ( const SplineBase& right ,
505  const double low )
506  : AbsFunction ()
507  , m_spline ( right )
508  , m_low ( low )
509  {}
510  // ========================================================================
511 
512  // ========================================================================
513  double GSLSplineInteg::operator() ( double x ) const
514  { return m_spline.integ ( m_low , x ) ; }
515  // ========================================================================
516  double GSLSplineInteg::operator() ( const Argument& x ) const
517  { return m_spline.integ ( m_low , x[0] ) ; }
518  // ========================================================================
519 
520  // ========================================================================
522  // ========================================================================
524  {
525  if ( i >= 1 )
526  {
527  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
528  return Genfun::FunctionNoop( &aux ) ;
529  }
530  const AbsFunction& aux = GSLSpline( *this ) ;
531  return Genfun::FunctionNoop( &aux ) ;
532  }
533  // ========================================================================
534 
535  }
536 }
double integ(const double a, const double b) const
evaluate the integral on [a,b]
Definition: Splines.cpp:125
double operator()(double a) const override
main method: evaluate the function
Definition: Splines.cpp:314
SplineBase(const Data1D &x, const Data1D &y, const GaudiMath::Interpolation::Type type)
constructor from vectors and type
Definition: Splines.cpp:33
double eval(const double x) const
evaluate the function
Definition: Splines.cpp:101
double operator()(double a) const override
main method: evaluate the function
Definition: Splines.cpp:410
double operator()(double a) const override
main method: evaluate the function
Definition: Splines.cpp:513
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:523
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:29
T data(T...args)
GaudiMath::Interpolation::Type m_type
transient
Definition: Splines.h:141
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:324
double deriv2(const double x) const
evaluate the second derivative
Definition: Splines.cpp:117
double deriv(const double x) const
evaluate the first derivative
Definition: Splines.cpp:109
GSLSplineDeriv2(const Data1D &x, const Data1D &y, const GaudiMath::Interpolation::Type type)
mandatory macro from CLHEP/GenericFunctions
Definition: Splines.cpp:363
GSLSplineDeriv()
default construtor is desabled ;
T reset(T...args)
double operator()(double a) const override
main methgod: evaluate teh function
Definition: Splines.cpp:208
T get(T...args)
T size(T...args)
GSLSpline(const Data1D &x, const Data1D &y, const GaudiMath::Interpolation::Type type)
mandatory macro from CLHEP/GenericFunctions
Definition: Splines.cpp:161
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:218
CLHEP.
Definition: IEqSolver.h:13
std::unique_ptr< gsl_interp_accel, details::gsl_deleter > m_accel
transient
Definition: Splines.h:140
GSLSplineInteg(const Data1D &x, const Data1D &y, const GaudiMath::Interpolation::Type type, const double low=0)
mandatory macro from CLHEP/GenericFunctions
Definition: Splines.cpp:460
Genfun::GaudiMathImplementation::Constant Constant
Definition: GaudiMath.h:27
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:420
std::unique_ptr< gsl_spline, details::gsl_deleter > m_spline
Definition: Splines.h:139