16#ifndef KOKKOS_COMPLEX_HPP
17#define KOKKOS_COMPLEX_HPP
18#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
19#define KOKKOS_IMPL_PUBLIC_INCLUDE
20#define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_COMPLEX
24#include <Kokkos_MathematicalFunctions.hpp>
25#include <Kokkos_NumericTraits.hpp>
26#include <Kokkos_ReductionIdentity.hpp>
27#include <impl/Kokkos_Error.hpp>
42template <
class RealType>
44#ifdef KOKKOS_ENABLE_COMPLEX_ALIGN
45 alignas(2 *
sizeof(RealType))
48 static_assert(std::is_floating_point_v<RealType> &&
49 std::is_same_v<RealType, std::remove_cv_t<RealType>>,
50 "Kokkos::complex can only be instantiated for a cv-unqualified "
51 "floating point type");
62 KOKKOS_DEFAULTED_FUNCTION
66 KOKKOS_DEFAULTED_FUNCTION
69 KOKKOS_DEFAULTED_FUNCTION
73 template <
class RType,
74 std::enable_if_t<std::is_convertible_v<RType, RealType>,
int> = 0>
79 : re_(other.real()), im_(other.imag()) {}
86 KOKKOS_INLINE_FUNCTION
87 complex(
const std::complex<RealType>& src)
noexcept
94 : re_(
reinterpret_cast<const RealType (&)[2]
>(src)[0]),
95 im_(
reinterpret_cast<const RealType (&)[2]
>(src)[1]) {}
103 operator std::complex<RealType>() const noexcept {
104 return std::complex<RealType>(re_, im_);
109 KOKKOS_INLINE_FUNCTION
complex(
const RealType& val) noexcept
110 : re_(val), im_(
static_cast<RealType
>(0)) {}
113 KOKKOS_INLINE_FUNCTION
114 complex(
const RealType& re,
const RealType& im) noexcept : re_(re), im_(im) {}
134 KOKKOS_INLINE_FUNCTION
135 constexpr RealType&
imag() noexcept {
return im_; }
138 KOKKOS_INLINE_FUNCTION
139 constexpr RealType&
real() noexcept {
return re_; }
142 KOKKOS_INLINE_FUNCTION
143 constexpr RealType
imag() const noexcept {
return im_; }
146 KOKKOS_INLINE_FUNCTION
147 constexpr RealType
real() const noexcept {
return re_; }
150 KOKKOS_INLINE_FUNCTION
151 constexpr void imag(RealType v)
noexcept { im_ = v; }
154 KOKKOS_INLINE_FUNCTION
155 constexpr void real(RealType v)
noexcept { re_ = v; }
157 constexpr KOKKOS_INLINE_FUNCTION
complex& operator+=(
164 constexpr KOKKOS_INLINE_FUNCTION complex& operator+=(
165 const RealType& src)
noexcept {
170 constexpr KOKKOS_INLINE_FUNCTION
complex& operator-=(
177 constexpr KOKKOS_INLINE_FUNCTION
complex& operator-=(
178 const RealType& src)
noexcept {
183 constexpr KOKKOS_INLINE_FUNCTION
complex& operator*=(
185 const RealType realPart = re_ * src.re_ - im_ * src.im_;
186 const RealType imagPart = re_ * src.im_ + im_ * src.re_;
192 constexpr KOKKOS_INLINE_FUNCTION
complex& operator*=(
193 const RealType& src)
noexcept {
200 constexpr KOKKOS_INLINE_FUNCTION
complex& operator/=(
205 const RealType s = fabs(y.real()) + fabs(y.imag());
211 if (s == RealType(0)) {
215 const complex x_scaled(this->re_ / s, this->im_ / s);
216 const complex y_conj_scaled(y.re_ / s, -(y.im_) / s);
217 const RealType y_scaled_abs =
218 y_conj_scaled.re_ * y_conj_scaled.re_ +
219 y_conj_scaled.im_ * y_conj_scaled.im_;
220 *
this = x_scaled * y_conj_scaled;
221 *
this /= y_scaled_abs;
226 constexpr KOKKOS_INLINE_FUNCTION
complex& operator/=(
227 const std::complex<RealType>& y)
noexcept(
noexcept(RealType{} /
232 const RealType s = fabs(y.real()) + fabs(y.imag());
237 if (s == RealType(0)) {
241 const complex x_scaled(this->re_ / s, this->im_ / s);
242 const complex y_conj_scaled(y.re_ / s, -(y.im_) / s);
243 const RealType y_scaled_abs =
244 y_conj_scaled.re_ * y_conj_scaled.re_ +
245 y_conj_scaled.im_ * y_conj_scaled.im_;
246 *
this = x_scaled * y_conj_scaled;
247 *
this /= y_scaled_abs;
252 constexpr KOKKOS_INLINE_FUNCTION
complex& operator/=(
253 const RealType& src)
noexcept(
noexcept(RealType{} / RealType{})) {
259 template <
size_t I,
typename RT>
260 friend constexpr const RT& get(
const complex<RT>&)
noexcept;
262 template <
size_t I,
typename RT>
263 friend constexpr const RT&& get(
const complex<RT>&&) noexcept;
265#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
267 template <
class RType,
268 std::enable_if_t<std::is_convertible_v<RType, RealType>,
int> = 0>
269 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
274 : re_(src.re_), im_(src.im_) {}
296 template <
class Complex,
297 std::enable_if_t<std::is_same_v<Complex, complex>,
int> = 0>
298 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
void operator=(
299 const Complex& src)
volatile noexcept {
319 template <
class Complex,
320 std::enable_if_t<std::is_same_v<Complex, complex>,
int> = 0>
321 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
volatile complex& operator=(
322 const volatile Complex& src)
volatile noexcept {
341 template <
class Complex,
342 std::enable_if_t<std::is_same_v<Complex, complex>,
int> = 0>
343 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
complex& operator=(
344 const volatile Complex& src)
noexcept {
354 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
void operator=(
355 const volatile RealType& val)
noexcept {
363 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
complex& operator=(
364 const RealType& val)
volatile noexcept {
372 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
complex& operator=(
373 const volatile RealType& val)
volatile noexcept {
380 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
volatile RealType&
381 imag() volatile noexcept {
386 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
volatile RealType&
387 real() volatile noexcept {
392 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION RealType imag() const
398 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION RealType real() const
403 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
void operator+=(
409 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
void operator+=(
410 const volatile RealType& src)
volatile noexcept {
414 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
void operator*=(
416 const RealType realPart = re_ * src.re_ - im_ * src.im_;
417 const RealType imagPart = re_ * src.im_ + im_ * src.re_;
423 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
void operator*=(
424 const volatile RealType& src)
volatile noexcept {
436template <
typename RealType>
437struct std::tuple_size<Kokkos::complex<RealType>>
438 : std::integral_constant<size_t, 2> {};
440template <
size_t I,
typename RealType>
441struct std::tuple_element<I, Kokkos::complex<RealType>> {
442 static_assert(I < 2);
443 using type = RealType;
450template <
size_t I,
typename RealType>
452 static_assert(I < 2);
453 if constexpr (I == 0)
457#ifdef KOKKOS_COMPILER_INTEL
458 __builtin_unreachable();
462template <
size_t I,
typename RealType>
464 static_assert(I < 2);
465 if constexpr (I == 0)
466 return std::move(z.real());
468 return std::move(z.imag());
469#ifdef KOKKOS_COMPILER_INTEL
470 __builtin_unreachable();
474template <
size_t I,
typename RealType>
475KOKKOS_FUNCTION
constexpr const RealType& get(
477 static_assert(I < 2);
478 if constexpr (I == 0)
482#ifdef KOKKOS_COMPILER_INTEL
483 __builtin_unreachable();
487template <
size_t I,
typename RealType>
488KOKKOS_FUNCTION
constexpr const RealType&& get(
490 static_assert(I < 2);
491 if constexpr (I == 0)
492 return std::move(z.re_);
494 return std::move(z.im_);
495#ifdef KOKKOS_COMPILER_INTEL
496 __builtin_unreachable();
508template <
class RealType1,
class RealType2>
511 using common_type = std::common_type_t<RealType1, RealType2>;
512 return common_type(x.real()) == common_type(y.real()) &&
513 common_type(x.imag()) == common_type(y.imag());
519template <
class RealType1,
class RealType2>
520inline bool operator==(std::complex<RealType1>
const& x,
522 using common_type = std::common_type_t<RealType1, RealType2>;
523 return common_type(x.real()) == common_type(y.real()) &&
524 common_type(x.imag()) == common_type(y.imag());
528template <
class RealType1,
class RealType2>
530 std::complex<RealType2>
const& y)
noexcept {
531 using common_type = std::common_type_t<RealType1, RealType2>;
532 return common_type(x.real()) == common_type(y.real()) &&
533 common_type(x.imag()) == common_type(y.imag());
538 class RealType1,
class RealType2,
540 std::enable_if_t<std::is_convertible_v<RealType2, RealType1>,
int> = 0>
542 RealType2
const& y)
noexcept {
543 using common_type = std::common_type_t<RealType1, RealType2>;
544 return common_type(x.real()) == common_type(y) &&
545 common_type(x.imag()) == common_type(0);
550 class RealType1,
class RealType2,
552 std::enable_if_t<std::is_convertible_v<RealType1, RealType2>,
int> = 0>
553KOKKOS_INLINE_FUNCTION
bool operator==(RealType1
const& x,
555 using common_type = std::common_type_t<RealType1, RealType2>;
556 return common_type(x) == common_type(y.real()) &&
557 common_type(0) == common_type(y.imag());
561template <
class RealType1,
class RealType2>
564 using common_type = std::common_type_t<RealType1, RealType2>;
565 return common_type(x.real()) != common_type(y.real()) ||
566 common_type(x.imag()) != common_type(y.imag());
570template <
class RealType1,
class RealType2>
571inline bool operator!=(std::complex<RealType1>
const& x,
573 using common_type = std::common_type_t<RealType1, RealType2>;
574 return common_type(x.real()) != common_type(y.real()) ||
575 common_type(x.imag()) != common_type(y.imag());
579template <
class RealType1,
class RealType2>
581 std::complex<RealType2>
const& y)
noexcept {
582 using common_type = std::common_type_t<RealType1, RealType2>;
583 return common_type(x.real()) != common_type(y.real()) ||
584 common_type(x.imag()) != common_type(y.imag());
589 class RealType1,
class RealType2,
591 std::enable_if_t<std::is_convertible_v<RealType2, RealType1>,
int> = 0>
593 RealType2
const& y)
noexcept {
594 using common_type = std::common_type_t<RealType1, RealType2>;
595 return common_type(x.real()) != common_type(y) ||
596 common_type(x.imag()) != common_type(0);
601 class RealType1,
class RealType2,
603 std::enable_if_t<std::is_convertible_v<RealType1, RealType2>,
int> = 0>
604KOKKOS_INLINE_FUNCTION
bool operator!=(RealType1
const& x,
606 using common_type = std::common_type_t<RealType1, RealType2>;
607 return common_type(x) != common_type(y.real()) ||
608 common_type(0) != common_type(y.imag());
615template <
class RealType1,
class RealType2>
619 x.imag() + y.imag());
623template <
class RealType1,
class RealType2>
631template <
class RealType1,
class RealType2>
639template <
class RealType>
646template <
class RealType1,
class RealType2>
650 x.imag() - y.imag());
654template <
class RealType1,
class RealType2>
662template <
class RealType1,
class RealType2>
670template <
class RealType>
677template <
class RealType1,
class RealType2>
681 x.real() * y.real() - x.imag() * y.imag(),
682 x.real() * y.imag() + x.imag() * y.real());
693template <
class RealType1,
class RealType2>
697 x.real() * y.real() - x.imag() * y.imag(),
698 x.real() * y.imag() + x.imag() * y.real());
705template <
class RealType1,
class RealType2>
716template <
class RealType1,
class RealType2>
724template <
class RealType>
729template <
class ArithmeticType>
730KOKKOS_INLINE_FUNCTION
constexpr Impl::promote_t<ArithmeticType> imag(
732 return ArithmeticType();
736template <
class RealType>
741template <
class ArithmeticType>
742KOKKOS_INLINE_FUNCTION
constexpr Impl::promote_t<ArithmeticType> real(
749KOKKOS_INLINE_FUNCTION
complex<T> polar(
const T& r,
const T& theta = T()) {
750 KOKKOS_EXPECTS(r >= 0);
751 return complex<T>(r * cos(theta), r * sin(theta));
755template <
class RealType>
757 return hypot(x.real(), x.imag());
764 T theta = atan2(x.imag(), x.real());
765 return polar(pow(r, y), y * theta);
776 return x == T() ? T() : exp(y * log(x));
779template <
class T,
class U,
class = std::enable_if_t<std::is_arithmetic_v<T>>>
782 using type = Impl::promote_2_t<T, U>;
786template <
class T,
class U,
class = std::enable_if_t<std::is_arithmetic_v<U>>>
789 using type = Impl::promote_2_t<T, U>;
793template <
class T,
class U>
796 using type = Impl::promote_2_t<T, U>;
802template <
class RealType>
803KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> sqrt(
805 RealType r = x.real();
806 RealType i = x.imag();
808 if (r == RealType()) {
809 RealType t = sqrt(fabs(i) / 2);
810 return Kokkos::complex<RealType>(t, i < RealType() ? -t : t);
812 RealType t = sqrt(2 * (abs(x) + fabs(r)));
814 return r > RealType() ? Kokkos::complex<RealType>(u, i / t)
815 : Kokkos::
complex<RealType>(fabs(i) / t,
816 i < RealType() ? -u : u);
821template <
class RealType>
827template <
class ArithmeticType>
830 using type = Impl::promote_t<ArithmeticType>;
835template <
class RealType>
841template <
class RealType>
842KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> log(
844 RealType phi = atan2(x.imag(), x.real());
845 return Kokkos::complex<RealType>(log(abs(x)), phi);
849template <
class RealType>
850KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> log10(
852 return log(x) / log(RealType(10));
856template <
class RealType>
857KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> sin(
859 return Kokkos::complex<RealType>(sin(x.real()) * cosh(x.imag()),
860 cos(x.real()) * sinh(x.imag()));
864template <
class RealType>
865KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> cos(
867 return Kokkos::complex<RealType>(cos(x.real()) * cosh(x.imag()),
868 -sin(x.real()) * sinh(x.imag()));
872template <
class RealType>
873KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> tan(
875 return sin(x) / cos(x);
879template <
class RealType>
880KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> sinh(
882 return Kokkos::complex<RealType>(sinh(x.real()) * cos(x.imag()),
883 cosh(x.real()) * sin(x.imag()));
887template <
class RealType>
888KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> cosh(
890 return Kokkos::complex<RealType>(cosh(x.real()) * cos(x.imag()),
891 sinh(x.real()) * sin(x.imag()));
895template <
class RealType>
896KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> tanh(
898 return sinh(x) / cosh(x);
902template <
class RealType>
903KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> asinh(
905 return log(x + sqrt(x * x + RealType(1.0)));
909template <
class RealType>
910KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> acosh(
912 return RealType(2.0) * log(sqrt(RealType(0.5) * (x + RealType(1.0))) +
913 sqrt(RealType(0.5) * (x - RealType(1.0))));
917template <
class RealType>
918KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> atanh(
920 const RealType i2 = x.imag() * x.imag();
921 const RealType r = RealType(1.0) - i2 - x.real() * x.real();
923 RealType p = RealType(1.0) + x.real();
924 RealType m = RealType(1.0) - x.real();
929 RealType phi = atan2(RealType(2.0) * x.imag(), r);
930 return Kokkos::complex<RealType>(RealType(0.25) * (log(p) - log(m)),
931 RealType(0.5) * phi);
935template <
class RealType>
936KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> asin(
938 Kokkos::complex<RealType> t =
939 asinh(Kokkos::complex<RealType>(-x.imag(), x.real()));
940 return Kokkos::complex<RealType>(t.
imag(), -t.
real());
944template <
class RealType>
945KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> acos(
947 Kokkos::complex<RealType> t = asin(x);
948 RealType pi_2 = acos(RealType(0.0));
949 return Kokkos::complex<RealType>(pi_2 - t.
real(), -t.
imag());
953template <
class RealType>
954KOKKOS_INLINE_FUNCTION Kokkos::complex<RealType> atan(
956 const RealType r2 = x.real() * x.real();
957 const RealType i = RealType(1.0) - r2 - x.imag() * x.imag();
959 RealType p = x.imag() + RealType(1.0);
960 RealType m = x.imag() - RealType(1.0);
965 return Kokkos::complex<RealType>(
966 RealType(0.5) * atan2(RealType(2.0) * x.real(), i),
967 RealType(0.25) * log(p / m));
973template <
class RealType>
976 std::exp(c.real()) * std::sin(c.imag()));
980template <
class RealType1,
class RealType2>
983 const RealType2& y)
noexcept(
noexcept(RealType1{} / RealType2{})) {
989template <
class RealType1,
class RealType2>
997 using common_real_type = std::common_type_t<RealType1, RealType2>;
998 const common_real_type s = fabs(real(y)) + fabs(imag(y));
1008 const RealType1 y_scaled_abs =
1009 real(y_conj_scaled) * real(y_conj_scaled) +
1010 imag(y_conj_scaled) * imag(y_conj_scaled);
1012 result /= y_scaled_abs;
1018template <
class RealType1,
class RealType2>
1020operator/(
const RealType1& x,
1026template <
class RealType>
1028 const std::complex<RealType> x_std(Kokkos::real(x), Kokkos::imag(x));
1033template <
class RealType>
1035 std::complex<RealType> x_std;
1042struct reduction_identity<Kokkos::
complex<T>> {
1043 using t_red_ident = reduction_identity<T>;
1044 KOKKOS_FORCEINLINE_FUNCTION
constexpr static Kokkos::complex<T>
1046 return Kokkos::complex<T>(t_red_ident::sum(), t_red_ident::sum());
1048 KOKKOS_FORCEINLINE_FUNCTION
constexpr static Kokkos::complex<T>
1050 return Kokkos::complex<T>(t_red_ident::prod(), t_red_ident::sum());
1056#ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_COMPLEX
1057#undef KOKKOS_IMPL_PUBLIC_INCLUDE
1058#undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_COMPLEX
Partial reimplementation of std::complex that works as the result of a Kokkos::parallel_reduce.
KOKKOS_INLINE_FUNCTION constexpr RealType imag() const noexcept
The imaginary part of this complex number.
RealType value_type
The type of the real or imaginary parts of this complex number.
KOKKOS_INLINE_FUNCTION constexpr RealType & real() noexcept
The real part of this complex number.
complex & operator=(const std::complex< RealType > &src) noexcept
Assignment operator from std::complex.
KOKKOS_DEFAULTED_FUNCTION complex(const complex &) noexcept=default
Copy constructor.
KOKKOS_INLINE_FUNCTION constexpr void real(RealType v) noexcept
Set the real part of this complex number.
KOKKOS_INLINE_FUNCTION complex(const RealType &re, const RealType &im) noexcept
Constructor that takes the real and imaginary parts.
KOKKOS_INLINE_FUNCTION constexpr RealType & imag() noexcept
The imaginary part of this complex number.
KOKKOS_INLINE_FUNCTION constexpr RealType real() const noexcept
The real part of this complex number.
KOKKOS_INLINE_FUNCTION complex & operator=(const RealType &val) noexcept
Assignment operator (from a real number).
KOKKOS_INLINE_FUNCTION constexpr void imag(RealType v) noexcept
Set the imaginary part of this complex number.
KOKKOS_DEFAULTED_FUNCTION complex()=default
Default constructor (initializes both real and imaginary parts to zero).
KOKKOS_INLINE_FUNCTION complex(const RealType &val) noexcept
Constructor that takes just the real part, and sets the imaginary part to zero.
KOKKOS_INLINE_FUNCTION complex(const std::complex< RealType > &src) noexcept
Conversion constructor from std::complex.
KOKKOS_INLINE_FUNCTION complex(const complex< RType > &other) noexcept
Conversion constructor from compatible RType.