hypergeometric_asym.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2014 Anton Bikineev
  3. // Copyright 2014 Christopher Kormanyos
  4. // Copyright 2014 John Maddock
  5. // Copyright 2014 Paul Bristow
  6. // Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_MATH_HYPERGEOMETRIC_ASYM_HPP
  11. #define BOOST_MATH_HYPERGEOMETRIC_ASYM_HPP
  12. #include <boost/math/special_functions/gamma.hpp>
  13. #include <boost/math/special_functions/hypergeometric_2F0.hpp>
  14. #ifdef _MSC_VER
  15. #pragma warning(push)
  16. #pragma warning(disable:4127)
  17. #endif
  18. namespace boost { namespace math {
  19. namespace detail {
  20. //
  21. // Asymptotic series based on https://dlmf.nist.gov/13.7#E1
  22. //
  23. // Note that a and b must not be negative integers, in addition
  24. // we require z > 0 and so apply Kummer's relation for z < 0.
  25. //
  26. template <class T, class Policy>
  27. inline T hypergeometric_1F1_asym_large_z_series(T a, const T& b, T z, const Policy& pol, long long& log_scaling)
  28. {
  29. BOOST_MATH_STD_USING
  30. static const char* function = "boost::math::hypergeometric_1F1_asym_large_z_series<%1%>(%1%, %1%, %1%)";
  31. T prefix;
  32. long long e;
  33. int s;
  34. if (z < 0)
  35. {
  36. a = b - a;
  37. z = -z;
  38. prefix = 1;
  39. }
  40. else
  41. {
  42. e = z > static_cast<T>((std::numeric_limits<long long>::max)()) ? (std::numeric_limits<long long>::max)() : lltrunc(z, pol);
  43. log_scaling += e;
  44. prefix = exp(z - e);
  45. }
  46. if ((fabs(a) < 10) && (fabs(b) < 10))
  47. {
  48. prefix *= pow(z, a) * pow(z, -b) * boost::math::tgamma(b, pol) / boost::math::tgamma(a, pol);
  49. }
  50. else
  51. {
  52. T t = log(z) * (a - b);
  53. e = lltrunc(t, pol);
  54. log_scaling += e;
  55. prefix *= exp(t - e);
  56. t = boost::math::lgamma(b, &s, pol);
  57. e = lltrunc(t, pol);
  58. log_scaling += e;
  59. prefix *= s * exp(t - e);
  60. t = boost::math::lgamma(a, &s, pol);
  61. e = lltrunc(t, pol);
  62. log_scaling -= e;
  63. prefix /= s * exp(t - e);
  64. }
  65. //
  66. // Checked 2F0:
  67. //
  68. unsigned k = 0;
  69. T a1_poch(1 - a);
  70. T a2_poch(b - a);
  71. T z_mult(1 / z);
  72. T sum = 0;
  73. T abs_sum = 0;
  74. T term = 1;
  75. T last_term = 0;
  76. do
  77. {
  78. sum += term;
  79. last_term = term;
  80. abs_sum += fabs(sum);
  81. term *= a1_poch * a2_poch * z_mult;
  82. term /= ++k;
  83. a1_poch += 1;
  84. a2_poch += 1;
  85. if (fabs(sum) * boost::math::policies::get_epsilon<T, Policy>() > fabs(term))
  86. break;
  87. if(fabs(sum) / abs_sum < boost::math::policies::get_epsilon<T, Policy>())
  88. return boost::math::policies::raise_evaluation_error<T>(function, "Large-z asymptotic approximation to 1F1 has destroyed all the digits in the result due to cancellation. Current best guess is %1%",
  89. prefix * sum, Policy());
  90. if(k > boost::math::policies::get_max_series_iterations<Policy>())
  91. return boost::math::policies::raise_evaluation_error<T>(function, "1F1: Unable to locate solution in a reasonable time:"
  92. " large-z asymptotic approximation. Current best guess is %1%", prefix * sum, Policy());
  93. if((k > 10) && (fabs(term) > fabs(last_term)))
  94. return boost::math::policies::raise_evaluation_error<T>(function, "Large-z asymptotic approximation to 1F1 is divergent. Current best guess is %1%", prefix * sum, Policy());
  95. } while (true);
  96. return prefix * sum;
  97. }
  98. // experimental range
  99. template <class T, class Policy>
  100. inline bool hypergeometric_1F1_asym_region(const T& a, const T& b, const T& z, const Policy&)
  101. {
  102. BOOST_MATH_STD_USING
  103. int half_digits = policies::digits<T, Policy>() / 2;
  104. bool in_region = false;
  105. if (fabs(a) < 0.001f)
  106. return false; // Haven't been able to make this work, why not? TODO!
  107. //
  108. // We use the following heuristic, if after we have had half_digits terms
  109. // of the 2F0 series, we require terms to be decreasing in size by a factor
  110. // of at least 0.7. Assuming the earlier terms were converging much faster
  111. // than this, then this should be enough to achieve convergence before the
  112. // series shoots off to infinity.
  113. //
  114. if (z > 0)
  115. {
  116. T one_minus_a = 1 - a;
  117. T b_minus_a = b - a;
  118. if (fabs((one_minus_a + half_digits) * (b_minus_a + half_digits) / (half_digits * z)) < 0.7)
  119. {
  120. in_region = true;
  121. //
  122. // double check that we are not divergent at the start if a,b < 0:
  123. //
  124. if ((one_minus_a < 0) || (b_minus_a < 0))
  125. {
  126. if (fabs(one_minus_a * b_minus_a / z) > 0.5)
  127. in_region = false;
  128. }
  129. }
  130. }
  131. else if (fabs((1 - (b - a) + half_digits) * (a + half_digits) / (half_digits * z)) < 0.7)
  132. {
  133. if ((floor(b - a) == (b - a)) && (b - a < 0))
  134. return false; // Can't have a negative integer b-a.
  135. in_region = true;
  136. //
  137. // double check that we are not divergent at the start if a,b < 0:
  138. //
  139. T a1 = 1 - (b - a);
  140. if ((a1 < 0) || (a < 0))
  141. {
  142. if (fabs(a1 * a / z) > 0.5)
  143. in_region = false;
  144. }
  145. }
  146. //
  147. // Check for a and b negative integers as these aren't supported by the approximation:
  148. //
  149. if (in_region)
  150. {
  151. if ((a < 0) && (floor(a) == a))
  152. in_region = false;
  153. if ((b < 0) && (floor(b) == b))
  154. in_region = false;
  155. if (fabs(z) < 40)
  156. in_region = false;
  157. }
  158. return in_region;
  159. }
  160. } } } // namespaces
  161. #ifdef _MSC_VER
  162. #pragma warning(pop)
  163. #endif
  164. #endif // BOOST_MATH_HYPERGEOMETRIC_ASYM_HPP