acos.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_ACOS_INCLUDED
  5. #define BOOST_MATH_COMPLEX_ACOS_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")]] std::complex<T> acos(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 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; // these hold our result
  51. //
  52. // Handle special cases specified by the C99 standard,
  53. // many of these special cases aren't really needed here,
  54. // but doing it this way prevents overflow/underflow arithmetic
  55. // in the main body of the logic, which may trip up some machines:
  56. //
  57. if((boost::math::isinf)(x))
  58. {
  59. if((boost::math::isinf)(y))
  60. {
  61. real = quarter_pi;
  62. imag = std::numeric_limits<T>::infinity();
  63. }
  64. else if((boost::math::isnan)(y))
  65. {
  66. return std::complex<T>(y, -std::numeric_limits<T>::infinity());
  67. }
  68. else
  69. {
  70. // y is not infinity or nan:
  71. real = 0;
  72. imag = std::numeric_limits<T>::infinity();
  73. }
  74. }
  75. else if((boost::math::isnan)(x))
  76. {
  77. if((boost::math::isinf)(y))
  78. return std::complex<T>(x, ((boost::math::signbit)(z.imag())) ? std::numeric_limits<T>::infinity() : -std::numeric_limits<T>::infinity());
  79. return std::complex<T>(x, x);
  80. }
  81. else if((boost::math::isinf)(y))
  82. {
  83. real = half_pi;
  84. imag = std::numeric_limits<T>::infinity();
  85. }
  86. else if((boost::math::isnan)(y))
  87. {
  88. return std::complex<T>((x == 0) ? half_pi : y, y);
  89. }
  90. else
  91. {
  92. //
  93. // What follows is the regular Hull et al code,
  94. // begin with the special case for real numbers:
  95. //
  96. if((y == 0) && (x <= one))
  97. return std::complex<T>((x == 0) ? half_pi : std::acos(z.real()), (boost::math::changesign)(z.imag()));
  98. //
  99. // Figure out if our input is within the "safe area" identified by Hull et al.
  100. // This would be more efficient with portable floating point exception handling;
  101. // fortunately the quantities M and u identified by Hull et al (figure 3),
  102. // match with the max and min methods of numeric_limits<T>.
  103. //
  104. T safe_max = detail::safe_max(static_cast<T>(8));
  105. T safe_min = detail::safe_min(static_cast<T>(4));
  106. T xp1 = one + x;
  107. T xm1 = x - one;
  108. if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min))
  109. {
  110. T yy = y * y;
  111. T r = std::sqrt(xp1*xp1 + yy);
  112. T s = std::sqrt(xm1*xm1 + yy);
  113. T a = half * (r + s);
  114. T b = x / a;
  115. if(b <= b_crossover)
  116. {
  117. real = std::acos(b);
  118. }
  119. else
  120. {
  121. T apx = a + x;
  122. if(x <= one)
  123. {
  124. real = std::atan(std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1)))/x);
  125. }
  126. else
  127. {
  128. real = std::atan((y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1))))/x);
  129. }
  130. }
  131. if(a <= a_crossover)
  132. {
  133. T am1;
  134. if(x < one)
  135. {
  136. am1 = half * (yy/(r + xp1) + yy/(s - xm1));
  137. }
  138. else
  139. {
  140. am1 = half * (yy/(r + xp1) + (s + xm1));
  141. }
  142. imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one)));
  143. }
  144. else
  145. {
  146. imag = std::log(a + std::sqrt(a*a - one));
  147. }
  148. }
  149. else
  150. {
  151. //
  152. // This is the Hull et al exception handling code from Fig 6 of their paper:
  153. //
  154. if(y <= (std::numeric_limits<T>::epsilon() * std::fabs(xm1)))
  155. {
  156. if(x < one)
  157. {
  158. real = std::acos(x);
  159. imag = y / std::sqrt(xp1*(one-x));
  160. }
  161. else
  162. {
  163. // This deviates from Hull et al's paper as per https://svn.boost.org/trac/boost/ticket/7290
  164. if(((std::numeric_limits<T>::max)() / xp1) > xm1)
  165. {
  166. // xp1 * xm1 won't overflow:
  167. real = y / std::sqrt(xm1*xp1);
  168. imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1));
  169. }
  170. else
  171. {
  172. real = y / x;
  173. imag = log_two + std::log(x);
  174. }
  175. }
  176. }
  177. else if(y <= safe_min)
  178. {
  179. // There is an assumption in Hull et al's analysis that
  180. // if we get here then x == 1. This is true for all "good"
  181. // machines where :
  182. //
  183. // E^2 > 8*sqrt(u); with:
  184. //
  185. // E = std::numeric_limits<T>::epsilon()
  186. // u = (std::numeric_limits<T>::min)()
  187. //
  188. // Hull et al provide alternative code for "bad" machines
  189. // but we have no way to test that here, so for now just assert
  190. // on the assumption:
  191. //
  192. BOOST_MATH_ASSERT(x == 1);
  193. real = std::sqrt(y);
  194. imag = std::sqrt(y);
  195. }
  196. else if(std::numeric_limits<T>::epsilon() * y - one >= x)
  197. {
  198. real = half_pi;
  199. imag = log_two + std::log(y);
  200. }
  201. else if(x > one)
  202. {
  203. real = std::atan(y/x);
  204. T xoy = x/y;
  205. imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy);
  206. }
  207. else
  208. {
  209. real = half_pi;
  210. T a = std::sqrt(one + y*y);
  211. imag = half * boost::math::log1p(static_cast<T>(2)*y*(y+a));
  212. }
  213. }
  214. }
  215. //
  216. // Finish off by working out the sign of the result:
  217. //
  218. if((boost::math::signbit)(z.real()))
  219. real = s_pi - real;
  220. if(!(boost::math::signbit)(z.imag()))
  221. imag = (boost::math::changesign)(imag);
  222. return std::complex<T>(real, imag);
  223. #ifdef _MSC_VER
  224. #pragma warning(pop)
  225. #endif
  226. }
  227. } } // namespaces
  228. #endif // BOOST_MATH_COMPLEX_ACOS_INCLUDED