SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
NumericalDerivative.h
Go to the documentation of this file.
1
17/*
18 * NumericalDerivative.h
19 *
20 * Created on: Sep 17, 2019
21 * Author: mschefer
22 */
23
24#ifndef _SEUTILS_NUMERICALDERIVATIVE_H_
25#define _SEUTILS_NUMERICALDERIVATIVE_H_
26
27#include <functional>
28
29namespace SourceXtractor {
30
32public:
33
34 static double centralDifference(std::function<double(double)> f, double x) {
35 double h = 1e-6;
36
37 // we need to consider that the rounding of those values could generate bad accuracy
38 // so rather than using 2. * h, we have to use the actual difference
39 // marked "volatile" to avoid possible compiler optimizations ruining this scheme
40 volatile double x_plus = x + h;
41 volatile double x_minus = x - h;
42 double h_2 = x_plus - x_minus;
43
44 return (f(x_plus) - f(x_minus)) / h_2;
45 }
46
47};
48
49
50} // end SourceXtractor
51
52#endif /* _SEUTILS_NUMERICALDERIVATIVE_H_ */
static double centralDifference(std::function< double(double)> f, double x)