24 typedef double ( *
FP ) ( double,
void * );
25 typedef double ( *
FP2 ) ( double );
46 std::vector<double> p(3);
59 std::cout <<
"Derivative of function inheriting from IGenFunction f(x) = 2 + 3x + 4x^2 at x = 2" << std::endl;
60 std::cout <<
"Return code: " << der->
Status() << std::endl;
61 std::cout <<
"Result: " << result <<
" +/- " << der->
Error() << std::endl;
62 std::cout <<
"Exact result: " << f1->
Derivative(x0) << std::endl;
63 std::cout <<
"EvalForward: " << der->
EvalForward(*f1, x0) << std::endl;
64 std::cout <<
"EvalBackward: " << der->
EvalBackward(x0, step) << std::endl << std::endl;;
73 std::cout <<
"Derivative of a free function f(x) = x^(3/2) at x = 2" << std::endl;
74 std::cout <<
"EvalCentral: " << der->
EvalCentral(x0) << std::endl;
75 std::cout <<
"EvalForward: " << der->
EvalForward(x0) << std::endl;
76 std::cout <<
"EvalBackward: " << der->
EvalBackward(x0) << std::endl;
78 std::cout <<
"Exact result: " << 1.5*
sqrt(x0) << std::endl << std::endl;
90 std::cout <<
"Derivative of a free function wrapped in a Functor f(x) = x^(3/2) at x = 2" << std::endl;
91 std::cout <<
"EvalCentral: " << der->
Eval( *f3, x0) << std::endl;
93 std::cout <<
"EvalForward: " << der->
EvalForward(x0) << std::endl;
94 std::cout <<
"EvalBackward: " << der->
EvalBackward(x0) << std::endl;
95 std::cout <<
"Exact result: " << 1.5*
sqrt(x0) << std::endl << std::endl;
105 std::cout <<
"Tes a derivator without a function" << std::endl;
106 std::cout << der2.
Eval(1.0) << std::endl;
129 void testDerivPerf() {
132 std::cout <<
"\n\n***************************************************************\n";
133 std::cout <<
"Test derivation performances....\n\n";
136 double p[3] = {2,3,4};
141 double x1 = 0;
double x2 = 10;
142 double dx = (x2-
x1)/
double(n);
147 for (
int i = 0; i <
n; ++i) {
148 double x = x1 + dx*i;
152 std::cout <<
"Time using ROOT::Math::Derivator :\t" << timer.
RealTime() << std::endl;
153 int pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
157 for (
int i = 0; i <
n; ++i) {
159 double x = x1 + dx*i;
163 std::cout <<
"Time using ROOT::Math::Derivator(2):\t" << timer.
RealTime() << std::endl;
164 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
168 for (
int i = 0; i <
n; ++i) {
169 double x = x1 + dx*i;
173 std::cout <<
"Time using ROOT::Math::Derivator(3):\t" << timer.
RealTime() << std::endl;
174 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
177 TF1 f2(
"pol",
"pol2",0,10);
182 for (
int i = 0; i <
n; ++i) {
183 double x = x1 + dx*i;
187 std::cout <<
"Time using TF1::Derivative :\t\t" << timer.
RealTime() << std::endl;
188 pr = std::cout.precision(18);
189 std::cout << s2 << std::endl;
190 std::cout.precision(pr);
196 double userFunc(
const double *
x,
const double *) {
199 double userFunc1(
double x) {
return userFunc(&x, 0); }
201 double userFunc2(
const double *
x) {
return userFunc(x, 0); }
203 void testDerivPerfUser() {
206 std::cout <<
"\n\n***************************************************************\n";
207 std::cout <<
"Test derivation performances - using a User function\n\n";
213 double x1 = 0;
double x2 = 10;
214 double dx = (x2-
x1)/
double(n);
219 for (
int i = 0; i <
n; ++i) {
220 double x = x1 + dx*i;
224 std::cout <<
"Time using ROOT::Math::Derivator :\t" << timer.
RealTime() << std::endl;
225 int pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
229 for (
int i = 0; i <
n; ++i) {
231 double x = x1 + dx*i;
235 std::cout <<
"Time using ROOT::Math::Derivator(2):\t" << timer.
RealTime() << std::endl;
236 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
240 for (
int i = 0; i <
n; ++i) {
241 double x = x1 + dx*i;
245 std::cout <<
"Time using ROOT::Math::Derivator(3):\t" << timer.
RealTime() << std::endl;
246 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
249 TF1 f2(
"uf",userFunc,0,10,0);
253 for (
int i = 0; i <
n; ++i) {
254 double x = x1 + dx*i;
258 std::cout <<
"Time using TF1::Derivative :\t\t" << timer.
RealTime() << std::endl;
259 pr = std::cout.precision(18);
260 std::cout << s2 << std::endl;
261 std::cout.precision(pr);
268 for (
int i = 0; i <
n; ++i) {
273 std::cout <<
"Time using ROOT::Math::Derivator Multi:\t" << timer.
RealTime() << std::endl;
274 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
281 double gausFunc(
const double *
x,
const double * p) {
286 void testDerivPerfParam() {
289 std::cout <<
"\n\n***************************************************************\n";
290 std::cout <<
"Test derivation performances - using a Gaussian Param function\n\n";
293 TF1 gaus(
"gaus",gausFunc,-10,10,3);
294 double params[3] = {10,1.,1.};
301 double x1 = 0;
double x2 = 10;
302 double dx = (x2-
x1)/
double(n);
306 for (
int i = 0; i <
n; ++i) {
307 double x = x1 + dx*i;
314 std::cout <<
"Time using ROOT::Math::Derivator (1D) :\t" << timer.
RealTime() << std::endl;
315 int pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
322 for (
int i = 0; i <
n; ++i) {
329 std::cout <<
"Time using ROOT::Math::Derivator(ND):\t" << timer.
RealTime() << std::endl;
330 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
340 for (
int i = 0; i <
n; ++i) {
348 std::cout <<
"Time using TF1::ParamGradient:\t\t" << timer.
RealTime() << std::endl;
349 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
364 testDerivPerfParam();
virtual void SetParameters(const Double_t *params)
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Class for computing numerical derivative of a function.
double(* FP)(double, void *)
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
virtual Double_t Derivative(Double_t x, Double_t *params=0, Double_t epsilon=0.001) const
Returns the first derivative of the function at point x, computed by Richardson's extrapolation metho...
Template class to wrap any C++ callable object which takes one argument i.e.
double normal_pdf(double x, double sigma=1, double x0=0)
Probability density function of the normal (Gaussian) distribution.
static const double x2[5]
void Stop()
Stop the stopwatch.
double pow(double, double)
int Status() const
return the error status of the last derivative calculation
virtual Double_t GradientPar(Int_t ipar, const Double_t *x, Double_t eps=0.01)
Compute the gradient (derivative) wrt a parameter ipar.
double EvalCentral(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive central difference algorithm with a ...
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
virtual void SetParameters(const double *p)
Set the parameter values.
void SetFunction(const IGenFunction &f)
Set the function for calculating the derivatives.
double myfunc(double x, void *)
double Eval(double x, double h=1E-8) const
Computes the numerical derivative of a function f at a point x.
Parametric Function class describing polynomials of order n.
static const double x1[5]
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
double f2(const double *x)
virtual Double_t GetParameter(Int_t ipar) const
Functor1D class for one-dimensional functions.
double Derivative(double x) const
Return the derivative of the function at a point x Use the private method DoDerivative.
double EvalForward(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive forward difference algorithm with a ...
WrappedParamFunction class to wrap any multi-dimensional function pbject implementing the operator()(...
double EvalBackward(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive backward difference algorithm with a...
double Error() const
return the estimate of the absolute error of the last derivative calculation