Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // @(#)root/mathcore:$Id$
0002 // Authors: L. Moneta, A. Zsenei   08/2005
0003 
0004  /**********************************************************************
0005   *                                                                    *
0006   * Copyright (c) 2004  CERN                                           *
0007   * All rights reserved.                                               *
0008   *                                                                    *
0009   * For the licensing terms see $ROOTSYS/LICENSE.                      *
0010   * For the list of contributors see $ROOTSYS/README/CREDITS.          *
0011   *                                                                    *
0012   **********************************************************************/
0013 
0014 #ifndef ROOT_Math_WrappedFunction
0015 #define ROOT_Math_WrappedFunction
0016 
0017 #include "IFunction.h"
0018 
0019 
0020 namespace ROOT {
0021 namespace Math {
0022 
0023 
0024 
0025 
0026 struct NullTypeFunc1D {};
0027 
0028 typedef double(*FreeFunctionPtr)(double);
0029 
0030 typedef double(*FreeMultiFunctionPtr)(const double*);
0031 
0032 /**
0033    Template class to wrap any C++ callable object which takes one argument
0034    i.e. implementing operator() (double x) in a One-dimensional function interface.
0035    It provides a ROOT::Math::IGenFunction-like signature
0036 
0037    Note: If you want to wrap just the reference (to avoid copying) you need to use
0038    Func& or const Func & as template parameter.  The former should be used when the
0039    operator() is not a const method of Func
0040 
0041    @ingroup  GenFunc
0042 
0043  */
0044 template< typename Func =  FreeFunctionPtr   >
0045 class WrappedFunction : public IGenFunction {
0046 
0047 
0048  public:
0049 
0050    /**
0051       construct from the pointer to the object and the member function
0052     */
0053    WrappedFunction( Func f ) :
0054       fFunc( f )
0055    { /* no op */ }
0056 
0057    // use default copy constructor and assignment operator
0058 
0059    /// clone (required by the interface)
0060    WrappedFunction * Clone() const override {
0061       return new WrappedFunction(fFunc);
0062    }
0063 
0064    //  virtual ~WrappedFunction() { /**/ }
0065 
0066 private:
0067 
0068    double DoEval (double x) const override {
0069       return fFunc( x );
0070    }
0071 
0072 
0073    Func fFunc;
0074 
0075 
0076 }; // WrappedFunction
0077 
0078 
0079 /**
0080    Template class to wrap any member function of a class
0081    taking a double and returning a double in a 1D function interface
0082    For example, if you have a class like:
0083    struct X {
0084        double Eval(double x);
0085    };
0086    you can wrapped in the following way:
0087    WrappedMemFunction<X, double ( X::* ) (double) > f;
0088 
0089 
0090    @ingroup  GenFunc
0091 
0092  */
0093 
0094 template<typename FuncObj, typename MemFuncPtr >
0095 class WrappedMemFunction : public IGenFunction {
0096 
0097 
0098  public:
0099 
0100    /**
0101       construct from the pointer to the object and the member function
0102     */
0103    WrappedMemFunction( FuncObj & obj, MemFuncPtr memFn ) :
0104       fObj(&obj),
0105       fMemFunc( memFn )
0106    { /* no op */ }
0107 
0108    // use default  copy constructor and assignment operator
0109 
0110    /// clone (required by the interface)
0111    WrappedMemFunction * Clone() const override {
0112       return new WrappedMemFunction(*fObj,fMemFunc);
0113    }
0114 
0115 
0116 private:
0117 
0118    double DoEval (double x) const override {
0119       return ((*fObj).*fMemFunc)( x );
0120    }
0121 
0122 
0123    FuncObj * fObj;
0124    MemFuncPtr fMemFunc;
0125 
0126 
0127 }; // WrappedMemFunction
0128 
0129 
0130 /**
0131    Template class to wrap any C++ callable object
0132    implementing operator() (const double * x) in a multi-dimensional function interface.
0133    It provides a ROOT::Math::IGenMultiFunction-like signature
0134 
0135    Note: If you want to wrap just the reference (to avoid copying) you need to use
0136    Func& or const Func & as template parameter. The former should be used when the
0137    operator() is not a const method of Func
0138 
0139    @ingroup  GenFunc
0140 
0141  */
0142 template< typename Func =  FreeMultiFunctionPtr   >
0143 class WrappedMultiFunction : public IMultiGenFunction {
0144 
0145 
0146  public:
0147 
0148    /**
0149       construct from the pointer to the object and the member function
0150     */
0151    WrappedMultiFunction( Func f , unsigned int dim = 1) :
0152       fFunc( f ),
0153       fDim( dim)
0154    { /* no op */ }
0155 
0156    // use default copy constructor and assignment operator
0157 
0158    /// clone (required by the interface)
0159    WrappedMultiFunction * Clone() const override {
0160       return new WrappedMultiFunction(fFunc,fDim);
0161    }
0162 
0163    unsigned int NDim() const override { return fDim; }
0164 
0165    //  virtual ~WrappedFunction() { /**/ }
0166 
0167 private:
0168 
0169    double DoEval (const double * x) const override {
0170       return fFunc( x );
0171    }
0172 
0173 
0174    Func fFunc;
0175    unsigned int fDim;
0176 
0177 
0178 }; // WrappedMultiFunction
0179 
0180 
0181 template<typename FuncObj, typename MemFuncPtr >
0182 class WrappedMemMultiFunction : public IMultiGenFunction {
0183 
0184 
0185  public:
0186 
0187    /**
0188       construct from the pointer to the object and the member function
0189     */
0190    WrappedMemMultiFunction( FuncObj & obj, MemFuncPtr memFn, unsigned int dim = 1 ) :
0191       fObj(&obj),
0192       fMemFunc( memFn ),
0193       fDim(dim)
0194    { /* no op */ }
0195 
0196    // use default  copy constructor and assignment operator
0197 
0198    /// clone (required by the interface)
0199    WrappedMemMultiFunction * Clone() const override {
0200       return new WrappedMemMultiFunction(*fObj,fMemFunc,fDim);
0201    }
0202 
0203 
0204    unsigned int NDim() const override { return fDim; }
0205 
0206 private:
0207 
0208    double DoEval (const double * x) const override {
0209       return ((*fObj).*fMemFunc)( x );
0210    }
0211 
0212 
0213    FuncObj * fObj;
0214    MemFuncPtr fMemFunc;
0215    unsigned int fDim;
0216 
0217 
0218 }; // WrappedMemMultiFunction
0219 
0220 
0221 } // namespace Math
0222 } // namespace ROOT
0223 
0224 
0225 
0226 #endif // ROOT_Math_WrappedFunction