Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
WrappedParamFunction.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: L. Moneta Thu Nov 23 10:38:32 2006
3
4 /**********************************************************************
5 * *
6 * Copyright (c) 2006 CERN *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 * *
12 **********************************************************************/
13
14// Header file for class WrappedParamFunction
15
16#ifndef ROOT_Math_WrappedParamFunction
17#define ROOT_Math_WrappedParamFunction
18
19#include "Math/IParamFunction.h"
20
21//#include <iostream>
22//#include <iterator>
23
24#include <vector>
25
26
27namespace ROOT {
28
29 namespace Math {
30
31
32typedef double( * FreeParamMultiFunctionPtr ) (const double *, const double * );
33
34/**
35 WrappedParamFunction class to wrap any multi-dimensional function object
36 implementing the operator()(const double * x, const double * p)
37 in an interface-like IParamFunction with a vector storing and caching internally the
38 parameter values
39
40 @ingroup ParamFunc
41
42*/
43template< typename FuncPtr = FreeParamMultiFunctionPtr >
45
46public:
47
48 /**
49 Constructor a wrapped function from a pointer to a callable object, the function dimension and number of parameters
50 which are set to zero by default
51 */
52 WrappedParamFunction (FuncPtr func, unsigned int dim = 1, unsigned int npar = 0, double * par = nullptr) :
53 fFunc(func),
54 fDim(dim),
55 fParams(std::vector<double>(npar) )
56 {
57 if (par) std::copy(par, par+npar, fParams.begin());
58 }
59
60// /**
61// Constructor a wrapped function from a non-const pointer to a callable object, the function dimension and number of parameters
62// which are set to zero by default
63// This constructor is needed in the case FuncPtr is a std::unique_ptr which has a copy ctor taking non const objects
64// */
65// WrappedParamFunction (FuncPtr & func, unsigned int dim = 1, unsigned int npar = 0, double * par = 0) :
66// fFunc(func),
67// fDim(dim),
68// fParams(std::vector<double>(npar) )
69// {
70// if (par != 0) std::copy(par,par+npar,fParams.begin() );
71// }
72
73 /**
74 Constructor a wrapped function from a pointer to a callable object, the function dimension and an iterator specifying begin and end
75 of parameters
76 */
77 template<class Iterator>
78 WrappedParamFunction (FuncPtr func, unsigned int dim, Iterator begin, Iterator end) :
79 fFunc(func),
80 fDim(dim),
81 fParams(std::vector<double>(begin,end) )
82 {}
83
84// /**
85// Constructor a wrapped function from a non - const pointer to a callable object, the function dimension and an iterator specifying begin and end of parameters.
86// This constructor is needed in the case FuncPtr is a std::unique_ptr which has a copy ctor taking non const objects
87// */
88// template<class Iterator>
89// WrappedParamFunction (FuncPtr func, unsigned int dim, Iterator begin, Iterator end) :
90// fFunc(func),
91// fDim(dim),
92// fParams(std::vector<double>(begin,end) )
93// {}
94
95 /// clone the function
96 IMultiGenFunction * Clone() const override {
97 return new WrappedParamFunction(fFunc, fDim, fParams.begin(), fParams.end());
98 }
99
100 const double * Parameters() const override {
101 return fParams.empty() ? nullptr : &fParams.front();
102 }
103
104 void SetParameters(const double * p) override {
105 std::copy(p, p+NPar(), fParams.begin() );
106 }
107
108 unsigned int NPar() const override { return fParams.size(); }
109
110 unsigned int NDim() const override { return fDim; }
111
112
113private:
114
115 /// evaluate the function given values and parameters (requested interface)
116 double DoEvalPar(const double * x, const double * p) const override {
117 return (*fFunc)( x, p );
118 }
119
120
121 FuncPtr fFunc;
122 unsigned int fDim;
123 std::vector<double> fParams;
124
125
126
127};
128
129
130typedef double( * FreeMultiFunctionPtr ) (const double *);
131
132/**
133 WrappedParamGenFunction class to wrap any multi-dimensional function
134 implementing the operator()(const double * )
135 in an interface-like IParamFunction, by fixing some of the variables and define them as
136 parameters.
137 i.e. transform any multi-dim function in a parametric function
138
139 @ingroup ParamFunc
140
141*/
142template< typename FuncPtr = FreeMultiFunctionPtr >
144
145public:
146
147 /**
148 Constructor a wrapped function from a pointer to a generic callable object implementation operator()(const double *), the new function dimension, the number of parameters (number of fixed variables) and an array specifying the index of the fixed variables which became
149 parameters in the new API
150 */
151
152 WrappedParamFunctionGen (const FuncPtr & func, unsigned int dim, unsigned int npar, const double * par, const unsigned int * idx) :
153 fFunc(func),
154 fDim(dim),
155 fParams(std::vector<double>(par,par+npar) ),
156 fParIndices(std::vector<unsigned int>(idx, idx + npar) ),
157 fX(std::vector<double>(npar+dim) ) // cached vector
158 {
159 DoInit();
160 }
161
162 /**
163 Constructor as before but taking now a non - const pointer to a callable object.
164 This constructor is needed in the case FuncPtr is a std::unique_ptr which has a copy ctor taking non const objects
165 */
166 WrappedParamFunctionGen (FuncPtr & func, unsigned int dim, unsigned int npar, const double * par, const unsigned int * idx) :
167 fFunc(func),
168 fDim(dim),
169 fParams(std::vector<double>(par,par+npar) ),
170 fParIndices(std::vector<unsigned int>(idx, idx + npar) ),
171 fX(std::vector<double>(npar+dim) ) // cached vector
172 {
173 DoInit();
174 }
175
176 /// clone the function
177 IMultiGenFunction * Clone() const override {
178 return new WrappedParamFunctionGen(fFunc, fDim, fParams.size(), fParams.empty() ? nullptr : &fParams.front(), fParIndices.empty() ? nullptr : &fParIndices.front());
179 }
180
181private:
182 // copy ctor
185
186public:
187
188 const double * Parameters() const override {
189 return fParams.empty() ? nullptr : &fParams.front();
190 }
191
192 void SetParameters(const double * p) override {
193 unsigned int npar = NPar();
194 std::copy(p, p+ npar, fParams.begin() );
195 SetParValues(npar, p);
196 }
197
198 unsigned int NPar() const override { return fParams.size(); }
199
200 unsigned int NDim() const override { return fDim; }
201
202// // re-implement this since is more efficient
203// double operator() (const double * x, const double * p) {
204// unsigned int n = fX.size();
205// unsigned int npar = fParams.size();
206// unsigned j = 0;
207// return (*fFunc)( fX);
208// }
209
210private:
211
212 /// evaluate the function (re-implement for being more efficient)
213 double DoEval(const double * x) const override {
214
215// std::cout << this << fDim << " x : ";
216// std::ostream_iterator<double> oix(std::cout," , ");
217// std::copy(x, x+fDim, oix);
218// std::cout << std::endl;
219// std::cout << "npar " << npar << std::endl;
220// std::cout << fVarIndices.size() << std::endl;
221// assert ( fVarIndices.size() == fDim); // otherwise something is wrong
222
223 for (unsigned int i = 0; i < fDim; ++i) {
224 unsigned int j = fVarIndices[i];
225 assert ( j < NPar() + fDim);
226 fX[ j ] = x[i];
227 }
228// std::cout << "X : (";
229// std::ostream_iterator<double> oi(std::cout," , ");
230// std::copy(fX.begin(), fX.end(), oi);
231// std::cout << std::endl;
232
233 return (*fFunc)( fX.empty() ? nullptr : &fX.front() );
234 }
235
236
237 /**
238 implement the required IParamFunction interface
239 */
240 double DoEvalPar(const double * x, const double * p ) const override {
241 SetParValues(NPar(), p);
242 return DoEval(x);
243 }
244
245
246 void DoInit() {
247 // calculate variable indices and set in X the parameter values
248 fVarIndices.reserve(fDim);
249 unsigned int npar = NPar();
250 for (unsigned int i = 0; i < npar + fDim; ++i) {
251 bool isVar = true;
252 for (unsigned int j = 0; j < npar; ++j) {
253 if (fParIndices[j] == i) {
254 isVar = false;
255 break;
256 }
257 }
258 if (isVar) fVarIndices.push_back(i);
259 }
260 assert ( fVarIndices.size() == fDim); // otherwise something is wrong
261
262// std::cout << "n variables " << fVarIndices.size() << std::endl;
263// std::ostream_iterator<int> oi(std::cout," ");
264// std::copy(fVarIndices.begin(), fVarIndices.end(), oi);
265// std::cout << std::endl;
266// assert( fVarIndices.size() == fDim);
267// std::cout << this << std::endl;
268
269 // set parameter values in fX
270 SetParValues(npar, fParams.empty() ? nullptr : &fParams.front());
271 for (unsigned int i = 0; i < npar; ++i) {
272 unsigned int j = fParIndices[i];
273 assert ( j < npar + fDim);
274 fX[j] = fParams[i];
275 }
276
277 }
278
279 // set the parameter values in the cached fX vector
280 // make const because it might be called from const methods
281 void SetParValues(unsigned int npar, const double * p) const {
282 for (unsigned int i = 0; i < npar; ++i) {
283 unsigned int j = fParIndices[i];
284 assert ( j < npar + fDim);
285 fX[j] = p[i];
286 }
287 }
288
289
290 mutable FuncPtr fFunc;
291 unsigned int fDim;
292 std::vector<double> fParams;
293 std::vector<unsigned int> fVarIndices;
294 std::vector<unsigned int> fParIndices;
295 mutable std::vector<double> fX;
296
297
298
299};
300
301
302 } // end namespace Math
303
304} // end namespace ROOT
305
306
307#endif /* ROOT_Math_WrappedParamFunction */
winID h TVirtualViewer3D TVirtualGLPainter p
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:61
WrappedParamGenFunction class to wrap any multi-dimensional function implementing the operator()(cons...
double DoEval(const double *x) const override
evaluate the function (re-implement for being more efficient)
WrappedParamFunctionGen & operator=(const WrappedParamFunctionGen &)
WrappedParamFunctionGen(const FuncPtr &func, unsigned int dim, unsigned int npar, const double *par, const unsigned int *idx)
Constructor a wrapped function from a pointer to a generic callable object implementation operator()(...
unsigned int NDim() const override
Retrieve the dimension of the function.
void SetParValues(unsigned int npar, const double *p) const
void SetParameters(const double *p) override
Set the parameter values.
WrappedParamFunctionGen(const WrappedParamFunctionGen &)
unsigned int NPar() const override
Return the number of Parameters.
std::vector< unsigned int > fParIndices
double DoEvalPar(const double *x, const double *p) const override
implement the required IParamFunction interface
WrappedParamFunctionGen(FuncPtr &func, unsigned int dim, unsigned int npar, const double *par, const unsigned int *idx)
Constructor as before but taking now a non - const pointer to a callable object.
IMultiGenFunction * Clone() const override
clone the function
std::vector< unsigned int > fVarIndices
const double * Parameters() const override
Access the parameter values.
WrappedParamFunction class to wrap any multi-dimensional function object implementing the operator()(...
unsigned int NDim() const override
Retrieve the dimension of the function.
const double * Parameters() const override
Access the parameter values.
void SetParameters(const double *p) override
Set the parameter values.
WrappedParamFunction(FuncPtr func, unsigned int dim, Iterator begin, Iterator end)
Constructor a wrapped function from a pointer to a callable object, the function dimension and an ite...
IMultiGenFunction * Clone() const override
clone the function
double DoEvalPar(const double *x, const double *p) const override
evaluate the function given values and parameters (requested interface)
unsigned int NPar() const override
Return the number of Parameters.
WrappedParamFunction(FuncPtr func, unsigned int dim=1, unsigned int npar=0, double *par=nullptr)
Constructor a wrapped function from a pointer to a callable object, the function dimension and number...
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
double(* FreeParamMultiFunctionPtr)(const double *, const double *)
double(* FreeMultiFunctionPtr)(const double *)
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.