airy.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Copyright John Maddock 2012.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_AIRY_HPP
  7. #define BOOST_MATH_AIRY_HPP
  8. #include <limits>
  9. #include <boost/math/special_functions/math_fwd.hpp>
  10. #include <boost/math/special_functions/bessel.hpp>
  11. #include <boost/math/special_functions/cbrt.hpp>
  12. #include <boost/math/special_functions/detail/airy_ai_bi_zero.hpp>
  13. #include <boost/math/tools/roots.hpp>
  14. namespace boost{ namespace math{
  15. namespace detail{
  16. template <class T, class Policy>
  17. T airy_ai_imp(T x, const Policy& pol)
  18. {
  19. BOOST_MATH_STD_USING
  20. if(x < 0)
  21. {
  22. T p = (-x * sqrt(-x) * 2) / 3;
  23. T v = T(1) / 3;
  24. T j1 = boost::math::cyl_bessel_j(v, p, pol);
  25. T j2 = boost::math::cyl_bessel_j(-v, p, pol);
  26. T ai = sqrt(-x) * (j1 + j2) / 3;
  27. //T bi = sqrt(-x / 3) * (j2 - j1);
  28. return ai;
  29. }
  30. else if(fabs(x * x * x) / 6 < tools::epsilon<T>())
  31. {
  32. T tg = boost::math::tgamma(constants::twothirds<T>(), pol);
  33. T ai = 1 / (pow(T(3), constants::twothirds<T>()) * tg);
  34. //T bi = 1 / (sqrt(boost::math::cbrt(T(3))) * tg);
  35. return ai;
  36. }
  37. else
  38. {
  39. T p = 2 * x * sqrt(x) / 3;
  40. T v = T(1) / 3;
  41. //T j1 = boost::math::cyl_bessel_i(-v, p, pol);
  42. //T j2 = boost::math::cyl_bessel_i(v, p, pol);
  43. //
  44. // Note that although we can calculate ai from j1 and j2, the accuracy is horrible
  45. // as we're subtracting two very large values, so use the Bessel K relation instead:
  46. //
  47. T ai = cyl_bessel_k(v, p, pol) * sqrt(x / 3) / boost::math::constants::pi<T>(); //sqrt(x) * (j1 - j2) / 3;
  48. //T bi = sqrt(x / 3) * (j1 + j2);
  49. return ai;
  50. }
  51. }
  52. template <class T, class Policy>
  53. T airy_bi_imp(T x, const Policy& pol)
  54. {
  55. BOOST_MATH_STD_USING
  56. if(x < 0)
  57. {
  58. T p = (-x * sqrt(-x) * 2) / 3;
  59. T v = T(1) / 3;
  60. T j1 = boost::math::cyl_bessel_j(v, p, pol);
  61. T j2 = boost::math::cyl_bessel_j(-v, p, pol);
  62. //T ai = sqrt(-x) * (j1 + j2) / 3;
  63. T bi = sqrt(-x / 3) * (j2 - j1);
  64. return bi;
  65. }
  66. else if(fabs(x * x * x) / 6 < tools::epsilon<T>())
  67. {
  68. T tg = boost::math::tgamma(constants::twothirds<T>(), pol);
  69. //T ai = 1 / (pow(T(3), constants::twothirds<T>()) * tg);
  70. T bi = 1 / (sqrt(boost::math::cbrt(T(3), pol)) * tg);
  71. return bi;
  72. }
  73. else
  74. {
  75. T p = 2 * x * sqrt(x) / 3;
  76. T v = T(1) / 3;
  77. T j1 = boost::math::cyl_bessel_i(-v, p, pol);
  78. T j2 = boost::math::cyl_bessel_i(v, p, pol);
  79. T bi = sqrt(x / 3) * (j1 + j2);
  80. return bi;
  81. }
  82. }
  83. template <class T, class Policy>
  84. T airy_ai_prime_imp(T x, const Policy& pol)
  85. {
  86. BOOST_MATH_STD_USING
  87. if(x < 0)
  88. {
  89. T p = (-x * sqrt(-x) * 2) / 3;
  90. T v = T(2) / 3;
  91. T j1 = boost::math::cyl_bessel_j(v, p, pol);
  92. T j2 = boost::math::cyl_bessel_j(-v, p, pol);
  93. T aip = -x * (j1 - j2) / 3;
  94. return aip;
  95. }
  96. else if(fabs(x * x) / 2 < tools::epsilon<T>())
  97. {
  98. T tg = boost::math::tgamma(constants::third<T>(), pol);
  99. T aip = 1 / (boost::math::cbrt(T(3), pol) * tg);
  100. return -aip;
  101. }
  102. else
  103. {
  104. T p = 2 * x * sqrt(x) / 3;
  105. T v = T(2) / 3;
  106. //T j1 = boost::math::cyl_bessel_i(-v, p, pol);
  107. //T j2 = boost::math::cyl_bessel_i(v, p, pol);
  108. //
  109. // Note that although we can calculate ai from j1 and j2, the accuracy is horrible
  110. // as we're subtracting two very large values, so use the Bessel K relation instead:
  111. //
  112. T aip = -cyl_bessel_k(v, p, pol) * x / (boost::math::constants::root_three<T>() * boost::math::constants::pi<T>());
  113. return aip;
  114. }
  115. }
  116. template <class T, class Policy>
  117. T airy_bi_prime_imp(T x, const Policy& pol)
  118. {
  119. BOOST_MATH_STD_USING
  120. if(x < 0)
  121. {
  122. T p = (-x * sqrt(-x) * 2) / 3;
  123. T v = T(2) / 3;
  124. T j1 = boost::math::cyl_bessel_j(v, p, pol);
  125. T j2 = boost::math::cyl_bessel_j(-v, p, pol);
  126. T aip = -x * (j1 + j2) / constants::root_three<T>();
  127. return aip;
  128. }
  129. else if(fabs(x * x) / 2 < tools::epsilon<T>())
  130. {
  131. T tg = boost::math::tgamma(constants::third<T>(), pol);
  132. T bip = sqrt(boost::math::cbrt(T(3), pol)) / tg;
  133. return bip;
  134. }
  135. else
  136. {
  137. T p = 2 * x * sqrt(x) / 3;
  138. T v = T(2) / 3;
  139. T j1 = boost::math::cyl_bessel_i(-v, p, pol);
  140. T j2 = boost::math::cyl_bessel_i(v, p, pol);
  141. T aip = x * (j1 + j2) / boost::math::constants::root_three<T>();
  142. return aip;
  143. }
  144. }
  145. template <class T, class Policy>
  146. T airy_ai_zero_imp(int m, const Policy& pol)
  147. {
  148. BOOST_MATH_STD_USING // ADL of std names, needed for log, sqrt.
  149. // Handle cases when a negative zero (negative rank) is requested.
  150. if(m < 0)
  151. {
  152. return policies::raise_domain_error<T>("boost::math::airy_ai_zero<%1%>(%1%, int)",
  153. "Requested the %1%'th zero, but the rank must be 1 or more !", static_cast<T>(m), pol);
  154. }
  155. // Handle case when the zero'th zero is requested.
  156. if(m == 0U)
  157. {
  158. return policies::raise_domain_error<T>("boost::math::airy_ai_zero<%1%>(%1%,%1%)",
  159. "The requested rank of the zero is %1%, but must be 1 or more !", static_cast<T>(m), pol);
  160. }
  161. // Set up the initial guess for the upcoming root-finding.
  162. const T guess_root = boost::math::detail::airy_zero::airy_ai_zero_detail::initial_guess<T>(m, pol);
  163. // Select the maximum allowed iterations based on the number
  164. // of decimal digits in the numeric type T, being at least 12.
  165. const int my_digits10 = static_cast<int>(static_cast<float>(policies::digits<T, Policy>() * 0.301F));
  166. const boost::uintmax_t iterations_allowed = static_cast<boost::uintmax_t>((std::max)(12, my_digits10 * 2));
  167. boost::uintmax_t iterations_used = iterations_allowed;
  168. // Use a dynamic tolerance because the roots get closer the higher m gets.
  169. T tolerance;
  170. if (m <= 10) { tolerance = T(0.3F); }
  171. else if(m <= 100) { tolerance = T(0.1F); }
  172. else if(m <= 1000) { tolerance = T(0.05F); }
  173. else { tolerance = T(1) / sqrt(T(m)); }
  174. // Perform the root-finding using Newton-Raphson iteration from Boost.Math.
  175. const T am =
  176. boost::math::tools::newton_raphson_iterate(
  177. boost::math::detail::airy_zero::airy_ai_zero_detail::function_object_ai_and_ai_prime<T, Policy>(pol),
  178. guess_root,
  179. T(guess_root - tolerance),
  180. T(guess_root + tolerance),
  181. policies::digits<T, Policy>(),
  182. iterations_used);
  183. static_cast<void>(iterations_used);
  184. return am;
  185. }
  186. template <class T, class Policy>
  187. T airy_bi_zero_imp(int m, const Policy& pol)
  188. {
  189. BOOST_MATH_STD_USING // ADL of std names, needed for log, sqrt.
  190. // Handle cases when a negative zero (negative rank) is requested.
  191. if(m < 0)
  192. {
  193. return policies::raise_domain_error<T>("boost::math::airy_bi_zero<%1%>(%1%, int)",
  194. "Requested the %1%'th zero, but the rank must 1 or more !", static_cast<T>(m), pol);
  195. }
  196. // Handle case when the zero'th zero is requested.
  197. if(m == 0U)
  198. {
  199. return policies::raise_domain_error<T>("boost::math::airy_bi_zero<%1%>(%1%,%1%)",
  200. "The requested rank of the zero is %1%, but must be 1 or more !", static_cast<T>(m), pol);
  201. }
  202. // Set up the initial guess for the upcoming root-finding.
  203. const T guess_root = boost::math::detail::airy_zero::airy_bi_zero_detail::initial_guess<T>(m, pol);
  204. // Select the maximum allowed iterations based on the number
  205. // of decimal digits in the numeric type T, being at least 12.
  206. const int my_digits10 = static_cast<int>(static_cast<float>(policies::digits<T, Policy>() * 0.301F));
  207. const boost::uintmax_t iterations_allowed = static_cast<boost::uintmax_t>((std::max)(12, my_digits10 * 2));
  208. boost::uintmax_t iterations_used = iterations_allowed;
  209. // Use a dynamic tolerance because the roots get closer the higher m gets.
  210. T tolerance;
  211. if (m <= 10) { tolerance = T(0.3F); }
  212. else if(m <= 100) { tolerance = T(0.1F); }
  213. else if(m <= 1000) { tolerance = T(0.05F); }
  214. else { tolerance = T(1) / sqrt(T(m)); }
  215. // Perform the root-finding using Newton-Raphson iteration from Boost.Math.
  216. const T bm =
  217. boost::math::tools::newton_raphson_iterate(
  218. boost::math::detail::airy_zero::airy_bi_zero_detail::function_object_bi_and_bi_prime<T, Policy>(pol),
  219. guess_root,
  220. T(guess_root - tolerance),
  221. T(guess_root + tolerance),
  222. policies::digits<T, Policy>(),
  223. iterations_used);
  224. static_cast<void>(iterations_used);
  225. return bm;
  226. }
  227. } // namespace detail
  228. template <class T, class Policy>
  229. inline typename tools::promote_args<T>::type airy_ai(T x, const Policy&)
  230. {
  231. BOOST_FPU_EXCEPTION_GUARD
  232. typedef typename tools::promote_args<T>::type result_type;
  233. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  234. typedef typename policies::normalise<
  235. Policy,
  236. policies::promote_float<false>,
  237. policies::promote_double<false>,
  238. policies::discrete_quantile<>,
  239. policies::assert_undefined<> >::type forwarding_policy;
  240. return policies::checked_narrowing_cast<result_type, Policy>(detail::airy_ai_imp<value_type>(static_cast<value_type>(x), forwarding_policy()), "boost::math::airy<%1%>(%1%)");
  241. }
  242. template <class T>
  243. inline typename tools::promote_args<T>::type airy_ai(T x)
  244. {
  245. return airy_ai(x, policies::policy<>());
  246. }
  247. template <class T, class Policy>
  248. inline typename tools::promote_args<T>::type airy_bi(T x, const Policy&)
  249. {
  250. BOOST_FPU_EXCEPTION_GUARD
  251. typedef typename tools::promote_args<T>::type result_type;
  252. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  253. typedef typename policies::normalise<
  254. Policy,
  255. policies::promote_float<false>,
  256. policies::promote_double<false>,
  257. policies::discrete_quantile<>,
  258. policies::assert_undefined<> >::type forwarding_policy;
  259. return policies::checked_narrowing_cast<result_type, Policy>(detail::airy_bi_imp<value_type>(static_cast<value_type>(x), forwarding_policy()), "boost::math::airy<%1%>(%1%)");
  260. }
  261. template <class T>
  262. inline typename tools::promote_args<T>::type airy_bi(T x)
  263. {
  264. return airy_bi(x, policies::policy<>());
  265. }
  266. template <class T, class Policy>
  267. inline typename tools::promote_args<T>::type airy_ai_prime(T x, const Policy&)
  268. {
  269. BOOST_FPU_EXCEPTION_GUARD
  270. typedef typename tools::promote_args<T>::type result_type;
  271. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  272. typedef typename policies::normalise<
  273. Policy,
  274. policies::promote_float<false>,
  275. policies::promote_double<false>,
  276. policies::discrete_quantile<>,
  277. policies::assert_undefined<> >::type forwarding_policy;
  278. return policies::checked_narrowing_cast<result_type, Policy>(detail::airy_ai_prime_imp<value_type>(static_cast<value_type>(x), forwarding_policy()), "boost::math::airy<%1%>(%1%)");
  279. }
  280. template <class T>
  281. inline typename tools::promote_args<T>::type airy_ai_prime(T x)
  282. {
  283. return airy_ai_prime(x, policies::policy<>());
  284. }
  285. template <class T, class Policy>
  286. inline typename tools::promote_args<T>::type airy_bi_prime(T x, const Policy&)
  287. {
  288. BOOST_FPU_EXCEPTION_GUARD
  289. typedef typename tools::promote_args<T>::type result_type;
  290. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  291. typedef typename policies::normalise<
  292. Policy,
  293. policies::promote_float<false>,
  294. policies::promote_double<false>,
  295. policies::discrete_quantile<>,
  296. policies::assert_undefined<> >::type forwarding_policy;
  297. return policies::checked_narrowing_cast<result_type, Policy>(detail::airy_bi_prime_imp<value_type>(static_cast<value_type>(x), forwarding_policy()), "boost::math::airy<%1%>(%1%)");
  298. }
  299. template <class T>
  300. inline typename tools::promote_args<T>::type airy_bi_prime(T x)
  301. {
  302. return airy_bi_prime(x, policies::policy<>());
  303. }
  304. template <class T, class Policy>
  305. inline T airy_ai_zero(int m, const Policy& /*pol*/)
  306. {
  307. BOOST_FPU_EXCEPTION_GUARD
  308. typedef typename policies::evaluation<T, Policy>::type value_type;
  309. typedef typename policies::normalise<
  310. Policy,
  311. policies::promote_float<false>,
  312. policies::promote_double<false>,
  313. policies::discrete_quantile<>,
  314. policies::assert_undefined<> >::type forwarding_policy;
  315. BOOST_STATIC_ASSERT_MSG( false == std::numeric_limits<T>::is_specialized
  316. || ( true == std::numeric_limits<T>::is_specialized
  317. && false == std::numeric_limits<T>::is_integer),
  318. "Airy value type must be a floating-point type.");
  319. return policies::checked_narrowing_cast<T, Policy>(detail::airy_ai_zero_imp<value_type>(m, forwarding_policy()), "boost::math::airy_ai_zero<%1%>(unsigned)");
  320. }
  321. template <class T>
  322. inline T airy_ai_zero(int m)
  323. {
  324. return airy_ai_zero<T>(m, policies::policy<>());
  325. }
  326. template <class T, class OutputIterator, class Policy>
  327. inline OutputIterator airy_ai_zero(
  328. int start_index,
  329. unsigned number_of_zeros,
  330. OutputIterator out_it,
  331. const Policy& pol)
  332. {
  333. typedef T result_type;
  334. BOOST_STATIC_ASSERT_MSG( false == std::numeric_limits<T>::is_specialized
  335. || ( true == std::numeric_limits<T>::is_specialized
  336. && false == std::numeric_limits<T>::is_integer),
  337. "Airy value type must be a floating-point type.");
  338. for(unsigned i = 0; i < number_of_zeros; ++i)
  339. {
  340. *out_it = boost::math::airy_ai_zero<result_type>(start_index + i, pol);
  341. ++out_it;
  342. }
  343. return out_it;
  344. }
  345. template <class T, class OutputIterator>
  346. inline OutputIterator airy_ai_zero(
  347. int start_index,
  348. unsigned number_of_zeros,
  349. OutputIterator out_it)
  350. {
  351. return airy_ai_zero<T>(start_index, number_of_zeros, out_it, policies::policy<>());
  352. }
  353. template <class T, class Policy>
  354. inline T airy_bi_zero(int m, const Policy& /*pol*/)
  355. {
  356. BOOST_FPU_EXCEPTION_GUARD
  357. typedef typename policies::evaluation<T, Policy>::type value_type;
  358. typedef typename policies::normalise<
  359. Policy,
  360. policies::promote_float<false>,
  361. policies::promote_double<false>,
  362. policies::discrete_quantile<>,
  363. policies::assert_undefined<> >::type forwarding_policy;
  364. BOOST_STATIC_ASSERT_MSG( false == std::numeric_limits<T>::is_specialized
  365. || ( true == std::numeric_limits<T>::is_specialized
  366. && false == std::numeric_limits<T>::is_integer),
  367. "Airy value type must be a floating-point type.");
  368. return policies::checked_narrowing_cast<T, Policy>(detail::airy_bi_zero_imp<value_type>(m, forwarding_policy()), "boost::math::airy_bi_zero<%1%>(unsigned)");
  369. }
  370. template <typename T>
  371. inline T airy_bi_zero(int m)
  372. {
  373. return airy_bi_zero<T>(m, policies::policy<>());
  374. }
  375. template <class T, class OutputIterator, class Policy>
  376. inline OutputIterator airy_bi_zero(
  377. int start_index,
  378. unsigned number_of_zeros,
  379. OutputIterator out_it,
  380. const Policy& pol)
  381. {
  382. typedef T result_type;
  383. BOOST_STATIC_ASSERT_MSG( false == std::numeric_limits<T>::is_specialized
  384. || ( true == std::numeric_limits<T>::is_specialized
  385. && false == std::numeric_limits<T>::is_integer),
  386. "Airy value type must be a floating-point type.");
  387. for(unsigned i = 0; i < number_of_zeros; ++i)
  388. {
  389. *out_it = boost::math::airy_bi_zero<result_type>(start_index + i, pol);
  390. ++out_it;
  391. }
  392. return out_it;
  393. }
  394. template <class T, class OutputIterator>
  395. inline OutputIterator airy_bi_zero(
  396. int start_index,
  397. unsigned number_of_zeros,
  398. OutputIterator out_it)
  399. {
  400. return airy_bi_zero<T>(start_index, number_of_zeros, out_it, policies::policy<>());
  401. }
  402. }} // namespaces
  403. #endif // BOOST_MATH_AIRY_HPP