generated from xlladdins/xll_template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfms_derivative.h
35 lines (29 loc) · 857 Bytes
/
fms_derivative.h
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
// fms_derivative.h - Test derivatives
#pragma once
#include <functional>
#include <limits>
namespace fms {
inline constexpr double epsilon = std::numeric_limits<double>::epsilon();
// symmetric difference quotient
template<class X, class Y>
inline auto difference_quotient(const std::function<Y(X)>& f, X dx)
{
return [dx, &f](X x) {
return (f(x + dx) - f(x - dx)) / (2 * dx);
};
}
// (f(x + h) - f(x - h))/2h = f'(x) + f'''(x) h^2/3! + O(h^3)
template<class X, class Y>
inline bool derivative_test(const std::function<Y(X)>& f, X x, X h, X df, X dddf, X O = 1)
{
dddf = std::max(fabs(dddf), 1.);
auto Df = difference_quotient(f, h);
double lhs = Df(x) - df;
double rhs = dddf * h * h / 6;
bool b = fabs(lhs) <= O * rhs;
if (!b) {
O = fabs(lhs)/fabs(rhs); // set breakpoint to check tolerance
}
return b;
}
}