airy_zeros_example.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. //[airy_zeros_example_1
  22. /*`This example demonstrates calculating zeros of the 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/airy.hpp>
  33. /*`This example shows obtaining both a single zero of the Airy functions,
  34. and then placing multiple zeros into a container like `std::vector` by providing an iterator.
  35. The signature of the single-value Airy Ai function is:
  36. template <class T>
  37. T airy_ai_zero(unsigned m); // 1-based index of the zero.
  38. The signature of multiple zeros Airy Ai function is:
  39. template <class T, class OutputIterator>
  40. OutputIterator airy_ai_zero(
  41. unsigned start_index, // 1-based index of the zero.
  42. unsigned number_of_zeros, // How many zeros to generate.
  43. OutputIterator out_it); // Destination for zeros.
  44. There are also versions which allows control of the __policy_section for error handling and precision.
  45. template <class T, class OutputIterator, class Policy>
  46. OutputIterator airy_ai_zero(
  47. unsigned start_index, // 1-based index of the zero.
  48. unsigned number_of_zeros, // How many zeros to generate.
  49. OutputIterator out_it, // Destination for zeros.
  50. const Policy& pol); // Policy to use.
  51. */
  52. //] [/airy_zeros_example_1]
  53. int main()
  54. {
  55. try
  56. {
  57. //[airy_zeros_example_2
  58. /*`[tip It is always wise to place code using Boost.Math inside `try'n'catch` blocks;
  59. this will ensure that helpful error messages are shown when exceptional conditions arise.]
  60. First, evaluate a single Airy zero.
  61. The precision is controlled by the template parameter `T`,
  62. so this example has `double` precision, at least 15 but up to 17 decimal digits
  63. (for the common 64-bit double).
  64. */
  65. double aiz1 = boost::math::airy_ai_zero<double>(1);
  66. std::cout << "boost::math::airy_ai_zero<double>(1) = " << aiz1 << std::endl;
  67. double aiz2 = boost::math::airy_ai_zero<double>(2);
  68. std::cout << "boost::math::airy_ai_zero<double>(2) = " << aiz2 << std::endl;
  69. double biz3 = boost::math::airy_bi_zero<double>(3);
  70. std::cout << "boost::math::airy_bi_zero<double>(3) = " << biz3 << std::endl;
  71. /*`Other versions of `airy_ai_zero` and `airy_bi_zero`
  72. allow calculation of multiple zeros with one call,
  73. placing the results in a container, often `std::vector`.
  74. For example, generate and display the first five `double` roots
  75. [@http://mathworld.wolfram.com/AiryFunctionZeros.html Wolfram Airy Functions Zeros].
  76. */
  77. unsigned int n_roots = 5U;
  78. std::vector<double> roots;
  79. boost::math::airy_ai_zero<double>(1U, n_roots, std::back_inserter(roots));
  80. std::cout << "airy_ai_zeros:" << std::endl;
  81. std::copy(roots.begin(),
  82. roots.end(),
  83. std::ostream_iterator<double>(std::cout, "\n"));
  84. /*`The first few real roots of Ai(x) are approximately -2.33811, -4.08795, -5.52056, -6.7867144, -7.94413, -9.02265 ...
  85. Or we can use Boost.Multiprecision to generate 50 decimal digit roots.
  86. We set the precision of the output stream, and show trailing zeros to display a fixed 50 decimal digits.
  87. */
  88. std::cout.precision(std::numeric_limits<float_type>::digits10); // float_type has 50 decimal digits.
  89. std::cout << std::showpoint << std::endl; // Show trailing zeros too.
  90. unsigned int m = 1U;
  91. float_type r = boost::math::airy_ai_zero<float_type>(1U); // 1st root.
  92. std::cout << "boost::math::airy_bi_zero<float_type>(" << m << ") = " << r << std::endl;
  93. m = 2;
  94. r = boost::math::airy_ai_zero<float_type>(2U); // 2nd root.
  95. std::cout << "boost::math::airy_bi_zero<float_type>(" << m << ") = " << r << std::endl;
  96. m = 7U;
  97. r = boost::math::airy_bi_zero<float_type>(7U); // 7th root.
  98. std::cout << "boost::math::airy_bi_zero<float_type>(" << m << ") = " << r << std::endl;
  99. std::vector<float_type> zeros;
  100. boost::math::airy_ai_zero<float_type>(1U, 3, std::back_inserter(zeros));
  101. std::cout << "airy_ai_zeros:" << std::endl;
  102. // Print the roots to the output stream.
  103. std::copy(zeros.begin(), zeros.end(),
  104. std::ostream_iterator<float_type>(std::cout, "\n"));
  105. //] [/airy_zeros_example_2]
  106. }
  107. catch (std::exception ex)
  108. {
  109. std::cout << "Thrown exception " << ex.what() << std::endl;
  110. }
  111. } // int main()
  112. /*
  113. Output:
  114. Description: Autorun "J:\Cpp\big_number\Debug\airy_zeros_example.exe"
  115. boost::math::airy_ai_zero<double>(1) = -2.33811
  116. boost::math::airy_ai_zero<double>(2) = -4.08795
  117. boost::math::airy_bi_zero<double>(3) = -4.83074
  118. airy_ai_zeros:
  119. -2.33811
  120. -4.08795
  121. -5.52056
  122. -6.78671
  123. -7.94413
  124. boost::math::airy_bi_zero<float_type>(1) = -2.3381074104597670384891972524467354406385401456711
  125. boost::math::airy_bi_zero<float_type>(2) = -4.0879494441309706166369887014573910602247646991085
  126. boost::math::airy_bi_zero<float_type>(7) = -9.5381943793462388866329885451560196208390720763825
  127. airy_ai_zeros:
  128. -2.3381074104597670384891972524467354406385401456711
  129. -4.0879494441309706166369887014573910602247646991085
  130. -5.5205598280955510591298555129312935737972142806175
  131. */