Logo ROOT   6.13/01
Reference Guide
WrappedFunction.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: L. Moneta, A. Zsenei 08/2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 
25 #ifndef ROOT_Math_WrappedFunction
26 #define ROOT_Math_WrappedFunction
27 
28 #include "IFunction.h"
29 
30 
31 namespace ROOT {
32 namespace Math {
33 
34 
35 
36 
37 struct NullTypeFunc1D {};
38 
39 typedef double(*FreeFunctionPtr)(double);
40 
41 typedef double(*FreeMultiFunctionPtr)(const double*);
42 
43 /**
44  Template class to wrap any C++ callable object which takes one argument
45  i.e. implementing operator() (double x) in a One-dimensional function interface.
46  It provides a ROOT::Math::IGenFunction-like signature
47 
48  Note: If you want to wrap just the reference (to avoid copying) you need to use
49  Func& or const Func & as template parameter. The former should be used when the
50  operator() is not a const method of Func
51 
52  @ingroup GenFunc
53 
54  */
55 template< typename Func = FreeFunctionPtr >
56 class WrappedFunction : public IGenFunction {
57 
58 
59  public:
60 
61  /**
62  construct from the pointer to the object and the member function
63  */
64  WrappedFunction( Func f ) :
65  fFunc( f )
66  { /* no op */ }
67 
68  // use default copy contructor and assignment operator
69 
70  /// clone (required by the interface)
71  WrappedFunction * Clone() const {
72  return new WrappedFunction(fFunc);
73  }
74 
75  // virtual ~WrappedFunction() { /**/ }
76 
77 private:
78 
79  virtual double DoEval (double x) const {
80  return fFunc( x );
81  }
82 
83 
84  Func fFunc;
85 
86 
87 }; // WrappedFunction
88 
89 
90 /**
91  Template class to wrap any member function of a class
92  taking a double and returning a double in a 1D function interface
93  For example, if you have a class like:
94  struct X {
95  double Eval(double x);
96  };
97  you can wrapped in the following way:
98  WrappedMemFunction<X, double ( X::* ) (double) > f;
99 
100 
101  @ingroup GenFunc
102 
103  */
104 
105 template<typename FuncObj, typename MemFuncPtr >
107 
108 
109  public:
110 
111  /**
112  construct from the pointer to the object and the member function
113  */
114  WrappedMemFunction( FuncObj & obj, MemFuncPtr memFn ) :
115  fObj(&obj),
116  fMemFunc( memFn )
117  { /* no op */ }
118 
119  // use default copy contructor and assignment operator
120 
121  /// clone (required by the interface)
123  return new WrappedMemFunction(*fObj,fMemFunc);
124  }
125 
126 
127 private:
128 
129  virtual double DoEval (double x) const {
130  return ((*fObj).*fMemFunc)( x );
131  }
132 
133 
134  FuncObj * fObj;
135  MemFuncPtr fMemFunc;
136 
137 
138 }; // WrappedMemFunction
139 
140 
141 /**
142  Template class to wrap any C++ callable object
143  implementing operator() (const double * x) in a multi-dimensional function interface.
144  It provides a ROOT::Math::IGenMultiFunction-like signature
145 
146  Note: If you want to wrap just the reference (to avoid copying) you need to use
147  Func& or const Func & as template parameter. The former should be used when the
148  operator() is not a const method of Func
149 
150  @ingroup GenFunc
151 
152  */
153 template< typename Func = FreeMultiFunctionPtr >
155 
156 
157  public:
158 
159  /**
160  construct from the pointer to the object and the member function
161  */
162  WrappedMultiFunction( Func f , unsigned int dim = 1) :
163  fFunc( f ),
164  fDim( dim)
165  { /* no op */ }
166 
167  // use default copy contructor and assignment operator
168 
169  /// clone (required by the interface)
171  return new WrappedMultiFunction(fFunc,fDim);
172  }
173 
174  unsigned int NDim() const { return fDim; }
175 
176  // virtual ~WrappedFunction() { /**/ }
177 
178 private:
179 
180  virtual double DoEval (const double * x) const {
181  return fFunc( x );
182  }
183 
184 
185  Func fFunc;
186  unsigned int fDim;
187 
188 
189 }; // WrappedMultiFunction
190 
191 
192 template<typename FuncObj, typename MemFuncPtr >
194 
195 
196  public:
197 
198  /**
199  construct from the pointer to the object and the member function
200  */
201  WrappedMemMultiFunction( FuncObj & obj, MemFuncPtr memFn, unsigned int dim = 1 ) :
202  fObj(&obj),
203  fMemFunc( memFn ),
204  fDim(dim)
205  { /* no op */ }
206 
207  // use default copy contructor and assignment operator
208 
209  /// clone (required by the interface)
211  return new WrappedMemMultiFunction(*fObj,fMemFunc,fDim);
212  }
213 
214 
215  unsigned int NDim() const { return fDim; }
216 
217 private:
218 
219  virtual double DoEval (const double * x) const {
220  return ((*fObj).*fMemFunc)( x );
221  }
222 
223 
224  FuncObj * fObj;
225  MemFuncPtr fMemFunc;
226  unsigned int fDim;
227 
228 
229 }; // WrappedMemMultiFunction
230 
231 
232 } // namespace Math
233 } // namespace ROOT
234 
235 
236 
237 #endif // ROOT_Math_WrappedFunction
virtual double DoEval(const double *x) const
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:135
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
virtual double DoEval(double x) const
implementation of the evaluation function. Must be implemented by derived classes ...
WrappedMemFunction(FuncObj &obj, MemFuncPtr memFn)
construct from the pointer to the object and the member function
WrappedMultiFunction(Func f, unsigned int dim=1)
construct from the pointer to the object and the member function
double(* FreeFunctionPtr)(double)
Template class to wrap any C++ callable object which takes one argument i.e.
WrappedMultiFunction * Clone() const
clone (required by the interface)
virtual double DoEval(double x) const
implementation of the evaluation function. Must be implemented by derived classes ...
WrappedMemFunction * Clone() const
clone (required by the interface)
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
virtual double DoEval(const double *x) const
WrappedFunction(Func f)
construct from the pointer to the object and the member function
unsigned int NDim() const
Retrieve the dimension of the function.
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
Namespace for new Math classes and functions.
WrappedMemMultiFunction(FuncObj &obj, MemFuncPtr memFn, unsigned int dim=1)
construct from the pointer to the object and the member function
WrappedFunction * Clone() const
clone (required by the interface)
WrappedMemMultiFunction * Clone() const
clone (required by the interface)
double(* FreeMultiFunctionPtr)(const double *)
unsigned int NDim() const
Retrieve the dimension of the function.
Template class to wrap any member function of a class taking a double and returning a double in a 1D ...