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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
// @(#)root/mathcore:$Id$
// Authors: L. Moneta 11/2006
/**********************************************************************
* *
* Copyright (c) 2006 , LCG ROOT MathLib Team *
* *
* *
**********************************************************************/
// Header file for function interfaces
//
// Generic Interfaces for one or multi-dimensional functions
//
// Created by: Lorenzo Moneta : Wed Nov 13 2006
//
//
#ifndef ROOT_Math_IFunction
#define ROOT_Math_IFunction
/**
@defgroup CppFunctions Function Classes and Interfaces
Interfaces (abstract classes) and Base classes used in MathCore and MathMore numerical methods
for describing function classes. They define function and gradient evaluation and as well the
functionality for dealing with parameters in the case of parametric functions which are used for
fitting and data modeling.
Included are also adapter classes, such as functors, to wrap generic callable C++ objects
in the desired interface.
@ingroup MathCore
*/
//typedefs and tags definitions
#include "Math/IFunctionfwd.h"
namespace ROOT {
namespace Math {
/**
@defgroup GenFunc Generic Function Evaluation Interfaces
Interface classes for evaluation of function object classes in one or multi-dimensions.
@ingroup CppFunctions
*/
//___________________________________________________________________________________
/**
Documentation for the abstract class IBaseFunctionMultiDim.
Interface (abstract class) for generic functions objects of multi-dimension
Provides a method to evaluate the function given a vector of coordinate values,
by implementing operator() (const double *).
In addition it defines the interface for copying functions via the pure virtual method Clone()
and the interface for getting the function dimension via the NDim() method.
Derived classes must implement the pure private virtual method DoEval(const double *) for the
function evaluation in addition to NDim() and Clone().
@ingroup GenFunc
*/
template<class T>
class IBaseFunctionMultiDimTempl {
public:
typedef T BackendType;
typedef IBaseFunctionMultiDimTempl<T> BaseFunc;
IBaseFunctionMultiDimTempl() {}
/**
virtual destructor
*/
virtual ~IBaseFunctionMultiDimTempl() {}
/**
Clone a function.
Each derived class must implement their version of the Clone method
*/
virtual IBaseFunctionMultiDimTempl<T> *Clone() const = 0;
/**
Retrieve the dimension of the function
*/
virtual unsigned int NDim() const = 0;
/**
Evaluate the function at a point x[].
Use the pure virtual private method DoEval which must be implemented by the sub-classes
*/
T operator()(const T *x) const
{
return DoEval(x);
}
#ifdef LATER
/**
Template method to eveluate the function using the begin of an iterator
User is responsible to provide correct size for the iterator
*/
template <class Iterator>
T operator()(const Iterator it) const
{
return DoEval(&(*it));
}
#endif
private:
/**
Implementation of the evaluation function. Must be implemented by derived classes
*/
virtual T DoEval(const T *x) const = 0;
};
//___________________________________________________________________________________
/**
Interface (abstract class) for generic functions objects of one-dimension
Provides a method to evaluate the function given a value (simple double)
by implementing operator() (const double ).
In addition it defines the interface for copying functions via the pure virtual method Clone().
Derived classes must implement the pure virtual private method DoEval(double ) for the
function evaluation in addition to Clone().
An interface for evaluating the function passing a vector (like for multidim functions) is also
provided
@ingroup GenFunc
*/
class IBaseFunctionOneDim {
public:
typedef IBaseFunctionOneDim BaseFunc;
IBaseFunctionOneDim() {}
/**
virtual destructor
*/
virtual ~IBaseFunctionOneDim() {}
/**
Clone a function.
Each derived class will implement their version of the provate DoClone method
*/
virtual IBaseFunctionOneDim *Clone() const = 0;
/**
Evaluate the function at a point x
Use the a pure virtual private method DoEval which must be implemented by sub-classes
*/
double operator()(double x) const
{
return DoEval(x);
}
/**
Evaluate the function at a point x[].
Compatible method with multi-dimensional functions
*/
double operator()(const double *x) const
{
return DoEval(*x);
}
private:
// use private virtual inheritance
/// implementation of the evaluation function. Must be implemented by derived classes
virtual double DoEval(double x) const = 0;
};
//-------- GRAD functions---------------------------
//___________________________________________________________________________________
/**
Gradient interface (abstract class) defining the signature for calculating the gradient of a
multi-dimensional function.
Three methods are provided:
- Gradient(const double *x, double * grad) evaluate the full gradient vector at the vector value x
- Derivative(const double * x, int icoord) evaluate the partial derivative for the icoord coordinate
- FdF(const double *x, double &f, double * g) evaluate at the same time gradient and function/
Concrete classes should derive from ROOT::Math::IGradientFunctionMultiDim and not from this class.
@ingroup GenFunc
*/
template <class T>
class IGradientMultiDimTempl {
public:
/// virual destructor
virtual ~IGradientMultiDimTempl() {}
/**
Evaluate all the vector of function derivatives (gradient) at a point x.
Derived classes must re-implement if it is more efficient than evaluting one at a time
*/
virtual void Gradient(const T *x, T *grad) const = 0;
/**
Return the partial derivative with respect to the passed coordinate
*/
T Derivative(const T *x, unsigned int icoord = 0) const { return DoDerivative(x, icoord); }
/**
Optimized method to evaluate at the same time the function value and derivative at a point x.
Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
Derived class should implement this method if performances play an important role and if it is faster to
evaluate value and derivative at the same time
*/
virtual void FdF(const T *x, T &f, T *df) const = 0;
private:
/**
function to evaluate the derivative with respect each coordinate. To be implemented by the derived class
*/
virtual T DoDerivative(const T *x, unsigned int icoord) const = 0;
};
//___________________________________________________________________________________
/**
Specialized Gradient interface(abstract class) for one dimensional functions
It provides a method to evaluate the derivative of the function, Derivative and a
method to evaluate at the same time the function and the derivative FdF
Concrete classes should derive from ROOT::Math::IGradientFunctionOneDim and not from this class.
@ingroup GenFunc
*/
class IGradientOneDim {
public:
/// virtual destructor
virtual ~IGradientOneDim() {}
/**
Return the derivative of the function at a point x
Use the private method DoDerivative
*/
double Derivative(double x) const
{
return DoDerivative(x);
}
/**
Optimized method to evaluate at the same time the function value and derivative at a point x.
Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
Derived class should implement this method if performances play an important role and if it is faster to
evaluate value and derivative at the same time
*/
virtual void FdF(double x, double &f, double &df) const = 0;
/**
Compatibility method with multi-dimensional interface for partial derivative
*/
double Derivative(const double *x) const
{
return DoDerivative(*x);
}
/**
Compatibility method with multi-dimensional interface for Gradient
*/
void Gradient(const double *x, double *g) const
{
g[0] = DoDerivative(*x);
}
/**
Compatibility method with multi-dimensional interface for Gradient and function evaluation
*/
void FdF(const double *x, double &f, double *df) const
{
FdF(*x, f, *df);
}
private:
/**
function to evaluate the derivative with respect each coordinate. To be implemented by the derived class
*/
virtual double DoDerivative(double x) const = 0;
};
//___________________________________________________________________________________
/**
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
It implements both the ROOT::Math::IBaseFunctionMultiDimTempl and
ROOT::Math::IGradientMultiDimTempl interfaces.
The method ROOT::Math::IFunction::Gradient calculates the full gradient vector,
ROOT::Math::IFunction::Derivative calculates the partial derivative for each coordinate and
ROOT::Math::Fdf calculates the gradient and the function value at the same time.
The pure private virtual method DoDerivative() must be implemented by the derived classes, while
Gradient and FdF are by default implemented using DoDerivative, butthey can be overloaded by the
derived classes to improve the efficiency in the derivative calculation.
@ingroup GenFunc
*/
template <class T>
class IGradientFunctionMultiDimTempl : virtual public IBaseFunctionMultiDimTempl<T>,
public IGradientMultiDimTempl<T> {
public:
typedef IBaseFunctionMultiDimTempl<T> BaseFunc;
typedef IGradientMultiDimTempl<T> BaseGrad;
/**
Virtual Destructor (no operations)
*/
virtual ~IGradientFunctionMultiDimTempl() {}
/**
Evaluate all the vector of function derivatives (gradient) at a point x.
Derived classes must re-implement it if more efficient than evaluting one at a time
*/
virtual void Gradient(const T *x, T *grad) const
{
unsigned int ndim = NDim();
for (unsigned int icoord = 0; icoord < ndim; ++icoord)
grad[icoord] = BaseGrad::Derivative(x, icoord);
}
using BaseFunc::NDim;
/**
Optimized method to evaluate at the same time the function value and derivative at a point x.
Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
Derived class should implement this method if performances play an important role and if it is faster to
evaluate value and derivative at the same time
*/
virtual void FdF(const T *x, T &f, T *df) const
{
f = BaseFunc::operator()(x);
Gradient(x, df);
}
};
//___________________________________________________________________________________
/**
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
It implements both the ROOT::Math::IBaseFunctionOneDim and
ROOT::Math::IGradientOneDim interfaces.
The method ROOT::Math::IFunction::Derivative calculates the derivative and
ROOT::Math::Fdf calculates the derivative and the function values at the same time.
The pure private virtual method DoDerivative() must be implemented by the derived classes, while
FdF is by default implemented using DoDerivative, but it can be overloaded by the
derived classes to improve the efficiency in the derivative calculation.
@ingroup GenFunc
*/
//template <>
class IGradientFunctionOneDim :
virtual public IBaseFunctionOneDim ,
public IGradientOneDim {
public:
typedef IBaseFunctionOneDim BaseFunc;
typedef IGradientOneDim BaseGrad;
/**
Virtual Destructor (no operations)
*/
virtual ~IGradientFunctionOneDim() {}
/**
Optimized method to evaluate at the same time the function value and derivative at a point x.
Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
Derived class should implement this method if performances play an important role and if it is faster to
evaluate value and derivative at the same time
*/
virtual void FdF(double x, double &f, double &df) const
{
f = operator()(x);
df = Derivative(x);
}
};
} // namespace Math
} // namespace ROOT
#endif /* ROOT_Math_IFunction */
|