complex.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright John Maddock 2018.
  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. //
  6. // Tools for operator on complex as well as scalar types.
  7. //
  8. #ifndef BOOST_MATH_TOOLS_COMPLEX_HPP
  9. #define BOOST_MATH_TOOLS_COMPLEX_HPP
  10. #include <boost/math/tools/config.hpp>
  11. #include <boost/math/tools/is_detected.hpp>
  12. #ifdef BOOST_MATH_ENABLE_CUDA
  13. #include <cuda/std/utility>
  14. #include <cuda/std/complex>
  15. namespace boost {
  16. namespace math {
  17. template <typename T>
  18. using complex = cuda::std::complex<T>;
  19. } // namespace math
  20. } // namespace boost
  21. #else
  22. #include <utility>
  23. #include <complex>
  24. namespace boost {
  25. namespace math {
  26. template <typename T>
  27. using complex = std::complex<T>;
  28. } // namespace math
  29. } // namespace boost
  30. #endif
  31. namespace boost {
  32. namespace math {
  33. namespace tools {
  34. namespace detail {
  35. template <typename T, typename = void>
  36. struct is_complex_type_impl
  37. {
  38. static constexpr bool value = false;
  39. };
  40. #ifndef BOOST_MATH_ENABLE_CUDA
  41. template <typename T>
  42. struct is_complex_type_impl<T, void_t<decltype(std::declval<T>().real()),
  43. decltype(std::declval<T>().imag())>>
  44. {
  45. static constexpr bool value = true;
  46. };
  47. #else
  48. template <typename T>
  49. struct is_complex_type_impl<T, void_t<decltype(cuda::std::declval<T>().real()),
  50. decltype(cuda::std::declval<T>().imag())>>
  51. {
  52. static constexpr bool value = true;
  53. };
  54. #endif
  55. } // Namespace detail
  56. template <typename T>
  57. struct is_complex_type : public detail::is_complex_type_impl<T> {};
  58. //
  59. // Use this trait to typecast integer literals to something
  60. // that will interoperate with T:
  61. //
  62. template <class T, bool = is_complex_type<T>::value>
  63. struct integer_scalar_type
  64. {
  65. typedef int type;
  66. };
  67. template <class T>
  68. struct integer_scalar_type<T, true>
  69. {
  70. typedef typename T::value_type type;
  71. };
  72. template <class T, bool = is_complex_type<T>::value>
  73. struct unsigned_scalar_type
  74. {
  75. typedef unsigned type;
  76. };
  77. template <class T>
  78. struct unsigned_scalar_type<T, true>
  79. {
  80. typedef typename T::value_type type;
  81. };
  82. template <class T, bool = is_complex_type<T>::value>
  83. struct scalar_type
  84. {
  85. typedef T type;
  86. };
  87. template <class T>
  88. struct scalar_type<T, true>
  89. {
  90. typedef typename T::value_type type;
  91. };
  92. } } }
  93. #endif // BOOST_MATH_TOOLS_COMPLEX_HPP