43 #ifndef KOKKOS_COMPLEX_HPP 44 #define KOKKOS_COMPLEX_HPP 59 template<
class RealType>
75 re_ (src.re_), im_ (src.im_)
80 re_ (src.re_), im_ (src.im_)
88 template<
class InputRealType>
89 complex (
const std::complex<InputRealType>& src) :
90 re_ (std::
real (src)), im_ (std::
imag (src))
98 operator std::complex<RealType> ()
const {
99 return std::complex<RealType> (re_, im_);
104 template<
class InputRealType>
105 KOKKOS_INLINE_FUNCTION
complex (
const InputRealType& val) :
110 template<
class RealType1,
class RealType2>
111 KOKKOS_INLINE_FUNCTION
complex (
const RealType1& re,
const RealType2& im) :
116 template<
class InputRealType>
117 KOKKOS_INLINE_FUNCTION
133 template<
class InputRealType>
134 KOKKOS_INLINE_FUNCTION
143 template<
class InputRealType>
144 KOKKOS_INLINE_FUNCTION
152 template<
class InputRealType>
153 KOKKOS_INLINE_FUNCTION
161 template<
class InputRealType>
162 KOKKOS_INLINE_FUNCTION
165 im_ =
static_cast<RealType
> (0.0);
170 template<
class InputRealType>
171 KOKKOS_INLINE_FUNCTION
174 im_ =
static_cast<RealType
> (0.0);
182 template<
class InputRealType>
184 re_ = std::real (src);
185 im_ = std::imag (src);
190 KOKKOS_INLINE_FUNCTION RealType&
imag () {
195 KOKKOS_INLINE_FUNCTION RealType&
real () {
200 KOKKOS_INLINE_FUNCTION
const RealType
imag ()
const {
205 KOKKOS_INLINE_FUNCTION
const RealType
real ()
const {
210 KOKKOS_INLINE_FUNCTION
volatile RealType&
imag ()
volatile {
215 KOKKOS_INLINE_FUNCTION
volatile RealType&
real ()
volatile {
220 KOKKOS_INLINE_FUNCTION
const RealType
imag ()
const volatile {
225 KOKKOS_INLINE_FUNCTION
const RealType
real ()
const volatile {
229 KOKKOS_INLINE_FUNCTION
236 KOKKOS_INLINE_FUNCTION
237 void operator += (
const volatile complex<RealType>& src)
volatile {
242 KOKKOS_INLINE_FUNCTION
243 complex<RealType>& operator += (
const RealType& src) {
248 KOKKOS_INLINE_FUNCTION
249 void operator += (
const volatile RealType& src)
volatile {
253 KOKKOS_INLINE_FUNCTION
254 complex<RealType>& operator -= (
const complex<RealType>& src) {
260 KOKKOS_INLINE_FUNCTION
261 complex<RealType>& operator -= (
const RealType& src) {
266 KOKKOS_INLINE_FUNCTION
267 complex<RealType>& operator *= (
const complex<RealType>& src) {
268 const RealType realPart = re_ * src.re_ - im_ * src.im_;
269 const RealType imagPart = re_ * src.im_ + im_ * src.re_;
275 KOKKOS_INLINE_FUNCTION
276 void operator *= (
const volatile complex<RealType>& src)
volatile {
277 const RealType realPart = re_ * src.re_ - im_ * src.im_;
278 const RealType imagPart = re_ * src.im_ + im_ * src.re_;
283 KOKKOS_INLINE_FUNCTION
284 complex<RealType>& operator *= (
const RealType& src) {
290 KOKKOS_INLINE_FUNCTION
291 void operator *= (
const volatile RealType& src)
volatile {
296 KOKKOS_INLINE_FUNCTION
297 complex<RealType>& operator /= (
const complex<RealType>& y) {
301 const RealType s = ::fabs (y.real ()) + ::fabs (y.imag ());
311 const complex<RealType> x_scaled (this->re_ / s, this->im_ / s);
312 const complex<RealType> y_conj_scaled (y.re_ / s, -(y.im_) / s);
313 const RealType y_scaled_abs = y_conj_scaled.re_ * y_conj_scaled.re_ +
314 y_conj_scaled.im_ * y_conj_scaled.im_;
315 *
this = x_scaled * y_conj_scaled;
316 *
this /= y_scaled_abs;
321 KOKKOS_INLINE_FUNCTION
322 complex<RealType>& operator /= (
const RealType& src) {
330 template<
class RealType>
331 KOKKOS_INLINE_FUNCTION
338 template<
class RealType>
339 KOKKOS_INLINE_FUNCTION
346 template<
class RealType>
347 KOKKOS_INLINE_FUNCTION
354 template<
class RealType>
355 KOKKOS_INLINE_FUNCTION
362 template<
class RealType>
363 KOKKOS_INLINE_FUNCTION
380 template<
class RealType>
384 x.real () * y.
imag () + x.imag () * y.
real ());
391 template<
class RealType>
392 KOKKOS_INLINE_FUNCTION
400 template<
class RealType>
401 KOKKOS_INLINE_FUNCTION
407 template<
class RealType>
408 KOKKOS_INLINE_FUNCTION
414 template<
class RealType>
415 KOKKOS_INLINE_FUNCTION
422 template<
class RealType>
423 KOKKOS_INLINE_FUNCTION
430 template<
class RealType1,
class RealType2>
431 KOKKOS_INLINE_FUNCTION
438 template<
class RealType>
439 KOKKOS_INLINE_FUNCTION
445 const RealType s = ::fabs (
real (y)) + ::fabs (
imag (y));
456 const RealType y_scaled_abs =
real (y_conj_scaled) *
real (y_conj_scaled) +
457 imag (y_conj_scaled) *
imag (y_conj_scaled);
459 result /= y_scaled_abs;
465 template<
class RealType>
466 KOKKOS_INLINE_FUNCTION
472 template<
class RealType>
473 KOKKOS_INLINE_FUNCTION
475 return std::real (x) ==
real (y) && std::imag (x) ==
imag (y);
479 template<
class RealType1,
class RealType2>
480 KOKKOS_INLINE_FUNCTION
482 return real (x) == y &&
imag (x) ==
static_cast<RealType1
> (0.0);
486 template<
class RealType>
487 KOKKOS_INLINE_FUNCTION
493 template<
class RealType>
494 KOKKOS_INLINE_FUNCTION
500 template<
class RealType>
501 KOKKOS_INLINE_FUNCTION
503 return std::real (x) !=
real (y) || std::imag (x) !=
imag (y);
507 template<
class RealType1,
class RealType2>
508 KOKKOS_INLINE_FUNCTION
510 return real (x) != y ||
imag (x) !=
static_cast<RealType1
> (0.0);
514 template<
class RealType>
515 KOKKOS_INLINE_FUNCTION
520 template<
class RealType>
521 std::ostream& operator << (std::ostream& os, const complex<RealType>& x) {
527 template<
class RealType>
528 std::ostream& operator >> (std::ostream& os, complex<RealType>& x) {
529 std::complex<RealType> x_std;
538 #endif // KOKKOS_COMPLEX_HPP KOKKOS_INLINE_FUNCTION const RealType imag() const
The imaginary part of this complex number.
KOKKOS_INLINE_FUNCTION const RealType real() const
The real part of this complex number.
KOKKOS_INLINE_FUNCTION RealType & imag()
The imaginary 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 bool operator!=(const complex< RealType > &x, const complex< RealType > &y)
Inequality operator for two complex numbers.
KOKKOS_INLINE_FUNCTION complex(const volatile complex< RealType > &src)
Copy constructor from volatile.
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.
KOKKOS_INLINE_FUNCTION RealType & real()
The real part of this complex number.
complex(const std::complex< InputRealType > &src)
Conversion constructor from std::complex.
KOKKOS_INLINE_FUNCTION volatile RealType & imag() volatile
The imaginary part of this complex number (volatile overload).
KOKKOS_INLINE_FUNCTION complex< RealType > & operator=(const complex< InputRealType > &src)
Assignment operator.
KOKKOS_INLINE_FUNCTION complex< RealType > operator+(const complex< RealType > &x, const complex< RealType > &y)
Binary + operator for complex.
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 volatile RealType & real() volatile
The real part of this complex number (volatile overload).
KOKKOS_INLINE_FUNCTION const RealType imag() const volatile
The imaginary part of this complex number (volatile overload).
KOKKOS_INLINE_FUNCTION const RealType real() const volatile
The real part of this complex number (volatile overload).
KOKKOS_INLINE_FUNCTION complex< RealType > conj(const complex< RealType > &x)
Conjugate of a complex number.
KOKKOS_INLINE_FUNCTION bool operator==(const complex< RealType > &x, const complex< RealType > &y)
Equality operator for two complex numbers.
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.