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