IParamFunction.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: IParamFunction.h 32583 2010-03-12 09:57:42Z moneta $
00002 // Author: L. Moneta Tue Nov 14 14:20:07 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class IParamFunction
00012 
00013 #ifndef ROOT_Math_IParamFunction
00014 #define ROOT_Math_IParamFunction
00015 
00016 #ifndef ROOT_Math_IFunction
00017 #include "Math/IFunction.h"
00018 #endif
00019 
00020 #ifndef ROOT_Math_IParamFunctionfwd
00021 #include "Math/IParamFunctionfwd.h"
00022 #endif
00023 
00024 #ifndef ROOT_Math_Util
00025 #include "Math/Util.h"
00026 #endif
00027 
00028 
00029 #include <cassert> 
00030 
00031 /**
00032    @defgroup ParamFunc Interfaces for parametric functions 
00033    @ingroup CppFunctions
00034 */
00035 
00036 
00037 namespace ROOT { 
00038 
00039 namespace Math { 
00040 
00041 
00042 //___________________________________________________________________
00043 /** 
00044     Documentation for the abstract class IBaseParam.
00045     It defines the interface for dealing with the function parameters
00046     This is used only for internal convinience, to avoid redefining the Parameter API  
00047     for the one and the multi-dim functions. 
00048     Concrete class should derive from ROOT::Math::IParamFunction and not from this class.  
00049     
00050     @ingroup  ParamFunc
00051 */ 
00052 
00053 class IBaseParam  {
00054 
00055 public: 
00056 
00057 
00058    /** 
00059       Virtual Destructor (no operations)
00060    */ 
00061    virtual ~IBaseParam ()  {}  
00062 
00063 
00064    /**
00065       Access the parameter values
00066    */
00067    virtual const double * Parameters() const = 0;
00068 
00069    /**
00070       Set the parameter values
00071       @param p vector of doubles containing the parameter values. 
00072 
00073       to be defined:  can user change number of params ? At the moment no. 
00074 
00075    */
00076    virtual void SetParameters(const double * p ) = 0;
00077 
00078     
00079    /**
00080       Return the number of Parameters
00081    */
00082    virtual unsigned int NPar() const = 0; 
00083 
00084    /**
00085       Return the name of the i-th parameter (starting from zero)
00086       Overwrite if want to avoid the default name ("Par_0, Par_1, ...") 
00087     */
00088    virtual std::string ParameterName(unsigned int i) const { 
00089       assert(i < NPar() ); 
00090       return "Par_" + Util::ToString(i);
00091    }
00092 
00093 
00094 };
00095 
00096 //___________________________________________________________________
00097 /** 
00098    IParamFunction interface (abstract class) describing multi-dimensional parameteric functions
00099    It is a derived class from ROOT::Math::IBaseFunctionMultiDim and 
00100    ROOT::Math::IBaseParam
00101 
00102    Provides the interface for evaluating a function passing a coordinate vector and a parameter vector.  
00103 
00104    @ingroup  ParamFunc
00105 */ 
00106 
00107 class IParametricFunctionMultiDim : 
00108          virtual public IBaseFunctionMultiDim , 
00109          public IBaseParam {
00110 
00111 public: 
00112 
00113    typedef IBaseFunctionMultiDim  BaseFunc; 
00114 
00115 
00116 
00117    /**
00118       Evaluate function at a point x and for given parameters p.
00119       This method does not change the internal status of the function (internal parameter values). 
00120       If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be 
00121       called.
00122       Use the pure virtual function DoEvalPar to implement it
00123    */
00124    double operator() (const double * x, const double *  p ) const { 
00125       return DoEvalPar(x, p); 
00126    }
00127 
00128    using BaseFunc::operator();
00129 
00130 
00131 private: 
00132 
00133    /**
00134       Implementation of the evaluation function using the x values and the parameters. 
00135       Must be implemented by derived classes
00136    */
00137    virtual double DoEvalPar(const double * x, const double * p) const = 0; 
00138 
00139    /**
00140       Implement the ROOT::Math::IBaseFunctionMultiDim interface DoEval(x) using the cached parameter values
00141    */
00142    virtual double DoEval(const double *x) const { 
00143       return DoEvalPar( x, Parameters() );  
00144    }
00145 
00146 }; 
00147 
00148 //___________________________________________________________________
00149 /** 
00150    Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions
00151    It is a derived class from ROOT::Math::IBaseFunctionOneDim and 
00152    ROOT::Math::IBaseParam
00153 
00154    @ingroup  ParamFunc
00155 */ 
00156 
00157 class IParametricFunctionOneDim : 
00158          virtual public IBaseFunctionOneDim, 
00159          public IBaseParam { 
00160 
00161 
00162 public: 
00163 
00164    typedef IBaseFunctionOneDim   BaseFunc; 
00165 
00166 
00167    using BaseFunc::operator();
00168 
00169    /**
00170       Evaluate function at a point x and for given parameters p.
00171       This method does not change the internal status of the function (internal parameter values). 
00172       If for some reason one prefers caching the parameter values, SetParameters(p) and then operator()(x) should be 
00173       called.
00174       Use the pure virtual function DoEvalPar to implement it
00175    */
00176    double operator() ( double x, const double *  p ) const { 
00177       return DoEvalPar(x, p); 
00178    }
00179 
00180 
00181    /**
00182       multidim-like interface
00183    */
00184    double operator() (const double * x, const double *  p ) const
00185    { 
00186       return DoEvalPar(*x, p); 
00187    }
00188 
00189 private:
00190 
00191    /**
00192       Implementation of the evaluation function using the x value and the parameters. 
00193       Must be implemented by derived classes
00194    */
00195    virtual double DoEvalPar(double x, const double * p) const = 0; 
00196 
00197    /**
00198       Implement the ROOT::Math::IBaseFunctionOneDim interface DoEval(x) using the cached parameter values
00199    */
00200    virtual double DoEval(double x) const { 
00201       return DoEvalPar( x, Parameters() );  
00202    }
00203 
00204 }; 
00205 
00206 
00207 
00208 //_______________________________________________________________________________
00209 /** 
00210    Interface (abstract class) for parametric gradient multi-dimensional functions providing 
00211    in addition to function evaluation with respect to the coordinates 
00212    also the gradient with respect to the parameters, via the method ParameterGradient. 
00213 
00214    It is a derived class from ROOT::Math::IParametricFunctionMultiDim.
00215 
00216    The pure private virtual method DoParameterGradient must be implemented by the derived classes 
00217    in addition to those inherited by the base abstract classes. 
00218 
00219    @ingroup  ParamFunc
00220 */ 
00221 
00222 class IParametricGradFunctionMultiDim : 
00223          public IParametricFunctionMultiDim 
00224 //         ,public IGradientFunctionMultiDim   
00225 {
00226 
00227 public: 
00228 
00229    typedef IParametricFunctionMultiDim                BaseParamFunc; 
00230    typedef IGradientFunctionMultiDim                  BaseGradFunc; 
00231    typedef IParametricFunctionMultiDim::BaseFunc  BaseFunc; 
00232 
00233 
00234    /** 
00235       Virtual Destructor (no operations)
00236    */ 
00237    virtual ~IParametricGradFunctionMultiDim ()  {}  
00238 
00239 
00240 
00241    using BaseParamFunc::operator();
00242 
00243    /**
00244       Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at a point x.
00245       It is optional to be implemented by the derived classes for better efficiency
00246    */
00247    virtual void ParameterGradient(const double * x , const double * p, double * grad ) const { 
00248       unsigned int npar = NPar(); 
00249       for (unsigned int ipar  = 0; ipar < npar; ++ipar) 
00250          grad[ipar] = DoParameterDerivative(x,p,ipar); 
00251    } 
00252 
00253    /**
00254       Evaluate the partial derivative w.r.t a parameter ipar from values and parameters
00255     */
00256    double ParameterDerivative(const double * x, const double * p, unsigned int ipar = 0) const { 
00257       return DoParameterDerivative(x, p, ipar); 
00258    }  
00259 
00260    /**
00261       Evaluate all derivatives using cached parameter values
00262    */
00263    void ParameterGradient(const double * x , double * grad ) const { 
00264       return ParameterGradient(x, Parameters(), grad); 
00265    }
00266    /**
00267       Evaluate partial derivative using cached parameter values
00268    */
00269    double ParameterDerivative(const double * x, unsigned int ipar = 0) const { 
00270       return DoParameterDerivative(x, Parameters() , ipar); 
00271    }
00272 
00273 private: 
00274 
00275 
00276 
00277    /**
00278       Evaluate the partial derivative w.r.t a parameter ipar , to be implemented by the derived classes
00279     */
00280    virtual double DoParameterDerivative(const double * x, const double * p, unsigned int ipar) const = 0;  
00281 
00282 
00283 };
00284 
00285 //_______________________________________________________________________________
00286 /** 
00287    Interface (abstract class) for parametric one-dimensional gradient functions providing 
00288    in addition to function evaluation with respect the coordinates 
00289    also the gradient with respect to the parameters, via the method ParameterGradient. 
00290 
00291    It is a derived class from ROOT::Math::IParametricFunctionOneDim.
00292    
00293    The pure private virtual method DoParameterGradient must be implemented by the derived classes 
00294    in addition to those inherited by the base abstract classes. 
00295 
00296    @ingroup  ParamFunc
00297 */ 
00298 
00299 class IParametricGradFunctionOneDim : 
00300          public IParametricFunctionOneDim
00301 //         ,public IGradientFunctionOneDim
00302 {
00303 
00304 public: 
00305 
00306    typedef IParametricFunctionOneDim            BaseParamFunc; 
00307    typedef IGradientFunctionOneDim              BaseGradFunc; 
00308    typedef IParametricFunctionOneDim::BaseFunc  BaseFunc; 
00309 
00310 
00311    /** 
00312       Virtual Destructor (no operations)
00313    */ 
00314    virtual ~IParametricGradFunctionOneDim ()  {}  
00315 
00316 
00317    using BaseParamFunc::operator();
00318 
00319    /**
00320       Evaluate the derivatives of the function with respect to the parameters at a point x.
00321       It is optional to be implemented by the derived classes for better efficiency if needed
00322    */
00323    virtual void ParameterGradient(double x , const double * p, double * grad ) const { 
00324       unsigned int npar = NPar(); 
00325       for (unsigned int ipar  = 0; ipar < npar; ++ipar) 
00326          grad[ipar] = DoParameterDerivative(x, p, ipar); 
00327    } 
00328 
00329    /**
00330       Evaluate all derivatives using cached parameter values
00331    */
00332    void ParameterGradient(double  x , double * grad ) const { 
00333       return ParameterGradient( x, Parameters(), grad); 
00334    }
00335 
00336    /**
00337       Compatibility interface with multi-dimensional functions 
00338    */
00339    void ParameterGradient(const double * x , const double * p, double * grad ) const { 
00340       ParameterGradient(*x, p, grad); 
00341    } 
00342 
00343    /**
00344       Evaluate all derivatives using cached parameter values (multi-dim like interface)
00345    */
00346    void ParameterGradient(const double * x , double * grad ) const { 
00347       return ParameterGradient( *x, Parameters(), grad); 
00348    }
00349 
00350 
00351    /**
00352       Partial derivative with respect a parameter
00353     */
00354    double ParameterDerivative(double x, const double * p, unsigned int ipar = 0) const { 
00355       return DoParameterDerivative(x, p, ipar); 
00356    }
00357 
00358    /**
00359       Evaluate partial derivative using cached parameter values
00360    */
00361    double ParameterDerivative(double x, unsigned int ipar = 0) const { 
00362       return DoParameterDerivative(x, Parameters() , ipar); 
00363    }
00364 
00365    /**
00366       Partial derivative with respect a parameter
00367       Compatibility interface with multi-dimensional functions 
00368    */
00369    double ParameterDerivative(const double * x, const double * p, unsigned int ipar = 0) const { 
00370       return DoParameterDerivative(*x, p, ipar); 
00371    }
00372 
00373 
00374    /**
00375       Evaluate partial derivative using cached parameter values (multi-dim like interface)
00376    */
00377    double ParameterDerivative(const double * x, unsigned int ipar = 0) const { 
00378       return DoParameterDerivative( *x, Parameters() , ipar); 
00379    }
00380 
00381 
00382 
00383 private: 
00384 
00385 
00386    /**
00387       Evaluate the gradient, to be implemented by the derived classes
00388     */
00389    virtual double DoParameterDerivative(double x, const double * p, unsigned int ipar ) const = 0;  
00390 
00391 
00392 };
00393 
00394 
00395 
00396 
00397    } // end namespace Math
00398 
00399 } // end namespace ROOT
00400 
00401 
00402 
00403 #endif /* ROOT_Math_IParamFunction */

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