bessel_zeros_example.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Copyright Christopher Kormanyos 2013.
  2. // Copyright Paul A. Bristow 2013.
  3. // Copyright John Maddock 2013.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or
  6. // copy at http://www.boost.org/LICENSE_1_0.txt).
  7. #ifdef _MSC_VER
  8. # pragma warning (disable : 4512) // assignment operator could not be generated.
  9. # pragma warning (disable : 4996) // assignment operator could not be generated.
  10. #endif
  11. #include <iostream>
  12. #include <limits>
  13. #include <vector>
  14. #include <algorithm>
  15. #include <iomanip>
  16. #include <iterator>
  17. // Weisstein, Eric W. "Bessel Function Zeros." From MathWorld--A Wolfram Web Resource.
  18. // http://mathworld.wolfram.com/BesselFunctionZeros.html
  19. // Test values can be calculated using [@wolframalpha.com WolframAplha]
  20. // See also http://dlmf.nist.gov/10.21
  21. //[bessel_zero_example_1
  22. /*`This example demonstrates calculating zeros of the Bessel, Neumann and Airy functions.
  23. It also shows how Boost.Math and Boost.Multiprecision can be combined to provide
  24. a many decimal digit precision. For 50 decimal digit precision we need to include
  25. */
  26. #include <boost/multiprecision/cpp_dec_float.hpp>
  27. /*`and a `typedef` for `float_type` may be convenient
  28. (allowing a quick switch to re-compute at built-in `double` or other precision)
  29. */
  30. typedef boost::multiprecision::cpp_dec_float_50 float_type;
  31. //`To use the functions for finding zeros of the functions we need
  32. #include <boost/math/special_functions/bessel.hpp>
  33. //`This file includes the forward declaration signatures for the zero-finding functions:
  34. // #include <boost/math/special_functions/math_fwd.hpp>
  35. /*`but more details are in the full documentation, for example at
  36. [@http://www.boost.org/doc/libs/1_53_0/libs/math/doc/sf_and_dist/html/math_toolkit/special/bessel/bessel_over.html Boost.Math Bessel functions]
  37. */
  38. /*`This example shows obtaining both a single zero of the Bessel function,
  39. and then placing multiple zeros into a container like `std::vector` by providing an iterator.
  40. The signature of the single value function is:
  41. template <class T>
  42. inline typename detail::bessel_traits<T, T, policies::policy<> >::result_type
  43. cyl_bessel_j_zero(T v, // Floating-point value for Jv.
  44. int m); // start index.
  45. The result type is controlled by the floating-point type of parameter `v`
  46. (but subject to the usual __precision_policy and __promotion_policy).
  47. The signature of multiple zeros function is:
  48. template <class T, class OutputIterator>
  49. inline OutputIterator cyl_bessel_j_zero(T v, // Floating-point value for Jv.
  50. int start_index, // 1-based start index.
  51. unsigned number_of_zeros,
  52. OutputIterator out_it); // iterator into container for zeros.
  53. There is also a version which allows control of the __policy_section for error handling and precision.
  54. template <class T, class OutputIterator, class Policy>
  55. inline OutputIterator cyl_bessel_j_zero(T v, // Floating-point value for Jv.
  56. int start_index, // 1-based start index.
  57. unsigned number_of_zeros,
  58. OutputIterator out_it,
  59. const Policy& pol); // iterator into container for zeros.
  60. */
  61. //] [/bessel_zero_example_1]
  62. //[bessel_zero_example_iterator_1]
  63. /*`We use the `cyl_bessel_j_zero` output iterator parameter `out_it`
  64. to create a sum of 1/zeros[super 2] by defining a custom output iterator:
  65. */
  66. template <class T>
  67. struct output_summation_iterator
  68. {
  69. output_summation_iterator(T* p) : p_sum(p)
  70. {}
  71. output_summation_iterator& operator*()
  72. { return *this; }
  73. output_summation_iterator& operator++()
  74. { return *this; }
  75. output_summation_iterator& operator++(int)
  76. { return *this; }
  77. output_summation_iterator& operator = (T const& val)
  78. {
  79. *p_sum += 1./ (val * val); // Summing 1/zero^2.
  80. return *this;
  81. }
  82. private:
  83. T* p_sum;
  84. };
  85. //] [/bessel_zero_example_iterator_1]
  86. int main()
  87. {
  88. try
  89. {
  90. //[bessel_zero_example_2]
  91. /*`[tip It is always wise to place code using Boost.Math inside try'n'catch blocks;
  92. this will ensure that helpful error messages can be shown when exceptional conditions arise.]
  93. First, evaluate a single Bessel zero.
  94. The precision is controlled by the float-point type of template parameter `T` of `v`
  95. so this example has `double` precision, at least 15 but up to 17 decimal digits (for the common 64-bit double).
  96. */
  97. double root = boost::math::cyl_bessel_j_zero(0.0, 1);
  98. // Displaying with default precision of 6 decimal digits:
  99. std::cout << "boost::math::cyl_bessel_j_zero(0.0, 1) " << root << std::endl; // 2.40483
  100. // And with all the guaranteed (15) digits:
  101. std::cout.precision(std::numeric_limits<double>::digits10);
  102. std::cout << "boost::math::cyl_bessel_j_zero(0.0, 1) " << root << std::endl; // 2.40482555769577
  103. /*`But note that because the parameter `v` controls the precision of the result,
  104. `v` [*must be a floating-point type].
  105. So if you provide an integer type, say 0, rather than 0.0, then it will fail to compile thus:
  106. ``
  107. root = boost::math::cyl_bessel_j_zero(0, 1);
  108. ``
  109. with this error message
  110. ``
  111. error C2338: Order must be a floating-point type.
  112. ``
  113. Optionally, we can use a policy to ignore errors, C-style, returning some value
  114. perhaps infinity or NaN, or the best that can be done. (See __user_error_handling).
  115. To create a (possibly unwise!) policy that ignores all errors:
  116. */
  117. typedef boost::math::policies::policy
  118. <
  119. boost::math::policies::domain_error<boost::math::policies::ignore_error>,
  120. boost::math::policies::overflow_error<boost::math::policies::ignore_error>,
  121. boost::math::policies::underflow_error<boost::math::policies::ignore_error>,
  122. boost::math::policies::denorm_error<boost::math::policies::ignore_error>,
  123. boost::math::policies::pole_error<boost::math::policies::ignore_error>,
  124. boost::math::policies::evaluation_error<boost::math::policies::ignore_error>
  125. > ignore_all_policy;
  126. double inf = std::numeric_limits<double>::infinity();
  127. double nan = std::numeric_limits<double>::quiet_NaN();
  128. std::cout << "boost::math::cyl_bessel_j_zero(-1.0, 0) " << std::endl;
  129. double dodgy_root = boost::math::cyl_bessel_j_zero(-1.0, 0, ignore_all_policy());
  130. std::cout << "boost::math::cyl_bessel_j_zero(-1.0, 1) " << dodgy_root << std::endl; // 1.#QNAN
  131. double inf_root = boost::math::cyl_bessel_j_zero(inf, 1, ignore_all_policy());
  132. std::cout << "boost::math::cyl_bessel_j_zero(inf, 1) " << inf_root << std::endl; // 1.#QNAN
  133. double nan_root = boost::math::cyl_bessel_j_zero(nan, 1, ignore_all_policy());
  134. std::cout << "boost::math::cyl_bessel_j_zero(nan, 1) " << nan_root << std::endl; // 1.#QNAN
  135. /*`Another version of `cyl_bessel_j_zero` allows calculation of multiple zeros with one call,
  136. placing the results in a container, often `std::vector`.
  137. For example, generate five `double` roots of J[sub v] for integral order 2.
  138. showing the same results as column J[sub 2](x) in table 1 of
  139. [@ http://mathworld.wolfram.com/BesselFunctionZeros.html Wolfram Bessel Function Zeros].
  140. */
  141. unsigned int n_roots = 5U;
  142. std::vector<double> roots;
  143. boost::math::cyl_bessel_j_zero(2.0, 1, n_roots, std::back_inserter(roots));
  144. std::copy(roots.begin(),
  145. roots.end(),
  146. std::ostream_iterator<double>(std::cout, "\n"));
  147. /*`Or generate 50 decimal digit roots of J[sub v] for non-integral order `v = 71/19`.
  148. We set the precision of the output stream and show trailing zeros to display a fixed 50 decimal digits.
  149. */
  150. std::cout.precision(std::numeric_limits<float_type>::digits10); // 50 decimal digits.
  151. std::cout << std::showpoint << std::endl; // Show trailing zeros.
  152. float_type x = float_type(71) / 19;
  153. float_type r = boost::math::cyl_bessel_j_zero(x, 1); // 1st root.
  154. std::cout << "x = " << x << ", r = " << r << std::endl;
  155. r = boost::math::cyl_bessel_j_zero(x, 20U); // 20th root.
  156. std::cout << "x = " << x << ", r = " << r << std::endl;
  157. std::vector<float_type> zeros;
  158. boost::math::cyl_bessel_j_zero(x, 1, 3, std::back_inserter(zeros));
  159. std::cout << "cyl_bessel_j_zeros" << std::endl;
  160. // Print the roots to the output stream.
  161. std::copy(zeros.begin(), zeros.end(),
  162. std::ostream_iterator<float_type>(std::cout, "\n"));
  163. /*`The Neumann function zeros are evaluated very similarly:
  164. */
  165. using boost::math::cyl_neumann_zero;
  166. double zn = cyl_neumann_zero(2., 1);
  167. std::cout << "cyl_neumann_zero(2., 1) = " << std::endl;
  168. //double zn0 = zn;
  169. // std::cout << "zn0 = " << std::endl;
  170. // std::cout << zn0 << std::endl;
  171. //
  172. std::cout << zn << std::endl;
  173. // std::cout << cyl_neumann_zero(2., 1) << std::endl;
  174. std::vector<float> nzeros(3); // Space for 3 zeros.
  175. cyl_neumann_zero<float>(2.F, 1, nzeros.size(), nzeros.begin());
  176. std::cout << "cyl_neumann_zero<float>(2.F, 1, " << std::endl;
  177. // Print the zeros to the output stream.
  178. std::copy(nzeros.begin(), nzeros.end(),
  179. std::ostream_iterator<float>(std::cout, "\n"));
  180. std::cout << cyl_neumann_zero(static_cast<float_type>(220)/100, 1) << std::endl;
  181. // 3.6154383428745996706772556069431792744372398748422
  182. /*`Finally we show how the output iterator can be used to compute a sum of zeros.
  183. (See [@https://doi.org/10.1017/S2040618500034067 Ian N. Sneddon, Infinite Sums of Bessel Zeros],
  184. page 150 equation 40).
  185. */
  186. //] [/bessel_zero_example_2]
  187. {
  188. //[bessel_zero_example_iterator_2]
  189. /*`The sum is calculated for many values, converging on the analytical exact value of `1/8`.
  190. */
  191. using boost::math::cyl_bessel_j_zero;
  192. double nu = 1.;
  193. double sum = 0;
  194. output_summation_iterator<double> it(&sum); // sum of 1/zeros^2
  195. cyl_bessel_j_zero(nu, 1, 10000, it);
  196. double s = 1/(4 * (nu + 1)); // 0.125 = 1/8 is exact analytical solution.
  197. std::cout << std::setprecision(6) << "nu = " << nu << ", sum = " << sum
  198. << ", exact = " << s << std::endl;
  199. // nu = 1.00000, sum = 0.124990, exact = 0.125000
  200. //] [/bessel_zero_example_iterator_2]
  201. }
  202. }
  203. catch (std::exception& ex)
  204. {
  205. std::cout << "Thrown exception " << ex.what() << std::endl;
  206. }
  207. //[bessel_zero_example_iterator_3]
  208. /*`Examples below show effect of 'bad' arguments that throw a `domain_error` exception.
  209. */
  210. try
  211. { // Try a negative rank m.
  212. std::cout << "boost::math::cyl_bessel_j_zero(-1.F, -1) " << std::endl;
  213. float dodgy_root = boost::math::cyl_bessel_j_zero(-1.F, -1);
  214. std::cout << "boost::math::cyl_bessel_j_zero(-1.F, -1) " << dodgy_root << std::endl;
  215. // Throw exception Error in function boost::math::cyl_bessel_j_zero<double>(double, int):
  216. // Order argument is -1, but must be >= 0 !
  217. }
  218. catch (std::exception& ex)
  219. {
  220. std::cout << "Throw exception " << ex.what() << std::endl;
  221. }
  222. /*`[note The type shown is the type [*after promotion],
  223. using __precision_policy and __promotion_policy, from `float` to `double` in this case.]
  224. In this example the promotion goes:
  225. # Arguments are `float` and `int`.
  226. # Treat `int` "as if" it were a `double`, so arguments are `float` and `double`.
  227. # Common type is `double` - so that's the precision we want (and the type that will be returned).
  228. # Evaluate internally as `long double` for full `double` precision.
  229. See full code for other examples that promote from `double` to `long double`.
  230. */
  231. //] [/bessel_zero_example_iterator_3]
  232. try
  233. { // order v = inf
  234. std::cout << "boost::math::cyl_bessel_j_zero(infF, 1) " << std::endl;
  235. float infF = std::numeric_limits<float>::infinity();
  236. float inf_root = boost::math::cyl_bessel_j_zero(infF, 1);
  237. std::cout << "boost::math::cyl_bessel_j_zero(infF, 1) " << inf_root << std::endl;
  238. // boost::math::cyl_bessel_j_zero(-1.F, -1)
  239. //Thrown exception Error in function boost::math::cyl_bessel_j_zero<double>(double, int):
  240. // Requested the -1'th zero, but the rank must be positive !
  241. }
  242. catch (std::exception& ex)
  243. {
  244. std::cout << "Thrown exception " << ex.what() << std::endl;
  245. }
  246. try
  247. { // order v = inf
  248. double inf = std::numeric_limits<double>::infinity();
  249. double inf_root = boost::math::cyl_bessel_j_zero(inf, 1);
  250. std::cout << "boost::math::cyl_bessel_j_zero(inf, 1) " << inf_root << std::endl;
  251. // Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, unsigned):
  252. // Order argument is 1.#INF, but must be finite >= 0 !
  253. }
  254. catch (std::exception& ex)
  255. {
  256. std::cout << "Thrown exception " << ex.what() << std::endl;
  257. }
  258. try
  259. { // order v = NaN
  260. double nan = std::numeric_limits<double>::quiet_NaN();
  261. double nan_root = boost::math::cyl_bessel_j_zero(nan, 1);
  262. std::cout << "boost::math::cyl_bessel_j_zero(nan, 1) " << nan_root << std::endl;
  263. // Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, unsigned):
  264. // Order argument is 1.#QNAN, but must be finite >= 0 !
  265. }
  266. catch (std::exception& ex)
  267. {
  268. std::cout << "Thrown exception " << ex.what() << std::endl;
  269. }
  270. try
  271. { // Try a negative m.
  272. double dodgy_root = boost::math::cyl_bessel_j_zero(0.0, -1);
  273. // warning C4146: unary minus operator applied to unsigned type, result still unsigned.
  274. std::cout << "boost::math::cyl_bessel_j_zero(0.0, -1) " << dodgy_root << std::endl;
  275. // boost::math::cyl_bessel_j_zero(0.0, -1) 6.74652e+009
  276. // This *should* fail because m is unreasonably large.
  277. }
  278. catch (std::exception& ex)
  279. {
  280. std::cout << "Thrown exception " << ex.what() << std::endl;
  281. }
  282. try
  283. { // m = inf
  284. double inf = std::numeric_limits<double>::infinity();
  285. double inf_root = boost::math::cyl_bessel_j_zero(0.0, inf);
  286. // warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data.
  287. std::cout << "boost::math::cyl_bessel_j_zero(0.0, inf) " << inf_root << std::endl;
  288. // Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int):
  289. // Requested the 0'th zero, but must be > 0 !
  290. }
  291. catch (std::exception& ex)
  292. {
  293. std::cout << "Thrown exception " << ex.what() << std::endl;
  294. }
  295. try
  296. { // m = NaN
  297. std::cout << "boost::math::cyl_bessel_j_zero(0.0, nan) " << std::endl ;
  298. double nan = std::numeric_limits<double>::quiet_NaN();
  299. double nan_root = boost::math::cyl_bessel_j_zero(0.0, nan);
  300. // warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data.
  301. std::cout << nan_root << std::endl;
  302. // Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int):
  303. // Requested the 0'th zero, but must be > 0 !
  304. }
  305. catch (std::exception& ex)
  306. {
  307. std::cout << "Thrown exception " << ex.what() << std::endl;
  308. }
  309. } // int main()
  310. /*
  311. Mathematica: Table[N[BesselJZero[71/19, n], 50], {n, 1, 20, 1}]
  312. 7.2731751938316489503185694262290765588963196701623
  313. 10.724858308883141732536172745851416647110749599085
  314. 14.018504599452388106120459558042660282427471931581
  315. 17.25249845917041718216248716654977734919590383861
  316. 20.456678874044517595180234083894285885460502077814
  317. 23.64363089714234522494551422714731959985405172504
  318. 26.819671140255087745421311470965019261522390519297
  319. 29.988343117423674742679141796661432043878868194142
  320. 33.151796897690520871250862469973445265444791966114
  321. 36.3114160002162074157243540350393860813165201842
  322. 39.468132467505236587945197808083337887765967032029
  323. 42.622597801391236474855034831297954018844433480227
  324. 45.775281464536847753390206207806726581495950012439
  325. 48.926530489173566198367766817478553992471739894799
  326. 52.076607045343002794279746041878924876873478063472
  327. 55.225712944912571393594224327817265689059002890192
  328. 58.374006101538886436775188150439025201735151418932
  329. 61.521611873000965273726742659353136266390944103571
  330. 64.66863105379093036834648221487366079456596628716
  331. 67.815145619696290925556791375555951165111460585458
  332. Mathematica: Table[N[BesselKZero[2, n], 50], {n, 1, 5, 1}]
  333. n |
  334. 1 | 3.3842417671495934727014260185379031127323883259329
  335. 2 | 6.7938075132682675382911671098369487124493222183854
  336. 3 | 10.023477979360037978505391792081418280789658279097
  337. */
  338. /*
  339. [bessel_zero_output]
  340. boost::math::cyl_bessel_j_zero(0.0, 1) 2.40483
  341. boost::math::cyl_bessel_j_zero(0.0, 1) 2.40482555769577
  342. boost::math::cyl_bessel_j_zero(-1.0, 1) 1.#QNAN
  343. boost::math::cyl_bessel_j_zero(inf, 1) 1.#QNAN
  344. boost::math::cyl_bessel_j_zero(nan, 1) 1.#QNAN
  345. 5.13562230184068
  346. 8.41724414039986
  347. 11.6198411721491
  348. 14.7959517823513
  349. 17.9598194949878
  350. x = 3.7368421052631578947368421052631578947368421052632, r = 7.2731751938316489503185694262290765588963196701623
  351. x = 3.7368421052631578947368421052631578947368421052632, r = 67.815145619696290925556791375555951165111460585458
  352. 7.2731751938316489503185694262290765588963196701623
  353. 10.724858308883141732536172745851416647110749599085
  354. 14.018504599452388106120459558042660282427471931581
  355. cyl_neumann_zero(2., 1) = 3.3842417671495935000000000000000000000000000000000
  356. 3.3842418193817139000000000000000000000000000000000
  357. 6.7938075065612793000000000000000000000000000000000
  358. 10.023477554321289000000000000000000000000000000000
  359. 3.6154383428745996706772556069431792744372398748422
  360. nu = 1.00000, sum = 0.124990, exact = 0.125000
  361. Throw exception Error in function boost::math::cyl_bessel_j_zero<double>(double, int): Order argument is -1, but must be >= 0 !
  362. Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int): Order argument is 1.#INF, but must be finite >= 0 !
  363. Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int): Order argument is 1.#QNAN, but must be finite >= 0 !
  364. Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int): Requested the -1'th zero, but must be > 0 !
  365. Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int): Requested the -2147483648'th zero, but must be > 0 !
  366. Throw exception Error in function boost::math::cyl_bessel_j_zero<long double>(long double, int): Requested the -2147483648'th zero, but must be > 0 !
  367. ] [/bessel_zero_output]
  368. */