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;
128 void testDerivPerf() {
131 std::cout <<
"\n\n***************************************************************\n";
132 std::cout <<
"Test derivation performances....\n\n";
135 double p[3] = {2,3,4};
140 double x1 = 0;
double x2 = 10;
141 double dx = (x2-
x1)/
double(n);
146 for (
int i = 0; i <
n; ++i) {
147 double x = x1 + dx*i;
151 std::cout <<
"Time using ROOT::Math::Derivator :\t" << timer.
RealTime() << std::endl;
152 int pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
156 for (
int i = 0; i <
n; ++i) {
158 double x = x1 + dx*i;
162 std::cout <<
"Time using ROOT::Math::Derivator(2):\t" << timer.
RealTime() << std::endl;
163 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
167 for (
int i = 0; i <
n; ++i) {
168 double x = x1 + dx*i;
172 std::cout <<
"Time using ROOT::Math::Derivator(3):\t" << timer.
RealTime() << std::endl;
173 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
176 TF1 f2(
"pol",
"pol2",0,10);
181 for (
int i = 0; i <
n; ++i) {
182 double x = x1 + dx*i;
186 std::cout <<
"Time using TF1::Derivative :\t\t" << timer.
RealTime() << std::endl;
187 pr = std::cout.precision(18);
188 std::cout << s2 << std::endl;
189 std::cout.precision(pr);
195 double userFunc(
const double *
x,
const double *) {
198 double userFunc1(
double x) {
return userFunc(&x, 0); }
200 double userFunc2(
const double *
x) {
return userFunc(x, 0); }
202 void testDerivPerfUser() {
205 std::cout <<
"\n\n***************************************************************\n";
206 std::cout <<
"Test derivation performances - using a User function\n\n";
212 double x1 = 0;
double x2 = 10;
213 double dx = (x2-
x1)/
double(n);
218 for (
int i = 0; i <
n; ++i) {
219 double x = x1 + dx*i;
223 std::cout <<
"Time using ROOT::Math::Derivator :\t" << timer.
RealTime() << std::endl;
224 int pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
228 for (
int i = 0; i <
n; ++i) {
230 double x = x1 + dx*i;
234 std::cout <<
"Time using ROOT::Math::Derivator(2):\t" << timer.
RealTime() << std::endl;
235 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
239 for (
int i = 0; i <
n; ++i) {
240 double x = x1 + dx*i;
244 std::cout <<
"Time using ROOT::Math::Derivator(3):\t" << timer.
RealTime() << std::endl;
245 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
248 TF1 f2(
"uf",userFunc,0,10,0);
252 for (
int i = 0; i <
n; ++i) {
253 double x = x1 + dx*i;
257 std::cout <<
"Time using TF1::Derivative :\t\t" << timer.
RealTime() << std::endl;
258 pr = std::cout.precision(18);
259 std::cout << s2 << std::endl;
260 std::cout.precision(pr);
267 for (
int i = 0; i <
n; ++i) {
272 std::cout <<
"Time using ROOT::Math::Derivator Multi:\t" << timer.
RealTime() << std::endl;
273 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
280 double gausFunc(
const double *
x,
const double * p) {
285 void testDerivPerfParam() {
288 std::cout <<
"\n\n***************************************************************\n";
289 std::cout <<
"Test derivation performances - using a Gaussian Param function\n\n";
292 TF1 gaus(
"gaus",gausFunc,-10,10,3);
293 double params[3] = {10,1.,1.};
300 double x1 = 0;
double x2 = 10;
301 double dx = (x2-
x1)/
double(n);
305 for (
int i = 0; i <
n; ++i) {
306 double x = x1 + dx*i;
313 std::cout <<
"Time using ROOT::Math::Derivator (1D) :\t" << timer.
RealTime() << std::endl;
314 int pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
321 for (
int i = 0; i <
n; ++i) {
328 std::cout <<
"Time using ROOT::Math::Derivator(ND):\t" << timer.
RealTime() << std::endl;
329 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
339 for (
int i = 0; i <
n; ++i) {
347 std::cout <<
"Time using TF1::ParamGradient:\t\t" << timer.
RealTime() << std::endl;
348 pr = std::cout.precision(18); std::cout << s1 << std::endl; std::cout.precision(pr);
363 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 *)
double Eval(double x, double h=1E-8) const
Computes the numerical derivative of a function f at a point x.
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
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 ...
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)
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 Error() const
return the estimate of the absolute error of the last derivative calculation
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 EvalForward(double x, double h=1E-8) const
Computes the numerical derivative at a point x using an adaptive forward difference algorithm with a ...
double Derivative(double x) const
Return the derivative of the function at a point x Use the private method DoDerivative.
Parametric Function class describing polynomials of order n.
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...
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...
static const double x1[5]
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
virtual Double_t GetParameter(Int_t ipar) const
double f2(const double *x)
Functor1D class for one-dimensional functions.
WrappedParamFunction class to wrap any multi-dimensional function pbject implementing the operator()(...
int Status() const
return the error status of the last derivative calculation