43 #ifndef KOKKOS_COMPLEX_HPP
44 #define KOKKOS_COMPLEX_HPP
59 template<
class RealType>
75 re_ (src.re_), im_ (src.im_)
83 template<
class InputRealType>
84 complex (
const std::complex<InputRealType>& src) :
85 re_ (std::
real (src)), im_ (std::
imag (src))
93 operator std::complex<RealType> ()
const {
94 return std::complex<RealType> (re_, im_);
99 template<
class InputRealType>
100 KOKKOS_INLINE_FUNCTION
complex (
const InputRealType& val) :
105 template<
class RealType1,
class RealType2>
106 KOKKOS_INLINE_FUNCTION
complex (
const RealType1& re,
const RealType2& im) :
111 template<
class InputRealType>
112 KOKKOS_INLINE_FUNCTION
120 template<
class InputRealType>
121 KOKKOS_INLINE_FUNCTION
129 template<
class InputRealType>
130 KOKKOS_INLINE_FUNCTION
133 im_ =
static_cast<RealType
> (0.0);
142 template<
class InputRealType>
144 re_ = std::real (src);
145 im_ = std::imag (src);
150 KOKKOS_INLINE_FUNCTION RealType
imag ()
const {
155 KOKKOS_INLINE_FUNCTION RealType
real ()
const {
160 KOKKOS_INLINE_FUNCTION RealType
imag ()
const volatile {
165 KOKKOS_INLINE_FUNCTION RealType
real ()
const volatile {
169 KOKKOS_INLINE_FUNCTION
176 KOKKOS_INLINE_FUNCTION
177 void operator += (
const volatile complex<RealType>& src)
volatile {
182 KOKKOS_INLINE_FUNCTION
183 complex<RealType>& operator += (
const RealType& src) {
188 KOKKOS_INLINE_FUNCTION
189 void operator += (
const volatile RealType& src)
volatile {
193 KOKKOS_INLINE_FUNCTION
void atomic_add (
const complex<RealType>& x)
volatile {
197 ::Kokkos::atomic_add (&re_, x.real ());
198 ::Kokkos::atomic_add (&im_, x.imag ());
201 KOKKOS_INLINE_FUNCTION
202 complex<RealType>& operator -= (
const complex<RealType>& src) {
208 KOKKOS_INLINE_FUNCTION
209 complex<RealType>& operator -= (
const RealType& src) {
214 KOKKOS_INLINE_FUNCTION
215 complex<RealType>& operator *= (
const complex<RealType>& src) {
216 const RealType realPart = re_ * src.re_ - im_ * src.im_;
217 const RealType imagPart = re_ * src.im_ + im_ * src.re_;
223 KOKKOS_INLINE_FUNCTION
224 void operator *= (
const volatile complex<RealType>& src)
volatile {
225 const RealType realPart = re_ * src.re_ - im_ * src.im_;
226 const RealType imagPart = re_ * src.im_ + im_ * src.re_;
231 KOKKOS_INLINE_FUNCTION
232 complex<RealType>& operator *= (
const RealType& src) {
238 KOKKOS_INLINE_FUNCTION
239 void operator *= (
const volatile RealType& src)
volatile {
244 KOKKOS_INLINE_FUNCTION
245 complex<RealType>& operator /= (
const complex<RealType>& y) {
249 const RealType s = ::fabs (
real (y)) + ::fabs (
imag (y));
259 const complex<RealType> x_scaled (this->re_ / s, this->im_ / s);
260 const complex<RealType> y_conj_scaled (y.re_ / s, -(y.im_) / s);
261 const RealType y_scaled_abs = y_conj_scaled.re_ * y_conj_scaled.re_ +
262 y_conj_scaled.im_ * y_conj_scaled.im_;
263 *
this = x_scaled * y_conj_scaled;
264 *
this /= y_scaled_abs;
269 KOKKOS_INLINE_FUNCTION
270 complex<RealType>& operator /= (
const RealType& src) {
278 template<
class RealType>
279 KOKKOS_INLINE_FUNCTION
286 template<
class RealType>
287 KOKKOS_INLINE_FUNCTION
294 template<
class RealType>
295 KOKKOS_INLINE_FUNCTION
302 template<
class RealType>
303 KOKKOS_INLINE_FUNCTION
310 template<
class RealType>
311 KOKKOS_INLINE_FUNCTION
319 template<
class RealType>
320 KOKKOS_INLINE_FUNCTION
326 template<
class RealType>
327 KOKKOS_INLINE_FUNCTION
333 template<
class RealType>
334 KOKKOS_INLINE_FUNCTION
341 template<
class RealType>
342 KOKKOS_INLINE_FUNCTION
349 template<
class RealType1,
class RealType2>
350 KOKKOS_INLINE_FUNCTION
357 template<
class RealType>
358 KOKKOS_INLINE_FUNCTION
364 const RealType s = ::fabs (
real (y)) + ::fabs (
imag (y));
375 const RealType y_scaled_abs =
real (y_conj_scaled) *
real (y_conj_scaled) +
376 imag (y_conj_scaled) *
imag (y_conj_scaled);
378 result /= y_scaled_abs;
384 template<
class RealType>
385 KOKKOS_INLINE_FUNCTION
391 template<
class RealType1,
class RealType2>
392 KOKKOS_INLINE_FUNCTION
394 return real (x) == y &&
imag (x) ==
static_cast<RealType1
> (0.0);
398 template<
class RealType>
399 KOKKOS_INLINE_FUNCTION
405 template<
class RealType>
406 KOKKOS_INLINE_FUNCTION
412 template<
class RealType1,
class RealType2>
413 KOKKOS_INLINE_FUNCTION
415 return real (x) != y ||
imag (x) !=
static_cast<RealType1
> (0.0);
419 template<
class RealType>
420 KOKKOS_INLINE_FUNCTION
425 template<
class RealType>
426 std::ostream& operator << (std::ostream& os, const complex<RealType>& x) {
432 template<
class RealType>
433 std::ostream& operator >> (std::ostream& os, complex<RealType>& x) {
434 std::complex<RealType> x_std;
440 KOKKOS_INLINE_FUNCTION
void
441 atomic_add (volatile ::Kokkos::complex<double>*
const dest,
442 const ::Kokkos::complex<double> src)
444 dest->atomic_add (src);
449 #endif // KOKKOS_COMPLEX_HPP
KOKKOS_INLINE_FUNCTION RealType real() const
The real part of this complex number.
KOKKOS_INLINE_FUNCTION complex(const RealType1 &re, const RealType2 &im)
Constructor that takes the real and imaginary parts.
KOKKOS_INLINE_FUNCTION complex< RealType > operator*(const complex< RealType > &x, const complex< RealType > &y)
Binary * operator for complex.
Partial reimplementation of std::complex that works as the result of a Kokkos::parallel_reduce.
KOKKOS_INLINE_FUNCTION complex(const InputRealType &val)
Constructor that takes just the real part, and sets the imaginary part to zero.
KOKKOS_INLINE_FUNCTION complex(const complex< RealType > &src)
Copy constructor.
KOKKOS_INLINE_FUNCTION complex()
Default constructor (initializes both real and imaginary parts to zero).
KOKKOS_INLINE_FUNCTION RealType abs(const complex< RealType > &x)
Absolute value (magnitude) of a complex number.
complex(const std::complex< InputRealType > &src)
Conversion constructor from std::complex.
KOKKOS_INLINE_FUNCTION complex< RealType > & operator=(const complex< InputRealType > &src)
Assignment operator.
KOKKOS_FORCEINLINE_FUNCTION bool operator==(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Equality operator for Kokkos::pair.
KOKKOS_INLINE_FUNCTION complex< RealType > operator+(const complex< RealType > &x, const complex< RealType > &y)
Binary + operator for complex.
KOKKOS_FORCEINLINE_FUNCTION bool operator!=(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Inequality operator for Kokkos::pair.
KOKKOS_INLINE_FUNCTION complex< RealType > operator-(const complex< RealType > &x, const complex< RealType > &y)
Binary - operator for complex.
RealType value_type
The type of the real or imaginary parts of this complex number.
KOKKOS_INLINE_FUNCTION RealType real(const complex< RealType > &x)
Real part of a complex number.
KOKKOS_INLINE_FUNCTION complex< RealType > conj(const complex< RealType > &x)
Conjugate of a complex number.
KOKKOS_INLINE_FUNCTION RealType real() const volatile
The real part of this complex number (volatile overload).
KOKKOS_INLINE_FUNCTION RealType imag() const
The imaginary part of this complex number.
KOKKOS_INLINE_FUNCTION RealType imag() const volatile
The imaginary part of this complex number (volatile overload).
KOKKOS_INLINE_FUNCTION complex< RealType1 > operator/(const complex< RealType1 > &x, const RealType2 &y)
Binary operator / for complex and real numbers.
KOKKOS_INLINE_FUNCTION RealType imag(const complex< RealType > &x)
Imaginary part of a complex number.