asin.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // (C) Copyright John Maddock 2005.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_MATH_COMPLEX_ASIN_INCLUDED
  5. #define BOOST_MATH_COMPLEX_ASIN_INCLUDED
  6. #ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED
  7. # include <boost/math/complex/details.hpp>
  8. #endif
  9. #ifndef BOOST_MATH_LOG1P_INCLUDED
  10. # include <boost/math/special_functions/log1p.hpp>
  11. #endif
  12. #include <boost/math/tools/assert.hpp>
  13. #ifdef BOOST_NO_STDC_NAMESPACE
  14. namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; }
  15. #endif
  16. namespace boost{ namespace math{
  17. template<class T>
  18. [[deprecated("Replaced by C++11")]] inline std::complex<T> asin(const std::complex<T>& z)
  19. {
  20. //
  21. // This implementation is a transcription of the pseudo-code in:
  22. //
  23. // "Implementing the complex Arcsine and Arccosine Functions using Exception Handling."
  24. // T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang.
  25. // ACM Transactions on Mathematical Software, Vol 23, No 3, Sept 1997.
  26. //
  27. //
  28. // These static constants should really be in a maths constants library,
  29. // note that we have tweaked the value of a_crossover as per https://svn.boost.org/trac/boost/ticket/7290:
  30. //
  31. static const T one = static_cast<T>(1);
  32. //static const T two = static_cast<T>(2);
  33. static const T half = static_cast<T>(0.5L);
  34. static const T a_crossover = static_cast<T>(10);
  35. static const T b_crossover = static_cast<T>(0.6417L);
  36. static const T s_pi = boost::math::constants::pi<T>();
  37. static const T half_pi = s_pi / 2;
  38. static const T log_two = boost::math::constants::ln_two<T>();
  39. static const T quarter_pi = s_pi / 4;
  40. #ifdef _MSC_VER
  41. #pragma warning(push)
  42. #pragma warning(disable:4127)
  43. #endif
  44. //
  45. // Get real and imaginary parts, discard the signs as we can
  46. // figure out the sign of the result later:
  47. //
  48. T x = std::fabs(z.real());
  49. T y = std::fabs(z.imag());
  50. T real, imag; // our results
  51. //
  52. // Begin by handling the special cases for infinities and nan's
  53. // specified in C99, most of this is handled by the regular logic
  54. // below, but handling it as a special case prevents overflow/underflow
  55. // arithmetic which may trip up some machines:
  56. //
  57. if((boost::math::isnan)(x))
  58. {
  59. if((boost::math::isnan)(y))
  60. return std::complex<T>(x, x);
  61. if((boost::math::isinf)(y))
  62. {
  63. real = x;
  64. imag = std::numeric_limits<T>::infinity();
  65. }
  66. else
  67. return std::complex<T>(x, x);
  68. }
  69. else if((boost::math::isnan)(y))
  70. {
  71. if(x == 0)
  72. {
  73. real = 0;
  74. imag = y;
  75. }
  76. else if((boost::math::isinf)(x))
  77. {
  78. real = y;
  79. imag = std::numeric_limits<T>::infinity();
  80. }
  81. else
  82. return std::complex<T>(y, y);
  83. }
  84. else if((boost::math::isinf)(x))
  85. {
  86. if((boost::math::isinf)(y))
  87. {
  88. real = quarter_pi;
  89. imag = std::numeric_limits<T>::infinity();
  90. }
  91. else
  92. {
  93. real = half_pi;
  94. imag = std::numeric_limits<T>::infinity();
  95. }
  96. }
  97. else if((boost::math::isinf)(y))
  98. {
  99. real = 0;
  100. imag = std::numeric_limits<T>::infinity();
  101. }
  102. else
  103. {
  104. //
  105. // special case for real numbers:
  106. //
  107. if((y == 0) && (x <= one))
  108. return std::complex<T>(std::asin(z.real()), z.imag());
  109. //
  110. // Figure out if our input is within the "safe area" identified by Hull et al.
  111. // This would be more efficient with portable floating point exception handling;
  112. // fortunately the quantities M and u identified by Hull et al (figure 3),
  113. // match with the max and min methods of numeric_limits<T>.
  114. //
  115. T safe_max = detail::safe_max(static_cast<T>(8));
  116. T safe_min = detail::safe_min(static_cast<T>(4));
  117. T xp1 = one + x;
  118. T xm1 = x - one;
  119. if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min))
  120. {
  121. T yy = y * y;
  122. T r = std::sqrt(xp1*xp1 + yy);
  123. T s = std::sqrt(xm1*xm1 + yy);
  124. T a = half * (r + s);
  125. T b = x / a;
  126. if(b <= b_crossover)
  127. {
  128. real = std::asin(b);
  129. }
  130. else
  131. {
  132. T apx = a + x;
  133. if(x <= one)
  134. {
  135. real = std::atan(x/std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1))));
  136. }
  137. else
  138. {
  139. real = std::atan(x/(y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1)))));
  140. }
  141. }
  142. if(a <= a_crossover)
  143. {
  144. T am1;
  145. if(x < one)
  146. {
  147. am1 = half * (yy/(r + xp1) + yy/(s - xm1));
  148. }
  149. else
  150. {
  151. am1 = half * (yy/(r + xp1) + (s + xm1));
  152. }
  153. imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one)));
  154. }
  155. else
  156. {
  157. imag = std::log(a + std::sqrt(a*a - one));
  158. }
  159. }
  160. else
  161. {
  162. //
  163. // This is the Hull et al exception handling code from Fig 3 of their paper:
  164. //
  165. if(y <= (std::numeric_limits<T>::epsilon() * std::fabs(xm1)))
  166. {
  167. if(x < one)
  168. {
  169. real = std::asin(x);
  170. imag = y / std::sqrt(-xp1*xm1);
  171. }
  172. else
  173. {
  174. real = half_pi;
  175. if(((std::numeric_limits<T>::max)() / xp1) > xm1)
  176. {
  177. // xp1 * xm1 won't overflow:
  178. imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1));
  179. }
  180. else
  181. {
  182. imag = log_two + std::log(x);
  183. }
  184. }
  185. }
  186. else if(y <= safe_min)
  187. {
  188. // There is an assumption in Hull et al's analysis that
  189. // if we get here then x == 1. This is true for all "good"
  190. // machines where :
  191. //
  192. // E^2 > 8*sqrt(u); with:
  193. //
  194. // E = std::numeric_limits<T>::epsilon()
  195. // u = (std::numeric_limits<T>::min)()
  196. //
  197. // Hull et al provide alternative code for "bad" machines
  198. // but we have no way to test that here, so for now just assert
  199. // on the assumption:
  200. //
  201. BOOST_MATH_ASSERT(x == 1);
  202. real = half_pi - std::sqrt(y);
  203. imag = std::sqrt(y);
  204. }
  205. else if(std::numeric_limits<T>::epsilon() * y - one >= x)
  206. {
  207. real = x/y; // This can underflow!
  208. imag = log_two + std::log(y);
  209. }
  210. else if(x > one)
  211. {
  212. real = std::atan(x/y);
  213. T xoy = x/y;
  214. imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy);
  215. }
  216. else
  217. {
  218. T a = std::sqrt(one + y*y);
  219. real = x/a; // This can underflow!
  220. imag = half * boost::math::log1p(static_cast<T>(2)*y*(y+a));
  221. }
  222. }
  223. }
  224. //
  225. // Finish off by working out the sign of the result:
  226. //
  227. if((boost::math::signbit)(z.real()))
  228. real = (boost::math::changesign)(real);
  229. if((boost::math::signbit)(z.imag()))
  230. imag = (boost::math::changesign)(imag);
  231. return std::complex<T>(real, imag);
  232. #ifdef _MSC_VER
  233. #pragma warning(pop)
  234. #endif
  235. }
  236. } } // namespaces
  237. #endif // BOOST_MATH_COMPLEX_ASIN_INCLUDED