MultiDimParamFunctionAdapter.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: MultiDimParamFunctionAdapter.h 30749 2009-10-15 16:33:04Z brun $
00002 // Author: L. Moneta Wed Dec  6 11:45:55 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class MultiDimParamFunctionAdapter
00012 
00013 #ifndef ROOT_Math_MultiDimParamFunctionAdapter
00014 #define ROOT_Math_MultiDimParamFunctionAdapter
00015 
00016 #ifndef ROOT_Math_IFunction
00017 #include "Math/IFunction.h"
00018 #endif
00019 #ifndef ROOT_Math_IParamFunction
00020 #include "Math/IParamFunction.h"
00021 #endif
00022 
00023 #ifndef ROOT_Math_WrappedFunction
00024 #include "Math/WrappedFunction.h"
00025 #endif
00026 
00027 #include <cassert> 
00028 
00029 namespace ROOT { 
00030 
00031 namespace Math { 
00032 
00033 
00034 /** 
00035    MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in 
00036    a multi dimensional parameteric function interface
00037    This is used typically in fitting where internally the function is stored as multidimension
00038 
00039    To wrap a non-parametric one-dim function in a multi-dim interface one can use simply a 
00040    ROOT::Math::WrappedFunction<ROOT::Math::IGenFunction> or ROOT::Math::Functor
00041    and ROOT::Math::GradFunctor for gradient functions
00042 
00043    This class differs from WrappedParamFunction in the fact that the parameters are not stored in 
00044    the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
00045 
00046    @ingroup  ParamFunc
00047    
00048 */ 
00049 class MultiDimParamFunctionAdapter : public IParamMultiFunction  {
00050 
00051 public: 
00052 
00053    typedef IParamMultiFunction::BaseFunc BaseFunc; 
00054 
00055   
00056    /** 
00057       Constructor from a parametric one dim function interface from a const reference
00058       Own the function in this case
00059    */ 
00060    MultiDimParamFunctionAdapter (const IParamFunction & f ) : 
00061       fOwn(true)
00062    {  
00063       fFunc = dynamic_cast<IParamFunction *>( f.Clone() ); 
00064    }  
00065 
00066    /** 
00067       Constructor from a parametric one dim function interface from a non-const reference
00068       Do not own the function in this case
00069    */ 
00070    MultiDimParamFunctionAdapter (IParamFunction & f ) : 
00071       fOwn(false),
00072       fFunc(&f)
00073    { }
00074 
00075 
00076    /**
00077       Copy constructor. Different behaviour according if function is owned or not
00078     */
00079    MultiDimParamFunctionAdapter (const MultiDimParamFunctionAdapter & rhs) : 
00080       BaseFunc(),
00081       IParamMultiFunction(),
00082       fOwn(rhs.fOwn), 
00083       fFunc(0)
00084    {  
00085       if (fOwn) 
00086          fFunc = dynamic_cast<IParamFunction *>( (rhs.fFunc)->Clone() ); 
00087    }  
00088 
00089    /** 
00090       Destructor (no operations)
00091    */ 
00092    virtual ~MultiDimParamFunctionAdapter ()  { 
00093       if (fOwn && fFunc != 0) delete fFunc; 
00094    }  
00095 
00096 
00097    /**
00098       Assignment operator
00099     */
00100    MultiDimParamFunctionAdapter & operator=(const MultiDimParamFunctionAdapter & rhs) { 
00101       fOwn = rhs.fOwn;  
00102       if (fOwn) { 
00103          if (fFunc) delete fFunc; // delete previously existing copy
00104          fFunc = dynamic_cast<IParamFunction *> ( (rhs.fFunc)->Clone() ); 
00105       }
00106       else 
00107          fFunc = rhs.fFunc;
00108 
00109       return *this;
00110    }  
00111 
00112    /**
00113       clone
00114    */
00115    virtual BaseFunc * Clone( ) const { 
00116       return new MultiDimParamFunctionAdapter( *this); 
00117    }
00118 
00119 public: 
00120 
00121    // methods required by interface 
00122    const double * Parameters() const {  return  fFunc->Parameters();  }
00123 
00124    void SetParameters(const double * p)  { fFunc->SetParameters(p);  }
00125 
00126    unsigned int NPar() const { return fFunc->NPar(); }
00127 
00128    unsigned int NDim() const { return 1; }
00129 
00130 
00131 private: 
00132 
00133    /// needed by the interface
00134    double DoEvalPar(const double * x, const double * p) const { 
00135       return (*fFunc)(*x, p);
00136    }
00137 
00138 
00139 private: 
00140 
00141    bool fOwn; 
00142    IParamFunction * fFunc; 
00143 
00144 }; 
00145 
00146 
00147 
00148 /** 
00149    MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in 
00150    a multi dimensional parameteric gradient function interface
00151    This is used typically in fitting where internally the function is stored as multidimension
00152 
00153    To wrap a non-parametric one-dim gradient function in a multi-dim interface one can use simply a 
00154      a ROOT::Math::GradFunctor 
00155 
00156    The parameters are not stored in the adapter class and by default the pointer to the 1D function is owned. 
00157    This means that deleteing the class deletes also the 1D function and copying the class copies also the 
00158    1D function 
00159    This class differs from WrappedParamFunction in the fact that the parameters are not stored in 
00160    the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
00161 
00162    @ingroup  ParamFunc
00163    
00164 */ 
00165 class MultiDimParamGradFunctionAdapter : public IParamMultiGradFunction  {
00166 
00167 public: 
00168 
00169    typedef IParamMultiGradFunction::BaseFunc BaseFunc; 
00170 
00171   
00172    /** 
00173       Constructor from a param one dim function interface from a const reference
00174       Copy and manage the own function pointer
00175    */ 
00176    MultiDimParamGradFunctionAdapter (const IParamGradFunction & f) : 
00177       fOwn(true)
00178    { 
00179       fFunc = dynamic_cast<IParamGradFunction *>( f.Clone() ); 
00180    }  
00181 
00182    /** 
00183       Constructor from a param one dim function interface from a non const reference
00184       Do not  own the function pointer in this case
00185    */ 
00186    MultiDimParamGradFunctionAdapter (IParamGradFunction & f) : 
00187       fOwn(false),
00188       fFunc(&f)
00189    { }  
00190 
00191 
00192    /**
00193       Copy constructor. Different behaviour according if function is owned or not
00194     */
00195    MultiDimParamGradFunctionAdapter (const MultiDimParamGradFunctionAdapter & rhs) : 
00196       BaseFunc(),
00197       IParamMultiGradFunction(),
00198       fOwn(rhs.fOwn), 
00199       fFunc(rhs.fFunc)
00200    {  
00201       if (fOwn) 
00202          fFunc = dynamic_cast<IParamGradFunction *>( (rhs.fFunc)->Clone() ); 
00203    }  
00204 
00205    /** 
00206       Destructor (no operations)
00207    */ 
00208    virtual ~MultiDimParamGradFunctionAdapter ()  { if (fOwn && fFunc != 0) delete fFunc; }  
00209 
00210 
00211    /**
00212       Assignment operator
00213     */
00214    MultiDimParamGradFunctionAdapter & operator=(const MultiDimParamGradFunctionAdapter & rhs) { 
00215       fOwn = rhs.fOwn;  
00216       if (fOwn) { 
00217          if (fFunc) delete fFunc; // delete previously existing copy
00218          fFunc = dynamic_cast<IParamGradFunction *> ( (rhs.fFunc)->Clone() ); 
00219       }
00220       else 
00221          fFunc = rhs.fFunc;
00222 
00223       return *this;
00224    }  
00225 
00226    /**
00227       clone
00228    */
00229    virtual BaseFunc * Clone( ) const { 
00230       return new MultiDimParamGradFunctionAdapter( *this); 
00231    }
00232 
00233 public: 
00234 
00235    // methods required by interface 
00236    const double * Parameters() const {  return  fFunc->Parameters();  }
00237 
00238    void SetParameters(const double * p)  { fFunc->SetParameters(p);  }
00239 
00240    unsigned int NPar() const { return fFunc->NPar(); }
00241 
00242    unsigned int NDim() const { return 1; }
00243 
00244 //    void Gradient(const double *x, double * grad) const { 
00245 //       grad[0] = fFunc->Derivative( *x);  
00246 //    }
00247 
00248    void ParameterGradient(const double *x, const double * p, double * grad) const { 
00249       fFunc->ParameterGradient(*x, p, grad); 
00250    } 
00251 
00252    //  using IParamMultiGradFunction::BaseFunc::operator();
00253 
00254 private: 
00255 
00256    /// functions needed by interface
00257    double DoEvalPar(const double * x, const double * p) const { 
00258       return (*fFunc)(*x, p);
00259    }
00260 
00261 //    double DoDerivative(const double * x, unsigned int ) const { 
00262 //       return fFunc->Derivative(*x); 
00263 //    } 
00264 
00265    double DoParameterDerivative(const double * x, const double * p, unsigned int ipar ) const { 
00266       return fFunc->ParameterDerivative(*x, p, ipar); 
00267    } 
00268 
00269 private: 
00270 
00271    bool fOwn; 
00272    IParamGradFunction * fFunc; 
00273 
00274 }; 
00275 
00276 
00277 
00278 
00279 } // end namespace Math
00280 
00281 } // end namespace ROOT
00282 
00283 
00284 #endif /* ROOT_Math_MultiDimParamFunctionAdapter */

Generated on Tue Jul 5 14:25:36 2011 for ROOT_528-00b_version by  doxygen 1.5.1