Logo ROOT   6.08/07
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 #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 #ifndef ROOT_Math_WrappedFunction
24 #include "Math/WrappedFunction.h"
25 #endif
26 
27 #include <cassert>
28 
29 namespace ROOT {
30 
31 namespace Math {
32 
33 
34 /**
35  MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in
36  a multi dimensional parameteric function interface
37  This is used typically in fitting where internally the function is stored as multidimension
38 
39  To wrap a non-parametric one-dim function in a multi-dim interface one can use simply a
40  ROOT::Math::WrappedFunction<ROOT::Math::IGenFunction> or ROOT::Math::Functor
41  and ROOT::Math::GradFunctor for gradient functions
42 
43  This class differs from WrappedParamFunction in the fact that the parameters are not stored in
44  the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
45 
46  @ingroup ParamFunc
47 
48 */
50 
51 public:
52 
54 
55 
56  /**
57  Constructor from a parametric one dim function interface from a const reference
58  Own the function in this case
59  */
61  fOwn(true)
62  {
63  fFunc = dynamic_cast<IParamFunction *>( f.Clone() );
64  }
65 
66  /**
67  Constructor from a parametric one dim function interface from a non-const reference
68  Do not own the function in this case
69  */
71  fOwn(false),
72  fFunc(&f)
73  { }
74 
75 
76  /**
77  Copy constructor. Different behaviour according if function is owned or not
78  */
80  BaseFunc(),
82  fOwn(rhs.fOwn),
83  fFunc(0)
84  {
85  if (fOwn)
86  fFunc = dynamic_cast<IParamFunction *>( (rhs.fFunc)->Clone() );
87  }
88 
89  /**
90  Destructor (no operations)
91  */
93  if (fOwn && fFunc != 0) delete fFunc;
94  }
95 
96 
97  /**
98  Assignment operator
99  */
101  fOwn = rhs.fOwn;
102  if (fOwn) {
103  if (fFunc) delete fFunc; // delete previously existing copy
104  fFunc = dynamic_cast<IParamFunction *> ( (rhs.fFunc)->Clone() );
105  }
106  else
107  fFunc = rhs.fFunc;
108 
109  return *this;
110  }
111 
112  /**
113  clone
114  */
115  virtual BaseFunc * Clone( ) const {
116  return new MultiDimParamFunctionAdapter( *this);
117  }
118 
119 public:
120 
121  // methods required by interface
122  const double * Parameters() const { return fFunc->Parameters(); }
123 
124  void SetParameters(const double * p) { fFunc->SetParameters(p); }
125 
126  unsigned int NPar() const { return fFunc->NPar(); }
127 
128  unsigned int NDim() const { return 1; }
129 
130 
131 private:
132 
133  /// needed by the interface
134  double DoEvalPar(const double * x, const double * p) const {
135  return (*fFunc)(*x, p);
136  }
137 
138 
139 private:
140 
141  bool fOwn;
143 
144 };
145 
146 
147 
148 /**
149  MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in
150  a multi dimensional parameteric gradient function interface
151  This is used typically in fitting where internally the function is stored as multidimension
152 
153  To wrap a non-parametric one-dim gradient function in a multi-dim interface one can use simply a
154  a ROOT::Math::GradFunctor
155 
156  The parameters are not stored in the adapter class and by default the pointer to the 1D function is owned.
157  This means that deleteing the class deletes also the 1D function and copying the class copies also the
158  1D function
159  This class differs from WrappedParamFunction in the fact that the parameters are not stored in
160  the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
161 
162  @ingroup ParamFunc
163 
164 */
166 
167 public:
168 
170 
171 
172  /**
173  Constructor from a param one dim function interface from a const reference
174  Copy and manage the own function pointer
175  */
177  fOwn(true)
178  {
179  fFunc = dynamic_cast<IParamGradFunction *>( f.Clone() );
180  }
181 
182  /**
183  Constructor from a param one dim function interface from a non const reference
184  Do not own the function pointer in this case
185  */
187  fOwn(false),
188  fFunc(&f)
189  { }
190 
191 
192  /**
193  Copy constructor. Different behaviour according if function is owned or not
194  */
196  BaseFunc(),
198  fOwn(rhs.fOwn),
199  fFunc(rhs.fFunc)
200  {
201  if (fOwn)
202  fFunc = dynamic_cast<IParamGradFunction *>( (rhs.fFunc)->Clone() );
203  }
204 
205  /**
206  Destructor (no operations)
207  */
208  virtual ~MultiDimParamGradFunctionAdapter () { if (fOwn && fFunc != 0) delete fFunc; }
209 
210 
211  /**
212  Assignment operator
213  */
215  fOwn = rhs.fOwn;
216  if (fOwn) {
217  if (fFunc) delete fFunc; // delete previously existing copy
218  fFunc = dynamic_cast<IParamGradFunction *> ( (rhs.fFunc)->Clone() );
219  }
220  else
221  fFunc = rhs.fFunc;
222 
223  return *this;
224  }
225 
226  /**
227  clone
228  */
229  virtual BaseFunc * Clone( ) const {
230  return new MultiDimParamGradFunctionAdapter( *this);
231  }
232 
233 public:
234 
235  // methods required by interface
236  const double * Parameters() const { return fFunc->Parameters(); }
237 
238  void SetParameters(const double * p) { fFunc->SetParameters(p); }
239 
240  unsigned int NPar() const { return fFunc->NPar(); }
241 
242  unsigned int NDim() const { return 1; }
243 
244 // void Gradient(const double *x, double * grad) const {
245 // grad[0] = fFunc->Derivative( *x);
246 // }
247 
248  void ParameterGradient(const double *x, const double * p, double * grad) const {
249  fFunc->ParameterGradient(*x, p, grad);
250  }
251 
252  // using IParamMultiGradFunction::BaseFunc::operator();
253 
254 private:
255 
256  /// functions needed by interface
257  double DoEvalPar(const double * x, const double * p) const {
258  return (*fFunc)(*x, p);
259  }
260 
261 // double DoDerivative(const double * x, unsigned int ) const {
262 // return fFunc->Derivative(*x);
263 // }
264 
265  double DoParameterDerivative(const double * x, const double * p, unsigned int ipar ) const {
266  return fFunc->ParameterDerivative(*x, p, ipar);
267  }
268 
269 private:
270 
271  bool fOwn;
273 
274 };
275 
276 
277 
278 
279 } // end namespace Math
280 
281 } // end namespace ROOT
282 
283 
284 #endif /* ROOT_Math_MultiDimParamFunctionAdapter */
virtual const double * Parameters() const =0
Access the parameter values.
unsigned int NDim() const
Retrieve the dimension of the function.
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
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
Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at ...
MultiDimParamFunctionAdapter & operator=(const MultiDimParamFunctionAdapter &rhs)
Assignment operator.
double DoEvalPar(const double *x, const double *p) const
functions needed by interface
MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in a multi dimension...
const double * Parameters() const
Access the parameter values.
Double_t x[n]
Definition: legend1.C:17
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
Evaluate the partial derivative w.r.t a parameter ipar , to be implemented by the derived classes...
unsigned int NPar() const
Return the number of Parameters.
virtual unsigned int NPar() const =0
Return the number of Parameters.
void SetParameters(const double *p)
Set the parameter values.
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
unsigned int NDim() const
Retrieve the dimension of the function.
unsigned int NPar() const
Return the number of Parameters.
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
virtual void SetParameters(const double *p)=0
Set the parameter values.
double f(double x)
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
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...
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63