Logo ROOT   6.08/07
Reference Guide
WrappedParamFunction.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Thu Nov 23 10:38:32 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class WrappedParamFunction
12 
13 #ifndef ROOT_Math_WrappedParamFunction
14 #define ROOT_Math_WrappedParamFunction
15 
16 #ifndef ROOT_Math_IParamFunction
17 #include "Math/IParamFunction.h"
18 #endif
19 
20 //#include <iostream>
21 //#include <iterator>
22 
23 #include <vector>
24 
25 
26 namespace ROOT {
27 
28  namespace Math {
29 
30 
31 typedef double( * FreeParamMultiFunctionPtr ) (const double *, const double * );
32 
33 /**
34  WrappedParamFunction class to wrap any multi-dimensional function pbject
35  implementing the operator()(const double * x, const double * p)
36  in an interface-like IParamFunction with a vector storing and caching internally the
37  parameter values
38 
39  @ingroup ParamFunc
40 
41 */
42 template< typename FuncPtr = FreeParamMultiFunctionPtr >
44 
45 public:
46 
47  /**
48  Constructor a wrapped function from a pointer to a callable object, the function dimension and number of parameters
49  which are set to zero by default
50  */
51  WrappedParamFunction (FuncPtr func, unsigned int dim = 1, unsigned int npar = 0, double * par = 0) :
52  fFunc(func),
53  fDim(dim),
54  fParams(std::vector<double>(npar) )
55  {
56  if (par != 0) std::copy(par,par+npar,fParams.begin() );
57  }
58 
59 // /**
60 // Constructor a wrapped function from a non-const pointer to a callable object, the function dimension and number of parameters
61 // which are set to zero by default
62 // This constructor is needed in the case FuncPtr is a std::auto_ptr which has a copy ctor taking non const objects
63 // */
64 // WrappedParamFunction (FuncPtr & func, unsigned int dim = 1, unsigned int npar = 0, double * par = 0) :
65 // fFunc(func),
66 // fDim(dim),
67 // fParams(std::vector<double>(npar) )
68 // {
69 // if (par != 0) std::copy(par,par+npar,fParams.begin() );
70 // }
71 
72  /**
73  Constructor a wrapped function from a pointer to a callable object, the function dimension and an iterator specifying begin and end
74  of parameters
75  */
76  template<class Iterator>
77  WrappedParamFunction (FuncPtr func, unsigned int dim, Iterator begin, Iterator end) :
78  fFunc(func),
79  fDim(dim),
80  fParams(std::vector<double>(begin,end) )
81  {}
82 
83 // /**
84 // Constructor a wrapped function from a non - const pointer to a callable object, the function dimension and an iterator specifying begin and end of parameters.
85 // This constructor is needed in the case FuncPtr is a std::auto_ptr which has a copy ctor taking non const objects
86 // */
87 // template<class Iterator>
88 // WrappedParamFunction (FuncPtr func, unsigned int dim, Iterator begin, Iterator end) :
89 // fFunc(func),
90 // fDim(dim),
91 // fParams(std::vector<double>(begin,end) )
92 // {}
93 
94  /// clone the function
96  return new WrappedParamFunction(fFunc, fDim, fParams.begin(), fParams.end());
97  }
98 
99  const double * Parameters() const {
100  return &(fParams.front());
101  }
102 
103  void SetParameters(const double * p) {
104  std::copy(p, p+NPar(), fParams.begin() );
105  }
106 
107  unsigned int NPar() const { return fParams.size(); }
108 
109  unsigned int NDim() const { return fDim; }
110 
111 
112 private:
113 
114  /// evaluate the function given values and parameters (requested interface)
115  double DoEvalPar(const double * x, const double * p) const {
116  return (*fFunc)( x, p );
117  }
118 
119 
120  FuncPtr fFunc;
121  unsigned int fDim;
122  std::vector<double> fParams;
123 
124 
125 
126 };
127 
128 
129 typedef double( * FreeMultiFunctionPtr ) (const double *);
130 
131 /**
132  WrappedParamGenFunction class to wrap any multi-dimensional function
133  implementing the operator()(const double * )
134  in an interface-like IParamFunction, by fixing some of the variables and define them as
135  parameters.
136  i.e. transform any multi-dim function in a parametric function
137 
138  @ingroup ParamFunc
139 
140 */
141 template< typename FuncPtr = FreeMultiFunctionPtr >
143 
144 public:
145 
146  /**
147  Constructor a wrapped function from a pointer to a generic callable object implemention operator()(const double *), the new function dimension, the number of parameters (number of fixed variables) and an array specifying the index of the fixed variables which becames
148  parameters in the new API
149  */
150 
151  WrappedParamFunctionGen (const FuncPtr & func, unsigned int dim, unsigned int npar, const double * par, const unsigned int * idx) :
152  fFunc(func),
153  fDim(dim),
154  fParams(std::vector<double>(par,par+npar) ),
155  fParIndices(std::vector<unsigned int>(idx, idx + npar) ),
156  fX(std::vector<double>(npar+dim) ) // cached vector
157  {
158  DoInit();
159  }
160 
161  /**
162  Constructor as before but taking now a non - const pointer to a callable object.
163  This constructor is needed in the case FuncPtr is a std::auto_ptr which has a copy ctor taking non const objects
164  */
165  WrappedParamFunctionGen (FuncPtr & func, unsigned int dim, unsigned int npar, const double * par, const unsigned int * idx) :
166  fFunc(func),
167  fDim(dim),
168  fParams(std::vector<double>(par,par+npar) ),
169  fParIndices(std::vector<unsigned int>(idx, idx + npar) ),
170  fX(std::vector<double>(npar+dim) ) // cached vector
171  {
172  DoInit();
173  }
174 
175  /// clone the function
177  return new WrappedParamFunctionGen(fFunc, fDim, fParams.size() , &fParams.front(), &fParIndices.front());
178  }
179 
180 private:
181  // copy ctor
182  WrappedParamFunctionGen(const WrappedParamFunctionGen &); // not implemented
183  WrappedParamFunctionGen & operator=(const WrappedParamFunctionGen &); // not implemented
184 
185 public:
186 
187  const double * Parameters() const {
188  return &(fParams.front());
189  }
190 
191  void SetParameters(const double * p) {
192  unsigned int npar = NPar();
193  std::copy(p, p+ npar, fParams.begin() );
194  SetParValues(npar, p);
195  }
196 
197  unsigned int NPar() const { return fParams.size(); }
198 
199  unsigned int NDim() const { return fDim; }
200 
201 // // re-implement this since is more efficient
202 // double operator() (const double * x, const double * p) {
203 // unsigned int n = fX.size();
204 // unsigned int npar = fParams.size();
205 // unsigned j = 0;
206 // return (*fFunc)( fX);
207 // }
208 
209 private:
210 
211  /// evaluate the function (re-implement for being more efficient)
212  double DoEval(const double * x) const {
213 
214 // std::cout << this << fDim << " x : ";
215 // std::ostream_iterator<double> oix(std::cout," , ");
216 // std::copy(x, x+fDim, oix);
217 // std::cout << std::endl;
218 // std::cout << "npar " << npar << std::endl;
219 // std::cout << fVarIndices.size() << std::endl;
220 // assert ( fVarIndices.size() == fDim); // otherwise something is wrong
221 
222  for (unsigned int i = 0; i < fDim; ++i) {
223  unsigned int j = fVarIndices[i];
224  assert ( j < NPar() + fDim);
225  fX[ j ] = x[i];
226  }
227 // std::cout << "X : (";
228 // std::ostream_iterator<double> oi(std::cout," , ");
229 // std::copy(fX.begin(), fX.end(), oi);
230 // std::cout << std::endl;
231 
232  return (*fFunc)( &fX.front() );
233  }
234 
235 
236  /**
237  implement the required IParamFunction interface
238  */
239  double DoEvalPar(const double * x, const double * p ) const {
240  SetParValues(NPar(), p);
241  return DoEval(x);
242  }
243 
244 
245  void DoInit() {
246  // calculate variable indices and set in X the parameter values
247  fVarIndices.reserve(fDim);
248  unsigned int npar = NPar();
249  for (unsigned int i = 0; i < npar + fDim; ++i) {
250  bool isVar = true;
251  for (unsigned int j = 0; j < npar; ++j) {
252  if (fParIndices[j] == i) {
253  isVar = false;
254  break;
255  }
256  }
257  if (isVar) fVarIndices.push_back(i);
258  }
259  assert ( fVarIndices.size() == fDim); // otherwise something is wrong
260 
261 // std::cout << "n variables " << fVarIndices.size() << std::endl;
262 // std::ostream_iterator<int> oi(std::cout," ");
263 // std::copy(fVarIndices.begin(), fVarIndices.end(), oi);
264 // std::cout << std::endl;
265 // assert( fVarIndices.size() == fDim);
266 // std::cout << this << std::endl;
267 
268  // set parameter values in fX
269  SetParValues(npar, &fParams.front() );
270  for (unsigned int i = 0; i < npar; ++i) {
271  unsigned int j = fParIndices[i];
272  assert ( j < npar + fDim);
273  fX[j] = fParams[i];
274  }
275 
276  }
277 
278  // set the parameter values in the cached fX vector
279  // makme const because it might be called from const methods
280  void SetParValues(unsigned int npar, const double * p) const {
281  for (unsigned int i = 0; i < npar; ++i) {
282  unsigned int j = fParIndices[i];
283  assert ( j < npar + fDim);
284  fX[j] = p[i];
285  }
286  }
287 
288 
289  mutable FuncPtr fFunc;
290  unsigned int fDim;
291  std::vector<double> fParams;
292  std::vector<unsigned int> fVarIndices;
293  std::vector<unsigned int> fParIndices;
294  mutable std::vector<double> fX;
295 
296 
297 
298 };
299 
300 
301  } // end namespace Math
302 
303 } // end namespace ROOT
304 
305 
306 #endif /* ROOT_Math_WrappedParamFunction */
double par[1]
Definition: unuranDistr.cxx:38
void SetParValues(unsigned int npar, const double *p) const
double DoEvalPar(const double *x, const double *p) const
implement the required IParamFunction interface
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
WrappedParamFunction(FuncPtr func, unsigned int dim, Iterator begin, Iterator end)
Constructor a wrapped function from a non-const pointer to a callable object, the function dimension ...
unsigned int NDim() const
Retrieve the dimension of the function.
double DoEval(const double *x) const
evaluate the function (re-implement for being more efficient)
STL namespace.
WrappedParamFunction(FuncPtr func, unsigned int dim=1, unsigned int npar=0, double *par=0)
Constructor a wrapped function from a pointer to a callable object, the function dimension and number...
const double * Parameters() const
Access the parameter values.
WrappedParamGenFunction class to wrap any multi-dimensional function implementing the operator()(cons...
Double_t x[n]
Definition: legend1.C:17
virtual double DoEval(const double *x) const
Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values...
std::vector< unsigned int > fVarIndices
WrappedParamFunctionGen(FuncPtr &func, unsigned int dim, unsigned int npar, const double *par, const unsigned int *idx)
Constructor as before but taking now a non - const pointer to a callable object.
const double * Parameters() const
Access the parameter values.
void SetParameters(const double *p)
Set the parameter values.
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
WrappedParamFunctionGen(const FuncPtr &func, unsigned int dim, unsigned int npar, const double *par, const unsigned int *idx)
Constructor a wrapped function from a pointer to a generic callable object implemention operator()(co...
double(* FreeParamMultiFunctionPtr)(const double *, const double *)
double func(double *x, double *p)
Definition: stressTF1.cxx:213
std::vector< unsigned int > fParIndices
Namespace for new Math classes and functions.
IMultiGenFunction * Clone() const
Constructor a wrapped function from a non - const pointer to a callable object, the function dimensio...
double DoEvalPar(const double *x, const double *p) const
evaluate the function given values and parameters (requested interface)
unsigned int NPar() const
Return the number of Parameters.
IMultiGenFunction * Clone() const
clone the function
void SetParameters(const double *p)
Set the parameter values.
unsigned int NPar() const
Return the number of Parameters.
unsigned int NDim() const
Retrieve the dimension of the function.
double(* FreeMultiFunctionPtr)(const double *)
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
WrappedParamFunction class to wrap any multi-dimensional function pbject implementing the operator()(...