ROOT  6.06/09
Reference Guide
OneDimFunctionAdapter.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id: OneDimFunctionAdapter.h 20063 2007-09-24 13:16:14Z moneta $
2 // Author: L. Moneta Wed Dec 6 11:45:55 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class OneDimMultiFunctionAdapter
12 
13 #ifndef ROOT_Math_OneDimFunctionAdapter
14 #define ROOT_Math_OneDimFunctionAdapter
15 
16 #ifndef ROOT_Math_IFunction
17 #include "Math/IFunction.h"
18 #endif
19 #ifndef ROOT_Math_IParamFunction
20 #include "Math/IParamFunction.h"
21 #endif
22 
23 #include <cassert>
24 
25 namespace ROOT {
26 
27 namespace Math {
28 
29 
30 
31 // struct using for evaluating the function
32 template<class MultiFuncType>
34  // evaluate function (in general case no param)
35  static double F (MultiFuncType f, const double * x, const double * = 0 ) {
36  return f( x );
37  }
38 };
39 // specialized for param functions
40 template<>
42  static double F ( const ROOT::Math::IParamMultiFunction & f, const double * x, const double * p = 0 ) {
43  return f( x, p );
44  }
45 };
46 
47 
48 /**
49  OneDimMultiFunctionAdapter class to wrap a multidimensional function in
50  one dimensional one.
51  Given a f(x1,x2,x3,....xn) transforms in a f( x_i) given the coordinate intex i and the vector x[]
52  of the coordinates.
53  It provides the possibility to copy and own the data array of the coordinates or to maintain internally a pointer to an external array
54  for being more efficient. In this last case the user must garantee the life of the given passed pointer
55 
56  @ingroup GenFunc
57 
58 */
59 template <class MultiFuncType = const ROOT::Math::IMultiGenFunction &>
61 
62 public:
63 
64 
65  /**
66  Constructor from the function object , pointer to an external array of x values
67  and coordinate we want to adapt
68  */
69  OneDimMultiFunctionAdapter (MultiFuncType f, const double * x, unsigned int icoord =0, const double * p = 0 ) :
70  fFunc(f),
71  fX( const_cast<double *>(x) ), // wee need to modify x but then we restore it as before
72  fParams(p),
73  fCoord(icoord),
74  fDim(0),
75  fOwn(false)
76  {
77  assert(fX != 0);
78  }
79  /**
80  Constructor from the function object , dimension of the function and
81  and coordinate we want to adapt.
82  The coordinate cached vector is created inside and eventually the values must be passed
83  later with the SetX which will copy them
84  */
85  OneDimMultiFunctionAdapter (MultiFuncType f, unsigned int dim = 1, unsigned int icoord =0, const double * p = 0 ) :
86  fFunc(f),
87  fX(0 ),
88  fParams(p),
89  fCoord(icoord),
90  fDim(dim),
91  fOwn(true)
92  {
93  fX = new double[dim];
94  }
95 
96  /**
97  Destructor (no operations)
98  */
99  virtual ~OneDimMultiFunctionAdapter () { if (fOwn && fX) delete [] fX; }
100 
101  /**
102  clone
103  */
104  virtual OneDimMultiFunctionAdapter * Clone( ) const {
105  if (fOwn) {
107  std::copy(fX, fX+fDim, f->fX);
108  return f;
109  }
110  else
112  }
113 
114 public:
115 
116  /**
117  Set X values in case vector is own, iterator size must match previous
118  set dimension
119  */
120  template<class Iterator>
121  void SetX(Iterator begin, Iterator end) {
122  if (fOwn) std::copy(begin, end, fX);
123  }
124 
125 
126  /**
127  set pointer without copying the values
128  */
129  void SetX(double * x) {
130  if (!fOwn) fX = x;
131  }
132 
133  /**
134  set values
135  */
136  void SetX(const double * x) {
137  if (fOwn) std::copy(x, x+fDim, fX);
138  else
139  SetX( const_cast<double *>(x) ); // wee need to modify x but then we restore it as before
140  }
141 
142 
143  void SetCoord(int icoord) { fCoord=icoord;}
144 
145  // copy constructor
147  fFunc(rhs.fFunc),
148  fParams(rhs.fParams),
149  fCoord(rhs.fCoord),
150  fDim(rhs.fDim),
151  fOwn(rhs.fOwn)
152  {
153  if (fOwn) {
154  fX = new double[fDim];
155  std::copy( rhs.fX, rhs.fX+fDim, fX);
156  }
157  else fX = rhs.fX;
158  }
159 
160 
161 private:
162 
163  // dummy assignment (should never be called and clone must be used)
165  if (this == &rhs) return *this;
166  assert(false);
167  }
168 
169  /**
170  evaluate function at the values x[] given in the constructor and
171  as function of the coordinate fCoord.
172  */
173  double DoEval(double x) const {
174  if (fOwn) {
175  fX[fCoord] = x;
177  }
178  else {
179 
180  // case vector fX represents useful values needed later
181  // need to modify fX and restore afterwards the original values
182  double xprev = fX[fCoord]; // keep original value to restore in fX
183  fX[fCoord] = x;
185  // restore original values
186  fX[fCoord] = xprev;
187  return y;
188  }
189  }
190 
191 
192 private:
193 
194  MultiFuncType fFunc;
195  mutable double * fX;
196  const double * fParams;
197  unsigned int fCoord;
198  unsigned int fDim;
199  bool fOwn;
200 
201 };
202 
203 
204 /**
205  OneDimParamFunctionAdapter class to wrap a multi-dim parameteric function in
206  one dimensional one.
207  Given a f(x[],p1,...pn) transforms in a f( p_i) given the param index i and the vectors x[] and p[]
208  of the coordinates and parameters
209  It has to be used carefully, since for efficiency reason it does not copy the parameter object
210  but re-uses the given pointer for the p[] vector.
211  The ParamFuncType reference by default is not const because the operator()(x,p) is not a const method
212 
213  @ingroup GenFunc
214 
215 */
216 template <class ParamFuncType = ROOT::Math::IParamMultiFunction &>
218 
219 public:
220 
221 
222  /**
223  Constructor from the function object , x value and coordinate we want to adapt
224  */
225  OneDimParamFunctionAdapter (ParamFuncType f, const double * x, const double * p, unsigned int ipar =0 ) :
226  fFunc(f),
227  fX(x ),
228  fParams(p),
229  fIpar(ipar)
230  {
231  assert(fX != 0);
232  assert(fParams != 0);
233  }
234 
235  /**
236  Destructor (no operations)
237  */
239 
240  /**
241  clone
242  */
243  virtual OneDimParamFunctionAdapter * Clone( ) const {
245  }
246 
247  // can use default copy constructor
248 
249 private:
250 
251  /**
252  evaluate function at the values x[] given in the constructor and
253  as function of the coordinate fCoord.
254  */
255  double DoEval(double x) const {
256  // HACK: use const_cast to modify the function values x[] and restore afterwards the original ones
257  double * p = const_cast<double *>(fParams);
258  double pprev = fParams[fIpar]; // keep original value to restore in fX
259  p[fIpar] = x;
260  double y = fFunc( fX, p );
261  p[fIpar] = pprev;
262  return y;
263  }
264 
265 
266 private:
267 
268  ParamFuncType fFunc;
269  const double * fX;
270  const double * fParams;
271  unsigned int fIpar;
272 
273 };
274 
275 
276 
277 
278 } // end namespace Math
279 
280 } // end namespace ROOT
281 
282 
283 #endif /* ROOT_Math_OneDimFunctionAdapter */
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
static double F(MultiFuncType f, const double *x, const double *=0)
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
OneDimMultiFunctionAdapter & operator=(const OneDimMultiFunctionAdapter &rhs)
#define assert(cond)
Definition: unittest.h:542
virtual ~OneDimMultiFunctionAdapter()
Destructor (no operations)
double DoEval(double x) const
evaluate function at the values x[] given in the constructor and as function of the coordinate fCoord...
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
Definition: TIterator.cxx:20
static double F(const ROOT::Math::IParamMultiFunction &f, const double *x, const double *p=0)
Double_t x[n]
Definition: legend1.C:17
OneDimMultiFunctionAdapter class to wrap a multidimensional function in one dimensional one...
~OneDimParamFunctionAdapter()
Destructor (no operations)
OneDimMultiFunctionAdapter(MultiFuncType f, const double *x, unsigned int icoord=0, const double *p=0)
Constructor from the function object , pointer to an external array of x values and coordinate we wan...
OneDimMultiFunctionAdapter(const OneDimMultiFunctionAdapter &rhs)
double DoEval(double x) const
evaluate function at the values x[] given in the constructor and as function of the coordinate fCoord...
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
virtual OneDimParamFunctionAdapter * Clone() const
clone
virtual OneDimMultiFunctionAdapter * Clone() const
clone
OneDimMultiFunctionAdapter(MultiFuncType f, unsigned int dim=1, unsigned int icoord=0, const double *p=0)
Constructor from the function object , dimension of the function and and coordinate we want to adapt...
OneDimParamFunctionAdapter class to wrap a multi-dim parameteric function in one dimensional one...
void SetX(const double *x)
set values
double f(double x)
void SetX(Iterator begin, Iterator end)
Set X values in case vector is own, iterator size must match previous set dimension.
Double_t y[n]
Definition: legend1.C:17
Namespace for new Math classes and functions.
void SetX(double *x)
set pointer without copying the values
OneDimParamFunctionAdapter(ParamFuncType f, const double *x, const double *p, unsigned int ipar=0)
Constructor from the function object , x value and coordinate we want to adapt.