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