bessel_zeros_example_1.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_zeros_example_1
  22. /*`This example demonstrates calculating zeros of the Bessel and Neumann 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. */
  41. //] [/bessel_zeros_example_1]
  42. /*The signature of the single value function is:
  43. template <class T>
  44. inline typename detail::bessel_traits<T, T, policies::policy<> >::result_type
  45. cyl_bessel_j_zero(
  46. T v, // Floating-point value for Jv.
  47. int m); // start index.
  48. The result type is controlled by the floating-point type of parameter `v`
  49. (but subject to the usual __precision_policy and __promotion_policy).
  50. The signature of multiple zeros function is:
  51. template <class T, class OutputIterator>
  52. inline OutputIterator cyl_bessel_j_zero(
  53. T v, // Floating-point value for Jv.
  54. int start_index, // 1-based start index.
  55. unsigned number_of_zeros, // How many zeros to generate
  56. OutputIterator out_it); // Destination for zeros.
  57. There is also a version which allows control of the __policy_section for error handling and precision.
  58. template <class T, class OutputIterator, class Policy>
  59. inline OutputIterator cyl_bessel_j_zero(
  60. T v, // Floating-point value for Jv.
  61. int start_index, // 1-based start index.
  62. unsigned number_of_zeros, // How many zeros to generate
  63. OutputIterator out_it, // Destination for zeros.
  64. const Policy& pol); // Policy to use.
  65. */
  66. int main()
  67. {
  68. try
  69. {
  70. //[bessel_zeros_example_2
  71. /*`[tip It is always wise to place code using Boost.Math inside try'n'catch blocks;
  72. this will ensure that helpful error messages are shown when exceptional conditions arise.]
  73. First, evaluate a single Bessel zero.
  74. The precision is controlled by the float-point type of template parameter `T` of `v`
  75. so this example has `double` precision, at least 15 but up to 17 decimal digits (for the common 64-bit double).
  76. */
  77. // double root = boost::math::cyl_bessel_j_zero(0.0, 1);
  78. // // Displaying with default precision of 6 decimal digits:
  79. // std::cout << "boost::math::cyl_bessel_j_zero(0.0, 1) " << root << std::endl; // 2.40483
  80. // // And with all the guaranteed (15) digits:
  81. // std::cout.precision(std::numeric_limits<double>::digits10);
  82. // std::cout << "boost::math::cyl_bessel_j_zero(0.0, 1) " << root << std::endl; // 2.40482555769577
  83. /*`But note that because the parameter `v` controls the precision of the result,
  84. `v` [*must be a floating-point type].
  85. So if you provide an integer type, say 0, rather than 0.0, then it will fail to compile thus:
  86. ``
  87. root = boost::math::cyl_bessel_j_zero(0, 1);
  88. ``
  89. with this error message
  90. ``
  91. error C2338: Order must be a floating-point type.
  92. ``
  93. Optionally, we can use a policy to ignore errors, C-style, returning some value,
  94. perhaps infinity or NaN, or the best that can be done. (See __user_error_handling).
  95. To create a (possibly unwise!) policy `ignore_all_policy` that ignores all errors:
  96. */
  97. typedef boost::math::policies::policy<
  98. boost::math::policies::domain_error<boost::math::policies::ignore_error>,
  99. boost::math::policies::overflow_error<boost::math::policies::ignore_error>,
  100. boost::math::policies::underflow_error<boost::math::policies::ignore_error>,
  101. boost::math::policies::denorm_error<boost::math::policies::ignore_error>,
  102. boost::math::policies::pole_error<boost::math::policies::ignore_error>,
  103. boost::math::policies::evaluation_error<boost::math::policies::ignore_error>
  104. > ignore_all_policy;
  105. //`Examples of use of this `ignore_all_policy` are
  106. double inf = std::numeric_limits<double>::infinity();
  107. double nan = std::numeric_limits<double>::quiet_NaN();
  108. double dodgy_root = boost::math::cyl_bessel_j_zero(-1.0, 1, ignore_all_policy());
  109. std::cout << "boost::math::cyl_bessel_j_zero(-1.0, 1) " << dodgy_root << std::endl; // 1.#QNAN
  110. double inf_root = boost::math::cyl_bessel_j_zero(inf, 1, ignore_all_policy());
  111. std::cout << "boost::math::cyl_bessel_j_zero(inf, 1) " << inf_root << std::endl; // 1.#QNAN
  112. double nan_root = boost::math::cyl_bessel_j_zero(nan, 1, ignore_all_policy());
  113. std::cout << "boost::math::cyl_bessel_j_zero(nan, 1) " << nan_root << std::endl; // 1.#QNAN
  114. /*`Another version of `cyl_bessel_j_zero` allows calculation of multiple zeros with one call,
  115. placing the results in a container, often `std::vector`.
  116. For example, generate and display the first five `double` roots of J[sub v] for integral order 2,
  117. as column ['J[sub 2](x)] in table 1 of
  118. [@ http://mathworld.wolfram.com/BesselFunctionZeros.html Wolfram Bessel Function Zeros].
  119. */
  120. unsigned int n_roots = 5U;
  121. std::vector<double> roots;
  122. boost::math::cyl_bessel_j_zero(2.0, 1, n_roots, std::back_inserter(roots));
  123. std::copy(roots.begin(),
  124. roots.end(),
  125. std::ostream_iterator<double>(std::cout, "\n"));
  126. /*`Or we can use Boost.Multiprecision to generate 50 decimal digit roots of ['J[sub v]]
  127. for non-integral order `v= 71/19 == 3.736842`, expressed as an exact-integer fraction
  128. to generate the most accurate value possible for all floating-point types.
  129. We set the precision of the output stream, and show trailing zeros to display a fixed 50 decimal digits.
  130. */
  131. std::cout.precision(std::numeric_limits<float_type>::digits10); // 50 decimal digits.
  132. std::cout << std::showpoint << std::endl; // Show trailing zeros.
  133. float_type x = float_type(71) / 19;
  134. float_type r = boost::math::cyl_bessel_j_zero(x, 1); // 1st root.
  135. std::cout << "x = " << x << ", r = " << r << std::endl;
  136. r = boost::math::cyl_bessel_j_zero(x, 20U); // 20th root.
  137. std::cout << "x = " << x << ", r = " << r << std::endl;
  138. std::vector<float_type> zeros;
  139. boost::math::cyl_bessel_j_zero(x, 1, 3, std::back_inserter(zeros));
  140. std::cout << "cyl_bessel_j_zeros" << std::endl;
  141. // Print the roots to the output stream.
  142. std::copy(zeros.begin(), zeros.end(),
  143. std::ostream_iterator<float_type>(std::cout, "\n"));
  144. //] [/bessel_zeros_example_2]
  145. }
  146. catch (std::exception const& ex)
  147. {
  148. std::cout << "Thrown exception " << ex.what() << std::endl;
  149. }
  150. } // int main()
  151. /*
  152. Output:
  153. Description: Autorun "J:\Cpp\big_number\Debug\bessel_zeros_example_1.exe"
  154. boost::math::cyl_bessel_j_zero(-1.0, 1) 3.83171
  155. boost::math::cyl_bessel_j_zero(inf, 1) 1.#QNAN
  156. boost::math::cyl_bessel_j_zero(nan, 1) 1.#QNAN
  157. 5.13562
  158. 8.41724
  159. 11.6198
  160. 14.796
  161. 17.9598
  162. x = 3.7368421052631578947368421052631578947368421052632, r = 7.2731751938316489503185694262290765588963196701623
  163. x = 3.7368421052631578947368421052631578947368421052632, r = 67.815145619696290925556791375555951165111460585458
  164. cyl_bessel_j_zeros
  165. 7.2731751938316489503185694262290765588963196701623
  166. 10.724858308883141732536172745851416647110749599085
  167. 14.018504599452388106120459558042660282427471931581
  168. */