Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:20

0001 // @(#)root/mathmore:$Id$
0002 // Author: L. Moneta Wed Dec  6 11:45:55 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for class MultiDimParamFunctionAdapter
0012 
0013 #ifndef ROOT_Math_MultiDimParamFunctionAdapter
0014 #define ROOT_Math_MultiDimParamFunctionAdapter
0015 
0016 #include "Math/IFunction.h"
0017 #include "Math/IParamFunction.h"
0018 
0019 #include "Math/WrappedFunction.h"
0020 
0021 namespace ROOT {
0022 
0023    namespace Math {
0024 
0025 
0026       /**
0027          MultiDimParamFunctionAdapter class to wrap a one-dimensional parametric function in
0028          a multi dimensional parametric function interface
0029          This is used typically in fitting where internally the function is stored as multidimensional
0030 
0031          To wrap a non-parametric one-dim function in a multi-dim interface one can use simply a
0032          ROOT::Math::WrappedFunction<ROOT::Math::IGenFunction> or ROOT::Math::Functor
0033          and ROOT::Math::GradFunctor for gradient functions
0034 
0035          This class differs from WrappedParamFunction in the fact that the parameters are not stored in
0036          the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
0037 
0038          @ingroup  ParamFunc
0039 
0040       */
0041       class MultiDimParamFunctionAdapter : public IParametricFunctionMultiDimTempl<double>  {
0042 
0043       public:
0044 
0045          typedef IParamMultiFunction::BaseFunc BaseFunc;
0046 
0047 
0048          /**
0049             Constructor from a parametric one dim function interface from a const reference
0050             Own the function in this case
0051          */
0052          MultiDimParamFunctionAdapter(const IParamFunction &f) :
0053             fOwn(true)
0054          {
0055             fFunc = dynamic_cast<IParamFunction *>(f.Clone());
0056          }
0057 
0058          /**
0059             Constructor from a parametric one dim function interface from a non-const reference
0060             Do not own the function in this case
0061          */
0062          MultiDimParamFunctionAdapter(IParamFunction &f) :
0063             fOwn(false),
0064             fFunc(&f)
0065          { }
0066 
0067 
0068          /**
0069             Copy constructor. Different behaviour according if function is owned or not
0070           */
0071          MultiDimParamFunctionAdapter(const MultiDimParamFunctionAdapter &rhs) :
0072             BaseFunc(),
0073             IParamMultiFunction(),
0074             fOwn(rhs.fOwn),
0075             fFunc(nullptr)
0076          {
0077             if (fOwn)
0078                fFunc = dynamic_cast<IParamFunction *>((rhs.fFunc)->Clone());
0079          }
0080 
0081          /**
0082             Destructor (no operations)
0083          */
0084          ~MultiDimParamFunctionAdapter() override
0085          {
0086             if (fOwn && fFunc) delete fFunc;
0087          }
0088 
0089 
0090          /**
0091             Assignment operator
0092           */
0093          MultiDimParamFunctionAdapter &operator=(const MultiDimParamFunctionAdapter &rhs)
0094          {
0095             fOwn = rhs.fOwn;
0096             if (fOwn) {
0097                if (fFunc) delete fFunc; // delete previously existing copy
0098                fFunc = dynamic_cast<IParamFunction *>((rhs.fFunc)->Clone());
0099             } else
0100                fFunc = rhs.fFunc;
0101 
0102             return *this;
0103          }
0104 
0105          /**
0106             clone
0107          */
0108          BaseFunc *Clone() const override
0109          {
0110             return new MultiDimParamFunctionAdapter(*this);
0111          }
0112 
0113       public:
0114 
0115          // methods required by interface
0116          const double *Parameters() const override
0117          {
0118             return  fFunc->Parameters();
0119          }
0120 
0121          void SetParameters(const double *p) override
0122          {
0123             fFunc->SetParameters(p);
0124          }
0125 
0126          unsigned int NPar() const override
0127          {
0128             return fFunc->NPar();
0129          }
0130 
0131          unsigned int NDim() const override
0132          {
0133             return 1;
0134          }
0135 
0136 
0137       private:
0138 
0139          /// needed by the interface
0140          double DoEvalPar(const double *x, const double *p) const override
0141          {
0142             return (*fFunc)(*x, p);
0143          }
0144 
0145 
0146       private:
0147 
0148          bool fOwn;
0149          IParamFunction *fFunc;
0150 
0151       };
0152 
0153 
0154 
0155       /**
0156          MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in
0157          a multi dimensional parametric gradient function interface
0158          This is used typically in fitting where internally the function is stored as multidimensional
0159 
0160          To wrap a non-parametric one-dim gradient function in a multi-dim interface one can use simply a
0161            a ROOT::Math::GradFunctor
0162 
0163          The parameters are not stored in the adapter class and by default the pointer to the 1D function is owned.
0164          This means that deleting the class deletes also the 1D function and copying the class copies also the
0165          1D function
0166          This class differs from WrappedParamFunction in the fact that the parameters are not stored in
0167          the adapter class and optionally it keeps a cloned and managed copy of the adapter class.
0168 
0169          @ingroup  ParamFunc
0170 
0171       */
0172       class MultiDimParamGradFunctionAdapter : public IParamMultiGradFunction  {
0173 
0174       public:
0175 
0176          typedef IParamMultiGradFunction::BaseFunc BaseFunc;
0177 
0178 
0179          /**
0180             Constructor from a param one dim function interface from a const reference
0181             Copy and manage the own function pointer
0182          */
0183          MultiDimParamGradFunctionAdapter(const IParamGradFunction &f) :
0184             fOwn(true)
0185          {
0186             fFunc = dynamic_cast<IParamGradFunction *>(f.Clone());
0187          }
0188 
0189          /**
0190             Constructor from a param one dim function interface from a non const reference
0191             Do not  own the function pointer in this case
0192          */
0193          MultiDimParamGradFunctionAdapter(IParamGradFunction &f) :
0194             fOwn(false),
0195             fFunc(&f)
0196          { }
0197 
0198 
0199          /**
0200             Copy constructor. Different behaviour according if function is owned or not
0201           */
0202          MultiDimParamGradFunctionAdapter(const MultiDimParamGradFunctionAdapter &rhs) :
0203             BaseFunc(),
0204             IParamMultiGradFunction(),
0205             fOwn(rhs.fOwn),
0206             fFunc(rhs.fFunc)
0207          {
0208             if (fOwn)
0209                fFunc = dynamic_cast<IParamGradFunction *>((rhs.fFunc)->Clone());
0210          }
0211 
0212          /**
0213             Destructor (no operations)
0214          */
0215          ~MultiDimParamGradFunctionAdapter() override
0216          {
0217             if (fOwn && fFunc) delete fFunc;
0218          }
0219 
0220 
0221          /**
0222             Assignment operator
0223           */
0224          MultiDimParamGradFunctionAdapter &operator=(const MultiDimParamGradFunctionAdapter &rhs)
0225          {
0226             fOwn = rhs.fOwn;
0227             if (fOwn) {
0228                if (fFunc) delete fFunc; // delete previously existing copy
0229                fFunc = dynamic_cast<IParamGradFunction *>((rhs.fFunc)->Clone());
0230             } else
0231                fFunc = rhs.fFunc;
0232 
0233             return *this;
0234          }
0235 
0236          /**
0237             clone
0238          */
0239          BaseFunc *Clone() const override
0240          {
0241             return new MultiDimParamGradFunctionAdapter(*this);
0242          }
0243 
0244       public:
0245 
0246          // methods required by interface
0247          const double *Parameters() const override
0248          {
0249             return  fFunc->Parameters();
0250          }
0251 
0252          void SetParameters(const double *p) override
0253          {
0254             fFunc->SetParameters(p);
0255          }
0256 
0257          unsigned int NPar() const override
0258          {
0259             return fFunc->NPar();
0260          }
0261 
0262          unsigned int NDim() const override
0263          {
0264             return 1;
0265          }
0266 
0267 //    void Gradient(const double *x, double * grad) const {
0268 //       grad[0] = fFunc->Derivative( *x);
0269 //    }
0270 
0271          void ParameterGradient(const double *x, const double *p, double *grad) const override
0272          {
0273             fFunc->ParameterGradient(*x, p, grad);
0274          }
0275 
0276          //  using IParamMultiGradFunction::BaseFunc::operator();
0277 
0278       private:
0279 
0280          /// functions needed by interface
0281          double DoEvalPar(const double *x, const double *p) const override
0282          {
0283             return (*fFunc)(*x, p);
0284          }
0285 
0286 //    double DoDerivative(const double * x, unsigned int ) const {
0287 //       return fFunc->Derivative(*x);
0288 //    }
0289 
0290          double DoParameterDerivative(const double *x, const double *p, unsigned int ipar) const override
0291          {
0292             return fFunc->ParameterDerivative(*x, p, ipar);
0293          }
0294 
0295       private:
0296 
0297          bool fOwn;
0298          IParamGradFunction *fFunc;
0299 
0300       };
0301 
0302 
0303 
0304 
0305    } // end namespace Math
0306 
0307 } // end namespace ROOT
0308 
0309 
0310 #endif /* ROOT_Math_MultiDimParamFunctionAdapter */