Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
OneDimFunctionAdapter.h
Go to the documentation of this file.
1// @(#)root/mathmore:$Id: OneDimFunctionAdapter.h 20063 2007-09-24 13:16:14Z moneta $
2// Author: L. Moneta Wed Dec 6 11:45:55 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for class OneDimMultiFunctionAdapter
12
13#ifndef ROOT_Math_OneDimFunctionAdapter
14#define ROOT_Math_OneDimFunctionAdapter
15
16#include "Math/IFunction.h"
17#include "Math/IParamFunction.h"
18
19#include <cassert>
20
21namespace ROOT {
22
23namespace Math {
24
25
26
27// struct using for evaluating the function
28template<class MultiFuncType>
30 // evaluate function (in general case no param)
31 static double F (MultiFuncType f, const double * x, const double * = nullptr ) {
32 return f( x );
33 }
34};
35// specialized for param functions
36template<>
38 static double F ( const ROOT::Math::IParamMultiFunction & f, const double * x, const double * p = nullptr ) {
39 return f( x, p );
40 }
41};
42
43
44/**
45 OneDimMultiFunctionAdapter class to wrap a multidimensional function in
46 one dimensional one.
47 Given a f(x1,x2,x3,....xn) transforms in a f( x_i) given the coordinate intex i and the vector x[]
48 of the coordinates.
49 It provides the possibility to copy and own the data array of the coordinates or to maintain internally a pointer to an external array
50 for being more efficient. In this last case the user must guarantee the life of the given passed pointer
51
52 @ingroup GenFunc
53
54*/
55template <class MultiFuncType = const ROOT::Math::IMultiGenFunction &>
57
58public:
59
60
61 /**
62 Constructor from the function object , pointer to an external array of x values
63 and coordinate we want to adapt
64 */
65 OneDimMultiFunctionAdapter (MultiFuncType f, const double * x, unsigned int icoord = 0, const double * p = nullptr ) :
66 fFunc(f),
67 fX( const_cast<double *>(x) ), // we need to modify x but then we restore it as before
68 fParams(p),
69 fCoord(icoord),
70 fDim(0),
71 fOwn(false)
72 {
73 assert(fX != nullptr);
74 }
75 /**
76 Constructor from the function object , dimension of the function and
77 and coordinate we want to adapt.
78 The coordinate cached vector is created inside and eventually the values must be passed
79 later with the SetX which will copy them
80 */
81 OneDimMultiFunctionAdapter (MultiFuncType f, unsigned int dim = 1, unsigned int icoord = 0, const double *p = nullptr) :
82 fFunc(f),
83 fX(nullptr),
84 fParams(p),
85 fCoord(icoord),
86 fDim(dim),
87 fOwn(true)
88 {
89 fX = new double[dim];
90 }
91
92 /**
93 Destructor (no operations)
94 */
95 ~OneDimMultiFunctionAdapter () override { if (fOwn && fX) delete [] fX; }
96
97 /**
98 clone
99 */
100 OneDimMultiFunctionAdapter * Clone( ) const override {
101 if (fOwn) {
103 std::copy(fX, fX+fDim, f->fX);
104 return f;
105 }
106 else
108 }
109
110public:
111
112 /**
113 Set X values in case vector is own, iterator size must match previous
114 set dimension
115 */
116 template<class Iterator>
117 void SetX(Iterator begin, Iterator end) {
118 if (fOwn) std::copy(begin, end, fX);
119 }
120
121
122 /**
123 set pointer without copying the values
124 */
125 void SetX(double * x) {
126 if (!fOwn) fX = x;
127 }
128
129 /**
130 set values
131 */
132 void SetX(const double * x) {
133 if (fOwn) std::copy(x, x+fDim, fX);
134 else
135 SetX( const_cast<double *>(x) ); // we need to modify x but then we restore it as before
136 }
137
138
139 void SetCoord(int icoord) { fCoord=icoord;}
140
141 // copy constructor
143 fFunc(rhs.fFunc),
144 fParams(rhs.fParams),
145 fCoord(rhs.fCoord),
146 fDim(rhs.fDim),
147 fOwn(rhs.fOwn)
148 {
149 if (fOwn) {
150 fX = new double[fDim];
151 std::copy( rhs.fX, rhs.fX+fDim, fX);
152 }
153 else fX = rhs.fX;
154 }
155
156
157private:
158
159 // dummy assignment (should never be called and clone must be used)
161 if (this == &rhs) return *this;
162 assert(false);
163 }
164
165 /**
166 evaluate function at the values x[] given in the constructor and
167 as function of the coordinate fCoord.
168 */
169 double DoEval(double x) const override {
170 if (fOwn) {
171 fX[fCoord] = x;
173 }
174 else {
175
176 // case vector fX represents useful values needed later
177 // need to modify fX and restore afterwards the original values
178 double xprev = fX[fCoord]; // keep original value to restore in fX
179 fX[fCoord] = x;
181 // restore original values
182 fX[fCoord] = xprev;
183 return y;
184 }
185 }
186
187
188private:
189
190 MultiFuncType fFunc;
191 mutable double * fX;
192 const double * fParams;
193 unsigned int fCoord;
194 unsigned int fDim;
195 bool fOwn;
196
197};
198
199
200/**
201 OneDimParamFunctionAdapter class to wrap a multi-dim parametric function in
202 one dimensional one.
203 Given a f(x[],p1,...pn) transforms in a f( p_i) given the param index i and the vectors x[] and p[]
204 of the coordinates and parameters
205 It has to be used carefully, since for efficiency reason it does not copy the parameter object
206 but re-uses the given pointer for the p[] vector.
207 The ParamFuncType reference by default is not const because the operator()(x,p) is not a const method
208
209 @ingroup GenFunc
210
211*/
212template <class ParamFuncType = ROOT::Math::IParamMultiFunction &>
214
215public:
216
217
218 /**
219 Constructor from the function object , x value and coordinate we want to adapt
220 */
221 OneDimParamFunctionAdapter (ParamFuncType f, const double * x, const double * p, unsigned int ipar =0 ) :
222 fFunc(f),
223 fX(x ),
224 fParams(p),
225 fIpar(ipar)
226 {
227 assert(fX != nullptr);
228 assert(fParams != nullptr);
229 }
230
231 /**
232 Destructor (no operations)
233 */
235
236 /**
237 clone
238 */
239 OneDimParamFunctionAdapter * Clone( ) const override {
241 }
242
243 // can use default copy constructor
244
245private:
246
247 /**
248 evaluate function at the values x[] given in the constructor and
249 as function of the coordinate fCoord.
250 */
251 double DoEval(double x) const override {
252 // HACK: use const_cast to modify the function values x[] and restore afterwards the original ones
253 double * p = const_cast<double *>(fParams);
254 double pprev = fParams[fIpar]; // keep original value to restore in fX
255 p[fIpar] = x;
256 double y = fFunc( fX, p );
257 p[fIpar] = pprev;
258 return y;
259 }
260
261
262private:
263
264 ParamFuncType fFunc;
265 const double * fX;
266 const double * fParams;
267 unsigned int fIpar;
268
269};
270
271
272} // end namespace Math
273
274} // end namespace ROOT
275
276
277#endif /* ROOT_Math_OneDimFunctionAdapter */
#define f(i)
Definition RSha256.hxx:104
winID h TVirtualViewer3D TVirtualGLPainter p
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
OneDimMultiFunctionAdapter class to wrap a multidimensional function in one dimensional one.
OneDimMultiFunctionAdapter(MultiFuncType f, unsigned int dim=1, unsigned int icoord=0, const double *p=nullptr)
Constructor from the function object , dimension of the function and and coordinate we want to adapt.
OneDimMultiFunctionAdapter(const OneDimMultiFunctionAdapter &rhs)
OneDimMultiFunctionAdapter & operator=(const OneDimMultiFunctionAdapter &rhs)
void SetX(double *x)
set pointer without copying the values
~OneDimMultiFunctionAdapter() override
Destructor (no operations)
OneDimMultiFunctionAdapter * Clone() const override
clone
OneDimMultiFunctionAdapter(MultiFuncType f, const double *x, unsigned int icoord=0, const double *p=nullptr)
Constructor from the function object , pointer to an external array of x values and coordinate we wan...
void SetX(Iterator begin, Iterator end)
Set X values in case vector is own, iterator size must match previous set dimension.
double DoEval(double x) const override
evaluate function at the values x[] given in the constructor and as function of the coordinate fCoord...
OneDimParamFunctionAdapter class to wrap a multi-dim parametric function in one dimensional one.
double DoEval(double x) const override
evaluate function at the values x[] given in the constructor and as function of the coordinate fCoord...
~OneDimParamFunctionAdapter() override
Destructor (no operations)
OneDimParamFunctionAdapter * Clone() const override
clone
OneDimParamFunctionAdapter(ParamFuncType f, const double *x, const double *p, unsigned int ipar=0)
Constructor from the function object , x value and coordinate we want to adapt.
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static double F(const ROOT::Math::IParamMultiFunction &f, const double *x, const double *p=nullptr)
static double F(MultiFuncType f, const double *x, const double *=nullptr)