| 1 |
// { dg-do run } |
| 2 |
// { dg-additional-options "-std=c++20" } |
| 3 |
|
| 4 |
#include <cmath> |
| 5 |
#include <complex> |
| 6 |
#include <numbers> |
| 7 |
|
| 8 |
using namespace std::complex_literals; |
| 9 |
|
| 10 |
#define FP_EQUAL(x,y) (std::abs ((x) - (y)) < 1E-6) |
| 11 |
#define COMPLEX_EQUAL(x,y) (FP_EQUAL ((x).real (), (y).real ()) \ |
| 12 |
&& FP_EQUAL ((x).imag (), (y).imag ())) |
| 13 |
|
| 14 |
#pragma omp declare target |
| 15 |
template<typename T> bool test_complex () |
| 16 |
{ |
| 17 |
std::complex<T> z (-1.334, 5.763); |
| 18 |
|
| 19 |
if (!FP_EQUAL (z.real (), (T) -1.334)) |
| 20 |
return false; |
| 21 |
if (!FP_EQUAL (z.imag (), (T) 5.763)) |
| 22 |
return false; |
| 23 |
if (!FP_EQUAL (std::abs (z), |
| 24 |
std::sqrt (z.real () * z.real () + z.imag () * z.imag ()))) |
| 25 |
return false; |
| 26 |
if (!FP_EQUAL (std::arg (z), std::atan2 (z.imag (), z.real ()))) |
| 27 |
return false; |
| 28 |
if (!FP_EQUAL (std::norm (z), z.real () * z.real () + z.imag () * z.imag ())) |
| 29 |
return false; |
| 30 |
|
| 31 |
auto conj = std::conj (z); |
| 32 |
if (!FP_EQUAL (conj.real (), z.real ()) |
| 33 |
|| !FP_EQUAL (conj.imag (), -z.imag ())) |
| 34 |
return false; |
| 35 |
|
| 36 |
if (std::proj (z) != z) |
| 37 |
return false; |
| 38 |
|
| 39 |
auto infz1 = std::proj (std::complex<float> (INFINITY, -1)); |
| 40 |
if (infz1.real () != INFINITY || infz1.imag () != (T) -0.0) |
| 41 |
return false; |
| 42 |
auto infz2 = std::proj (std::complex<float> (0, -INFINITY)); |
| 43 |
if (infz2.real () != INFINITY || infz2.imag () != (T) -0.0) |
| 44 |
return false; |
| 45 |
|
| 46 |
auto polarz = std::polar ((T) 1.5, std::numbers::pi_v<T> / 4); |
| 47 |
if (!FP_EQUAL (polarz.real (), (T) 1.5 * std::cos (std::numbers::pi_v<T> / 4)) |
| 48 |
|| !FP_EQUAL (polarz.imag (), |
| 49 |
(T) 1.5* std::sin (std::numbers::pi_v<T> / 4))) |
| 50 |
return false; |
| 51 |
|
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
template<typename T> bool test_complex_exp_log () |
| 56 |
{ |
| 57 |
std::complex<T> z (-1.724, -3.763); |
| 58 |
|
| 59 |
// Euler's identity |
| 60 |
auto eulerz = std::exp (std::complex<T> (0, std::numbers::pi)); |
| 61 |
eulerz += 1.0; |
| 62 |
if (!COMPLEX_EQUAL (eulerz, std::complex<T> ())) |
| 63 |
return false; |
| 64 |
|
| 65 |
auto my_exp_z |
| 66 |
= std::complex<T> (std::exp (z.real ()) * std::cos (z.imag ()), |
| 67 |
std::exp (z.real ()) * std::sin (z.imag ())); |
| 68 |
if (!COMPLEX_EQUAL (std::exp (z), my_exp_z)) |
| 69 |
return false; |
| 70 |
|
| 71 |
if (!COMPLEX_EQUAL (std::log10 (z), |
| 72 |
std::log (z) / std::log (std::complex<T> (10)))) |
| 73 |
return false; |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
template<typename T> bool test_complex_trig () |
| 79 |
{ |
| 80 |
std::complex<T> z (std::numbers::pi / 8, std::numbers::pi / 10); |
| 81 |
const std::complex<T> i (0, 1); |
| 82 |
|
| 83 |
auto my_sin_z |
| 84 |
= std::complex<T> (std::sin (z.real ()) * std::cosh (z.imag ()), |
| 85 |
std::cos (z.real ()) * std::sinh (z.imag ())); |
| 86 |
if (!COMPLEX_EQUAL (std::sin (z), my_sin_z)) |
| 87 |
return false; |
| 88 |
|
| 89 |
auto my_cos_z |
| 90 |
= std::complex<T> (std::cos (z.real ()) * std::cosh (z.imag ()), |
| 91 |
-std::sin (z.real ()) * std::sinh (z.imag ())); |
| 92 |
if (!COMPLEX_EQUAL (std::cos (z), my_cos_z)) |
| 93 |
return false; |
| 94 |
|
| 95 |
auto my_tan_z |
| 96 |
= std::complex<T> (std::sin (2*z.real ()), std::sinh (2*z.imag ())) |
| 97 |
/ (std::cos (2*z.real ()) + std::cosh (2*z.imag ())); |
| 98 |
if (!COMPLEX_EQUAL (std::tan (z), my_tan_z)) |
| 99 |
return false; |
| 100 |
|
| 101 |
auto my_sinh_z |
| 102 |
= std::complex<T> (std::sinh (z.real ()) * std::cos (z.imag ()), |
| 103 |
std::cosh (z.real ()) * std::sin (z.imag ())); |
| 104 |
if (!COMPLEX_EQUAL (std::sinh (z), my_sinh_z)) |
| 105 |
return false; |
| 106 |
|
| 107 |
auto my_cosh_z |
| 108 |
= std::complex<T> (std::cosh (z.real ()) * std::cos (z.imag ()), |
| 109 |
std::sinh (z.real ()) * std::sin (z.imag ())); |
| 110 |
if (!COMPLEX_EQUAL (std::cosh (z), my_cosh_z)) |
| 111 |
return false; |
| 112 |
|
| 113 |
auto my_tanh_z |
| 114 |
= std::complex<T> (std::sinh (2*z.real ()), |
| 115 |
std::sin (2*z.imag ())) |
| 116 |
/ (std::cosh (2*z.real ()) + std::cos (2*z.imag ())); |
| 117 |
if (!COMPLEX_EQUAL (std::tanh (z), my_tanh_z)) |
| 118 |
return false; |
| 119 |
|
| 120 |
auto my_asin_z = -i * std::log (i * z + std::sqrt ((T) 1.0 - z*z)); |
| 121 |
if (!COMPLEX_EQUAL (std::asin (z), my_asin_z)) |
| 122 |
return false; |
| 123 |
|
| 124 |
auto my_acos_z |
| 125 |
= std::complex<T> (std::numbers::pi / 2) |
| 126 |
+ i * std::log (i * z + std::sqrt ((T) 1.0 - z*z)); |
| 127 |
if (!COMPLEX_EQUAL (std::acos (z), my_acos_z)) |
| 128 |
return false; |
| 129 |
|
| 130 |
auto my_atan_z = std::complex<T> (0, -0.5) * (std::log ((i - z) / (i + z))); |
| 131 |
if (!COMPLEX_EQUAL (std::atan (z), my_atan_z)) |
| 132 |
return false; |
| 133 |
|
| 134 |
auto my_asinh_z = std::log (z + std::sqrt (z*z + (T) 1.0)); |
| 135 |
if (!COMPLEX_EQUAL (std::asinh (z), my_asinh_z)) |
| 136 |
return false; |
| 137 |
|
| 138 |
auto my_acosh_z = std::log (z + std::sqrt (z*z - (T) 1.0)); |
| 139 |
if (!COMPLEX_EQUAL (std::acosh (z), my_acosh_z)) |
| 140 |
return false; |
| 141 |
|
| 142 |
auto my_atanh_z |
| 143 |
= std::complex<T> (0.5) * (std::log ((T) 1.0 + z) - std::log ((T) 1.0 - z)); |
| 144 |
if (!COMPLEX_EQUAL (std::atanh (z), my_atanh_z)) |
| 145 |
return false; |
| 146 |
|
| 147 |
return true; |
| 148 |
} |
| 149 |
#pragma omp end declare target |
| 150 |
|
| 151 |
#define RUN_TEST(func) \ |
| 152 |
{ \ |
| 153 |
pass++; \ |
| 154 |
bool ok = test_##func<float> (); \ |
| 155 |
if (!ok) { result = pass; break; } \ |
| 156 |
pass++; \ |
| 157 |
ok = test_##func<double> (); \ |
| 158 |
if (!ok) { result = pass; break; } \ |
| 159 |
} |
| 160 |
|
| 161 |
int main (void) |
| 162 |
{ |
| 163 |
int result = 0; |
| 164 |
|
| 165 |
#pragma omp target map (tofrom: result) |
| 166 |
do { |
| 167 |
int pass = 0; |
| 168 |
|
| 169 |
RUN_TEST (complex); |
| 170 |
RUN_TEST (complex_exp_log); |
| 171 |
RUN_TEST (complex_trig); |
| 172 |
} while (false); |
| 173 |
|
| 174 |
return result; |
| 175 |
} |