|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once |
|
|
|
|
|
#include <thrust/detail/config.h> |
|
|
|
|
|
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
|
|
# pragma GCC system_header |
|
|
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
|
|
# pragma clang system_header |
|
|
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
|
|
# pragma system_header |
|
|
#endif |
|
|
|
|
|
#include <thrust/complex.h> |
|
|
#include <thrust/detail/complex/c99math.h> |
|
|
#include <cfloat> |
|
|
#include <cmath> |
|
|
|
|
|
THRUST_NAMESPACE_BEGIN |
|
|
|
|
|
|
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator+(const complex<T0>& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x.real() + y.real(), x.imag() + y.imag()); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator+(const complex<T0>& x, const T1& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x.real() + y, x.imag()); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator+(const T0& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x + y.real(), y.imag()); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator-(const complex<T0>& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x.real() - y.real(), x.imag() - y.imag()); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator-(const complex<T0>& x, const T1& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x.real() - y, x.imag()); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator-(const T0& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x - y.real(), -y.imag()); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator*(const complex<T0>& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>( x.real() * y.real() - x.imag() * y.imag() |
|
|
, x.real() * y.imag() + x.imag() * y.real()); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator*(const complex<T0>& x, const T1& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x.real() * y, x.imag() * y); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator*(const T0& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x * y.real(), x * y.imag()); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator/(const complex<T0>& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
|
|
|
|
|
|
using std::abs; |
|
|
|
|
|
T s = abs(y.real()) + abs(y.imag()); |
|
|
|
|
|
T oos = T(1.0) / s; |
|
|
|
|
|
T ars = x.real() * oos; |
|
|
T ais = x.imag() * oos; |
|
|
T brs = y.real() * oos; |
|
|
T bis = y.imag() * oos; |
|
|
|
|
|
s = (brs * brs) + (bis * bis); |
|
|
|
|
|
oos = T(1.0) / s; |
|
|
|
|
|
complex<T> quot( ((ars * brs) + (ais * bis)) * oos |
|
|
, ((ais * brs) - (ars * bis)) * oos); |
|
|
return quot; |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator/(const complex<T0>& x, const T1& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x.real() / y, x.imag() / y); |
|
|
} |
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
operator/(const T0& x, const complex<T1>& y) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
return complex<T>(x) / y; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
__host__ __device__ |
|
|
complex<T> operator+(const complex<T>& y) |
|
|
{ |
|
|
return y; |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
__host__ __device__ |
|
|
complex<T> operator-(const complex<T>& y) |
|
|
{ |
|
|
return y * -T(1); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
__host__ __device__ |
|
|
T abs(const complex<T>& z) |
|
|
{ |
|
|
return hypot(z.real(), z.imag()); |
|
|
} |
|
|
|
|
|
|
|
|
namespace detail { |
|
|
namespace complex { |
|
|
|
|
|
__host__ __device__ |
|
|
inline float abs(const thrust::complex<float>& z) |
|
|
{ |
|
|
return hypotf(z.real(),z.imag()); |
|
|
} |
|
|
|
|
|
__host__ __device__ |
|
|
inline double abs(const thrust::complex<double>& z) |
|
|
{ |
|
|
return hypot(z.real(),z.imag()); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
template <> |
|
|
__host__ __device__ |
|
|
inline float abs(const complex<float>& z) |
|
|
{ |
|
|
return detail::complex::abs(z); |
|
|
} |
|
|
|
|
|
template <> |
|
|
__host__ __device__ |
|
|
inline double abs(const complex<double>& z) |
|
|
{ |
|
|
return detail::complex::abs(z); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
__host__ __device__ |
|
|
T arg(const complex<T>& z) |
|
|
{ |
|
|
|
|
|
using std::atan2; |
|
|
return atan2(z.imag(), z.real()); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
__host__ __device__ |
|
|
complex<T> conj(const complex<T>& z) |
|
|
{ |
|
|
return complex<T>(z.real(), -z.imag()); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
__host__ __device__ |
|
|
T norm(const complex<T>& z) |
|
|
{ |
|
|
return z.real() * z.real() + z.imag() * z.imag(); |
|
|
} |
|
|
|
|
|
|
|
|
template <> |
|
|
__host__ __device__ |
|
|
inline float norm(const complex<float>& z) |
|
|
{ |
|
|
|
|
|
using std::abs; |
|
|
using std::sqrt; |
|
|
|
|
|
if (abs(z.real()) < sqrt(FLT_MIN) && abs(z.imag()) < sqrt(FLT_MIN)) |
|
|
{ |
|
|
float a = z.real() * 4.0f; |
|
|
float b = z.imag() * 4.0f; |
|
|
return (a * a + b * b) / 16.0f; |
|
|
} |
|
|
|
|
|
return z.real() * z.real() + z.imag() * z.imag(); |
|
|
} |
|
|
|
|
|
template <> |
|
|
__host__ __device__ |
|
|
inline double norm(const complex<double>& z) |
|
|
{ |
|
|
|
|
|
using std::abs; |
|
|
using std::sqrt; |
|
|
|
|
|
if (abs(z.real()) < sqrt(DBL_MIN) && abs(z.imag()) < sqrt(DBL_MIN)) |
|
|
{ |
|
|
double a = z.real() * 4.0; |
|
|
double b = z.imag() * 4.0; |
|
|
return (a * a + b * b) / 16.0; |
|
|
} |
|
|
|
|
|
return z.real() * z.real() + z.imag() * z.imag(); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T0, typename T1> |
|
|
__host__ __device__ |
|
|
complex<typename detail::promoted_numerical_type<T0, T1>::type> |
|
|
polar(const T0& m, const T1& theta) |
|
|
{ |
|
|
typedef typename detail::promoted_numerical_type<T0, T1>::type T; |
|
|
|
|
|
|
|
|
using std::cos; |
|
|
using std::sin; |
|
|
|
|
|
return complex<T>(m * cos(theta), m * sin(theta)); |
|
|
} |
|
|
|
|
|
THRUST_NAMESPACE_END |
|
|
|
|
|
|