Logo ROOT   6.13/01
Reference Guide
MultiDimParamFunctionAdapter.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
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 MultiDimParamFunctionAdapter
12 
13 #ifndef ROOT_Math_MultiDimParamFunctionAdapter
14 #define ROOT_Math_MultiDimParamFunctionAdapter
15 
16 #include "Math/IFunction.h"
17 #include "Math/IParamFunction.h"
18 
19 #include "Math/WrappedFunction.h"
20 
21 #include <cassert>
22 
23 namespace ROOT {
24 
25  namespace Math {
26 
27 
28  /**
29  MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in
30  a multi dimensional parameteric function interface
31  This is used typically in fitting where internally the function is stored as multidimension
32 
33  To wrap a non-parametric one-dim function in a multi-dim interface one can use simply a
34  ROOT::Math::WrappedFunction<ROOT::Math::IGenFunction> or ROOT::Math::Functor
35  and ROOT::Math::GradFunctor for gradient functions
36 
37  This class differs from WrappedParamFunction in the fact that the parameters are not stored in
38  the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
39 
40  @ingroup ParamFunc
41 
42  */
44 
45  public:
46 
48 
49 
50  /**
51  Constructor from a parametric one dim function interface from a const reference
52  Own the function in this case
53  */
55  fOwn(true)
56  {
57  fFunc = dynamic_cast<IParamFunction *>(f.Clone());
58  }
59 
60  /**
61  Constructor from a parametric one dim function interface from a non-const reference
62  Do not own the function in this case
63  */
65  fOwn(false),
66  fFunc(&f)
67  { }
68 
69 
70  /**
71  Copy constructor. Different behaviour according if function is owned or not
72  */
74  BaseFunc(),
76  fOwn(rhs.fOwn),
77  fFunc(0)
78  {
79  if (fOwn)
80  fFunc = dynamic_cast<IParamFunction *>((rhs.fFunc)->Clone());
81  }
82 
83  /**
84  Destructor (no operations)
85  */
87  {
88  if (fOwn && fFunc != 0) delete fFunc;
89  }
90 
91 
92  /**
93  Assignment operator
94  */
96  {
97  fOwn = rhs.fOwn;
98  if (fOwn) {
99  if (fFunc) delete fFunc; // delete previously existing copy
100  fFunc = dynamic_cast<IParamFunction *>((rhs.fFunc)->Clone());
101  } else
102  fFunc = rhs.fFunc;
103 
104  return *this;
105  }
106 
107  /**
108  clone
109  */
110  virtual BaseFunc *Clone() const
111  {
112  return new MultiDimParamFunctionAdapter(*this);
113  }
114 
115  public:
116 
117  // methods required by interface
118  const double *Parameters() const
119  {
120  return fFunc->Parameters();
121  }
122 
123  void SetParameters(const double *p)
124  {
125  fFunc->SetParameters(p);
126  }
127 
128  unsigned int NPar() const
129  {
130  return fFunc->NPar();
131  }
132 
133  unsigned int NDim() const
134  {
135  return 1;
136  }
137 
138 
139  private:
140 
141  /// needed by the interface
142  double DoEvalPar(const double *x, const double *p) const
143  {
144  return (*fFunc)(*x, p);
145  }
146 
147 
148  private:
149 
150  bool fOwn;
152 
153  };
154 
155 
156 
157  /**
158  MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in
159  a multi dimensional parameteric gradient function interface
160  This is used typically in fitting where internally the function is stored as multidimension
161 
162  To wrap a non-parametric one-dim gradient function in a multi-dim interface one can use simply a
163  a ROOT::Math::GradFunctor
164 
165  The parameters are not stored in the adapter class and by default the pointer to the 1D function is owned.
166  This means that deleteing the class deletes also the 1D function and copying the class copies also the
167  1D function
168  This class differs from WrappedParamFunction in the fact that the parameters are not stored in
169  the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
170 
171  @ingroup ParamFunc
172 
173  */
175 
176  public:
177 
179 
180 
181  /**
182  Constructor from a param one dim function interface from a const reference
183  Copy and manage the own function pointer
184  */
186  fOwn(true)
187  {
188  fFunc = dynamic_cast<IParamGradFunction *>(f.Clone());
189  }
190 
191  /**
192  Constructor from a param one dim function interface from a non const reference
193  Do not own the function pointer in this case
194  */
196  fOwn(false),
197  fFunc(&f)
198  { }
199 
200 
201  /**
202  Copy constructor. Different behaviour according if function is owned or not
203  */
205  BaseFunc(),
207  fOwn(rhs.fOwn),
208  fFunc(rhs.fFunc)
209  {
210  if (fOwn)
211  fFunc = dynamic_cast<IParamGradFunction *>((rhs.fFunc)->Clone());
212  }
213 
214  /**
215  Destructor (no operations)
216  */
218  {
219  if (fOwn && fFunc != 0) delete fFunc;
220  }
221 
222 
223  /**
224  Assignment operator
225  */
227  {
228  fOwn = rhs.fOwn;
229  if (fOwn) {
230  if (fFunc) delete fFunc; // delete previously existing copy
231  fFunc = dynamic_cast<IParamGradFunction *>((rhs.fFunc)->Clone());
232  } else
233  fFunc = rhs.fFunc;
234 
235  return *this;
236  }
237 
238  /**
239  clone
240  */
241  virtual BaseFunc *Clone() const
242  {
243  return new MultiDimParamGradFunctionAdapter(*this);
244  }
245 
246  public:
247 
248  // methods required by interface
249  const double *Parameters() const
250  {
251  return fFunc->Parameters();
252  }
253 
254  void SetParameters(const double *p)
255  {
256  fFunc->SetParameters(p);
257  }
258 
259  unsigned int NPar() const
260  {
261  return fFunc->NPar();
262  }
263 
264  unsigned int NDim() const
265  {
266  return 1;
267  }
268 
269 // void Gradient(const double *x, double * grad) const {
270 // grad[0] = fFunc->Derivative( *x);
271 // }
272 
273  void ParameterGradient(const double *x, const double *p, double *grad) const
274  {
275  fFunc->ParameterGradient(*x, p, grad);
276  }
277 
278  // using IParamMultiGradFunction::BaseFunc::operator();
279 
280  private:
281 
282  /// functions needed by interface
283  double DoEvalPar(const double *x, const double *p) const
284  {
285  return (*fFunc)(*x, p);
286  }
287 
288 // double DoDerivative(const double * x, unsigned int ) const {
289 // return fFunc->Derivative(*x);
290 // }
291 
292  double DoParameterDerivative(const double *x, const double *p, unsigned int ipar) const
293  {
294  return fFunc->ParameterDerivative(*x, p, ipar);
295  }
296 
297  private:
298 
299  bool fOwn;
301 
302  };
303 
304 
305 
306 
307  } // end namespace Math
308 
309 } // end namespace ROOT
310 
311 
312 #endif /* ROOT_Math_MultiDimParamFunctionAdapter */
virtual const double * Parameters() const =0
Access the parameter values.
unsigned int NDim() const
Retrieve the dimension of the function.
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
MultiDimParamGradFunctionAdapter(IParamGradFunction &f)
Constructor from a param one dim function interface from a non const reference Do not own the functio...
double DoEvalPar(const double *x, const double *p) const
needed by the interface
virtual ~MultiDimParamFunctionAdapter()
Destructor (no operations)
void ParameterGradient(const double *x, const double *p, double *grad) const
MultiDimParamFunctionAdapter & operator=(const MultiDimParamFunctionAdapter &rhs)
Assignment operator.
double DoEvalPar(const double *x, const double *p) const
functions needed by interface
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
typename IParametricFunctionMultiDimTempl< T >::BaseFunc BaseFunc
MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in a multi dimension...
const double * Parameters() const
Access the parameter values.
virtual ~MultiDimParamGradFunctionAdapter()
Destructor (no operations)
MultiDimParamGradFunctionAdapter & operator=(const MultiDimParamGradFunctionAdapter &rhs)
Assignment operator.
MultiDimParamGradFunctionAdapter(const MultiDimParamGradFunctionAdapter &rhs)
Copy constructor.
double DoParameterDerivative(const double *x, const double *p, unsigned int ipar) const
unsigned int NPar() const
Return the number of Parameters.
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
virtual unsigned int NPar() const =0
Return the number of Parameters.
void SetParameters(const double *p)
Set the parameter values.
unsigned int NDim() const
Retrieve the dimension of the function.
unsigned int NPar() const
Return the number of Parameters.
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
virtual void SetParameters(const double *p)=0
Set the parameter values.
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
const double * Parameters() const
Access the parameter values.
void SetParameters(const double *p)
Set the parameter values.
Namespace for new Math classes and functions.
MultiDimParamFunctionAdapter(IParamFunction &f)
Constructor from a parametric one dim function interface from a non-const reference Do not own the fu...
MultiDimParamGradFunctionAdapter(const IParamGradFunction &f)
Constructor from a param one dim function interface from a const reference Copy and manage the own fu...
MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in a mu...
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.
MultiDimParamFunctionAdapter(const MultiDimParamFunctionAdapter &rhs)
Copy constructor.
MultiDimParamFunctionAdapter(const IParamFunction &f)
Constructor from a parametric one dim function interface from a const reference Own the function in t...