All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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_init ( false )
37  , m_dim ( x.size() )
38  , m_x ( new double[ x.size() ] )
39  , m_y ( new double[ y.size() ] )
40  , m_spline ( nullptr )
41  , m_accel ( nullptr )
42  , m_type ( type )
43  {
44 #ifdef WIN32
45 // Disable the warning
46 // C4996: 'std::copy': Function call with parameters that may be unsafe
47 // The parameters are checked
48 #pragma warning(push)
49 #pragma warning(disable:4996)
50 #endif
51  std::copy( x.begin() , x.end() , m_x.get() ) ;
52  std::copy( y.begin() , y.end() , m_y.get() ) ;
53 #ifdef WIN32
54 #pragma warning(pop)
55 #endif
56 
57  }
58  // ========================================================================
59 
60  // ========================================================================
65  // ========================================================================
67  ( const SplineBase::Data2D& data ,
69  : m_init ( false )
70  , m_dim ( data.size() )
71  , m_x ( new double[ data.size() ] )
72  , m_y ( new double[ data.size() ] )
73  , m_type ( type )
74  {
75  double* _x = m_x.get() ;
76  double* _y = m_y.get() ;
77  for( const auto& i : data )
78  {
79  *_x = i.first ; ++_x ;
80  *_y = i.second ; ++_y ;
81  }
82  }
83  // ========================================================================
84 
85  // ========================================================================
87  // ========================================================================
89  : m_init ( false )
90  , m_dim ( right.m_dim )
91  , m_x ( new double[ right.m_dim ] )
92  , m_y ( new double[ right.m_dim ] )
93  , m_type ( right.m_type )
94  {
95  std::copy_n(right.m_x.get(),m_dim,m_x.get());
96  std::copy_n(right.m_y.get(),m_dim,m_y.get());
97  }
98  // ========================================================================
99 
100  // ========================================================================
102  // ========================================================================
104  {
105  if ( m_spline ) { gsl_spline_free ( m_spline ) ; }
106  if ( m_accel ) { gsl_interp_accel_free ( m_accel ) ; }
107  }
108  // ========================================================================
109 
110  // ========================================================================
112  {
113  if ( m_init ) { return ; } // RETURN
114 
115  const gsl_interp_type* T = nullptr ;
116 
117  switch ( m_type )
118  {
120  T = gsl_interp_linear ; break ;
122  T = gsl_interp_polynomial ; break ;
124  T = gsl_interp_cspline ; break ;
126  T = gsl_interp_cspline_periodic ; break ;
128  T = gsl_interp_akima ; break ;
130  T = gsl_interp_akima_periodic ; break ;
131  default :
132  T = gsl_interp_cspline ; break ;
133  };
134 
135  m_spline = gsl_spline_alloc( T , m_dim ) ;
136 
137  gsl_spline_init( m_spline , m_x.get() , m_y.get() , m_dim ) ;
138 
139  m_accel = gsl_interp_accel_alloc() ;
140 
141  m_init = true ;
142 
143  }
144  // ========================================================================
145 
146  // ========================================================================
147  double SplineBase::eval ( const double x ) const
148  {
149  if ( !m_init ) { initialize() ; }
150  return gsl_spline_eval ( m_spline , x , m_accel );
151  }
152  // ========================================================================
153 
154  // ========================================================================
155  double SplineBase::deriv ( const double x ) const
156  {
157  if ( !m_init ) { initialize() ; }
158  return gsl_spline_eval_deriv ( m_spline , x , m_accel );
159  }
160  // ========================================================================
161 
162  // ========================================================================
163  double SplineBase::deriv2 ( const double x ) const
164  {
165  if ( !m_init ) { initialize() ; }
166  return gsl_spline_eval_deriv2 ( m_spline , x , m_accel );
167  }
168  // ========================================================================
169 
170  // ========================================================================
171  double SplineBase::integ ( const double a ,
172  const double b ) const
173  {
174  if ( !m_init ) { initialize() ; }
175  return gsl_spline_eval_integ ( m_spline , a , b , m_accel ) ;
176  }
177  // ========================================================================
178 
179 
180  // ========================================================================
181  FUNCTION_OBJECT_IMP( GSLSpline )
182  // ========================================================================
183 
184  // ========================================================================
205  // ========================================================================
207  ( const GSLSpline::Data1D& x ,
208  const GSLSpline::Data1D& y ,
209  const GaudiMath::Interpolation::Type type )
210  : AbsFunction ()
211  , m_spline ( x , y , type )
212  {}
213  // ========================================================================
214 
215  // ========================================================================
234  // ========================================================================
236  ( const GSLSpline::Data2D& data ,
237  const GaudiMath::Interpolation::Type type )
238  : AbsFunction ()
239  , m_spline ( data , type )
240  {}
241  // ========================================================================
242 
243  // ========================================================================
245  // ========================================================================
247  ( const SplineBase& right )
248  : AbsFunction ()
249  , m_spline ( right )
250  {}
251  // ========================================================================
252 
253  // ========================================================================
255  // ========================================================================
257  ( const GSLSpline& right )
258  : AbsFunction ()
259  , m_spline ( right )
260  {}
261  // ========================================================================
262 
263  // ========================================================================
265  // ========================================================================
266  GSLSpline::~GSLSpline() = default;
267  // ========================================================================
268 
269  // ========================================================================
270  double GSLSpline::operator() ( double x ) const
271  { return m_spline.eval( x ) ; }
272  // ========================================================================
273  double GSLSpline::operator() ( const Argument& x ) const
274  { return m_spline.eval( x[0] ) ; }
275  // ========================================================================
276 
277  // ========================================================================
279  // ========================================================================
280  Genfun::Derivative GSLSpline::partial( unsigned int i ) const
281  {
282  if ( i >= 1 )
283  {
284  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
285  return Genfun::FunctionNoop( &aux ) ;
286  }
287  const AbsFunction& aux = GSLSplineDeriv( *this ) ;
288  return Genfun::FunctionNoop( &aux ) ;
289  }
290  // ========================================================================
291 
292  // ========================================================================
293  FUNCTION_OBJECT_IMP( GSLSplineDeriv )
294  // ========================================================================
295 
296  // ========================================================================
317  // ========================================================================
320  const GSLSplineDeriv::Data1D& y ,
321  const GaudiMath::Interpolation::Type type )
322  : AbsFunction ()
323  , m_spline ( x , y , type )
324  {}
325  // ========================================================================
326 
327  // ========================================================================
346  // ========================================================================
348  ( const GSLSplineDeriv::Data2D& data ,
349  const GaudiMath::Interpolation::Type type )
350  : AbsFunction ()
351  , m_spline ( data , type )
352  {}
353  // ========================================================================
354 
355  // ========================================================================
357  // ========================================================================
359  ( const SplineBase& right )
360  : AbsFunction ()
361  , m_spline ( right )
362  {}
363  // ========================================================================
364 
365  // ========================================================================
367  // ========================================================================
369  ( const GSLSplineDeriv& right )
370  : AbsFunction ()
371  , m_spline ( right )
372  {}
373  // ========================================================================
374 
375  // ========================================================================
377  // ========================================================================
379  // ========================================================================
380 
381  // ========================================================================
382  double GSLSplineDeriv::operator() ( double x ) const
383  { return m_spline.deriv ( x ) ; }
384  // ========================================================================
385  double GSLSplineDeriv::operator() ( const Argument& x ) const
386  { return m_spline.deriv ( x[0] ) ; }
387  // ========================================================================
388 
389  // ========================================================================
391  // ========================================================================
393  {
394  if ( i >= 1 )
395  {
396  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
397  return Genfun::FunctionNoop( &aux ) ;
398  }
399  const AbsFunction& aux = GSLSplineDeriv2( *this ) ;
400  return Genfun::FunctionNoop( &aux ) ;
401  }
402  // ========================================================================
403 
404  // ========================================================================
405  FUNCTION_OBJECT_IMP( GSLSplineDeriv2 )
406  // ========================================================================
407 
408  // ========================================================================
429  // ========================================================================
432  const GSLSplineDeriv2::Data1D& y ,
433  const GaudiMath::Interpolation::Type type )
434  : AbsFunction ()
435  , m_spline ( x , y , type )
436  {}
437  // ========================================================================
438 
439  // ========================================================================
458  // ========================================================================
460  ( const GSLSplineDeriv2::Data2D& data ,
461  const GaudiMath::Interpolation::Type type )
462  : AbsFunction ()
463  , m_spline ( data , type )
464  {}
465  // ========================================================================
466 
467  // ========================================================================
469  // ========================================================================
471  ( const SplineBase& right )
472  : AbsFunction ()
473  , m_spline ( right )
474  {}
475  // ========================================================================
476 
477  // ========================================================================
479  // ========================================================================
481  ( const GSLSplineDeriv2& right )
482  : AbsFunction ()
483  , m_spline ( right )
484  {}
485  // ========================================================================
486 
487  // ========================================================================
489  // ========================================================================
491  // ========================================================================
492 
493  // ========================================================================
494  double GSLSplineDeriv2::operator() ( double x ) const
495  { return m_spline.deriv2 ( x ) ; }
496  // ========================================================================
497  double GSLSplineDeriv2::operator() ( const Argument& x ) const
498  { return m_spline.deriv2 ( x[0] ) ; }
499  // ========================================================================
500 
501  // ========================================================================
503  // ========================================================================
505  {
506  if ( i >= 1 )
507  {
508  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
509  return Genfun::FunctionNoop( &aux ) ;
510  }
511  const AbsFunction& aux = GaudiMath::Derivative( *this , i ) ;
512  return Genfun::FunctionNoop( &aux ) ;
513  }
514  // ========================================================================
515 
516 
517  // ========================================================================
518  FUNCTION_OBJECT_IMP( GSLSplineInteg )
519  // ========================================================================
520 
521  // ========================================================================
542  // ========================================================================
545  const GSLSplineInteg::Data1D& y ,
546  const GaudiMath::Interpolation::Type type ,
547  const double low )
548  : AbsFunction ( )
549  , m_spline ( x , y , type )
550  , m_low ( low )
551  {}
552  // ========================================================================
553 
554  // ========================================================================
573  // ========================================================================
575  ( const GSLSplineInteg::Data2D& data ,
576  const GaudiMath::Interpolation::Type type ,
577  const double low )
578  : AbsFunction ()
579  , m_spline ( data , type )
580  , m_low ( low )
581  {}
582  // ========================================================================
583 
584  // ========================================================================
586  // ========================================================================
588  ( const SplineBase& right ,
589  const double low )
590  : AbsFunction ()
591  , m_spline ( right )
592  , m_low ( low )
593  {}
594  // ========================================================================
595 
596  // ========================================================================
598  // ========================================================================
600  ( const GSLSplineInteg& right )
601  : AbsFunction ()
602  , m_spline ( right )
603  , m_low ( right.m_low )
604  {}
605  // ========================================================================
606 
607  // ========================================================================
609  // ========================================================================
611  // ========================================================================
612 
613  // ========================================================================
614  double GSLSplineInteg::operator() ( double x ) const
615  { return m_spline.integ ( m_low , x ) ; }
616  // ========================================================================
617  double GSLSplineInteg::operator() ( const Argument& x ) const
618  { return m_spline.integ ( m_low , x[0] ) ; }
619  // ========================================================================
620 
621  // ========================================================================
623  // ========================================================================
625  {
626  if ( i >= 1 )
627  {
628  const AbsFunction& aux = GaudiMath::Constant( 0.0 , 1 ) ;
629  return Genfun::FunctionNoop( &aux ) ;
630  }
631  const AbsFunction& aux = GSLSpline( *this ) ;
632  return Genfun::FunctionNoop( &aux ) ;
633  }
634  // ========================================================================
635 
636  }
637 }
double integ(const double a, const double b) const
evaluate the integral on [a,b]
Definition: Splines.cpp:171
double operator()(double a) const override
main method: evaluate the function
Definition: Splines.cpp:382
GSLSpline()=delete
default construtor is disabled ;
T copy(T...args)
GSLSplineDeriv2()=delete
default construtor is disabled ;
double eval(const double x) const
evaluate the function
Definition: Splines.cpp:147
double operator()(double a) const override
main method: evaluate the function
Definition: Splines.cpp:494
double operator()(double a) const override
main method: evaluate the function
Definition: Splines.cpp:614
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:624
T end(T...args)
std::unique_ptr< double[]> m_y
Definition: Splines.h:132
T copy_n(T...args)
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:29
GSLSplineInteg()=delete
default construtor is disabled ;
GaudiMath::Interpolation::Type m_type
Definition: Splines.h:135
std::unique_ptr< double[]> m_x
Definition: Splines.h:131
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:392
double deriv2(const double x) const
evaluate the second derivative
Definition: Splines.cpp:163
double deriv(const double x) const
evaluate the first derivative
Definition: Splines.cpp:155
GSLSplineDeriv()
default construtor is desabled ;
double operator()(double a) const override
main methgod: evaluate teh function
Definition: Splines.cpp:270
T get(T...args)
T size(T...args)
T begin(T...args)
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:280
CLHEP.
Definition: IEqSolver.h:13
Genfun::GaudiMathImplementation::Constant Constant
Definition: GaudiMath.h:27
Genfun::Derivative partial(unsigned int i) const override
Derivatives.
Definition: Splines.cpp:504