complex.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // (C) Copyright John Maddock 2005.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_TR1_COMPLEX_HPP_INCLUDED
  6. # define BOOST_TR1_COMPLEX_HPP_INCLUDED
  7. # include <boost/tr1/detail/config.hpp>
  8. # include <complex>
  9. #ifndef BOOST_HAS_TR1_COMPLEX_INVERSE_TRIG
  10. #include <boost/math/complex.hpp>
  11. namespace std {
  12. namespace tr1 {
  13. using boost::math::acos;
  14. using boost::math::asin;
  15. using boost::math::atan;
  16. using boost::math::acosh;
  17. using boost::math::asinh;
  18. using boost::math::atanh;
  19. using boost::math::fabs;
  20. } }
  21. #else
  22. # if defined(BOOST_HAS_INCLUDE_NEXT) && !defined(BOOST_TR1_DISABLE_INCLUDE_NEXT)
  23. # include_next BOOST_TR1_HEADER(complex)
  24. # else
  25. # include <boost/tr1/detail/config_all.hpp>
  26. # include BOOST_TR1_STD_HEADER(BOOST_TR1_PATH(complex))
  27. # endif
  28. #endif
  29. #ifndef BOOST_HAS_TR1_COMPLEX_OVERLOADS
  30. #include <boost/tr1/detail/math_overloads.hpp>
  31. #include <boost/assert.hpp>
  32. #include <boost/detail/workaround.hpp>
  33. #include <boost/config/no_tr1/cmath.hpp>
  34. namespace std{
  35. #ifdef BOOST_NO_STDC_NAMESPACE
  36. using :: atan2;
  37. #endif
  38. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  39. template <class T>
  40. inline BOOST_TR1_MATH_RETURN(double) arg(const T& t)
  41. {
  42. return ::std::atan2(0.0, static_cast<double>(t));
  43. }
  44. #else
  45. inline double arg(const double& t)
  46. {
  47. return ::std::atan2(0.0, t);
  48. }
  49. #endif
  50. inline long double arg(const long double& t)
  51. {
  52. return ::std::atan2(0.0L, static_cast<long double>(t));
  53. }
  54. inline float arg(const float& t)
  55. {
  56. return ::std::atan2(0.0F, static_cast<float>(t));
  57. }
  58. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  59. template <class T>
  60. inline BOOST_TR1_MATH_RETURN(double) norm(const T& t)
  61. {
  62. double r = static_cast<double>(t);
  63. return r*r;
  64. }
  65. #else
  66. inline double norm(const double& t)
  67. {
  68. return t*t;
  69. }
  70. #endif
  71. inline long double norm(const long double& t)
  72. {
  73. long double l = t;
  74. return l*l;
  75. }
  76. inline float norm(const float& t)
  77. {
  78. float f = t;
  79. return f*f;
  80. }
  81. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  82. template <class T>
  83. inline BOOST_TR1_MATH_RETURN(std::complex<double>) conj(const T& t)
  84. {
  85. return ::std::conj(std::complex<double>(static_cast<double>(t)));
  86. }
  87. #else
  88. inline std::complex<double> conj(const double& t)
  89. {
  90. return ::std::conj(std::complex<double>(t));
  91. }
  92. #endif
  93. inline std::complex<long double> conj(const long double& t)
  94. {
  95. return ::std::conj(std::complex<long double>(t));
  96. }
  97. inline std::complex<float> conj(const float& t)
  98. {
  99. std::complex<float> ct(t);
  100. ct = ::std::conj(ct);
  101. return ct;
  102. }
  103. #if !BOOST_WORKAROUND(__BORLANDC__, <=0x570)
  104. inline complex<double> polar(const char& rho, const char& theta = 0)
  105. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  106. inline complex<double> polar(const unsigned char& rho, const unsigned char& theta = 0)
  107. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  108. inline complex<double> polar(const signed char& rho, const signed char& theta = 0)
  109. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  110. inline complex<double> polar(const short& rho, const short& theta = 0)
  111. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  112. inline complex<double> polar(const unsigned short& rho, const unsigned short& theta = 0)
  113. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  114. inline complex<double> polar(const int& rho, const int& theta = 0)
  115. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  116. inline complex<double> polar(const unsigned int& rho, const unsigned int& theta = 0)
  117. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  118. inline complex<double> polar(const long& rho, const long& theta = 0)
  119. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  120. inline complex<double> polar(const unsigned long& rho, const unsigned long& theta = 0)
  121. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  122. #ifdef BOOST_HAS_LONG_LONG
  123. inline complex<double> polar(const long long& rho, const long long& theta = 0)
  124. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  125. inline complex<double> polar(const unsigned long long& rho, const unsigned long long& theta = 0)
  126. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  127. #elif defined(BOOST_HAS_MS_INT64)
  128. inline complex<double> polar(const __int64& rho, const __int64& theta = 0)
  129. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  130. inline complex<double> polar(const unsigned __int64& rho, const unsigned __int64& theta = 0)
  131. { return ::std::polar(static_cast<double>(rho), static_cast<double>(theta)); }
  132. #endif
  133. template<class T, class U>
  134. inline complex<typename boost::tr1_detail::promote_to_real<T, U>::type>
  135. polar(const T& rho, const U& theta)
  136. {
  137. typedef typename boost::tr1_detail::promote_to_real<T, U>::type real_type;
  138. return std::polar(static_cast<real_type>(rho), static_cast<real_type>(theta));
  139. }
  140. #endif
  141. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  142. template <class T>
  143. inline BOOST_TR1_MATH_RETURN(double) imag(const T& )
  144. {
  145. return 0;
  146. }
  147. #else
  148. inline double imag(const double& )
  149. {
  150. return 0;
  151. }
  152. #endif
  153. inline long double imag(const long double& )
  154. {
  155. return 0;
  156. }
  157. inline float imag(const float& )
  158. {
  159. return 0;
  160. }
  161. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  162. template <class T>
  163. inline BOOST_TR1_MATH_RETURN(double) real(const T& t)
  164. {
  165. return static_cast<double>(t);
  166. }
  167. #else
  168. inline double real(const double& t)
  169. {
  170. return t;
  171. }
  172. #endif
  173. inline long double real(const long double& t)
  174. {
  175. return t;
  176. }
  177. inline float real(const float& t)
  178. {
  179. return t;
  180. }
  181. template<class T, class U>
  182. inline complex<typename boost::tr1_detail::largest_real<T, U>::type>
  183. pow(const complex<T>& x, const complex<U>& y)
  184. {
  185. typedef complex<typename boost::tr1_detail::largest_real<T, U>::type> result_type;
  186. typedef typename boost::mpl::if_<boost::is_same<result_type, complex<T> >, result_type const&, result_type>::type cast1_type;
  187. typedef typename boost::mpl::if_<boost::is_same<result_type, complex<U> >, result_type const&, result_type>::type cast2_type;
  188. cast1_type x1(x);
  189. cast2_type y1(y);
  190. return std::pow(x1, y1);
  191. }
  192. template<class T, class U>
  193. inline complex<typename boost::tr1_detail::promote_to_real<T, U>::type>
  194. pow (const complex<T>& x, const U& y)
  195. {
  196. typedef typename boost::tr1_detail::promote_to_real<T, U>::type real_type;
  197. typedef complex<typename boost::tr1_detail::promote_to_real<T, U>::type> result_type;
  198. typedef typename boost::mpl::if_<boost::is_same<result_type, complex<T> >, result_type const&, result_type>::type cast1_type;
  199. real_type r = y;
  200. cast1_type x1(x);
  201. std::complex<real_type> y1(r);
  202. return std::pow(x1, y1);
  203. }
  204. template<class T, class U>
  205. inline complex<typename boost::tr1_detail::promote_to_real<T, U>::type>
  206. pow (const T& x, const complex<U>& y)
  207. {
  208. typedef typename boost::tr1_detail::promote_to_real<T, U>::type real_type;
  209. typedef complex<typename boost::tr1_detail::promote_to_real<T, U>::type> result_type;
  210. typedef typename boost::mpl::if_<boost::is_same<result_type, complex<U> >, result_type const&, result_type>::type cast_type;
  211. real_type r = x;
  212. std::complex<real_type> x1(r);
  213. cast_type y1(y);
  214. return std::pow(x1, y1);
  215. }
  216. }
  217. #endif
  218. #endif