123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #ifndef BOOST_MATH_DIFFERENTIATION_FINITE_DIFFERENCE_HPP
- #define BOOST_MATH_DIFFERENTIATION_FINITE_DIFFERENCE_HPP
- #include <complex>
- #include <boost/math/special_functions/next.hpp>
- namespace boost{ namespace math{ namespace differentiation {
- namespace detail {
- template<class Real>
- Real make_xph_representable(Real x, Real h)
- {
- using std::numeric_limits;
-
-
- Real temp = x + h;
- h = temp - x;
-
- if (h == 0)
- {
- h = boost::math::nextafter(x, (numeric_limits<Real>::max)()) - x;
- }
- return h;
- }
- }
- template<class F, class Real>
- Real complex_step_derivative(const F f, Real x)
- {
-
-
-
-
- using std::complex;
- using std::numeric_limits;
- constexpr const Real step = (numeric_limits<Real>::epsilon)();
- constexpr const Real inv_step = 1/(numeric_limits<Real>::epsilon)();
- return f(complex<Real>(x, step)).imag()*inv_step;
- }
- namespace detail {
- template <unsigned>
- struct fd_tag {};
- template<class F, class Real>
- Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<1>&)
- {
- using std::sqrt;
- using std::pow;
- using std::abs;
- using std::numeric_limits;
- const Real eps = (numeric_limits<Real>::epsilon)();
-
-
-
-
- Real h = 2 * sqrt(eps);
- h = detail::make_xph_representable(x, h);
- Real yh = f(x + h);
- Real y0 = f(x);
- Real diff = yh - y0;
- if (error)
- {
- Real ym = f(x - h);
- Real ypph = abs(yh - 2 * y0 + ym) / h;
-
- *error = ypph / 2 + (abs(yh) + abs(y0))*eps / h;
- }
- return diff / h;
- }
- template<class F, class Real>
- Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<2>&)
- {
- using std::sqrt;
- using std::pow;
- using std::abs;
- using std::numeric_limits;
- const Real eps = (numeric_limits<Real>::epsilon)();
-
-
-
- Real h = pow(3 * eps, static_cast<Real>(1) / static_cast<Real>(3));
- h = detail::make_xph_representable(x, h);
- Real yh = f(x + h);
- Real ymh = f(x - h);
- Real diff = yh - ymh;
- if (error)
- {
- Real yth = f(x + 2 * h);
- Real ymth = f(x - 2 * h);
- *error = eps * (abs(yh) + abs(ymh)) / (2 * h) + abs((yth - ymth) / 2 - diff) / (6 * h);
- }
- return diff / (2 * h);
- }
- template<class F, class Real>
- Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<4>&)
- {
- using std::sqrt;
- using std::pow;
- using std::abs;
- using std::numeric_limits;
- const Real eps = (numeric_limits<Real>::epsilon)();
-
- Real h = pow(Real(11.25)*eps, static_cast<Real>(1) / static_cast<Real>(5));
- h = detail::make_xph_representable(x, h);
- Real ymth = f(x - 2 * h);
- Real yth = f(x + 2 * h);
- Real yh = f(x + h);
- Real ymh = f(x - h);
- Real y2 = ymth - yth;
- Real y1 = yh - ymh;
- if (error)
- {
-
-
- Real y_three_h = f(x + 3 * h);
- Real y_m_three_h = f(x - 3 * h);
-
- *error = abs((y_three_h - y_m_three_h) / 2 + 2 * (ymth - yth) + 5 * (yh - ymh) / 2) / (30 * h);
-
- *error += eps * (abs(yth) + abs(ymth) + 8 * (abs(ymh) + abs(yh))) / (12 * h);
- }
- return (y2 + 8 * y1) / (12 * h);
- }
- template<class F, class Real>
- Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<6>&)
- {
- using std::sqrt;
- using std::pow;
- using std::abs;
- using std::numeric_limits;
- const Real eps = (numeric_limits<Real>::epsilon)();
-
-
- Real h = pow(eps / 168, static_cast<Real>(1) / static_cast<Real>(7));
- h = detail::make_xph_representable(x, h);
- Real yh = f(x + h);
- Real ymh = f(x - h);
- Real y1 = yh - ymh;
- Real y2 = f(x - 2 * h) - f(x + 2 * h);
- Real y3 = f(x + 3 * h) - f(x - 3 * h);
- if (error)
- {
-
-
-
-
- Real y7 = (f(x + 4 * h) - f(x - 4 * h) - 6 * y3 - 14 * y1 - 14 * y2) / 2;
- *error = abs(y7) / (140 * h) + 5 * (abs(yh) + abs(ymh))*eps / h;
- }
- return (y3 + 9 * y2 + 45 * y1) / (60 * h);
- }
- template<class F, class Real>
- Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<8>&)
- {
- using std::sqrt;
- using std::pow;
- using std::abs;
- using std::numeric_limits;
- const Real eps = (numeric_limits<Real>::epsilon)();
-
-
-
-
-
-
- Real h = pow(Real(551.25)*eps, static_cast<Real>(1) / static_cast<Real>(9));
- h = detail::make_xph_representable(x, h);
- Real yh = f(x + h);
- Real ymh = f(x - h);
- Real y1 = yh - ymh;
- Real y2 = f(x - 2 * h) - f(x + 2 * h);
- Real y3 = f(x + 3 * h) - f(x - 3 * h);
- Real y4 = f(x - 4 * h) - f(x + 4 * h);
- Real tmp1 = 3 * y4 / 8 + 4 * y3;
- Real tmp2 = 21 * y2 + 84 * y1;
- if (error)
- {
-
-
-
-
- Real f9 = (f(x + 5 * h) - f(x - 5 * h)) / 2 + 4 * y4 + 27 * y3 / 2 + 24 * y2 + 21 * y1;
- *error = abs(f9) / (630 * h) + 7 * (abs(yh) + abs(ymh))*eps / h;
- }
- return (tmp1 + tmp2) / (105 * h);
- }
- template<class F, class Real, class tag>
- Real finite_difference_derivative(const F, Real, Real*, const tag&)
- {
-
- static_assert(sizeof(Real) == 0, "Finite difference not implemented for this order: try 1, 2, 4, 6 or 8");
- }
- }
- template<class F, class Real, size_t order=6>
- inline Real finite_difference_derivative(const F f, Real x, Real* error = nullptr)
- {
- return detail::finite_difference_derivative(f, x, error, detail::fd_tag<order>());
- }
- }}}
- #endif
|