complex.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <utility>
  11. #include <boost/math/tools/is_detected.hpp>
  12. namespace boost {
  13. namespace math {
  14. namespace tools {
  15. namespace detail {
  16. template <typename T, typename = void>
  17. struct is_complex_type_impl
  18. {
  19. static constexpr bool value = false;
  20. };
  21. template <typename T>
  22. struct is_complex_type_impl<T, void_t<decltype(std::declval<T>().real()),
  23. decltype(std::declval<T>().imag())>>
  24. {
  25. static constexpr bool value = true;
  26. };
  27. } // Namespace detail
  28. template <typename T>
  29. struct is_complex_type : public detail::is_complex_type_impl<T> {};
  30. //
  31. // Use this trait to typecast integer literals to something
  32. // that will interoperate with T:
  33. //
  34. template <class T, bool = is_complex_type<T>::value>
  35. struct integer_scalar_type
  36. {
  37. typedef int type;
  38. };
  39. template <class T>
  40. struct integer_scalar_type<T, true>
  41. {
  42. typedef typename T::value_type type;
  43. };
  44. template <class T, bool = is_complex_type<T>::value>
  45. struct unsigned_scalar_type
  46. {
  47. typedef unsigned type;
  48. };
  49. template <class T>
  50. struct unsigned_scalar_type<T, true>
  51. {
  52. typedef typename T::value_type type;
  53. };
  54. template <class T, bool = is_complex_type<T>::value>
  55. struct scalar_type
  56. {
  57. typedef T type;
  58. };
  59. template <class T>
  60. struct scalar_type<T, true>
  61. {
  62. typedef typename T::value_type type;
  63. };
  64. } } }
  65. #endif // BOOST_MATH_TOOLS_COMPLEX_HPP