ROOT  6.06/09
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 #ifndef ROOT_Math_IFunction
29 #include "IFunction.h"
30 #endif
31 
32 
33 namespace ROOT {
34 namespace Math {
35 
36 
37 
38 
39 struct NullTypeFunc1D {};
40 
42 
43 typedef double(*FreeMultiFunctionPtr)(const double*);
44 
45 /**
46  Template class to wrap any C++ callable object which takes one argument
47  i.e. implementing operator() (double x) in a One-dimensional function interface.
48  It provides a ROOT::Math::IGenFunction-like signature
49 
50  Note: If you want to wrap just the reference (to avoid copying) you need to use
51  Func& or const Func & as template parameter. The former should be used when the
52  operator() is not a const method of Func
53 
54  @ingroup GenFunc
55 
56  */
57 template< typename Func = FreeFunctionPtr >
58 class WrappedFunction : public IGenFunction {
59 
60 
61  public:
62 
63  /**
64  construct from the pointer to the object and the member function
65  */
67  fFunc( f )
68  { /* no op */ }
69 
70  // use default copy contructor and assignment operator
71 
72  /// clone (required by the interface)
73  WrappedFunction * Clone() const {
74  return new WrappedFunction(fFunc);
75  }
76 
77  // virtual ~WrappedFunction() { /**/ }
78 
79 private:
80 
81  virtual double DoEval (double x) const {
82  return fFunc( x );
83  }
84 
85 
87 
88 
89 }; // WrappedFunction
90 
91 
92 /**
93  Template class to wrap any member function of a class
94  taking a double and returning a double in a 1D function interface
95  For example, if you have a class like:
96  struct X {
97  double Eval(double x);
98  };
99  you can wrapped in the following way:
100  WrappedMemFunction<X, double ( X::* ) (double) > f;
101 
102 
103  @ingroup GenFunc
104 
105  */
106 
107 template<typename FuncObj, typename MemFuncPtr >
109 
110 
111  public:
112 
113  /**
114  construct from the pointer to the object and the member function
115  */
116  WrappedMemFunction( FuncObj & obj, MemFuncPtr memFn ) :
117  fObj(&obj),
118  fMemFunc( memFn )
119  { /* no op */ }
120 
121  // use default copy contructor and assignment operator
122 
123  /// clone (required by the interface)
125  return new WrappedMemFunction(*fObj,fMemFunc);
126  }
127 
128 
129 private:
130 
131  virtual double DoEval (double x) const {
132  return ((*fObj).*fMemFunc)( x );
133  }
134 
135 
136  FuncObj * fObj;
137  MemFuncPtr fMemFunc;
138 
139 
140 }; // WrappedMemFunction
141 
142 
143 /**
144  Template class to wrap any C++ callable object
145  implementing operator() (const double * x) in a multi-dimensional function interface.
146  It provides a ROOT::Math::IGenMultiFunction-like signature
147 
148  Note: If you want to wrap just the reference (to avoid copying) you need to use
149  Func& or const Func & as template parameter. The former should be used when the
150  operator() is not a const method of Func
151 
152  @ingroup GenFunc
153 
154  */
155 template< typename Func = FreeMultiFunctionPtr >
157 
158 
159  public:
160 
161  /**
162  construct from the pointer to the object and the member function
163  */
164  WrappedMultiFunction( Func f , unsigned int dim = 1) :
165  fFunc( f ),
166  fDim( dim)
167  { /* no op */ }
168 
169  // use default copy contructor and assignment operator
170 
171  /// clone (required by the interface)
173  return new WrappedMultiFunction(fFunc,fDim);
174  }
175 
176  unsigned int NDim() const { return fDim; }
177 
178  // virtual ~WrappedFunction() { /**/ }
179 
180 private:
181 
182  virtual double DoEval (const double * x) const {
183  return fFunc( x );
184  }
185 
186 
188  unsigned int fDim;
189 
190 
191 }; // WrappedMultiFunction
192 
193 
194 template<typename FuncObj, typename MemFuncPtr >
196 
197 
198  public:
199 
200  /**
201  construct from the pointer to the object and the member function
202  */
203  WrappedMemMultiFunction( FuncObj & obj, MemFuncPtr memFn, unsigned int dim = 1 ) :
204  fObj(&obj),
205  fMemFunc( memFn ),
206  fDim(dim)
207  { /* no op */ }
208 
209  // use default copy contructor and assignment operator
210 
211  /// clone (required by the interface)
214  }
215 
216 
217  unsigned int NDim() const { return fDim; }
218 
219 private:
220 
221  virtual double DoEval (const double * x) const {
222  return ((*fObj).*fMemFunc)( x );
223  }
224 
225 
226  FuncObj * fObj;
227  MemFuncPtr fMemFunc;
228  unsigned int fDim;
229 
230 
231 }; // WrappedMemMultiFunction
232 
233 
234 } // namespace Math
235 } // namespace ROOT
236 
237 
238 
239 #endif // ROOT_Math_WrappedFunction
WrappedMemMultiFunction * Clone() const
clone (required by the interface)
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
WrappedMemFunction(FuncObj &obj, MemFuncPtr memFn)
construct from the pointer to the object and the member function
virtual double DoEval(const double *x) const
Implementation of the evaluation function.
WrappedMultiFunction(Func f, unsigned int dim=1)
construct from the pointer to the object and the member function
WrappedFunction * Clone() const
clone (required by the interface)
unsigned int NDim() const
Retrieve the dimension of the function.
double(* FreeFunctionPtr)(double)
Template class to wrap any C++ callable object which takes one argument i.e.
Double_t x[n]
Definition: legend1.C:17
WrappedMemFunction * Clone() const
clone (required by the interface)
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
WrappedFunction(Func f)
construct from the pointer to the object and the member function
unsigned int NDim() const
Retrieve the dimension of the function.
double f(double x)
virtual double DoEval(double x) const
implementation of the evaluation function. Must be implemented by derived classes ...
virtual double DoEval(const double *x) const
Implementation of the evaluation function.
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
virtual double DoEval(double x) const
implementation of the evaluation function. Must be implemented by derived classes ...
WrappedMultiFunction * Clone() const
clone (required by the interface)
double(* FreeMultiFunctionPtr)(const double *)
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
TObject * obj
Template class to wrap any member function of a class taking a double and returning a double in a 1D ...