finite_difference.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // (C) Copyright Nick Thompson 2018.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_DIFFERENTIATION_FINITE_DIFFERENCE_HPP
  6. #define BOOST_MATH_DIFFERENTIATION_FINITE_DIFFERENCE_HPP
  7. /*
  8. * Performs numerical differentiation by finite-differences.
  9. *
  10. * All numerical differentiation using finite-differences are ill-conditioned, and these routines are no exception.
  11. * A simple argument demonstrates that the error is unbounded as h->0.
  12. * Take the one sides finite difference formula f'(x) = (f(x+h)-f(x))/h.
  13. * The evaluation of f induces an error as well as the error from the finite-difference approximation, giving
  14. * |f'(x) - (f(x+h) -f(x))/h| < h|f''(x)|/2 + (|f(x)|+|f(x+h)|)eps/h =: g(h), where eps is the unit roundoff for the type.
  15. * It is reasonable to choose h in a way that minimizes the maximum error bound g(h).
  16. * The value of h that minimizes g is h = sqrt(2eps(|f(x)| + |f(x+h)|)/|f''(x)|), and for this value of h the error bound is
  17. * sqrt(2eps(|f(x+h) +f(x)||f''(x)|)).
  18. * In fact it is not necessary to compute the ratio (|f(x+h)| + |f(x)|)/|f''(x)|; the error bound of ~\sqrt{\epsilon} still holds if we set it to one.
  19. *
  20. *
  21. * For more details on this method of analysis, see
  22. *
  23. * http://www.uio.no/studier/emner/matnat/math/MAT-INF1100/h08/kompendiet/diffint.pdf
  24. * http://web.archive.org/web/20150420195907/http://www.uio.no/studier/emner/matnat/math/MAT-INF1100/h08/kompendiet/diffint.pdf
  25. *
  26. *
  27. * It can be shown on general grounds that when choosing the optimal h, the maximum error in f'(x) is ~(|f(x)|eps)^k/k+1|f^(k-1)(x)|^1/k+1.
  28. * From this we can see that full precision can be recovered in the limit k->infinity.
  29. *
  30. * References:
  31. *
  32. * 1) Fornberg, Bengt. "Generation of finite difference formulas on arbitrarily spaced grids." Mathematics of computation 51.184 (1988): 699-706.
  33. *
  34. *
  35. * The second algorithm, the complex step derivative, is not ill-conditioned.
  36. * However, it requires that your function can be evaluated at complex arguments.
  37. * The idea is that f(x+ih) = f(x) +ihf'(x) - h^2f''(x) + ... so f'(x) \approx Im[f(x+ih)]/h.
  38. * No subtractive cancellation occurs. The error is ~ eps|f'(x)| + eps^2|f'''(x)|/6; hard to beat that.
  39. *
  40. * References:
  41. *
  42. * 1) Squire, William, and George Trapp. "Using complex variables to estimate derivatives of real functions." Siam Review 40.1 (1998): 110-112.
  43. */
  44. #include <complex>
  45. #include <boost/math/special_functions/next.hpp>
  46. namespace boost{ namespace math{ namespace differentiation {
  47. namespace detail {
  48. template<class Real>
  49. Real make_xph_representable(Real x, Real h)
  50. {
  51. using std::numeric_limits;
  52. // Redefine h so that x + h is representable. Not using this trick leads to large error.
  53. // The compiler flag -ffast-math evaporates these operations . . .
  54. Real temp = x + h;
  55. h = temp - x;
  56. // Handle the case x + h == x:
  57. if (h == 0)
  58. {
  59. h = boost::math::nextafter(x, (numeric_limits<Real>::max)()) - x;
  60. }
  61. return h;
  62. }
  63. }
  64. template<class F, class Real>
  65. Real complex_step_derivative(const F f, Real x)
  66. {
  67. // Is it really this easy? Yes.
  68. // Note that some authors recommend taking the stepsize h to be smaller than epsilon(), some recommending use of the min().
  69. // This idea was tested over a few billion test cases and found the make the error *much* worse.
  70. // Even 2eps and eps/2 made the error worse, which was surprising.
  71. using std::complex;
  72. using std::numeric_limits;
  73. constexpr const Real step = (numeric_limits<Real>::epsilon)();
  74. constexpr const Real inv_step = 1/(numeric_limits<Real>::epsilon)();
  75. return f(complex<Real>(x, step)).imag()*inv_step;
  76. }
  77. namespace detail {
  78. template <unsigned>
  79. struct fd_tag {};
  80. template<class F, class Real>
  81. Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<1>&)
  82. {
  83. using std::sqrt;
  84. using std::pow;
  85. using std::abs;
  86. using std::numeric_limits;
  87. const Real eps = (numeric_limits<Real>::epsilon)();
  88. // Error bound ~eps^1/2
  89. // Note that this estimate of h differs from the best estimate by a factor of sqrt((|f(x)| + |f(x+h)|)/|f''(x)|).
  90. // Since this factor is invariant under the scaling f -> kf, then we are somewhat justified in approximating it by 1.
  91. // This approximation will get better as we move to higher orders of accuracy.
  92. Real h = 2 * sqrt(eps);
  93. h = detail::make_xph_representable(x, h);
  94. Real yh = f(x + h);
  95. Real y0 = f(x);
  96. Real diff = yh - y0;
  97. if (error)
  98. {
  99. Real ym = f(x - h);
  100. Real ypph = abs(yh - 2 * y0 + ym) / h;
  101. // h*|f''(x)|*0.5 + (|f(x+h)+|f(x)|)*eps/h
  102. *error = ypph / 2 + (abs(yh) + abs(y0))*eps / h;
  103. }
  104. return diff / h;
  105. }
  106. template<class F, class Real>
  107. Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<2>&)
  108. {
  109. using std::sqrt;
  110. using std::pow;
  111. using std::abs;
  112. using std::numeric_limits;
  113. const Real eps = (numeric_limits<Real>::epsilon)();
  114. // Error bound ~eps^2/3
  115. // See the previous discussion to understand determination of h and the error bound.
  116. // Series[(f[x+h] - f[x-h])/(2*h), {h, 0, 4}]
  117. Real h = pow(3 * eps, static_cast<Real>(1) / static_cast<Real>(3));
  118. h = detail::make_xph_representable(x, h);
  119. Real yh = f(x + h);
  120. Real ymh = f(x - h);
  121. Real diff = yh - ymh;
  122. if (error)
  123. {
  124. Real yth = f(x + 2 * h);
  125. Real ymth = f(x - 2 * h);
  126. *error = eps * (abs(yh) + abs(ymh)) / (2 * h) + abs((yth - ymth) / 2 - diff) / (6 * h);
  127. }
  128. return diff / (2 * h);
  129. }
  130. template<class F, class Real>
  131. Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<4>&)
  132. {
  133. using std::sqrt;
  134. using std::pow;
  135. using std::abs;
  136. using std::numeric_limits;
  137. const Real eps = (numeric_limits<Real>::epsilon)();
  138. // Error bound ~eps^4/5
  139. Real h = pow(11.25*eps, (Real)1 / (Real)5);
  140. h = detail::make_xph_representable(x, h);
  141. Real ymth = f(x - 2 * h);
  142. Real yth = f(x + 2 * h);
  143. Real yh = f(x + h);
  144. Real ymh = f(x - h);
  145. Real y2 = ymth - yth;
  146. Real y1 = yh - ymh;
  147. if (error)
  148. {
  149. // Mathematica code to extract the remainder:
  150. // Series[(f[x-2*h]+ 8*f[x+h] - 8*f[x-h] - f[x+2*h])/(12*h), {h, 0, 7}]
  151. Real y_three_h = f(x + 3 * h);
  152. Real y_m_three_h = f(x - 3 * h);
  153. // Error from fifth derivative:
  154. *error = abs((y_three_h - y_m_three_h) / 2 + 2 * (ymth - yth) + 5 * (yh - ymh) / 2) / (30 * h);
  155. // Error from function evaluation:
  156. *error += eps * (abs(yth) + abs(ymth) + 8 * (abs(ymh) + abs(yh))) / (12 * h);
  157. }
  158. return (y2 + 8 * y1) / (12 * h);
  159. }
  160. template<class F, class Real>
  161. Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<6>&)
  162. {
  163. using std::sqrt;
  164. using std::pow;
  165. using std::abs;
  166. using std::numeric_limits;
  167. const Real eps = (numeric_limits<Real>::epsilon)();
  168. // Error bound ~eps^6/7
  169. // Error: h^6f^(7)(x)/140 + 5|f(x)|eps/h
  170. Real h = pow(eps / 168, (Real)1 / (Real)7);
  171. h = detail::make_xph_representable(x, h);
  172. Real yh = f(x + h);
  173. Real ymh = f(x - h);
  174. Real y1 = yh - ymh;
  175. Real y2 = f(x - 2 * h) - f(x + 2 * h);
  176. Real y3 = f(x + 3 * h) - f(x - 3 * h);
  177. if (error)
  178. {
  179. // Mathematica code to generate fd scheme for 7th derivative:
  180. // Sum[(-1)^i*Binomial[7, i]*(f[x+(3-i)*h] + f[x+(4-i)*h])/2, {i, 0, 7}]
  181. // Mathematica to demonstrate that this is a finite difference formula for 7th derivative:
  182. // Series[(f[x+4*h]-f[x-4*h] + 6*(f[x-3*h] - f[x+3*h]) + 14*(f[x-h] - f[x+h] + f[x+2*h] - f[x-2*h]))/2, {h, 0, 15}]
  183. Real y7 = (f(x + 4 * h) - f(x - 4 * h) - 6 * y3 - 14 * y1 - 14 * y2) / 2;
  184. *error = abs(y7) / (140 * h) + 5 * (abs(yh) + abs(ymh))*eps / h;
  185. }
  186. return (y3 + 9 * y2 + 45 * y1) / (60 * h);
  187. }
  188. template<class F, class Real>
  189. Real finite_difference_derivative(const F f, Real x, Real* error, const fd_tag<8>&)
  190. {
  191. using std::sqrt;
  192. using std::pow;
  193. using std::abs;
  194. using std::numeric_limits;
  195. const Real eps = (numeric_limits<Real>::epsilon)();
  196. // Error bound ~eps^8/9.
  197. // In double precision, we only expect to lose two digits of precision while using this formula, at the cost of 8 function evaluations.
  198. // Error: h^8|f^(9)(x)|/630 + 7|f(x)|eps/h assuming 7 unstabilized additions.
  199. // Mathematica code to get the error:
  200. // Series[(f[x+h]-f[x-h])*(4/5) + (1/5)*(f[x-2*h] - f[x+2*h]) + (4/105)*(f[x+3*h] - f[x-3*h]) + (1/280)*(f[x-4*h] - f[x+4*h]), {h, 0, 9}]
  201. // If we used Kahan summation, we could get the max error down to h^8|f^(9)(x)|/630 + |f(x)|eps/h.
  202. Real h = pow(551.25*eps, (Real)1 / (Real)9);
  203. h = detail::make_xph_representable(x, h);
  204. Real yh = f(x + h);
  205. Real ymh = f(x - h);
  206. Real y1 = yh - ymh;
  207. Real y2 = f(x - 2 * h) - f(x + 2 * h);
  208. Real y3 = f(x + 3 * h) - f(x - 3 * h);
  209. Real y4 = f(x - 4 * h) - f(x + 4 * h);
  210. Real tmp1 = 3 * y4 / 8 + 4 * y3;
  211. Real tmp2 = 21 * y2 + 84 * y1;
  212. if (error)
  213. {
  214. // Mathematica code to generate fd scheme for 7th derivative:
  215. // Sum[(-1)^i*Binomial[9, i]*(f[x+(4-i)*h] + f[x+(5-i)*h])/2, {i, 0, 9}]
  216. // Mathematica to demonstrate that this is a finite difference formula for 7th derivative:
  217. // Series[(f[x+5*h]-f[x- 5*h])/2 + 4*(f[x-4*h] - f[x+4*h]) + 27*(f[x+3*h] - f[x-3*h])/2 + 24*(f[x-2*h] - f[x+2*h]) + 21*(f[x+h] - f[x-h]), {h, 0, 15}]
  218. Real f9 = (f(x + 5 * h) - f(x - 5 * h)) / 2 + 4 * y4 + 27 * y3 / 2 + 24 * y2 + 21 * y1;
  219. *error = abs(f9) / (630 * h) + 7 * (abs(yh) + abs(ymh))*eps / h;
  220. }
  221. return (tmp1 + tmp2) / (105 * h);
  222. }
  223. template<class F, class Real, class tag>
  224. Real finite_difference_derivative(const F, Real, Real*, const tag&)
  225. {
  226. // Always fails, but condition is template-arg-dependent so only evaluated if we get instantiated.
  227. BOOST_STATIC_ASSERT_MSG(sizeof(Real) == 0, "Finite difference not implemented for this order: try 1, 2, 4, 6 or 8");
  228. }
  229. }
  230. template<class F, class Real, size_t order=6>
  231. inline Real finite_difference_derivative(const F f, Real x, Real* error = nullptr)
  232. {
  233. return detail::finite_difference_derivative(f, x, error, detail::fd_tag<order>());
  234. }
  235. }}} // namespaces
  236. #endif