1 1.1 mrg // The template and inlines for the -*- C++ -*- complex number classes. 2 1.1 mrg 3 1.16 mrg // Copyright (C) 1997-2022 Free Software Foundation, Inc. 4 1.1 mrg // 5 1.1 mrg // This file is part of the GNU ISO C++ Library. This library is free 6 1.1 mrg // software; you can redistribute it and/or modify it under the 7 1.1 mrg // terms of the GNU General Public License as published by the 8 1.1 mrg // Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg // any later version. 10 1.1 mrg 11 1.1 mrg // This library is distributed in the hope that it will be useful, 12 1.1 mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg // GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg // Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg // permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg // 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg // You should have received a copy of the GNU General Public License and 21 1.1 mrg // a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg // <http://www.gnu.org/licenses/>. 24 1.1 mrg 25 1.1 mrg /** @file include/complex 26 1.1 mrg * This is a Standard C++ Library header. 27 1.1 mrg */ 28 1.1 mrg 29 1.1 mrg // 30 1.1 mrg // ISO C++ 14882: 26.2 Complex Numbers 31 1.1 mrg // Note: this is not a conforming implementation. 32 1.1 mrg // Initially implemented by Ulrich Drepper <drepper (a] cygnus.com> 33 1.1 mrg // Improved by Gabriel Dos Reis <dosreis (a] cmla.ens-cachan.fr> 34 1.1 mrg // 35 1.1 mrg 36 1.1 mrg #ifndef _GLIBCXX_COMPLEX 37 1.1 mrg #define _GLIBCXX_COMPLEX 1 38 1.1 mrg 39 1.1 mrg #pragma GCC system_header 40 1.1 mrg 41 1.1 mrg #include <bits/c++config.h> 42 1.1 mrg #include <bits/cpp_type_traits.h> 43 1.1 mrg #include <ext/type_traits.h> 44 1.1 mrg #include <cmath> 45 1.1 mrg #include <sstream> 46 1.1 mrg 47 1.4 christos #if _GLIBCXX_USE_C99_COMPLEX 48 1.5 wiz // This is disgusting; we can't include ccomplex because that requires c++11 49 1.4 christos // and we can't use the builtins because those point to the wrong 50 1.4 christos // ABI-wise cabs/cabsf so we manually declare those here and use 51 1.4 christos // them directly. 52 1.4 christos extern "C" float __c99_cabsf(_Complex float); 53 1.4 christos extern "C" double __c99_cabs(_Complex double); 54 1.4 christos extern "C" long double __c99_cabsl(_Complex long double); 55 1.4 christos #endif 56 1.4 christos 57 1.3 mrg // Get rid of a macro possibly defined in <complex.h> 58 1.3 mrg #undef complex 59 1.3 mrg 60 1.15 mrg #if __cplusplus > 201703L 61 1.15 mrg # define __cpp_lib_constexpr_complex 201711L 62 1.15 mrg #endif 63 1.15 mrg 64 1.3 mrg namespace std _GLIBCXX_VISIBILITY(default) 65 1.3 mrg { 66 1.3 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 67 1.1 mrg 68 1.1 mrg /** 69 1.1 mrg * @defgroup complex_numbers Complex Numbers 70 1.1 mrg * @ingroup numerics 71 1.1 mrg * 72 1.1 mrg * Classes and functions for complex numbers. 73 1.1 mrg * @{ 74 1.1 mrg */ 75 1.1 mrg 76 1.1 mrg // Forward declarations. 77 1.1 mrg template<typename _Tp> class complex; 78 1.1 mrg template<> class complex<float>; 79 1.1 mrg template<> class complex<double>; 80 1.1 mrg template<> class complex<long double>; 81 1.1 mrg 82 1.1 mrg /// Return magnitude of @a z. 83 1.1 mrg template<typename _Tp> _Tp abs(const complex<_Tp>&); 84 1.1 mrg /// Return phase angle of @a z. 85 1.1 mrg template<typename _Tp> _Tp arg(const complex<_Tp>&); 86 1.1 mrg /// Return @a z magnitude squared. 87 1.14 mrg template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&); 88 1.1 mrg 89 1.1 mrg /// Return complex conjugate of @a z. 90 1.14 mrg template<typename _Tp> 91 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&); 92 1.1 mrg /// Return complex with magnitude @a rho and angle @a theta. 93 1.1 mrg template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 94 1.1 mrg 95 1.1 mrg // Transcendentals: 96 1.1 mrg /// Return complex cosine of @a z. 97 1.1 mrg template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 98 1.1 mrg /// Return complex hyperbolic cosine of @a z. 99 1.1 mrg template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 100 1.1 mrg /// Return complex base e exponential of @a z. 101 1.1 mrg template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 102 1.1 mrg /// Return complex natural logarithm of @a z. 103 1.1 mrg template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 104 1.1 mrg /// Return complex base 10 logarithm of @a z. 105 1.1 mrg template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 106 1.1 mrg /// Return @a x to the @a y'th power. 107 1.1 mrg template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 108 1.1 mrg /// Return @a x to the @a y'th power. 109 1.1 mrg template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 110 1.1 mrg /// Return @a x to the @a y'th power. 111 1.12 mrg template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 112 1.1 mrg const complex<_Tp>&); 113 1.1 mrg /// Return @a x to the @a y'th power. 114 1.1 mrg template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 115 1.1 mrg /// Return complex sine of @a z. 116 1.1 mrg template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 117 1.1 mrg /// Return complex hyperbolic sine of @a z. 118 1.1 mrg template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 119 1.1 mrg /// Return complex square root of @a z. 120 1.1 mrg template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 121 1.1 mrg /// Return complex tangent of @a z. 122 1.1 mrg template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 123 1.1 mrg /// Return complex hyperbolic tangent of @a z. 124 1.1 mrg template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 125 1.12 mrg 126 1.12 mrg 127 1.1 mrg // 26.2.2 Primary template class complex 128 1.1 mrg /** 129 1.1 mrg * Template to represent complex numbers. 130 1.1 mrg * 131 1.1 mrg * Specializations for float, double, and long double are part of the 132 1.1 mrg * library. Results with any other type are not guaranteed. 133 1.1 mrg * 134 1.1 mrg * @param Tp Type of real and imaginary values. 135 1.1 mrg */ 136 1.1 mrg template<typename _Tp> 137 1.16 mrg class complex 138 1.1 mrg { 139 1.16 mrg public: 140 1.1 mrg /// Value typedef. 141 1.1 mrg typedef _Tp value_type; 142 1.12 mrg 143 1.1 mrg /// Default constructor. First parameter is x, second parameter is y. 144 1.1 mrg /// Unspecified parameters default to 0. 145 1.3 mrg _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) 146 1.1 mrg : _M_real(__r), _M_imag(__i) { } 147 1.1 mrg 148 1.7 mrg // Let the compiler synthesize the copy constructor 149 1.7 mrg #if __cplusplus >= 201103L 150 1.7 mrg constexpr complex(const complex&) = default; 151 1.7 mrg #endif 152 1.7 mrg 153 1.7 mrg /// Converting constructor. 154 1.1 mrg template<typename _Up> 155 1.3 mrg _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) 156 1.1 mrg : _M_real(__z.real()), _M_imag(__z.imag()) { } 157 1.1 mrg 158 1.3 mrg #if __cplusplus >= 201103L 159 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 160 1.1 mrg // DR 387. std::complex over-encapsulated. 161 1.3 mrg _GLIBCXX_ABI_TAG_CXX11 162 1.12 mrg constexpr _Tp 163 1.7 mrg real() const { return _M_real; } 164 1.3 mrg 165 1.3 mrg _GLIBCXX_ABI_TAG_CXX11 166 1.12 mrg constexpr _Tp 167 1.7 mrg imag() const { return _M_imag; } 168 1.1 mrg #else 169 1.1 mrg /// Return real part of complex number. 170 1.12 mrg _Tp& 171 1.3 mrg real() { return _M_real; } 172 1.1 mrg 173 1.1 mrg /// Return real part of complex number. 174 1.12 mrg const _Tp& 175 1.3 mrg real() const { return _M_real; } 176 1.1 mrg 177 1.1 mrg /// Return imaginary part of complex number. 178 1.12 mrg _Tp& 179 1.3 mrg imag() { return _M_imag; } 180 1.1 mrg 181 1.1 mrg /// Return imaginary part of complex number. 182 1.12 mrg const _Tp& 183 1.3 mrg imag() const { return _M_imag; } 184 1.1 mrg #endif 185 1.1 mrg 186 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 187 1.1 mrg // DR 387. std::complex over-encapsulated. 188 1.14 mrg _GLIBCXX20_CONSTEXPR void 189 1.3 mrg real(_Tp __val) { _M_real = __val; } 190 1.1 mrg 191 1.14 mrg _GLIBCXX20_CONSTEXPR void 192 1.3 mrg imag(_Tp __val) { _M_imag = __val; } 193 1.1 mrg 194 1.7 mrg /// Assign a scalar to this complex number. 195 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&); 196 1.12 mrg 197 1.7 mrg /// Add a scalar to this complex number. 198 1.1 mrg // 26.2.5/1 199 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 200 1.1 mrg operator+=(const _Tp& __t) 201 1.1 mrg { 202 1.1 mrg _M_real += __t; 203 1.1 mrg return *this; 204 1.1 mrg } 205 1.1 mrg 206 1.7 mrg /// Subtract a scalar from this complex number. 207 1.1 mrg // 26.2.5/3 208 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 209 1.1 mrg operator-=(const _Tp& __t) 210 1.1 mrg { 211 1.1 mrg _M_real -= __t; 212 1.1 mrg return *this; 213 1.1 mrg } 214 1.1 mrg 215 1.7 mrg /// Multiply this complex number by a scalar. 216 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&); 217 1.7 mrg /// Divide this complex number by a scalar. 218 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&); 219 1.1 mrg 220 1.7 mrg // Let the compiler synthesize the copy assignment operator 221 1.7 mrg #if __cplusplus >= 201103L 222 1.14 mrg _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default; 223 1.7 mrg #endif 224 1.7 mrg 225 1.7 mrg /// Assign another complex number to this one. 226 1.1 mrg template<typename _Up> 227 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&); 228 1.7 mrg /// Add another complex number to this one. 229 1.1 mrg template<typename _Up> 230 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&); 231 1.7 mrg /// Subtract another complex number from this one. 232 1.1 mrg template<typename _Up> 233 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&); 234 1.7 mrg /// Multiply this complex number by another. 235 1.1 mrg template<typename _Up> 236 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&); 237 1.7 mrg /// Divide this complex number by another. 238 1.1 mrg template<typename _Up> 239 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&); 240 1.1 mrg 241 1.7 mrg _GLIBCXX_CONSTEXPR complex __rep() const 242 1.1 mrg { return *this; } 243 1.1 mrg 244 1.1 mrg private: 245 1.1 mrg _Tp _M_real; 246 1.1 mrg _Tp _M_imag; 247 1.1 mrg }; 248 1.1 mrg 249 1.1 mrg template<typename _Tp> 250 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 251 1.1 mrg complex<_Tp>::operator=(const _Tp& __t) 252 1.1 mrg { 253 1.1 mrg _M_real = __t; 254 1.1 mrg _M_imag = _Tp(); 255 1.1 mrg return *this; 256 1.12 mrg } 257 1.1 mrg 258 1.1 mrg // 26.2.5/5 259 1.1 mrg template<typename _Tp> 260 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 261 1.1 mrg complex<_Tp>::operator*=(const _Tp& __t) 262 1.1 mrg { 263 1.1 mrg _M_real *= __t; 264 1.1 mrg _M_imag *= __t; 265 1.1 mrg return *this; 266 1.1 mrg } 267 1.1 mrg 268 1.1 mrg // 26.2.5/7 269 1.1 mrg template<typename _Tp> 270 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 271 1.1 mrg complex<_Tp>::operator/=(const _Tp& __t) 272 1.1 mrg { 273 1.1 mrg _M_real /= __t; 274 1.1 mrg _M_imag /= __t; 275 1.1 mrg return *this; 276 1.1 mrg } 277 1.1 mrg 278 1.1 mrg template<typename _Tp> 279 1.1 mrg template<typename _Up> 280 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 281 1.1 mrg complex<_Tp>::operator=(const complex<_Up>& __z) 282 1.1 mrg { 283 1.1 mrg _M_real = __z.real(); 284 1.1 mrg _M_imag = __z.imag(); 285 1.1 mrg return *this; 286 1.1 mrg } 287 1.1 mrg 288 1.1 mrg // 26.2.5/9 289 1.1 mrg template<typename _Tp> 290 1.1 mrg template<typename _Up> 291 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 292 1.1 mrg complex<_Tp>::operator+=(const complex<_Up>& __z) 293 1.1 mrg { 294 1.1 mrg _M_real += __z.real(); 295 1.1 mrg _M_imag += __z.imag(); 296 1.1 mrg return *this; 297 1.1 mrg } 298 1.1 mrg 299 1.1 mrg // 26.2.5/11 300 1.1 mrg template<typename _Tp> 301 1.1 mrg template<typename _Up> 302 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 303 1.1 mrg complex<_Tp>::operator-=(const complex<_Up>& __z) 304 1.1 mrg { 305 1.1 mrg _M_real -= __z.real(); 306 1.1 mrg _M_imag -= __z.imag(); 307 1.1 mrg return *this; 308 1.1 mrg } 309 1.1 mrg 310 1.1 mrg // 26.2.5/13 311 1.1 mrg // XXX: This is a grammar school implementation. 312 1.1 mrg template<typename _Tp> 313 1.1 mrg template<typename _Up> 314 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 315 1.1 mrg complex<_Tp>::operator*=(const complex<_Up>& __z) 316 1.1 mrg { 317 1.1 mrg const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 318 1.1 mrg _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 319 1.1 mrg _M_real = __r; 320 1.1 mrg return *this; 321 1.1 mrg } 322 1.1 mrg 323 1.1 mrg // 26.2.5/15 324 1.1 mrg // XXX: This is a grammar school implementation. 325 1.1 mrg template<typename _Tp> 326 1.1 mrg template<typename _Up> 327 1.14 mrg _GLIBCXX20_CONSTEXPR complex<_Tp>& 328 1.1 mrg complex<_Tp>::operator/=(const complex<_Up>& __z) 329 1.1 mrg { 330 1.1 mrg const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 331 1.1 mrg const _Tp __n = std::norm(__z); 332 1.1 mrg _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 333 1.1 mrg _M_real = __r / __n; 334 1.1 mrg return *this; 335 1.1 mrg } 336 1.12 mrg 337 1.1 mrg // Operators: 338 1.15 mrg ///@{ 339 1.1 mrg /// Return new complex value @a x plus @a y. 340 1.1 mrg template<typename _Tp> 341 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 342 1.1 mrg operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 343 1.1 mrg { 344 1.1 mrg complex<_Tp> __r = __x; 345 1.1 mrg __r += __y; 346 1.1 mrg return __r; 347 1.1 mrg } 348 1.1 mrg 349 1.1 mrg template<typename _Tp> 350 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 351 1.1 mrg operator+(const complex<_Tp>& __x, const _Tp& __y) 352 1.1 mrg { 353 1.1 mrg complex<_Tp> __r = __x; 354 1.1 mrg __r += __y; 355 1.1 mrg return __r; 356 1.1 mrg } 357 1.1 mrg 358 1.1 mrg template<typename _Tp> 359 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 360 1.1 mrg operator+(const _Tp& __x, const complex<_Tp>& __y) 361 1.1 mrg { 362 1.1 mrg complex<_Tp> __r = __y; 363 1.1 mrg __r += __x; 364 1.1 mrg return __r; 365 1.1 mrg } 366 1.15 mrg ///@} 367 1.1 mrg 368 1.15 mrg ///@{ 369 1.1 mrg /// Return new complex value @a x minus @a y. 370 1.1 mrg template<typename _Tp> 371 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 372 1.1 mrg operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 373 1.1 mrg { 374 1.1 mrg complex<_Tp> __r = __x; 375 1.1 mrg __r -= __y; 376 1.1 mrg return __r; 377 1.1 mrg } 378 1.12 mrg 379 1.1 mrg template<typename _Tp> 380 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 381 1.1 mrg operator-(const complex<_Tp>& __x, const _Tp& __y) 382 1.1 mrg { 383 1.1 mrg complex<_Tp> __r = __x; 384 1.1 mrg __r -= __y; 385 1.1 mrg return __r; 386 1.1 mrg } 387 1.1 mrg 388 1.1 mrg template<typename _Tp> 389 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 390 1.1 mrg operator-(const _Tp& __x, const complex<_Tp>& __y) 391 1.1 mrg { 392 1.14 mrg complex<_Tp> __r = -__y; 393 1.14 mrg __r += __x; 394 1.1 mrg return __r; 395 1.1 mrg } 396 1.15 mrg ///@} 397 1.1 mrg 398 1.15 mrg ///@{ 399 1.1 mrg /// Return new complex value @a x times @a y. 400 1.1 mrg template<typename _Tp> 401 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 402 1.1 mrg operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 403 1.1 mrg { 404 1.1 mrg complex<_Tp> __r = __x; 405 1.1 mrg __r *= __y; 406 1.1 mrg return __r; 407 1.1 mrg } 408 1.1 mrg 409 1.1 mrg template<typename _Tp> 410 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 411 1.1 mrg operator*(const complex<_Tp>& __x, const _Tp& __y) 412 1.1 mrg { 413 1.1 mrg complex<_Tp> __r = __x; 414 1.1 mrg __r *= __y; 415 1.1 mrg return __r; 416 1.1 mrg } 417 1.1 mrg 418 1.1 mrg template<typename _Tp> 419 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 420 1.1 mrg operator*(const _Tp& __x, const complex<_Tp>& __y) 421 1.1 mrg { 422 1.1 mrg complex<_Tp> __r = __y; 423 1.1 mrg __r *= __x; 424 1.1 mrg return __r; 425 1.1 mrg } 426 1.15 mrg ///@} 427 1.1 mrg 428 1.15 mrg ///@{ 429 1.1 mrg /// Return new complex value @a x divided by @a y. 430 1.1 mrg template<typename _Tp> 431 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 432 1.1 mrg operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 433 1.1 mrg { 434 1.1 mrg complex<_Tp> __r = __x; 435 1.1 mrg __r /= __y; 436 1.1 mrg return __r; 437 1.1 mrg } 438 1.12 mrg 439 1.1 mrg template<typename _Tp> 440 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 441 1.1 mrg operator/(const complex<_Tp>& __x, const _Tp& __y) 442 1.1 mrg { 443 1.1 mrg complex<_Tp> __r = __x; 444 1.1 mrg __r /= __y; 445 1.1 mrg return __r; 446 1.1 mrg } 447 1.1 mrg 448 1.1 mrg template<typename _Tp> 449 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 450 1.1 mrg operator/(const _Tp& __x, const complex<_Tp>& __y) 451 1.1 mrg { 452 1.1 mrg complex<_Tp> __r = __x; 453 1.1 mrg __r /= __y; 454 1.1 mrg return __r; 455 1.1 mrg } 456 1.15 mrg ///@} 457 1.1 mrg 458 1.1 mrg /// Return @a x. 459 1.1 mrg template<typename _Tp> 460 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 461 1.1 mrg operator+(const complex<_Tp>& __x) 462 1.1 mrg { return __x; } 463 1.1 mrg 464 1.1 mrg /// Return complex negation of @a x. 465 1.1 mrg template<typename _Tp> 466 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 467 1.1 mrg operator-(const complex<_Tp>& __x) 468 1.14 mrg { return complex<_Tp>(-__x.real(), -__x.imag()); } 469 1.1 mrg 470 1.15 mrg ///@{ 471 1.1 mrg /// Return true if @a x is equal to @a y. 472 1.1 mrg template<typename _Tp> 473 1.3 mrg inline _GLIBCXX_CONSTEXPR bool 474 1.1 mrg operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 475 1.1 mrg { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 476 1.1 mrg 477 1.1 mrg template<typename _Tp> 478 1.3 mrg inline _GLIBCXX_CONSTEXPR bool 479 1.1 mrg operator==(const complex<_Tp>& __x, const _Tp& __y) 480 1.1 mrg { return __x.real() == __y && __x.imag() == _Tp(); } 481 1.1 mrg 482 1.15 mrg #if !(__cpp_impl_three_way_comparison >= 201907L) 483 1.1 mrg template<typename _Tp> 484 1.3 mrg inline _GLIBCXX_CONSTEXPR bool 485 1.1 mrg operator==(const _Tp& __x, const complex<_Tp>& __y) 486 1.1 mrg { return __x == __y.real() && _Tp() == __y.imag(); } 487 1.15 mrg ///@} 488 1.1 mrg 489 1.15 mrg ///@{ 490 1.1 mrg /// Return false if @a x is equal to @a y. 491 1.1 mrg template<typename _Tp> 492 1.3 mrg inline _GLIBCXX_CONSTEXPR bool 493 1.1 mrg operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) 494 1.1 mrg { return __x.real() != __y.real() || __x.imag() != __y.imag(); } 495 1.1 mrg 496 1.1 mrg template<typename _Tp> 497 1.3 mrg inline _GLIBCXX_CONSTEXPR bool 498 1.1 mrg operator!=(const complex<_Tp>& __x, const _Tp& __y) 499 1.1 mrg { return __x.real() != __y || __x.imag() != _Tp(); } 500 1.1 mrg 501 1.1 mrg template<typename _Tp> 502 1.3 mrg inline _GLIBCXX_CONSTEXPR bool 503 1.1 mrg operator!=(const _Tp& __x, const complex<_Tp>& __y) 504 1.1 mrg { return __x != __y.real() || _Tp() != __y.imag(); } 505 1.15 mrg #endif 506 1.15 mrg ///@} 507 1.1 mrg 508 1.1 mrg /// Extraction operator for complex values. 509 1.1 mrg template<typename _Tp, typename _CharT, class _Traits> 510 1.1 mrg basic_istream<_CharT, _Traits>& 511 1.1 mrg operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 512 1.1 mrg { 513 1.13 mrg bool __fail = true; 514 1.13 mrg _CharT __ch; 515 1.13 mrg if (__is >> __ch) 516 1.1 mrg { 517 1.13 mrg if (_Traits::eq(__ch, __is.widen('('))) 518 1.1 mrg { 519 1.13 mrg _Tp __u; 520 1.13 mrg if (__is >> __u >> __ch) 521 1.13 mrg { 522 1.13 mrg const _CharT __rparen = __is.widen(')'); 523 1.13 mrg if (_Traits::eq(__ch, __rparen)) 524 1.13 mrg { 525 1.13 mrg __x = __u; 526 1.13 mrg __fail = false; 527 1.13 mrg } 528 1.13 mrg else if (_Traits::eq(__ch, __is.widen(','))) 529 1.13 mrg { 530 1.13 mrg _Tp __v; 531 1.13 mrg if (__is >> __v >> __ch) 532 1.13 mrg { 533 1.13 mrg if (_Traits::eq(__ch, __rparen)) 534 1.13 mrg { 535 1.13 mrg __x = complex<_Tp>(__u, __v); 536 1.13 mrg __fail = false; 537 1.13 mrg } 538 1.13 mrg else 539 1.13 mrg __is.putback(__ch); 540 1.13 mrg } 541 1.13 mrg } 542 1.13 mrg else 543 1.13 mrg __is.putback(__ch); 544 1.13 mrg } 545 1.1 mrg } 546 1.1 mrg else 547 1.13 mrg { 548 1.13 mrg __is.putback(__ch); 549 1.13 mrg _Tp __u; 550 1.13 mrg if (__is >> __u) 551 1.13 mrg { 552 1.13 mrg __x = __u; 553 1.13 mrg __fail = false; 554 1.13 mrg } 555 1.13 mrg } 556 1.1 mrg } 557 1.13 mrg if (__fail) 558 1.13 mrg __is.setstate(ios_base::failbit); 559 1.1 mrg return __is; 560 1.1 mrg } 561 1.1 mrg 562 1.1 mrg /// Insertion operator for complex values. 563 1.1 mrg template<typename _Tp, typename _CharT, class _Traits> 564 1.1 mrg basic_ostream<_CharT, _Traits>& 565 1.1 mrg operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 566 1.1 mrg { 567 1.1 mrg basic_ostringstream<_CharT, _Traits> __s; 568 1.1 mrg __s.flags(__os.flags()); 569 1.1 mrg __s.imbue(__os.getloc()); 570 1.1 mrg __s.precision(__os.precision()); 571 1.1 mrg __s << '(' << __x.real() << ',' << __x.imag() << ')'; 572 1.1 mrg return __os << __s.str(); 573 1.1 mrg } 574 1.1 mrg 575 1.1 mrg // Values 576 1.3 mrg #if __cplusplus >= 201103L 577 1.1 mrg template<typename _Tp> 578 1.3 mrg constexpr _Tp 579 1.1 mrg real(const complex<_Tp>& __z) 580 1.1 mrg { return __z.real(); } 581 1.3 mrg 582 1.1 mrg template<typename _Tp> 583 1.3 mrg constexpr _Tp 584 1.1 mrg imag(const complex<_Tp>& __z) 585 1.1 mrg { return __z.imag(); } 586 1.1 mrg #else 587 1.1 mrg template<typename _Tp> 588 1.1 mrg inline _Tp& 589 1.1 mrg real(complex<_Tp>& __z) 590 1.1 mrg { return __z.real(); } 591 1.12 mrg 592 1.1 mrg template<typename _Tp> 593 1.1 mrg inline const _Tp& 594 1.1 mrg real(const complex<_Tp>& __z) 595 1.1 mrg { return __z.real(); } 596 1.12 mrg 597 1.1 mrg template<typename _Tp> 598 1.1 mrg inline _Tp& 599 1.1 mrg imag(complex<_Tp>& __z) 600 1.1 mrg { return __z.imag(); } 601 1.12 mrg 602 1.1 mrg template<typename _Tp> 603 1.1 mrg inline const _Tp& 604 1.1 mrg imag(const complex<_Tp>& __z) 605 1.1 mrg { return __z.imag(); } 606 1.1 mrg #endif 607 1.1 mrg 608 1.1 mrg // 26.2.7/3 abs(__z): Returns the magnitude of __z. 609 1.1 mrg template<typename _Tp> 610 1.1 mrg inline _Tp 611 1.1 mrg __complex_abs(const complex<_Tp>& __z) 612 1.1 mrg { 613 1.1 mrg _Tp __x = __z.real(); 614 1.1 mrg _Tp __y = __z.imag(); 615 1.1 mrg const _Tp __s = std::max(abs(__x), abs(__y)); 616 1.1 mrg if (__s == _Tp()) // well ... 617 1.1 mrg return __s; 618 1.12 mrg __x /= __s; 619 1.1 mrg __y /= __s; 620 1.1 mrg return __s * sqrt(__x * __x + __y * __y); 621 1.1 mrg } 622 1.1 mrg 623 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 624 1.4 christos // XXX: We can't use __builtin_cabs* because they are broken 625 1.1 mrg inline float 626 1.4 christos __complex_abs(__complex__ float __z) { return __c99_cabsf(__z); } 627 1.1 mrg 628 1.1 mrg inline double 629 1.4 christos __complex_abs(__complex__ double __z) { return __c99_cabs(__z); } 630 1.1 mrg 631 1.1 mrg inline long double 632 1.1 mrg __complex_abs(const __complex__ long double& __z) 633 1.4 christos { return __c99_cabsl(__z); } 634 1.1 mrg 635 1.1 mrg template<typename _Tp> 636 1.1 mrg inline _Tp 637 1.1 mrg abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 638 1.1 mrg #else 639 1.1 mrg template<typename _Tp> 640 1.1 mrg inline _Tp 641 1.1 mrg abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 642 1.12 mrg #endif 643 1.1 mrg 644 1.1 mrg 645 1.1 mrg // 26.2.7/4: arg(__z): Returns the phase angle of __z. 646 1.1 mrg template<typename _Tp> 647 1.1 mrg inline _Tp 648 1.1 mrg __complex_arg(const complex<_Tp>& __z) 649 1.1 mrg { return atan2(__z.imag(), __z.real()); } 650 1.1 mrg 651 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 652 1.1 mrg inline float 653 1.1 mrg __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 654 1.1 mrg 655 1.1 mrg inline double 656 1.1 mrg __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 657 1.1 mrg 658 1.1 mrg inline long double 659 1.1 mrg __complex_arg(const __complex__ long double& __z) 660 1.1 mrg { return __builtin_cargl(__z); } 661 1.1 mrg 662 1.1 mrg template<typename _Tp> 663 1.1 mrg inline _Tp 664 1.1 mrg arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 665 1.1 mrg #else 666 1.1 mrg template<typename _Tp> 667 1.1 mrg inline _Tp 668 1.1 mrg arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 669 1.1 mrg #endif 670 1.1 mrg 671 1.1 mrg // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 672 1.1 mrg // As defined, norm() is -not- a norm is the common mathematical 673 1.7 mrg // sense used in numerics. The helper class _Norm_helper<> tries to 674 1.1 mrg // distinguish between builtin floating point and the rest, so as 675 1.1 mrg // to deliver an answer as close as possible to the real value. 676 1.1 mrg template<bool> 677 1.1 mrg struct _Norm_helper 678 1.1 mrg { 679 1.1 mrg template<typename _Tp> 680 1.14 mrg static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z) 681 1.1 mrg { 682 1.1 mrg const _Tp __x = __z.real(); 683 1.1 mrg const _Tp __y = __z.imag(); 684 1.1 mrg return __x * __x + __y * __y; 685 1.1 mrg } 686 1.1 mrg }; 687 1.1 mrg 688 1.1 mrg template<> 689 1.1 mrg struct _Norm_helper<true> 690 1.1 mrg { 691 1.1 mrg template<typename _Tp> 692 1.14 mrg static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z) 693 1.1 mrg { 694 1.14 mrg //_Tp __res = std::abs(__z); 695 1.14 mrg //return __res * __res; 696 1.14 mrg const _Tp __x = __z.real(); 697 1.14 mrg const _Tp __y = __z.imag(); 698 1.14 mrg return __x * __x + __y * __y; 699 1.1 mrg } 700 1.1 mrg }; 701 1.12 mrg 702 1.1 mrg template<typename _Tp> 703 1.14 mrg inline _GLIBCXX20_CONSTEXPR _Tp 704 1.1 mrg norm(const complex<_Tp>& __z) 705 1.1 mrg { 706 1.12 mrg return _Norm_helper<__is_floating<_Tp>::__value 707 1.1 mrg && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 708 1.1 mrg } 709 1.1 mrg 710 1.1 mrg template<typename _Tp> 711 1.1 mrg inline complex<_Tp> 712 1.1 mrg polar(const _Tp& __rho, const _Tp& __theta) 713 1.9 mrg { 714 1.9 mrg __glibcxx_assert( __rho >= 0 ); 715 1.9 mrg return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); 716 1.9 mrg } 717 1.1 mrg 718 1.1 mrg template<typename _Tp> 719 1.14 mrg inline _GLIBCXX20_CONSTEXPR complex<_Tp> 720 1.1 mrg conj(const complex<_Tp>& __z) 721 1.1 mrg { return complex<_Tp>(__z.real(), -__z.imag()); } 722 1.12 mrg 723 1.1 mrg // Transcendentals 724 1.1 mrg 725 1.1 mrg // 26.2.8/1 cos(__z): Returns the cosine of __z. 726 1.1 mrg template<typename _Tp> 727 1.1 mrg inline complex<_Tp> 728 1.1 mrg __complex_cos(const complex<_Tp>& __z) 729 1.1 mrg { 730 1.1 mrg const _Tp __x = __z.real(); 731 1.1 mrg const _Tp __y = __z.imag(); 732 1.1 mrg return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 733 1.1 mrg } 734 1.1 mrg 735 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 736 1.1 mrg inline __complex__ float 737 1.1 mrg __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 738 1.1 mrg 739 1.1 mrg inline __complex__ double 740 1.1 mrg __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 741 1.1 mrg 742 1.1 mrg inline __complex__ long double 743 1.1 mrg __complex_cos(const __complex__ long double& __z) 744 1.1 mrg { return __builtin_ccosl(__z); } 745 1.1 mrg 746 1.1 mrg template<typename _Tp> 747 1.1 mrg inline complex<_Tp> 748 1.1 mrg cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 749 1.1 mrg #else 750 1.1 mrg template<typename _Tp> 751 1.1 mrg inline complex<_Tp> 752 1.1 mrg cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 753 1.1 mrg #endif 754 1.1 mrg 755 1.1 mrg // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 756 1.1 mrg template<typename _Tp> 757 1.1 mrg inline complex<_Tp> 758 1.1 mrg __complex_cosh(const complex<_Tp>& __z) 759 1.1 mrg { 760 1.1 mrg const _Tp __x = __z.real(); 761 1.1 mrg const _Tp __y = __z.imag(); 762 1.1 mrg return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 763 1.1 mrg } 764 1.1 mrg 765 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 766 1.1 mrg inline __complex__ float 767 1.1 mrg __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 768 1.1 mrg 769 1.1 mrg inline __complex__ double 770 1.1 mrg __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 771 1.1 mrg 772 1.1 mrg inline __complex__ long double 773 1.1 mrg __complex_cosh(const __complex__ long double& __z) 774 1.1 mrg { return __builtin_ccoshl(__z); } 775 1.1 mrg 776 1.1 mrg template<typename _Tp> 777 1.1 mrg inline complex<_Tp> 778 1.1 mrg cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 779 1.1 mrg #else 780 1.1 mrg template<typename _Tp> 781 1.1 mrg inline complex<_Tp> 782 1.1 mrg cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 783 1.1 mrg #endif 784 1.1 mrg 785 1.1 mrg // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 786 1.1 mrg template<typename _Tp> 787 1.1 mrg inline complex<_Tp> 788 1.1 mrg __complex_exp(const complex<_Tp>& __z) 789 1.7 mrg { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } 790 1.1 mrg 791 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 792 1.1 mrg inline __complex__ float 793 1.1 mrg __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 794 1.1 mrg 795 1.1 mrg inline __complex__ double 796 1.1 mrg __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 797 1.1 mrg 798 1.1 mrg inline __complex__ long double 799 1.1 mrg __complex_exp(const __complex__ long double& __z) 800 1.1 mrg { return __builtin_cexpl(__z); } 801 1.1 mrg 802 1.1 mrg template<typename _Tp> 803 1.1 mrg inline complex<_Tp> 804 1.1 mrg exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 805 1.1 mrg #else 806 1.1 mrg template<typename _Tp> 807 1.1 mrg inline complex<_Tp> 808 1.1 mrg exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 809 1.1 mrg #endif 810 1.1 mrg 811 1.1 mrg // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 812 1.1 mrg // The branch cut is along the negative axis. 813 1.1 mrg template<typename _Tp> 814 1.1 mrg inline complex<_Tp> 815 1.1 mrg __complex_log(const complex<_Tp>& __z) 816 1.1 mrg { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 817 1.1 mrg 818 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 819 1.1 mrg inline __complex__ float 820 1.1 mrg __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 821 1.1 mrg 822 1.1 mrg inline __complex__ double 823 1.1 mrg __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 824 1.1 mrg 825 1.1 mrg inline __complex__ long double 826 1.1 mrg __complex_log(const __complex__ long double& __z) 827 1.1 mrg { return __builtin_clogl(__z); } 828 1.1 mrg 829 1.1 mrg template<typename _Tp> 830 1.1 mrg inline complex<_Tp> 831 1.1 mrg log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 832 1.1 mrg #else 833 1.1 mrg template<typename _Tp> 834 1.1 mrg inline complex<_Tp> 835 1.1 mrg log(const complex<_Tp>& __z) { return __complex_log(__z); } 836 1.1 mrg #endif 837 1.1 mrg 838 1.1 mrg template<typename _Tp> 839 1.1 mrg inline complex<_Tp> 840 1.1 mrg log10(const complex<_Tp>& __z) 841 1.1 mrg { return std::log(__z) / log(_Tp(10.0)); } 842 1.1 mrg 843 1.1 mrg // 26.2.8/10 sin(__z): Returns the sine of __z. 844 1.1 mrg template<typename _Tp> 845 1.1 mrg inline complex<_Tp> 846 1.1 mrg __complex_sin(const complex<_Tp>& __z) 847 1.1 mrg { 848 1.1 mrg const _Tp __x = __z.real(); 849 1.1 mrg const _Tp __y = __z.imag(); 850 1.12 mrg return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 851 1.1 mrg } 852 1.1 mrg 853 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 854 1.1 mrg inline __complex__ float 855 1.1 mrg __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 856 1.1 mrg 857 1.1 mrg inline __complex__ double 858 1.1 mrg __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 859 1.1 mrg 860 1.1 mrg inline __complex__ long double 861 1.1 mrg __complex_sin(const __complex__ long double& __z) 862 1.1 mrg { return __builtin_csinl(__z); } 863 1.1 mrg 864 1.1 mrg template<typename _Tp> 865 1.1 mrg inline complex<_Tp> 866 1.1 mrg sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 867 1.1 mrg #else 868 1.1 mrg template<typename _Tp> 869 1.1 mrg inline complex<_Tp> 870 1.1 mrg sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 871 1.1 mrg #endif 872 1.1 mrg 873 1.1 mrg // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 874 1.1 mrg template<typename _Tp> 875 1.1 mrg inline complex<_Tp> 876 1.1 mrg __complex_sinh(const complex<_Tp>& __z) 877 1.1 mrg { 878 1.1 mrg const _Tp __x = __z.real(); 879 1.1 mrg const _Tp __y = __z.imag(); 880 1.1 mrg return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 881 1.1 mrg } 882 1.1 mrg 883 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 884 1.1 mrg inline __complex__ float 885 1.12 mrg __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 886 1.1 mrg 887 1.1 mrg inline __complex__ double 888 1.12 mrg __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 889 1.1 mrg 890 1.1 mrg inline __complex__ long double 891 1.1 mrg __complex_sinh(const __complex__ long double& __z) 892 1.12 mrg { return __builtin_csinhl(__z); } 893 1.1 mrg 894 1.1 mrg template<typename _Tp> 895 1.1 mrg inline complex<_Tp> 896 1.1 mrg sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 897 1.1 mrg #else 898 1.1 mrg template<typename _Tp> 899 1.1 mrg inline complex<_Tp> 900 1.1 mrg sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 901 1.1 mrg #endif 902 1.1 mrg 903 1.1 mrg // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 904 1.1 mrg // The branch cut is on the negative axis. 905 1.1 mrg template<typename _Tp> 906 1.1 mrg complex<_Tp> 907 1.1 mrg __complex_sqrt(const complex<_Tp>& __z) 908 1.1 mrg { 909 1.1 mrg _Tp __x = __z.real(); 910 1.1 mrg _Tp __y = __z.imag(); 911 1.1 mrg 912 1.1 mrg if (__x == _Tp()) 913 1.1 mrg { 914 1.1 mrg _Tp __t = sqrt(abs(__y) / 2); 915 1.1 mrg return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 916 1.1 mrg } 917 1.1 mrg else 918 1.1 mrg { 919 1.1 mrg _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 920 1.1 mrg _Tp __u = __t / 2; 921 1.1 mrg return __x > _Tp() 922 1.1 mrg ? complex<_Tp>(__u, __y / __t) 923 1.1 mrg : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 924 1.1 mrg } 925 1.1 mrg } 926 1.1 mrg 927 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 928 1.1 mrg inline __complex__ float 929 1.1 mrg __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 930 1.1 mrg 931 1.1 mrg inline __complex__ double 932 1.1 mrg __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 933 1.1 mrg 934 1.1 mrg inline __complex__ long double 935 1.1 mrg __complex_sqrt(const __complex__ long double& __z) 936 1.1 mrg { return __builtin_csqrtl(__z); } 937 1.1 mrg 938 1.1 mrg template<typename _Tp> 939 1.1 mrg inline complex<_Tp> 940 1.1 mrg sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 941 1.1 mrg #else 942 1.1 mrg template<typename _Tp> 943 1.1 mrg inline complex<_Tp> 944 1.1 mrg sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 945 1.1 mrg #endif 946 1.1 mrg 947 1.1 mrg // 26.2.8/14 tan(__z): Return the complex tangent of __z. 948 1.12 mrg 949 1.1 mrg template<typename _Tp> 950 1.1 mrg inline complex<_Tp> 951 1.1 mrg __complex_tan(const complex<_Tp>& __z) 952 1.1 mrg { return std::sin(__z) / std::cos(__z); } 953 1.1 mrg 954 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 955 1.1 mrg inline __complex__ float 956 1.1 mrg __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 957 1.1 mrg 958 1.1 mrg inline __complex__ double 959 1.1 mrg __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 960 1.1 mrg 961 1.1 mrg inline __complex__ long double 962 1.1 mrg __complex_tan(const __complex__ long double& __z) 963 1.1 mrg { return __builtin_ctanl(__z); } 964 1.1 mrg 965 1.1 mrg template<typename _Tp> 966 1.1 mrg inline complex<_Tp> 967 1.1 mrg tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 968 1.1 mrg #else 969 1.1 mrg template<typename _Tp> 970 1.1 mrg inline complex<_Tp> 971 1.1 mrg tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 972 1.1 mrg #endif 973 1.1 mrg 974 1.1 mrg 975 1.1 mrg // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 976 1.12 mrg 977 1.1 mrg template<typename _Tp> 978 1.1 mrg inline complex<_Tp> 979 1.1 mrg __complex_tanh(const complex<_Tp>& __z) 980 1.1 mrg { return std::sinh(__z) / std::cosh(__z); } 981 1.1 mrg 982 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 983 1.1 mrg inline __complex__ float 984 1.1 mrg __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 985 1.1 mrg 986 1.1 mrg inline __complex__ double 987 1.1 mrg __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 988 1.1 mrg 989 1.1 mrg inline __complex__ long double 990 1.1 mrg __complex_tanh(const __complex__ long double& __z) 991 1.1 mrg { return __builtin_ctanhl(__z); } 992 1.1 mrg 993 1.1 mrg template<typename _Tp> 994 1.1 mrg inline complex<_Tp> 995 1.1 mrg tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 996 1.1 mrg #else 997 1.1 mrg template<typename _Tp> 998 1.1 mrg inline complex<_Tp> 999 1.1 mrg tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 1000 1.1 mrg #endif 1001 1.1 mrg 1002 1.1 mrg 1003 1.1 mrg // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 1004 1.1 mrg // raised to the __y-th power. The branch 1005 1.1 mrg // cut is on the negative axis. 1006 1.3 mrg template<typename _Tp> 1007 1.3 mrg complex<_Tp> 1008 1.3 mrg __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 1009 1.3 mrg { 1010 1.3 mrg complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 1011 1.3 mrg 1012 1.3 mrg while (__n >>= 1) 1013 1.3 mrg { 1014 1.3 mrg __x *= __x; 1015 1.3 mrg if (__n % 2) 1016 1.3 mrg __y *= __x; 1017 1.3 mrg } 1018 1.3 mrg 1019 1.3 mrg return __y; 1020 1.3 mrg } 1021 1.3 mrg 1022 1.7 mrg // In C++11 mode we used to implement the resolution of 1023 1.1 mrg // DR 844. complex pow return type is ambiguous. 1024 1.7 mrg // thus the following overload was disabled in that mode. However, doing 1025 1.7 mrg // that causes all sorts of issues, see, for example: 1026 1.7 mrg // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html 1027 1.7 mrg // and also PR57974. 1028 1.1 mrg template<typename _Tp> 1029 1.1 mrg inline complex<_Tp> 1030 1.1 mrg pow(const complex<_Tp>& __z, int __n) 1031 1.3 mrg { 1032 1.3 mrg return __n < 0 1033 1.3 mrg ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 1034 1.3 mrg : std::__complex_pow_unsigned(__z, __n); 1035 1.3 mrg } 1036 1.1 mrg 1037 1.1 mrg template<typename _Tp> 1038 1.1 mrg complex<_Tp> 1039 1.1 mrg pow(const complex<_Tp>& __x, const _Tp& __y) 1040 1.1 mrg { 1041 1.9 mrg #if ! _GLIBCXX_USE_C99_COMPLEX 1042 1.1 mrg if (__x == _Tp()) 1043 1.1 mrg return _Tp(); 1044 1.1 mrg #endif 1045 1.1 mrg if (__x.imag() == _Tp() && __x.real() > _Tp()) 1046 1.1 mrg return pow(__x.real(), __y); 1047 1.1 mrg 1048 1.1 mrg complex<_Tp> __t = std::log(__x); 1049 1.7 mrg return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); 1050 1.1 mrg } 1051 1.1 mrg 1052 1.1 mrg template<typename _Tp> 1053 1.1 mrg inline complex<_Tp> 1054 1.1 mrg __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1055 1.1 mrg { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 1056 1.1 mrg 1057 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 1058 1.1 mrg inline __complex__ float 1059 1.1 mrg __complex_pow(__complex__ float __x, __complex__ float __y) 1060 1.1 mrg { return __builtin_cpowf(__x, __y); } 1061 1.1 mrg 1062 1.1 mrg inline __complex__ double 1063 1.1 mrg __complex_pow(__complex__ double __x, __complex__ double __y) 1064 1.1 mrg { return __builtin_cpow(__x, __y); } 1065 1.1 mrg 1066 1.1 mrg inline __complex__ long double 1067 1.1 mrg __complex_pow(const __complex__ long double& __x, 1068 1.1 mrg const __complex__ long double& __y) 1069 1.1 mrg { return __builtin_cpowl(__x, __y); } 1070 1.1 mrg 1071 1.1 mrg template<typename _Tp> 1072 1.1 mrg inline complex<_Tp> 1073 1.1 mrg pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1074 1.1 mrg { return __complex_pow(__x.__rep(), __y.__rep()); } 1075 1.1 mrg #else 1076 1.1 mrg template<typename _Tp> 1077 1.1 mrg inline complex<_Tp> 1078 1.1 mrg pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1079 1.1 mrg { return __complex_pow(__x, __y); } 1080 1.1 mrg #endif 1081 1.1 mrg 1082 1.1 mrg template<typename _Tp> 1083 1.1 mrg inline complex<_Tp> 1084 1.1 mrg pow(const _Tp& __x, const complex<_Tp>& __y) 1085 1.1 mrg { 1086 1.7 mrg return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), 1087 1.7 mrg __y.imag() * log(__x)) 1088 1.1 mrg : std::pow(complex<_Tp>(__x), __y); 1089 1.1 mrg } 1090 1.1 mrg 1091 1.3 mrg /// 26.2.3 complex specializations 1092 1.3 mrg /// complex<float> specialization 1093 1.1 mrg template<> 1094 1.16 mrg class complex<float> 1095 1.1 mrg { 1096 1.16 mrg public: 1097 1.1 mrg typedef float value_type; 1098 1.1 mrg typedef __complex__ float _ComplexT; 1099 1.1 mrg 1100 1.3 mrg _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1101 1.1 mrg 1102 1.3 mrg _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 1103 1.3 mrg #if __cplusplus >= 201103L 1104 1.3 mrg : _M_value{ __r, __i } { } 1105 1.3 mrg #else 1106 1.1 mrg { 1107 1.1 mrg __real__ _M_value = __r; 1108 1.1 mrg __imag__ _M_value = __i; 1109 1.1 mrg } 1110 1.3 mrg #endif 1111 1.1 mrg 1112 1.16 mrg #if __cplusplus >= 201103L 1113 1.16 mrg _GLIBCXX14_CONSTEXPR complex(const complex&) = default; 1114 1.16 mrg #endif 1115 1.16 mrg 1116 1.3 mrg explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 1117 1.12 mrg explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1118 1.1 mrg 1119 1.3 mrg #if __cplusplus >= 201103L 1120 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1121 1.1 mrg // DR 387. std::complex over-encapsulated. 1122 1.3 mrg __attribute ((__abi_tag__ ("cxx11"))) 1123 1.12 mrg constexpr float 1124 1.7 mrg real() const { return __real__ _M_value; } 1125 1.3 mrg 1126 1.3 mrg __attribute ((__abi_tag__ ("cxx11"))) 1127 1.12 mrg constexpr float 1128 1.7 mrg imag() const { return __imag__ _M_value; } 1129 1.1 mrg #else 1130 1.12 mrg float& 1131 1.3 mrg real() { return __real__ _M_value; } 1132 1.1 mrg 1133 1.12 mrg const float& 1134 1.12 mrg real() const { return __real__ _M_value; } 1135 1.1 mrg 1136 1.12 mrg float& 1137 1.3 mrg imag() { return __imag__ _M_value; } 1138 1.1 mrg 1139 1.12 mrg const float& 1140 1.3 mrg imag() const { return __imag__ _M_value; } 1141 1.1 mrg #endif 1142 1.1 mrg 1143 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1144 1.1 mrg // DR 387. std::complex over-encapsulated. 1145 1.14 mrg _GLIBCXX20_CONSTEXPR void 1146 1.3 mrg real(float __val) { __real__ _M_value = __val; } 1147 1.1 mrg 1148 1.14 mrg _GLIBCXX20_CONSTEXPR void 1149 1.3 mrg imag(float __val) { __imag__ _M_value = __val; } 1150 1.1 mrg 1151 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1152 1.1 mrg operator=(float __f) 1153 1.1 mrg { 1154 1.3 mrg _M_value = __f; 1155 1.1 mrg return *this; 1156 1.1 mrg } 1157 1.1 mrg 1158 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1159 1.1 mrg operator+=(float __f) 1160 1.1 mrg { 1161 1.3 mrg _M_value += __f; 1162 1.1 mrg return *this; 1163 1.1 mrg } 1164 1.1 mrg 1165 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1166 1.1 mrg operator-=(float __f) 1167 1.1 mrg { 1168 1.3 mrg _M_value -= __f; 1169 1.1 mrg return *this; 1170 1.1 mrg } 1171 1.1 mrg 1172 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1173 1.1 mrg operator*=(float __f) 1174 1.1 mrg { 1175 1.1 mrg _M_value *= __f; 1176 1.1 mrg return *this; 1177 1.1 mrg } 1178 1.1 mrg 1179 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1180 1.1 mrg operator/=(float __f) 1181 1.1 mrg { 1182 1.1 mrg _M_value /= __f; 1183 1.1 mrg return *this; 1184 1.1 mrg } 1185 1.1 mrg 1186 1.1 mrg // Let the compiler synthesize the copy and assignment 1187 1.1 mrg // operator. It always does a pretty good job. 1188 1.14 mrg #if __cplusplus >= 201103L 1189 1.14 mrg _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; 1190 1.14 mrg #endif 1191 1.1 mrg 1192 1.1 mrg template<typename _Tp> 1193 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1194 1.1 mrg operator=(const complex<_Tp>& __z) 1195 1.1 mrg { 1196 1.1 mrg __real__ _M_value = __z.real(); 1197 1.1 mrg __imag__ _M_value = __z.imag(); 1198 1.1 mrg return *this; 1199 1.1 mrg } 1200 1.1 mrg 1201 1.1 mrg template<typename _Tp> 1202 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1203 1.1 mrg operator+=(const complex<_Tp>& __z) 1204 1.1 mrg { 1205 1.14 mrg _M_value += __z.__rep(); 1206 1.1 mrg return *this; 1207 1.1 mrg } 1208 1.1 mrg 1209 1.1 mrg template<class _Tp> 1210 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1211 1.1 mrg operator-=(const complex<_Tp>& __z) 1212 1.1 mrg { 1213 1.14 mrg _M_value -= __z.__rep(); 1214 1.1 mrg return *this; 1215 1.1 mrg } 1216 1.1 mrg 1217 1.1 mrg template<class _Tp> 1218 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1219 1.1 mrg operator*=(const complex<_Tp>& __z) 1220 1.1 mrg { 1221 1.14 mrg const _ComplexT __t = __z.__rep(); 1222 1.1 mrg _M_value *= __t; 1223 1.1 mrg return *this; 1224 1.1 mrg } 1225 1.1 mrg 1226 1.1 mrg template<class _Tp> 1227 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1228 1.1 mrg operator/=(const complex<_Tp>& __z) 1229 1.1 mrg { 1230 1.14 mrg const _ComplexT __t = __z.__rep(); 1231 1.1 mrg _M_value /= __t; 1232 1.1 mrg return *this; 1233 1.1 mrg } 1234 1.1 mrg 1235 1.7 mrg _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1236 1.1 mrg 1237 1.1 mrg private: 1238 1.1 mrg _ComplexT _M_value; 1239 1.1 mrg }; 1240 1.1 mrg 1241 1.3 mrg /// 26.2.3 complex specializations 1242 1.3 mrg /// complex<double> specialization 1243 1.1 mrg template<> 1244 1.16 mrg class complex<double> 1245 1.1 mrg { 1246 1.16 mrg public: 1247 1.1 mrg typedef double value_type; 1248 1.1 mrg typedef __complex__ double _ComplexT; 1249 1.1 mrg 1250 1.3 mrg _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1251 1.1 mrg 1252 1.3 mrg _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 1253 1.3 mrg #if __cplusplus >= 201103L 1254 1.3 mrg : _M_value{ __r, __i } { } 1255 1.3 mrg #else 1256 1.1 mrg { 1257 1.1 mrg __real__ _M_value = __r; 1258 1.1 mrg __imag__ _M_value = __i; 1259 1.1 mrg } 1260 1.3 mrg #endif 1261 1.1 mrg 1262 1.16 mrg #if __cplusplus >= 201103L 1263 1.16 mrg _GLIBCXX14_CONSTEXPR complex(const complex&) = default; 1264 1.16 mrg #endif 1265 1.16 mrg 1266 1.3 mrg _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1267 1.1 mrg : _M_value(__z.__rep()) { } 1268 1.1 mrg 1269 1.12 mrg explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1270 1.1 mrg 1271 1.3 mrg #if __cplusplus >= 201103L 1272 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1273 1.1 mrg // DR 387. std::complex over-encapsulated. 1274 1.3 mrg __attribute ((__abi_tag__ ("cxx11"))) 1275 1.12 mrg constexpr double 1276 1.7 mrg real() const { return __real__ _M_value; } 1277 1.3 mrg 1278 1.3 mrg __attribute ((__abi_tag__ ("cxx11"))) 1279 1.12 mrg constexpr double 1280 1.7 mrg imag() const { return __imag__ _M_value; } 1281 1.1 mrg #else 1282 1.12 mrg double& 1283 1.3 mrg real() { return __real__ _M_value; } 1284 1.1 mrg 1285 1.12 mrg const double& 1286 1.3 mrg real() const { return __real__ _M_value; } 1287 1.1 mrg 1288 1.12 mrg double& 1289 1.3 mrg imag() { return __imag__ _M_value; } 1290 1.1 mrg 1291 1.12 mrg const double& 1292 1.3 mrg imag() const { return __imag__ _M_value; } 1293 1.1 mrg #endif 1294 1.1 mrg 1295 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1296 1.1 mrg // DR 387. std::complex over-encapsulated. 1297 1.14 mrg _GLIBCXX20_CONSTEXPR void 1298 1.3 mrg real(double __val) { __real__ _M_value = __val; } 1299 1.1 mrg 1300 1.14 mrg _GLIBCXX20_CONSTEXPR void 1301 1.3 mrg imag(double __val) { __imag__ _M_value = __val; } 1302 1.1 mrg 1303 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1304 1.1 mrg operator=(double __d) 1305 1.1 mrg { 1306 1.3 mrg _M_value = __d; 1307 1.1 mrg return *this; 1308 1.1 mrg } 1309 1.1 mrg 1310 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1311 1.1 mrg operator+=(double __d) 1312 1.1 mrg { 1313 1.3 mrg _M_value += __d; 1314 1.1 mrg return *this; 1315 1.1 mrg } 1316 1.12 mrg 1317 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1318 1.1 mrg operator-=(double __d) 1319 1.1 mrg { 1320 1.3 mrg _M_value -= __d; 1321 1.1 mrg return *this; 1322 1.1 mrg } 1323 1.1 mrg 1324 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1325 1.1 mrg operator*=(double __d) 1326 1.1 mrg { 1327 1.1 mrg _M_value *= __d; 1328 1.1 mrg return *this; 1329 1.1 mrg } 1330 1.1 mrg 1331 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1332 1.1 mrg operator/=(double __d) 1333 1.1 mrg { 1334 1.1 mrg _M_value /= __d; 1335 1.1 mrg return *this; 1336 1.1 mrg } 1337 1.1 mrg 1338 1.1 mrg // The compiler will synthesize this, efficiently. 1339 1.14 mrg #if __cplusplus >= 201103L 1340 1.14 mrg _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; 1341 1.14 mrg #endif 1342 1.1 mrg 1343 1.1 mrg template<typename _Tp> 1344 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1345 1.1 mrg operator=(const complex<_Tp>& __z) 1346 1.1 mrg { 1347 1.14 mrg _M_value = __z.__rep(); 1348 1.1 mrg return *this; 1349 1.1 mrg } 1350 1.1 mrg 1351 1.1 mrg template<typename _Tp> 1352 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1353 1.1 mrg operator+=(const complex<_Tp>& __z) 1354 1.1 mrg { 1355 1.14 mrg _M_value += __z.__rep(); 1356 1.1 mrg return *this; 1357 1.1 mrg } 1358 1.1 mrg 1359 1.1 mrg template<typename _Tp> 1360 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1361 1.1 mrg operator-=(const complex<_Tp>& __z) 1362 1.1 mrg { 1363 1.14 mrg _M_value -= __z.__rep(); 1364 1.1 mrg return *this; 1365 1.1 mrg } 1366 1.1 mrg 1367 1.1 mrg template<typename _Tp> 1368 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1369 1.1 mrg operator*=(const complex<_Tp>& __z) 1370 1.1 mrg { 1371 1.14 mrg const _ComplexT __t = __z.__rep(); 1372 1.1 mrg _M_value *= __t; 1373 1.1 mrg return *this; 1374 1.1 mrg } 1375 1.1 mrg 1376 1.1 mrg template<typename _Tp> 1377 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1378 1.1 mrg operator/=(const complex<_Tp>& __z) 1379 1.1 mrg { 1380 1.14 mrg const _ComplexT __t = __z.__rep(); 1381 1.1 mrg _M_value /= __t; 1382 1.1 mrg return *this; 1383 1.1 mrg } 1384 1.1 mrg 1385 1.7 mrg _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1386 1.1 mrg 1387 1.1 mrg private: 1388 1.1 mrg _ComplexT _M_value; 1389 1.1 mrg }; 1390 1.1 mrg 1391 1.3 mrg /// 26.2.3 complex specializations 1392 1.3 mrg /// complex<long double> specialization 1393 1.1 mrg template<> 1394 1.16 mrg class complex<long double> 1395 1.1 mrg { 1396 1.16 mrg public: 1397 1.1 mrg typedef long double value_type; 1398 1.1 mrg typedef __complex__ long double _ComplexT; 1399 1.1 mrg 1400 1.3 mrg _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1401 1.1 mrg 1402 1.12 mrg _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 1403 1.3 mrg long double __i = 0.0L) 1404 1.3 mrg #if __cplusplus >= 201103L 1405 1.3 mrg : _M_value{ __r, __i } { } 1406 1.3 mrg #else 1407 1.1 mrg { 1408 1.1 mrg __real__ _M_value = __r; 1409 1.1 mrg __imag__ _M_value = __i; 1410 1.1 mrg } 1411 1.3 mrg #endif 1412 1.1 mrg 1413 1.16 mrg #if __cplusplus >= 201103L 1414 1.16 mrg _GLIBCXX14_CONSTEXPR complex(const complex&) = default; 1415 1.16 mrg #endif 1416 1.16 mrg 1417 1.3 mrg _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1418 1.1 mrg : _M_value(__z.__rep()) { } 1419 1.1 mrg 1420 1.3 mrg _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 1421 1.1 mrg : _M_value(__z.__rep()) { } 1422 1.1 mrg 1423 1.3 mrg #if __cplusplus >= 201103L 1424 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1425 1.1 mrg // DR 387. std::complex over-encapsulated. 1426 1.3 mrg __attribute ((__abi_tag__ ("cxx11"))) 1427 1.12 mrg constexpr long double 1428 1.7 mrg real() const { return __real__ _M_value; } 1429 1.3 mrg 1430 1.3 mrg __attribute ((__abi_tag__ ("cxx11"))) 1431 1.12 mrg constexpr long double 1432 1.7 mrg imag() const { return __imag__ _M_value; } 1433 1.1 mrg #else 1434 1.12 mrg long double& 1435 1.3 mrg real() { return __real__ _M_value; } 1436 1.1 mrg 1437 1.12 mrg const long double& 1438 1.3 mrg real() const { return __real__ _M_value; } 1439 1.1 mrg 1440 1.12 mrg long double& 1441 1.3 mrg imag() { return __imag__ _M_value; } 1442 1.1 mrg 1443 1.12 mrg const long double& 1444 1.3 mrg imag() const { return __imag__ _M_value; } 1445 1.1 mrg #endif 1446 1.1 mrg 1447 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1448 1.1 mrg // DR 387. std::complex over-encapsulated. 1449 1.14 mrg _GLIBCXX20_CONSTEXPR void 1450 1.3 mrg real(long double __val) { __real__ _M_value = __val; } 1451 1.1 mrg 1452 1.14 mrg _GLIBCXX20_CONSTEXPR void 1453 1.3 mrg imag(long double __val) { __imag__ _M_value = __val; } 1454 1.1 mrg 1455 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1456 1.1 mrg operator=(long double __r) 1457 1.1 mrg { 1458 1.3 mrg _M_value = __r; 1459 1.1 mrg return *this; 1460 1.1 mrg } 1461 1.1 mrg 1462 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1463 1.1 mrg operator+=(long double __r) 1464 1.1 mrg { 1465 1.3 mrg _M_value += __r; 1466 1.1 mrg return *this; 1467 1.1 mrg } 1468 1.1 mrg 1469 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1470 1.1 mrg operator-=(long double __r) 1471 1.1 mrg { 1472 1.3 mrg _M_value -= __r; 1473 1.1 mrg return *this; 1474 1.1 mrg } 1475 1.1 mrg 1476 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1477 1.1 mrg operator*=(long double __r) 1478 1.1 mrg { 1479 1.1 mrg _M_value *= __r; 1480 1.1 mrg return *this; 1481 1.1 mrg } 1482 1.1 mrg 1483 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1484 1.1 mrg operator/=(long double __r) 1485 1.1 mrg { 1486 1.1 mrg _M_value /= __r; 1487 1.1 mrg return *this; 1488 1.1 mrg } 1489 1.1 mrg 1490 1.1 mrg // The compiler knows how to do this efficiently 1491 1.14 mrg #if __cplusplus >= 201103L 1492 1.14 mrg _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; 1493 1.14 mrg #endif 1494 1.1 mrg 1495 1.1 mrg template<typename _Tp> 1496 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1497 1.1 mrg operator=(const complex<_Tp>& __z) 1498 1.1 mrg { 1499 1.14 mrg _M_value = __z.__rep(); 1500 1.1 mrg return *this; 1501 1.1 mrg } 1502 1.1 mrg 1503 1.1 mrg template<typename _Tp> 1504 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1505 1.1 mrg operator+=(const complex<_Tp>& __z) 1506 1.1 mrg { 1507 1.14 mrg _M_value += __z.__rep(); 1508 1.1 mrg return *this; 1509 1.1 mrg } 1510 1.1 mrg 1511 1.1 mrg template<typename _Tp> 1512 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1513 1.1 mrg operator-=(const complex<_Tp>& __z) 1514 1.1 mrg { 1515 1.14 mrg _M_value -= __z.__rep(); 1516 1.1 mrg return *this; 1517 1.1 mrg } 1518 1.1 mrg 1519 1.1 mrg template<typename _Tp> 1520 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1521 1.1 mrg operator*=(const complex<_Tp>& __z) 1522 1.1 mrg { 1523 1.14 mrg const _ComplexT __t = __z.__rep(); 1524 1.1 mrg _M_value *= __t; 1525 1.1 mrg return *this; 1526 1.1 mrg } 1527 1.1 mrg 1528 1.1 mrg template<typename _Tp> 1529 1.14 mrg _GLIBCXX20_CONSTEXPR complex& 1530 1.1 mrg operator/=(const complex<_Tp>& __z) 1531 1.1 mrg { 1532 1.14 mrg const _ComplexT __t = __z.__rep(); 1533 1.1 mrg _M_value /= __t; 1534 1.1 mrg return *this; 1535 1.1 mrg } 1536 1.1 mrg 1537 1.7 mrg _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1538 1.1 mrg 1539 1.1 mrg private: 1540 1.1 mrg _ComplexT _M_value; 1541 1.1 mrg }; 1542 1.1 mrg 1543 1.1 mrg // These bits have to be at the end of this file, so that the 1544 1.1 mrg // specializations have all been defined. 1545 1.3 mrg inline _GLIBCXX_CONSTEXPR 1546 1.1 mrg complex<float>::complex(const complex<double>& __z) 1547 1.1 mrg : _M_value(__z.__rep()) { } 1548 1.1 mrg 1549 1.3 mrg inline _GLIBCXX_CONSTEXPR 1550 1.1 mrg complex<float>::complex(const complex<long double>& __z) 1551 1.1 mrg : _M_value(__z.__rep()) { } 1552 1.1 mrg 1553 1.3 mrg inline _GLIBCXX_CONSTEXPR 1554 1.1 mrg complex<double>::complex(const complex<long double>& __z) 1555 1.1 mrg : _M_value(__z.__rep()) { } 1556 1.1 mrg 1557 1.1 mrg // Inhibit implicit instantiations for required instantiations, 1558 1.1 mrg // which are defined via explicit instantiations elsewhere. 1559 1.1 mrg // NB: This syntax is a GNU extension. 1560 1.1 mrg #if _GLIBCXX_EXTERN_TEMPLATE 1561 1.1 mrg extern template istream& operator>>(istream&, complex<float>&); 1562 1.1 mrg extern template ostream& operator<<(ostream&, const complex<float>&); 1563 1.1 mrg extern template istream& operator>>(istream&, complex<double>&); 1564 1.1 mrg extern template ostream& operator<<(ostream&, const complex<double>&); 1565 1.1 mrg extern template istream& operator>>(istream&, complex<long double>&); 1566 1.1 mrg extern template ostream& operator<<(ostream&, const complex<long double>&); 1567 1.1 mrg 1568 1.1 mrg #ifdef _GLIBCXX_USE_WCHAR_T 1569 1.1 mrg extern template wistream& operator>>(wistream&, complex<float>&); 1570 1.1 mrg extern template wostream& operator<<(wostream&, const complex<float>&); 1571 1.1 mrg extern template wistream& operator>>(wistream&, complex<double>&); 1572 1.1 mrg extern template wostream& operator<<(wostream&, const complex<double>&); 1573 1.1 mrg extern template wistream& operator>>(wistream&, complex<long double>&); 1574 1.1 mrg extern template wostream& operator<<(wostream&, const complex<long double>&); 1575 1.1 mrg #endif 1576 1.1 mrg #endif 1577 1.1 mrg 1578 1.15 mrg /// @} group complex_numbers 1579 1.1 mrg 1580 1.3 mrg _GLIBCXX_END_NAMESPACE_VERSION 1581 1.3 mrg } // namespace 1582 1.1 mrg 1583 1.3 mrg #if __cplusplus >= 201103L 1584 1.1 mrg 1585 1.3 mrg namespace std _GLIBCXX_VISIBILITY(default) 1586 1.3 mrg { 1587 1.3 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 1588 1.3 mrg 1589 1.3 mrg // Forward declarations. 1590 1.3 mrg template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 1591 1.3 mrg template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 1592 1.3 mrg template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 1593 1.3 mrg 1594 1.3 mrg template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 1595 1.3 mrg template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 1596 1.3 mrg template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 1597 1.3 mrg // DR 595. 1598 1.3 mrg template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 1599 1.3 mrg 1600 1.3 mrg template<typename _Tp> 1601 1.3 mrg inline std::complex<_Tp> 1602 1.3 mrg __complex_acos(const std::complex<_Tp>& __z) 1603 1.3 mrg { 1604 1.3 mrg const std::complex<_Tp> __t = std::asin(__z); 1605 1.3 mrg const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 1606 1.3 mrg return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 1607 1.3 mrg } 1608 1.3 mrg 1609 1.3 mrg #if _GLIBCXX_USE_C99_COMPLEX_TR1 1610 1.3 mrg inline __complex__ float 1611 1.3 mrg __complex_acos(__complex__ float __z) 1612 1.3 mrg { return __builtin_cacosf(__z); } 1613 1.3 mrg 1614 1.3 mrg inline __complex__ double 1615 1.3 mrg __complex_acos(__complex__ double __z) 1616 1.3 mrg { return __builtin_cacos(__z); } 1617 1.3 mrg 1618 1.3 mrg inline __complex__ long double 1619 1.3 mrg __complex_acos(const __complex__ long double& __z) 1620 1.3 mrg { return __builtin_cacosl(__z); } 1621 1.3 mrg 1622 1.3 mrg template<typename _Tp> 1623 1.3 mrg inline std::complex<_Tp> 1624 1.3 mrg acos(const std::complex<_Tp>& __z) 1625 1.3 mrg { return __complex_acos(__z.__rep()); } 1626 1.3 mrg #else 1627 1.3 mrg /// acos(__z) [8.1.2]. 1628 1.3 mrg // Effects: Behaves the same as C99 function cacos, defined 1629 1.3 mrg // in subclause 7.3.5.1. 1630 1.3 mrg template<typename _Tp> 1631 1.3 mrg inline std::complex<_Tp> 1632 1.3 mrg acos(const std::complex<_Tp>& __z) 1633 1.3 mrg { return __complex_acos(__z); } 1634 1.3 mrg #endif 1635 1.3 mrg 1636 1.3 mrg template<typename _Tp> 1637 1.3 mrg inline std::complex<_Tp> 1638 1.3 mrg __complex_asin(const std::complex<_Tp>& __z) 1639 1.3 mrg { 1640 1.3 mrg std::complex<_Tp> __t(-__z.imag(), __z.real()); 1641 1.3 mrg __t = std::asinh(__t); 1642 1.3 mrg return std::complex<_Tp>(__t.imag(), -__t.real()); 1643 1.3 mrg } 1644 1.3 mrg 1645 1.3 mrg #if _GLIBCXX_USE_C99_COMPLEX_TR1 1646 1.3 mrg inline __complex__ float 1647 1.3 mrg __complex_asin(__complex__ float __z) 1648 1.3 mrg { return __builtin_casinf(__z); } 1649 1.3 mrg 1650 1.3 mrg inline __complex__ double 1651 1.3 mrg __complex_asin(__complex__ double __z) 1652 1.3 mrg { return __builtin_casin(__z); } 1653 1.3 mrg 1654 1.3 mrg inline __complex__ long double 1655 1.3 mrg __complex_asin(const __complex__ long double& __z) 1656 1.3 mrg { return __builtin_casinl(__z); } 1657 1.3 mrg 1658 1.3 mrg template<typename _Tp> 1659 1.3 mrg inline std::complex<_Tp> 1660 1.3 mrg asin(const std::complex<_Tp>& __z) 1661 1.3 mrg { return __complex_asin(__z.__rep()); } 1662 1.3 mrg #else 1663 1.3 mrg /// asin(__z) [8.1.3]. 1664 1.3 mrg // Effects: Behaves the same as C99 function casin, defined 1665 1.3 mrg // in subclause 7.3.5.2. 1666 1.3 mrg template<typename _Tp> 1667 1.3 mrg inline std::complex<_Tp> 1668 1.3 mrg asin(const std::complex<_Tp>& __z) 1669 1.3 mrg { return __complex_asin(__z); } 1670 1.3 mrg #endif 1671 1.12 mrg 1672 1.3 mrg template<typename _Tp> 1673 1.3 mrg std::complex<_Tp> 1674 1.3 mrg __complex_atan(const std::complex<_Tp>& __z) 1675 1.3 mrg { 1676 1.3 mrg const _Tp __r2 = __z.real() * __z.real(); 1677 1.3 mrg const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 1678 1.3 mrg 1679 1.3 mrg _Tp __num = __z.imag() + _Tp(1.0); 1680 1.3 mrg _Tp __den = __z.imag() - _Tp(1.0); 1681 1.3 mrg 1682 1.3 mrg __num = __r2 + __num * __num; 1683 1.3 mrg __den = __r2 + __den * __den; 1684 1.3 mrg 1685 1.3 mrg return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 1686 1.3 mrg _Tp(0.25) * log(__num / __den)); 1687 1.3 mrg } 1688 1.3 mrg 1689 1.3 mrg #if _GLIBCXX_USE_C99_COMPLEX_TR1 1690 1.3 mrg inline __complex__ float 1691 1.3 mrg __complex_atan(__complex__ float __z) 1692 1.3 mrg { return __builtin_catanf(__z); } 1693 1.3 mrg 1694 1.3 mrg inline __complex__ double 1695 1.3 mrg __complex_atan(__complex__ double __z) 1696 1.3 mrg { return __builtin_catan(__z); } 1697 1.3 mrg 1698 1.3 mrg inline __complex__ long double 1699 1.3 mrg __complex_atan(const __complex__ long double& __z) 1700 1.3 mrg { return __builtin_catanl(__z); } 1701 1.3 mrg 1702 1.3 mrg template<typename _Tp> 1703 1.3 mrg inline std::complex<_Tp> 1704 1.3 mrg atan(const std::complex<_Tp>& __z) 1705 1.3 mrg { return __complex_atan(__z.__rep()); } 1706 1.3 mrg #else 1707 1.3 mrg /// atan(__z) [8.1.4]. 1708 1.3 mrg // Effects: Behaves the same as C99 function catan, defined 1709 1.3 mrg // in subclause 7.3.5.3. 1710 1.3 mrg template<typename _Tp> 1711 1.3 mrg inline std::complex<_Tp> 1712 1.3 mrg atan(const std::complex<_Tp>& __z) 1713 1.3 mrg { return __complex_atan(__z); } 1714 1.3 mrg #endif 1715 1.3 mrg 1716 1.3 mrg template<typename _Tp> 1717 1.3 mrg std::complex<_Tp> 1718 1.3 mrg __complex_acosh(const std::complex<_Tp>& __z) 1719 1.3 mrg { 1720 1.3 mrg // Kahan's formula. 1721 1.3 mrg return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 1722 1.3 mrg + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 1723 1.3 mrg } 1724 1.3 mrg 1725 1.3 mrg #if _GLIBCXX_USE_C99_COMPLEX_TR1 1726 1.3 mrg inline __complex__ float 1727 1.3 mrg __complex_acosh(__complex__ float __z) 1728 1.3 mrg { return __builtin_cacoshf(__z); } 1729 1.3 mrg 1730 1.3 mrg inline __complex__ double 1731 1.3 mrg __complex_acosh(__complex__ double __z) 1732 1.3 mrg { return __builtin_cacosh(__z); } 1733 1.3 mrg 1734 1.3 mrg inline __complex__ long double 1735 1.3 mrg __complex_acosh(const __complex__ long double& __z) 1736 1.3 mrg { return __builtin_cacoshl(__z); } 1737 1.3 mrg 1738 1.3 mrg template<typename _Tp> 1739 1.3 mrg inline std::complex<_Tp> 1740 1.3 mrg acosh(const std::complex<_Tp>& __z) 1741 1.3 mrg { return __complex_acosh(__z.__rep()); } 1742 1.3 mrg #else 1743 1.3 mrg /// acosh(__z) [8.1.5]. 1744 1.3 mrg // Effects: Behaves the same as C99 function cacosh, defined 1745 1.3 mrg // in subclause 7.3.6.1. 1746 1.3 mrg template<typename _Tp> 1747 1.3 mrg inline std::complex<_Tp> 1748 1.3 mrg acosh(const std::complex<_Tp>& __z) 1749 1.3 mrg { return __complex_acosh(__z); } 1750 1.3 mrg #endif 1751 1.3 mrg 1752 1.3 mrg template<typename _Tp> 1753 1.3 mrg std::complex<_Tp> 1754 1.3 mrg __complex_asinh(const std::complex<_Tp>& __z) 1755 1.3 mrg { 1756 1.3 mrg std::complex<_Tp> __t((__z.real() - __z.imag()) 1757 1.3 mrg * (__z.real() + __z.imag()) + _Tp(1.0), 1758 1.3 mrg _Tp(2.0) * __z.real() * __z.imag()); 1759 1.3 mrg __t = std::sqrt(__t); 1760 1.3 mrg 1761 1.3 mrg return std::log(__t + __z); 1762 1.3 mrg } 1763 1.3 mrg 1764 1.3 mrg #if _GLIBCXX_USE_C99_COMPLEX_TR1 1765 1.3 mrg inline __complex__ float 1766 1.3 mrg __complex_asinh(__complex__ float __z) 1767 1.3 mrg { return __builtin_casinhf(__z); } 1768 1.3 mrg 1769 1.3 mrg inline __complex__ double 1770 1.3 mrg __complex_asinh(__complex__ double __z) 1771 1.3 mrg { return __builtin_casinh(__z); } 1772 1.3 mrg 1773 1.3 mrg inline __complex__ long double 1774 1.3 mrg __complex_asinh(const __complex__ long double& __z) 1775 1.3 mrg { return __builtin_casinhl(__z); } 1776 1.3 mrg 1777 1.3 mrg template<typename _Tp> 1778 1.3 mrg inline std::complex<_Tp> 1779 1.3 mrg asinh(const std::complex<_Tp>& __z) 1780 1.3 mrg { return __complex_asinh(__z.__rep()); } 1781 1.3 mrg #else 1782 1.3 mrg /// asinh(__z) [8.1.6]. 1783 1.3 mrg // Effects: Behaves the same as C99 function casin, defined 1784 1.3 mrg // in subclause 7.3.6.2. 1785 1.3 mrg template<typename _Tp> 1786 1.3 mrg inline std::complex<_Tp> 1787 1.3 mrg asinh(const std::complex<_Tp>& __z) 1788 1.3 mrg { return __complex_asinh(__z); } 1789 1.3 mrg #endif 1790 1.3 mrg 1791 1.3 mrg template<typename _Tp> 1792 1.3 mrg std::complex<_Tp> 1793 1.3 mrg __complex_atanh(const std::complex<_Tp>& __z) 1794 1.3 mrg { 1795 1.3 mrg const _Tp __i2 = __z.imag() * __z.imag(); 1796 1.3 mrg const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 1797 1.3 mrg 1798 1.3 mrg _Tp __num = _Tp(1.0) + __z.real(); 1799 1.3 mrg _Tp __den = _Tp(1.0) - __z.real(); 1800 1.3 mrg 1801 1.3 mrg __num = __i2 + __num * __num; 1802 1.3 mrg __den = __i2 + __den * __den; 1803 1.3 mrg 1804 1.3 mrg return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 1805 1.3 mrg _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 1806 1.3 mrg } 1807 1.3 mrg 1808 1.3 mrg #if _GLIBCXX_USE_C99_COMPLEX_TR1 1809 1.3 mrg inline __complex__ float 1810 1.3 mrg __complex_atanh(__complex__ float __z) 1811 1.3 mrg { return __builtin_catanhf(__z); } 1812 1.3 mrg 1813 1.3 mrg inline __complex__ double 1814 1.3 mrg __complex_atanh(__complex__ double __z) 1815 1.3 mrg { return __builtin_catanh(__z); } 1816 1.3 mrg 1817 1.3 mrg inline __complex__ long double 1818 1.3 mrg __complex_atanh(const __complex__ long double& __z) 1819 1.3 mrg { return __builtin_catanhl(__z); } 1820 1.3 mrg 1821 1.3 mrg template<typename _Tp> 1822 1.3 mrg inline std::complex<_Tp> 1823 1.3 mrg atanh(const std::complex<_Tp>& __z) 1824 1.3 mrg { return __complex_atanh(__z.__rep()); } 1825 1.3 mrg #else 1826 1.3 mrg /// atanh(__z) [8.1.7]. 1827 1.3 mrg // Effects: Behaves the same as C99 function catanh, defined 1828 1.3 mrg // in subclause 7.3.6.3. 1829 1.3 mrg template<typename _Tp> 1830 1.3 mrg inline std::complex<_Tp> 1831 1.3 mrg atanh(const std::complex<_Tp>& __z) 1832 1.3 mrg { return __complex_atanh(__z); } 1833 1.3 mrg #endif 1834 1.3 mrg 1835 1.3 mrg template<typename _Tp> 1836 1.3 mrg inline _Tp 1837 1.3 mrg /// fabs(__z) [8.1.8]. 1838 1.3 mrg // Effects: Behaves the same as C99 function cabs, defined 1839 1.3 mrg // in subclause 7.3.8.1. 1840 1.3 mrg fabs(const std::complex<_Tp>& __z) 1841 1.3 mrg { return std::abs(__z); } 1842 1.3 mrg 1843 1.3 mrg /// Additional overloads [8.1.9]. 1844 1.3 mrg template<typename _Tp> 1845 1.3 mrg inline typename __gnu_cxx::__promote<_Tp>::__type 1846 1.3 mrg arg(_Tp __x) 1847 1.3 mrg { 1848 1.3 mrg typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1849 1.9 mrg #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 1850 1.3 mrg return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 1851 1.3 mrg : __type(); 1852 1.3 mrg #else 1853 1.3 mrg return std::arg(std::complex<__type>(__x)); 1854 1.3 mrg #endif 1855 1.3 mrg } 1856 1.3 mrg 1857 1.3 mrg template<typename _Tp> 1858 1.12 mrg _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1859 1.3 mrg imag(_Tp) 1860 1.3 mrg { return _Tp(); } 1861 1.3 mrg 1862 1.3 mrg template<typename _Tp> 1863 1.14 mrg _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1864 1.3 mrg norm(_Tp __x) 1865 1.3 mrg { 1866 1.3 mrg typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1867 1.3 mrg return __type(__x) * __type(__x); 1868 1.3 mrg } 1869 1.3 mrg 1870 1.3 mrg template<typename _Tp> 1871 1.12 mrg _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1872 1.3 mrg real(_Tp __x) 1873 1.3 mrg { return __x; } 1874 1.3 mrg 1875 1.3 mrg template<typename _Tp, typename _Up> 1876 1.3 mrg inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1877 1.3 mrg pow(const std::complex<_Tp>& __x, const _Up& __y) 1878 1.3 mrg { 1879 1.3 mrg typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1880 1.3 mrg return std::pow(std::complex<__type>(__x), __type(__y)); 1881 1.3 mrg } 1882 1.3 mrg 1883 1.3 mrg template<typename _Tp, typename _Up> 1884 1.3 mrg inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1885 1.3 mrg pow(const _Tp& __x, const std::complex<_Up>& __y) 1886 1.3 mrg { 1887 1.3 mrg typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1888 1.3 mrg return std::pow(__type(__x), std::complex<__type>(__y)); 1889 1.3 mrg } 1890 1.3 mrg 1891 1.3 mrg template<typename _Tp, typename _Up> 1892 1.3 mrg inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1893 1.3 mrg pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 1894 1.3 mrg { 1895 1.3 mrg typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1896 1.3 mrg return std::pow(std::complex<__type>(__x), 1897 1.3 mrg std::complex<__type>(__y)); 1898 1.3 mrg } 1899 1.1 mrg 1900 1.1 mrg // Forward declarations. 1901 1.1 mrg // DR 781. 1902 1.14 mrg template<typename _Tp> 1903 1.14 mrg std::complex<_Tp> proj(const std::complex<_Tp>&); 1904 1.1 mrg 1905 1.14 mrg // Generic implementation of std::proj, does not work for infinities. 1906 1.1 mrg template<typename _Tp> 1907 1.14 mrg inline std::complex<_Tp> 1908 1.1 mrg __complex_proj(const std::complex<_Tp>& __z) 1909 1.14 mrg { return __z; } 1910 1.1 mrg 1911 1.1 mrg #if _GLIBCXX_USE_C99_COMPLEX 1912 1.14 mrg inline complex<float> 1913 1.14 mrg __complex_proj(const complex<float>& __z) 1914 1.14 mrg { return __builtin_cprojf(__z.__rep()); } 1915 1.14 mrg 1916 1.14 mrg inline complex<double> 1917 1.14 mrg __complex_proj(const complex<double>& __z) 1918 1.14 mrg { return __builtin_cproj(__z.__rep()); } 1919 1.14 mrg 1920 1.14 mrg inline complex<long double> 1921 1.14 mrg __complex_proj(const complex<long double>& __z) 1922 1.14 mrg { return __builtin_cprojl(__z.__rep()); } 1923 1.14 mrg #elif defined _GLIBCXX_USE_C99_MATH_TR1 1924 1.14 mrg inline complex<float> 1925 1.14 mrg __complex_proj(const complex<float>& __z) 1926 1.14 mrg { 1927 1.14 mrg if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) 1928 1.14 mrg return complex<float>(__builtin_inff(), 1929 1.14 mrg __builtin_copysignf(0.0f, __z.imag())); 1930 1.14 mrg return __z; 1931 1.14 mrg } 1932 1.14 mrg 1933 1.14 mrg inline complex<double> 1934 1.14 mrg __complex_proj(const complex<double>& __z) 1935 1.14 mrg { 1936 1.14 mrg if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) 1937 1.14 mrg return complex<double>(__builtin_inf(), 1938 1.14 mrg __builtin_copysign(0.0, __z.imag())); 1939 1.14 mrg return __z; 1940 1.14 mrg } 1941 1.14 mrg 1942 1.14 mrg inline complex<long double> 1943 1.14 mrg __complex_proj(const complex<long double>& __z) 1944 1.14 mrg { 1945 1.14 mrg if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) 1946 1.14 mrg return complex<long double>(__builtin_infl(), 1947 1.14 mrg __builtin_copysignl(0.0l, __z.imag())); 1948 1.14 mrg return __z; 1949 1.14 mrg } 1950 1.14 mrg #endif 1951 1.1 mrg 1952 1.1 mrg template<typename _Tp> 1953 1.1 mrg inline std::complex<_Tp> 1954 1.1 mrg proj(const std::complex<_Tp>& __z) 1955 1.1 mrg { return __complex_proj(__z); } 1956 1.1 mrg 1957 1.14 mrg // Overload for scalars 1958 1.1 mrg template<typename _Tp> 1959 1.12 mrg inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1960 1.1 mrg proj(_Tp __x) 1961 1.12 mrg { 1962 1.12 mrg typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1963 1.12 mrg return std::proj(std::complex<__type>(__x)); 1964 1.12 mrg } 1965 1.1 mrg 1966 1.1 mrg template<typename _Tp> 1967 1.14 mrg inline _GLIBCXX20_CONSTEXPR 1968 1.14 mrg std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1969 1.1 mrg conj(_Tp __x) 1970 1.12 mrg { 1971 1.12 mrg typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1972 1.12 mrg return std::complex<__type>(__x, -__type()); 1973 1.12 mrg } 1974 1.1 mrg 1975 1.7 mrg #if __cplusplus > 201103L 1976 1.7 mrg 1977 1.7 mrg inline namespace literals { 1978 1.7 mrg inline namespace complex_literals { 1979 1.13 mrg #pragma GCC diagnostic push 1980 1.13 mrg #pragma GCC diagnostic ignored "-Wliteral-suffix" 1981 1.16 mrg #define __cpp_lib_complex_udls 201309L 1982 1.7 mrg 1983 1.7 mrg constexpr std::complex<float> 1984 1.7 mrg operator""if(long double __num) 1985 1.7 mrg { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1986 1.7 mrg 1987 1.7 mrg constexpr std::complex<float> 1988 1.7 mrg operator""if(unsigned long long __num) 1989 1.7 mrg { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1990 1.7 mrg 1991 1.7 mrg constexpr std::complex<double> 1992 1.7 mrg operator""i(long double __num) 1993 1.7 mrg { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1994 1.7 mrg 1995 1.7 mrg constexpr std::complex<double> 1996 1.7 mrg operator""i(unsigned long long __num) 1997 1.7 mrg { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1998 1.7 mrg 1999 1.7 mrg constexpr std::complex<long double> 2000 1.7 mrg operator""il(long double __num) 2001 1.7 mrg { return std::complex<long double>{0.0L, __num}; } 2002 1.7 mrg 2003 1.7 mrg constexpr std::complex<long double> 2004 1.7 mrg operator""il(unsigned long long __num) 2005 1.7 mrg { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } 2006 1.7 mrg 2007 1.13 mrg #pragma GCC diagnostic pop 2008 1.7 mrg } // inline namespace complex_literals 2009 1.7 mrg } // inline namespace literals 2010 1.7 mrg 2011 1.7 mrg #endif // C++14 2012 1.7 mrg 2013 1.13 mrg _GLIBCXX_END_NAMESPACE_VERSION 2014 1.3 mrg } // namespace 2015 1.1 mrg 2016 1.3 mrg #endif // C++11 2017 1.1 mrg 2018 1.3 mrg #endif /* _GLIBCXX_COMPLEX */ 2019