Adapter.cpp
Go to the documentation of this file.
1 // Include files
2 // ============================================================================
3 // GaudiMath
4 // ============================================================================
5 #include "GaudiMath/GaudiMath.h"
6 // ============================================================================
7 // AIDA
8 // ============================================================================
9 #include "AIDA/IFunction.h"
10 // ============================================================================
11 #include <cstring>
12 
13 namespace Genfun
14 {
15  namespace GaudiMathImplementation
16  {
17 
18  FUNCTION_OBJECT_IMP( AdapterIFunction )
19 
20  AdapterIFunction::AdapterIFunction ( const AIDA::IFunction& fun)
21  : AbsFunction ( )
22  , m_fun ( &fun )
23  , m_dim ( fun.dimension() )
24  , m_arg ( fun.dimension() , 0 )
25  {}
26 
28  : AbsFunction ( )
29  , m_fun ( right.m_fun )
30  , m_dim ( right.m_dim )
31  , m_arg ( right.m_arg )
32  {}
33 
35 
36  double AdapterIFunction::operator() ( double x ) const
37  {
38  for ( size_t i = 0; i < m_dim; ++i ) { m_arg[i] = 0.0 ; }
39  m_arg[0] = x ;
40  return m_fun -> value ( m_arg ) ;
41  }
42 
43  double AdapterIFunction::operator() ( const Genfun::Argument& x ) const
44  {
45  for ( size_t i = 0; i < m_dim; ++i ) { m_arg[i] = x[i] ; }
46  return m_fun -> value ( m_arg ) ;
47  }
48 
50  {
51  if ( i >= m_dim )
52  {
53  const AbsFunction& aux = GaudiMath::Constant( 0 , m_dim ) ;
54  return Genfun::FunctionNoop( &aux ) ;
55  };
56  const AbsFunction& aux = GaudiMath::Derivative( *this , i ) ;
57  return Genfun::FunctionNoop ( &aux ) ;
58  }
59 
60  FUNCTION_OBJECT_IMP( Adapter2DoubleFunction )
61 
64  : AbsFunction ( )
65  , m_func ( func )
66  {}
67 
69  ( const Adapter2DoubleFunction& right )
70  : AbsFunction ( )
71  , m_func ( right.m_func )
72  {}
73 
75 
76  double Adapter2DoubleFunction::operator()
77  ( double x ) const
78  { return m_func ( x , 0 ) ; }
79 
80  double Adapter2DoubleFunction::operator()
81  ( const Genfun::Argument& x ) const
82  { return m_func ( x[0] , x[1] ) ; }
83 
84  double Adapter2DoubleFunction::operator()
85  ( const double x ,
86  const double y ) const
87  { return m_func ( x , y ) ; }
88 
91  {
92  if ( i >= 2 )
93  {
94  const AbsFunction& aux = GaudiMath::Constant( 0 , 2 ) ;
95  return Genfun::FunctionNoop( &aux ) ;
96  };
97  const AbsFunction& aux = GaudiMath::Derivative( *this , i ) ;
98  return Genfun::FunctionNoop ( &aux ) ;
99  }
100 
101  FUNCTION_OBJECT_IMP( Adapter3DoubleFunction )
102 
105  : AbsFunction ( )
106  , m_func ( func )
107  {}
108 
110  ( const Adapter3DoubleFunction& right )
111  : AbsFunction ( )
112  , m_func ( right.m_func )
113  {}
114 
116 
117  double Adapter3DoubleFunction::operator()
118  ( double x ) const
119  { return m_func ( x , 0 , 0 ) ; }
120 
121  double Adapter3DoubleFunction::operator()
122  ( const Genfun::Argument& x ) const
123  { return m_func ( x[0] , x[1] , x[2] ) ; }
124 
125  double Adapter3DoubleFunction::operator()
126  ( const double x ,
127  const double y ,
128  const double z ) const
129  { return m_func ( x , y , z ) ; }
130 
133  {
134  if ( i >= 3 )
135  {
136  const AbsFunction& aux = GaudiMath::Constant( 0 , 3 ) ;
137  return Genfun::FunctionNoop( &aux ) ;
138  };
139  const AbsFunction& aux = GaudiMath::Derivative( *this , i ) ;
140  return Genfun::FunctionNoop ( &aux ) ;
141  }
142 
143  // ========================================================================
145  // ========================================================================
146  FUNCTION_OBJECT_IMP( SimpleFunction )
147  // ========================================================================
148 
149  // =======================================================================
153  // ========================================================================
155  ( SimpleFunction::Function1 func )
156  : AbsFunction ()
157  , m_case ( SimpleFunction::TrivialArg )
158  , m_DIM ( 1 )
159  , m_func1 ( func )
160  , m_func2 ( nullptr )
161  , m_func3 ( nullptr )
162  {}
163  // ========================================================================
164 
165  // ========================================================================
170  // ========================================================================
173  const size_t dim )
174  : AbsFunction ()
175  , m_case ( SimpleFunction::ArrayArg )
176  , m_DIM ( dim )
177  , m_func1 ( nullptr )
178  , m_func2 ( func )
179  , m_arg2 ( new double[dim] )
180  , m_func3 ( nullptr )
181  {
182  }
183  // ========================================================================
184 
185  // ========================================================================
190  // ========================================================================
193  const size_t dim )
194  : AbsFunction ()
195  , m_case ( SimpleFunction::VectorArg )
196  , m_DIM ( dim )
197  , m_func1 ( nullptr )
198  , m_func2 ( nullptr )
199  , m_func3 ( func )
200  , m_arg3 ( dim , 0 )
201  {}
202  // ========================================================================
203 
204  // ========================================================================
206  // ========================================================================
208  ( const SimpleFunction& right )
209  : AbsFunction ()
210  , m_case ( right.m_case )
211  , m_DIM ( right.m_DIM )
212  , m_func1 ( right.m_func1 )
213  , m_func2 ( right.m_func2 )
214  , m_func3 ( right.m_func3 )
215  , m_arg3 ( right.m_arg3 )
216  {
217  if ( right.m_arg2 )
218  {
219  m_arg2.reset( new double[m_DIM] );
220  std::copy_n(right.m_arg2.get(), m_DIM, m_arg2.get());
221  }
222  }
223  // ========================================================================
224 
225  // ========================================================================
226 
227  // ========================================================================
229  // ========================================================================
231  {
232  if ( i >= m_DIM )
233  {
234  const AbsFunction& aux = GaudiMath::Constant( 0 , m_DIM ) ;
235  return Genfun::FunctionNoop( &aux ) ;
236  }
237  const AbsFunction& aux = GaudiMath::Derivative( *this , i ) ;
238  return Genfun::FunctionNoop( &aux );
239  }
240 
241  // ========================================================================
243  // ========================================================================
244  double SimpleFunction::operator() ( double value ) const
245  {
246  double result = 0 ;
247  switch ( m_case )
248  {
249  case TrivialArg :
250  result = (*m_func1) ( value ) ; break ;
251  case ArrayArg :
252  std::fill_n( m_arg2.get() , m_DIM , 0.0 );
253  m_arg2[0] = value ;
254  result = (*m_func2) ( m_arg2.get() ) ; break ;
255  case VectorArg :
256  std::fill ( m_arg3.begin () , m_arg3.end () , 0.0 );
257  m_arg3[0] = value ;
258  result = (*m_func3) ( m_arg3 ) ; break ;
259  default:
260  break ;
261  };
262  return result ;
263  }
264  // ========================================================================
265 
266  // ========================================================================
268  // ========================================================================
269  double SimpleFunction::operator() ( const Argument& argument ) const
270  {
271  double result = 0 ;
272  switch ( m_case )
273  {
274  case TrivialArg :
275  result = (*m_func1) ( argument[0] ) ; break ;
276  case ArrayArg :
277  for( size_t i = 0 ; i < m_DIM ; ++i ) { m_arg2[i] = argument[i] ; }
278  return (*m_func2)( m_arg2.get() ) ; ; break ;
279  case VectorArg :
280  for( size_t i = 0 ; i < m_DIM ; ++i ) { m_arg3[i] = argument[i] ; }
281  result = (*m_func3) ( m_arg3 ) ; break ;
282  default:
283  break ;
284  }
285  return result ;
286  }
287 
288  } // end of namespace GaudiMathImplementation
289 } // end of namespace Genfun
290 
291 
292 // ============================================================================
293 // The END
294 // ============================================================================
virtual Genfun::Derivative partial(unsigned int i) const
Derivatives.
Definition: Adapter.cpp:230
virtual double operator()(double a) const
Definition: Adapter.cpp:36
virtual Genfun::Derivative partial(unsigned int i) const
Derivatives.
Definition: Adapter.cpp:90
virtual Genfun::Derivative partial(unsigned int i) const
Derivatives.
Definition: Adapter.cpp:49
T end(T...args)
Genfun::AbsFunction Function
Definition: GaudiMath.h:24
T copy_n(T...args)
Genfun::GaudiMathImplementation::NumericalDerivative Derivative
Definition: GaudiMath.h:29
virtual Genfun::Derivative partial(unsigned int i) const
Derivatives.
Definition: Adapter.cpp:132
GaudiKernel.
Definition: Fill.h:8
constructor from the trivial function with two argument
Definition: FunAdapters.h:91
virtual double operator()(double) const
Function value.
Definition: Adapter.cpp:244
constructor from the trivial function with two argument
Definition: FunAdapters.h:154
T get(T...args)
T begin(T...args)
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:26
T fill_n(T...args)
double(* Function3)(const std::vector< double > &)
Definition: FunAdapters.h:207
CLHEP.
Definition: IEqSolver.h:13
T fill(T...args)
list i
Definition: ana.py:128
Genfun::GaudiMathImplementation::Constant Constant
Definition: GaudiMath.h:27
constructor from the IFunction ( see AIDA/IFunction.h)
Definition: FunAdapters.h:31