00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
#ifndef _GLIBCXX_COMPLEX
00044
#define _GLIBCXX_COMPLEX 1
00045
00046
#pragma GCC system_header
00047
00048
#include <bits/c++config.h>
00049
#include <bits/cpp_type_traits.h>
00050
#include <cmath>
00051
#include <sstream>
00052
00053
namespace std
00054 {
00055
00056
template<
typename _Tp>
class complex;
00057
template<>
class complex<float>;
00058
template<>
class complex<double>;
00059
template<>
class complex<long double>;
00060
00061
00062
template<
typename _Tp> _Tp
abs(
const complex<_Tp>&);
00063
00064
template<
typename _Tp> _Tp
arg(
const complex<_Tp>&);
00065
00066
template<
typename _Tp> _Tp
norm(
const complex<_Tp>&);
00067
00068
00069
template<
typename _Tp> complex<_Tp>
conj(
const complex<_Tp>&);
00070
00071
template<
typename _Tp> complex<_Tp>
polar(
const _Tp&,
const _Tp& = 0);
00072
00073
00074
00075
template<
typename _Tp> complex<_Tp>
cos(
const complex<_Tp>&);
00076
00077
template<
typename _Tp> complex<_Tp>
cosh(
const complex<_Tp>&);
00078
00079
template<
typename _Tp> complex<_Tp>
exp(
const complex<_Tp>&);
00080
00081
template<
typename _Tp> complex<_Tp>
log(
const complex<_Tp>&);
00082
00083
template<
typename _Tp> complex<_Tp>
log10(
const complex<_Tp>&);
00084
00085
template<
typename _Tp> complex<_Tp>
pow(
const complex<_Tp>&,
int);
00086
00087
template<
typename _Tp> complex<_Tp>
pow(
const complex<_Tp>&,
const _Tp&);
00088
00089
template<
typename _Tp> complex<_Tp>
pow(
const complex<_Tp>&,
00090
const complex<_Tp>&);
00091
00092
template<
typename _Tp> complex<_Tp>
pow(
const _Tp&,
const complex<_Tp>&);
00093
00094
template<
typename _Tp> complex<_Tp>
sin(
const complex<_Tp>&);
00095
00096
template<
typename _Tp> complex<_Tp>
sinh(
const complex<_Tp>&);
00097
00098
template<
typename _Tp> complex<_Tp>
sqrt(
const complex<_Tp>&);
00099
00100
template<
typename _Tp> complex<_Tp>
tan(
const complex<_Tp>&);
00101
00102
template<
typename _Tp> complex<_Tp>
tanh(
const complex<_Tp>&);
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
template<
typename _Tp>
00116 struct complex
00117 {
00118
00119 typedef _Tp
value_type;
00120
00121
00122
00123
complex(
const _Tp& = _Tp(),
const _Tp & = _Tp());
00124
00125
00126
00127
00128
template<
typename _Up>
00129
complex(
const complex<_Up>&);
00130
00131
00132 _Tp&
real();
00133
00134
const _Tp&
real() const;
00135
00136 _Tp& imag();
00137
00138 const _Tp& imag() const;
00139
00140
00141
complex<_Tp>& operator=(const _Tp&);
00142
00143
complex<_Tp>& operator+=(const _Tp&);
00144
00145
complex<_Tp>& operator-=(const _Tp&);
00146
00147
complex<_Tp>& operator*=(const _Tp&);
00148
00149
complex<_Tp>& operator/=(const _Tp&);
00150
00151
00152
00153
00154
00155 template<typename _Up>
00156
complex<_Tp>& operator=(const
complex<_Up>&);
00157
00158 template<typename _Up>
00159
complex<_Tp>& operator+=(const
complex<_Up>&);
00160
00161 template<typename _Up>
00162
complex<_Tp>& operator-=(const
complex<_Up>&);
00163
00164 template<typename _Up>
00165
complex<_Tp>& operator*=(const
complex<_Up>&);
00166
00167 template<typename _Up>
00168
complex<_Tp>& operator/=(const
complex<_Up>&);
00169
00170 const
complex& __rep() const;
00171
00172 private:
00173 _Tp _M_real;
00174 _Tp _M_imag;
00175 };
00176
00177 template<typename _Tp>
00178 inline _Tp&
00179 complex<_Tp>::real() {
return _M_real; }
00180
00181
template<
typename _Tp>
00182
inline const _Tp&
00183 complex<_Tp>::real()
const {
return _M_real; }
00184
00185
template<
typename _Tp>
00186
inline _Tp&
00187 complex<_Tp>::imag() {
return _M_imag; }
00188
00189
template<
typename _Tp>
00190
inline const _Tp&
00191 complex<_Tp>::imag()
const {
return _M_imag; }
00192
00193
template<
typename _Tp>
00194
inline
00195 complex<_Tp>::complex(
const _Tp& __r,
const _Tp& __i)
00196 : _M_real(__r), _M_imag(__i) { }
00197
00198
template<
typename _Tp>
00199
template<
typename _Up>
00200
inline
00201 complex<_Tp>::complex(
const complex<_Up>& __z)
00202 : _M_real(__z.real()), _M_imag(__z.imag()) { }
00203
00204
template<
typename _Tp>
00205
complex<_Tp>&
00206 complex<_Tp>::operator=(
const _Tp& __t)
00207 {
00208 _M_real = __t;
00209 _M_imag = _Tp();
00210
return *
this;
00211 }
00212
00213
00214
template<
typename _Tp>
00215
inline complex<_Tp>&
00216 complex<_Tp>::operator+=(
const _Tp& __t)
00217 {
00218 _M_real += __t;
00219
return *
this;
00220 }
00221
00222
00223
template<
typename _Tp>
00224
inline complex<_Tp>&
00225 complex<_Tp>::operator-=(
const _Tp& __t)
00226 {
00227 _M_real -= __t;
00228
return *
this;
00229 }
00230
00231
00232
template<
typename _Tp>
00233
complex<_Tp>&
00234 complex<_Tp>::operator*=(
const _Tp& __t)
00235 {
00236 _M_real *= __t;
00237 _M_imag *= __t;
00238
return *
this;
00239 }
00240
00241
00242
template<
typename _Tp>
00243
complex<_Tp>&
00244 complex<_Tp>::operator/=(
const _Tp& __t)
00245 {
00246 _M_real /= __t;
00247 _M_imag /= __t;
00248
return *
this;
00249 }
00250
00251
template<
typename _Tp>
00252
template<
typename _Up>
00253
complex<_Tp>&
00254 complex<_Tp>::operator=(
const complex<_Up>& __z)
00255 {
00256 _M_real = __z.
real();
00257 _M_imag = __z.
imag();
00258
return *
this;
00259 }
00260
00261
00262
template<
typename _Tp>
00263
template<
typename _Up>
00264
complex<_Tp>&
00265 complex<_Tp>::operator+=(
const complex<_Up>& __z)
00266 {
00267 _M_real += __z.
real();
00268 _M_imag += __z.
imag();
00269
return *
this;
00270 }
00271
00272
00273
template<
typename _Tp>
00274
template<
typename _Up>
00275
complex<_Tp>&
00276 complex<_Tp>::operator-=(
const complex<_Up>& __z)
00277 {
00278 _M_real -= __z.
real();
00279 _M_imag -= __z.
imag();
00280
return *
this;
00281 }
00282
00283
00284
00285
template<
typename _Tp>
00286
template<
typename _Up>
00287
complex<_Tp>&
00288 complex<_Tp>::operator*=(
const complex<_Up>& __z)
00289 {
00290
const _Tp __r = _M_real * __z.
real() - _M_imag * __z.
imag();
00291 _M_imag = _M_real * __z.
imag() + _M_imag * __z.
real();
00292 _M_real = __r;
00293
return *
this;
00294 }
00295
00296
00297
00298
template<
typename _Tp>
00299
template<
typename _Up>
00300
complex<_Tp>&
00301 complex<_Tp>::operator/=(
const complex<_Up>& __z)
00302 {
00303
const _Tp __r = _M_real * __z.
real() + _M_imag * __z.
imag();
00304
const _Tp __n =
std::norm(__z);
00305 _M_imag = (_M_imag * __z.
real() - _M_real * __z.
imag()) / __n;
00306 _M_real = __r / __n;
00307
return *
this;
00308 }
00309
00310
template<
typename _Tp>
00311
inline const complex<_Tp>&
00312
complex<_Tp>::__rep()
const {
return *
this; }
00313
00314
00315
00316
00317
template<
typename _Tp>
00318
inline complex<_Tp>
00319 operator+(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00320 {
00321
complex<_Tp> __r = __x;
00322 __r += __y;
00323
return __r;
00324 }
00325
00326
template<
typename _Tp>
00327
inline complex<_Tp>
00328 operator+(
const complex<_Tp>& __x,
const _Tp& __y)
00329 {
00330
complex<_Tp> __r = __x;
00331 __r.
real() += __y;
00332
return __r;
00333 }
00334
00335
template<
typename _Tp>
00336
inline complex<_Tp>
00337 operator+(
const _Tp& __x,
const complex<_Tp>& __y)
00338 {
00339
complex<_Tp> __r = __y;
00340 __r.
real() += __x;
00341
return __r;
00342 }
00343
00344
00345
00346
00347
template<
typename _Tp>
00348
inline complex<_Tp>
00349 operator-(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00350 {
00351
complex<_Tp> __r = __x;
00352 __r -= __y;
00353
return __r;
00354 }
00355
00356
template<
typename _Tp>
00357
inline complex<_Tp>
00358 operator-(
const complex<_Tp>& __x,
const _Tp& __y)
00359 {
00360
complex<_Tp> __r = __x;
00361 __r.
real() -= __y;
00362
return __r;
00363 }
00364
00365
template<
typename _Tp>
00366
inline complex<_Tp>
00367 operator-(
const _Tp& __x,
const complex<_Tp>& __y)
00368 {
00369
complex<_Tp> __r(__x, -__y.
imag());
00370 __r.
real() -= __y.
real();
00371
return __r;
00372 }
00373
00374
00375
00376
00377
template<
typename _Tp>
00378
inline complex<_Tp>
00379 operator*(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00380 {
00381
complex<_Tp> __r = __x;
00382 __r *= __y;
00383
return __r;
00384 }
00385
00386
template<
typename _Tp>
00387
inline complex<_Tp>
00388 operator*(
const complex<_Tp>& __x,
const _Tp& __y)
00389 {
00390
complex<_Tp> __r = __x;
00391 __r *= __y;
00392
return __r;
00393 }
00394
00395
template<
typename _Tp>
00396
inline complex<_Tp>
00397 operator*(
const _Tp& __x,
const complex<_Tp>& __y)
00398 {
00399
complex<_Tp> __r = __y;
00400 __r *= __x;
00401
return __r;
00402 }
00403
00404
00405
00406
00407
template<
typename _Tp>
00408
inline complex<_Tp>
00409 operator/(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00410 {
00411
complex<_Tp> __r = __x;
00412 __r /= __y;
00413
return __r;
00414 }
00415
00416
template<
typename _Tp>
00417
inline complex<_Tp>
00418 operator/(
const complex<_Tp>& __x,
const _Tp& __y)
00419 {
00420
complex<_Tp> __r = __x;
00421 __r /= __y;
00422
return __r;
00423 }
00424
00425
template<
typename _Tp>
00426
inline complex<_Tp>
00427 operator/(
const _Tp& __x,
const complex<_Tp>& __y)
00428 {
00429
complex<_Tp> __r = __x;
00430 __r /= __y;
00431
return __r;
00432 }
00433
00434
00435
00436
template<
typename _Tp>
00437
inline complex<_Tp>
00438 operator+(
const complex<_Tp>& __x)
00439 {
return __x; }
00440
00441
00442
template<
typename _Tp>
00443
inline complex<_Tp>
00444 operator-(
const complex<_Tp>& __x)
00445 {
return complex<_Tp>(-__x.
real(), -__x.
imag()); }
00446
00447
00448
00449
template<
typename _Tp>
00450
inline bool
00451 operator==(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00452 {
return __x.
real() == __y.
real() && __x.
imag() == __y.
imag(); }
00453
00454
template<
typename _Tp>
00455
inline bool
00456 operator==(
const complex<_Tp>& __x,
const _Tp& __y)
00457 {
return __x.
real() == __y && __x.
imag() == _Tp(); }
00458
00459
template<
typename _Tp>
00460
inline bool
00461 operator==(
const _Tp& __x,
const complex<_Tp>& __y)
00462 {
return __x == __y.
real() && _Tp() == __y.
imag(); }
00463
00464
00465
00466
00467
template<
typename _Tp>
00468
inline bool
00469 operator!=(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00470 {
return __x.
real() != __y.
real() || __x.
imag() != __y.
imag(); }
00471
00472
template<
typename _Tp>
00473
inline bool
00474 operator!=(
const complex<_Tp>& __x,
const _Tp& __y)
00475 {
return __x.
real() != __y || __x.
imag() != _Tp(); }
00476
00477
template<
typename _Tp>
00478
inline bool
00479 operator!=(
const _Tp& __x,
const complex<_Tp>& __y)
00480 {
return __x != __y.
real() || _Tp() != __y.
imag(); }
00481
00482
00483
00484
template<
typename _Tp,
typename _CharT,
class _Traits>
00485 basic_istream<_CharT, _Traits>&
00486 operator>>(
basic_istream<_CharT, _Traits>& __is,
complex<_Tp>& __x)
00487 {
00488 _Tp __re_x, __im_x;
00489 _CharT __ch;
00490 __is >> __ch;
00491
if (__ch ==
'(')
00492 {
00493 __is >> __re_x >> __ch;
00494
if (__ch ==
',')
00495 {
00496 __is >> __im_x >> __ch;
00497
if (__ch ==
')')
00498 __x =
complex<_Tp>(__re_x, __im_x);
00499
else
00500 __is.
setstate(ios_base::failbit);
00501 }
00502
else if (__ch ==
')')
00503 __x = __re_x;
00504
else
00505 __is.
setstate(ios_base::failbit);
00506 }
00507
else
00508 {
00509 __is.
putback(__ch);
00510 __is >> __re_x;
00511 __x = __re_x;
00512 }
00513
return __is;
00514 }
00515
00516
00517
template<
typename _Tp,
typename _CharT,
class _Traits>
00518 basic_ostream<_CharT, _Traits>&
00519 operator<<(basic_ostream<_CharT, _Traits>& __os,
const complex<_Tp>& __x)
00520 {
00521
basic_ostringstream<_CharT, _Traits> __s;
00522 __s.
flags(__os.flags());
00523 __s.
imbue(__os.getloc());
00524 __s.
precision(__os.precision());
00525 __s <<
'(' << __x.real() <<
',' << __x.imag() <<
')';
00526
return __os << __s.
str();
00527 }
00528
00529
00530
template<
typename _Tp>
00531
inline _Tp&
00532 real(complex<_Tp>& __z)
00533 {
return __z.real(); }
00534
00535
template<
typename _Tp>
00536
inline const _Tp&
00537 real(
const complex<_Tp>& __z)
00538 {
return __z.real(); }
00539
00540
template<
typename _Tp>
00541
inline _Tp&
00542 imag(complex<_Tp>& __z)
00543 {
return __z.imag(); }
00544
00545
template<
typename _Tp>
00546
inline const _Tp&
00547 imag(
const complex<_Tp>& __z)
00548 {
return __z.imag(); }
00549
00550
00551
template<
typename _Tp>
00552
inline _Tp
00553 __complex_abs(
const complex<_Tp>& __z)
00554 {
00555 _Tp __x = __z.real();
00556 _Tp __y = __z.imag();
00557
const _Tp __s =
std::max(
abs(__x),
abs(__y));
00558
if (__s == _Tp())
00559
return __s;
00560 __x /= __s;
00561 __y /= __s;
00562
return __s *
sqrt(__x * __x + __y * __y);
00563 }
00564
00565
inline float
00566 __complex_abs(__complex__
float __z) {
return __builtin_cabsf(__z); }
00567
00568
inline double
00569 __complex_abs(__complex__
double __z) {
return __builtin_cabs(__z); }
00570
00571
inline long double
00572 __complex_abs(
const __complex__
long double& __z)
00573 {
00574
return __builtin_cabsl(__z);
00575 }
00576
00577
template<
typename _Tp>
00578
inline _Tp
00579 abs(
const complex<_Tp>& __z) {
return __complex_abs(__z.
__rep()); }
00580
00581
00582
00583
template<
typename _Tp>
00584
inline _Tp
00585 __complex_arg(
const complex<_Tp>& __z)
00586 {
00587
return atan2(__z.imag(), __z.real());
00588 }
00589
00590
inline float
00591 __complex_arg(__complex__
float __z) {
return __builtin_cargf(__z); }
00592
00593
inline double
00594 __complex_arg(__complex__
double __z) {
return __builtin_carg(__z); }
00595
00596
inline long double
00597 __complex_arg(
const __complex__
long double& __z)
00598 {
return __builtin_cargl(__z); }
00599
00600
template<
typename _Tp>
00601
inline _Tp
00602 arg(
const complex<_Tp>& __z) {
return __complex_arg(__z.
__rep()); }
00603
00604
00605
00606
00607
00608
00609
template<
bool>
00610
struct _Norm_helper
00611 {
00612
template<
typename _Tp>
00613
static inline _Tp _S_do_it(
const complex<_Tp>& __z)
00614 {
00615
const _Tp __x = __z.real();
00616
const _Tp __y = __z.imag();
00617
return __x * __x + __y * __y;
00618 }
00619 };
00620
00621
template<>
00622
struct _Norm_helper<true>
00623 {
00624
template<
typename _Tp>
00625
static inline _Tp _S_do_it(
const complex<_Tp>& __z)
00626 {
00627 _Tp __res =
std::abs(__z);
00628
return __res * __res;
00629 }
00630 };
00631
00632
template<
typename _Tp>
00633
inline _Tp
00634 norm(
const complex<_Tp>& __z)
00635 {
00636
return _Norm_helper<__is_floating<_Tp>::_M_type && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
00637 }
00638
00639
template<
typename _Tp>
00640
inline complex<_Tp>
00641 polar(
const _Tp& __rho,
const _Tp& __theta)
00642 {
return complex<_Tp>(__rho *
cos(__theta), __rho *
sin(__theta)); }
00643
00644
template<
typename _Tp>
00645
inline complex<_Tp>
00646 conj(
const complex<_Tp>& __z)
00647 {
return complex<_Tp>(__z.
real(), -__z.
imag()); }
00648
00649
00650
00651
00652
template<
typename _Tp>
00653
inline complex<_Tp>
00654 __complex_cos(
const complex<_Tp>& __z)
00655 {
00656
const _Tp __x = __z.real();
00657
const _Tp __y = __z.imag();
00658
return complex<_Tp>(
cos(__x) *
cosh(__y), -
sin(__x) *
sinh(__y));
00659 }
00660
00661
inline __complex__
float
00662 __complex_cos(__complex__
float __z) {
return __builtin_ccosf(__z); }
00663
00664
inline __complex__
double
00665 __complex_cos(__complex__
double __z) {
return __builtin_ccos(__z); }
00666
00667
inline __complex__
long double
00668 __complex_cos(
const __complex__
long double& __z)
00669 {
return __builtin_ccosl(__z); }
00670
00671
template<
typename _Tp>
00672
inline complex<_Tp>
00673 cos(
const complex<_Tp>& __z) {
return __complex_cos(__z.
__rep()); }
00674
00675
00676
template<
typename _Tp>
00677
inline complex<_Tp>
00678 __complex_cosh(
const complex<_Tp>& __z)
00679 {
00680
const _Tp __x = __z.real();
00681
const _Tp __y = __z.imag();
00682
return complex<_Tp>(
cosh(__x) *
cos(__y),
sinh(__x) *
sin(__y));
00683 }
00684
00685
inline __complex__
float
00686 __complex_cosh(__complex__
float __z) {
return __builtin_ccoshf(__z); }
00687
00688
inline __complex__
double
00689 __complex_cosh(__complex__
double __z) {
return __builtin_ccosh(__z); }
00690
00691
inline __complex__
long double
00692 __complex_cosh(
const __complex__
long double& __z)
00693 {
return __builtin_ccoshl(__z); }
00694
00695
template<
typename _Tp>
00696
inline complex<_Tp>
00697 cosh(
const complex<_Tp>& __z) {
return __complex_cosh(__z.
__rep()); }
00698
00699
00700
template<
typename _Tp>
00701
inline complex<_Tp>
00702 __complex_exp(
const complex<_Tp>& __z)
00703 {
return std::polar(
exp(__z.real()), __z.imag()); }
00704
00705
inline __complex__
float
00706 __complex_exp(__complex__
float __z) {
return __builtin_cexpf(__z); }
00707
00708
inline __complex__
double
00709 __complex_exp(__complex__
double __z) {
return __builtin_cexp(__z); }
00710
00711
inline __complex__
long double
00712 __complex_exp(
const __complex__
long double& __z)
00713 {
return __builtin_cexpl(__z); }
00714
00715
template<
typename _Tp>
00716
inline complex<_Tp>
00717 exp(
const complex<_Tp>& __z) {
return __complex_exp(__z.
__rep()); }
00718
00719
00720
00721
template<
typename _Tp>
00722
inline complex<_Tp>
00723 __complex_log(
const complex<_Tp>& __z)
00724 {
return complex<_Tp>(
log(std::abs(__z)),
std::arg(__z)); }
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
template<
typename _Tp>
00741
inline complex<_Tp>
00742 log(
const complex<_Tp>& __z) {
return __complex_log(__z); }
00743
00744
template<
typename _Tp>
00745
inline complex<_Tp>
00746 log10(
const complex<_Tp>& __z)
00747 {
return std::log(__z) /
log(_Tp(10.0)); }
00748
00749
00750
template<
typename _Tp>
00751
inline complex<_Tp>
00752 __complex_sin(
const complex<_Tp>& __z)
00753 {
00754
const _Tp __x = __z.real();
00755
const _Tp __y = __z.imag();
00756
return complex<_Tp>(
sin(__x) *
cosh(__y),
cos(__x) *
sinh(__y));
00757 }
00758
00759
inline __complex__
float
00760 __complex_sin(__complex__
float __z) {
return __builtin_csinf(__z); }
00761
00762
inline __complex__
double
00763 __complex_sin(__complex__
double __z) {
return __builtin_csin(__z); }
00764
00765
inline __complex__
long double
00766 __complex_sin(
const __complex__
long double& __z)
00767 {
return __builtin_csinl(__z); }
00768
00769
template<
typename _Tp>
00770
inline complex<_Tp>
00771 sin(
const complex<_Tp>& __z) { __complex_sin(__z.
__rep()); }
00772
00773
00774
template<
typename _Tp>
00775
inline complex<_Tp>
00776 __complex_sinh(
const complex<_Tp>& __z)
00777 {
00778
const _Tp __x = __z.real();
00779
const _Tp __y = __z.imag();
00780
return complex<_Tp>(
sinh(__x) *
cos(__y),
cosh(__x) *
sin(__y));
00781 }
00782
00783
inline __complex__
float
00784 __complex_sinh(__complex__
float __z) {
return __builtin_csinhf(__z); }
00785
00786
inline __complex__
double
00787 __complex_sinh(__complex__
double __z) {
return __builtin_csinh(__z); }
00788
00789
inline __complex__
long double
00790 __complex_sinh(
const __complex__
long double& __z)
00791 {
return __builtin_csinhl(__z); }
00792
00793
template<
typename _Tp>
00794
inline complex<_Tp>
00795 sinh(
const complex<_Tp>& __z) {
return __complex_sinh(__z.
__rep()); }
00796
00797
00798
00799
template<
typename _Tp>
00800 complex<_Tp>
00801 __complex_sqrt(
const complex<_Tp>& __z)
00802 {
00803 _Tp __x = __z.real();
00804 _Tp __y = __z.imag();
00805
00806
if (__x == _Tp())
00807 {
00808 _Tp __t =
sqrt(
abs(__y) / 2);
00809
return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
00810 }
00811
else
00812 {
00813 _Tp __t =
sqrt(2 * (std::abs(__z) +
abs(__x)));
00814 _Tp __u = __t / 2;
00815
return __x > _Tp()
00816 ? complex<_Tp>(__u, __y / __t)
00817 : complex<_Tp>(
abs(__y) / __t, __y < _Tp() ? -__u : __u);
00818 }
00819 }
00820
00821
inline __complex__
float
00822 __complex_sqrt(__complex__
float __z) {
return __builtin_csqrtf(__z); }
00823
00824
inline __complex__
double
00825 __complex_sqrt(__complex__
double __z) {
return __builtin_csqrt(__z); }
00826
00827
inline __complex__
long double
00828 __complex_sqrt(
const __complex__
long double& __z)
00829 {
return __builtin_csqrtl(__z); }
00830
00831
template<
typename _Tp>
00832
inline complex<_Tp>
00833 sqrt(
const complex<_Tp>& __z) {
return __complex_sqrt(__z.
__rep()); }
00834
00835
00836
00837
template<
typename _Tp>
00838
inline complex<_Tp>
00839 __complex_tan(
const complex<_Tp>& __z)
00840 {
return std::sin(__z) /
std::cos(__z); }
00841
00842
inline __complex__
float
00843 __complex_tan(__complex__
float __z) {
return __builtin_ctanf(__z); }
00844
00845
inline __complex__
double
00846 __complex_tan(__complex__
double __z) {
return __builtin_ctan(__z); }
00847
00848
inline __complex__
long double
00849 __complex_tan(
const __complex__
long double& __z)
00850 {
return __builtin_ctanl(__z); }
00851
00852
template<
typename _Tp>
00853
inline complex<_Tp>
00854 tan(
const complex<_Tp>& __z) {
return __complex_tan(__z.
__rep()); }
00855
00856
00857
00858
template<
typename _Tp>
00859
inline complex<_Tp>
00860 __complex_tanh(
const complex<_Tp>& __z)
00861 {
return std::sinh(__z) /
std::cosh(__z); }
00862
00863
inline __complex__
float
00864 __complex_tanh(__complex__
float __z) {
return __builtin_ctanhf(__z); }
00865
00866
inline __complex__
double
00867 __complex_tanh(__complex__
double __z) {
return __builtin_ctanh(__z); }
00868
00869
inline __complex__
long double
00870 __complex_tanh(
const __complex__
long double& __z)
00871 {
return __builtin_ctanhl(__z); }
00872
00873
template<
typename _Tp>
00874
inline complex<_Tp>
00875 tanh(
const complex<_Tp>& __z) {
return __complex_tanh(__z.
__rep()); }
00876
00877
00878
00879
00880
template<
typename _Tp>
00881
inline complex<_Tp>
00882 pow(
const complex<_Tp>& __z,
int __n)
00883 {
00884
return std::__pow_helper(__z, __n);
00885 }
00886
00887
template<
typename _Tp>
00888 complex<_Tp>
00889 pow(
const complex<_Tp>& __x,
const _Tp& __y)
00890 {
00891
if (__x.
imag() == _Tp() && __x.
real() > _Tp())
00892
return pow(__x.
real(), __y);
00893
00894
complex<_Tp> __t =
std::log(__x);
00895
return std::polar(
exp(__y * __t.
real()), __y * __t.
imag());
00896 }
00897
00898
template<
typename _Tp>
00899
inline complex<_Tp>
00900 __complex_pow(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00901 {
return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
00902
00903
inline __complex__
float
00904 __complex_pow(__complex__
float __x, __complex__
float __y)
00905 {
return __builtin_cpowf(__x, __y); }
00906
00907
inline __complex__
double
00908 __complex_pow(__complex__
double __x, __complex__
double __y)
00909 {
return __builtin_cpow(__x, __y); }
00910
00911
inline __complex__
long double
00912 __complex_pow(__complex__
long double& __x, __complex__
long double& __y)
00913 {
return __builtin_cpowl(__x, __y); }
00914
00915
template<
typename _Tp>
00916
inline complex<_Tp>
00917 pow(
const complex<_Tp>& __x,
const complex<_Tp>& __y)
00918 {
return __complex_pow(__x, __y); }
00919
00920
template<
typename _Tp>
00921
inline complex<_Tp>
00922 pow(
const _Tp& __x,
const complex<_Tp>& __y)
00923 {
00924
return __x > _Tp() ?
std::polar(
pow(__x, __y.
real()),
00925 __y.
imag() *
log(__x))
00926 :
std::pow(
complex<_Tp>(__x, _Tp()), __y);
00927 }
00928
00929
00930
00931
template<>
00932
struct complex<float>
00933 {
00934
typedef float value_type;
00935
typedef __complex__
float _ComplexT;
00936
00937 complex(_ComplexT __z) : _M_value(__z) { }
00938
00939
complex(
float = 0.0f,
float = 0.0f);
00940
#ifdef _GLIBCXX_BUGGY_COMPLEX
00941
complex(
const complex& __z) : _M_value(__z._M_value) { }
00942
#endif
00943
explicit complex(
const complex<double>&);
00944
explicit complex(
const complex<long double>&);
00945
00946
float&
real();
00947
const float&
real() const;
00948
float& imag();
00949 const
float& imag() const;
00950
00951 complex<
float>& operator=(
float);
00952 complex<
float>& operator+=(
float);
00953 complex<
float>& operator-=(
float);
00954 complex<
float>& operator*=(
float);
00955 complex<
float>& operator/=(
float);
00956
00957
00958
00959
00960 template<typename _Tp>
00961 complex<
float>&operator=(const complex<_Tp>&);
00962 template<typename _Tp>
00963 complex<
float>& operator+=(const complex<_Tp>&);
00964 template<class _Tp>
00965 complex<
float>& operator-=(const complex<_Tp>&);
00966 template<class _Tp>
00967 complex<
float>& operator*=(const complex<_Tp>&);
00968 template<class _Tp>
00969 complex<
float>&operator/=(const complex<_Tp>&);
00970
00971 const _ComplexT& __rep()
const {
return _M_value; }
00972
00973
private:
00974 _ComplexT _M_value;
00975 };
00976
00977
inline float&
00978 complex<float>::real()
00979 {
return __real__ _M_value; }
00980
00981
inline const float&
00982 complex<float>::real()
const
00983
{
return __real__ _M_value; }
00984
00985
inline float&
00986 complex<float>::imag()
00987 {
return __imag__ _M_value; }
00988
00989
inline const float&
00990 complex<float>::imag()
const
00991
{
return __imag__ _M_value; }
00992
00993
inline
00994 complex<float>::complex(
float r,
float i)
00995 {
00996 __real__ _M_value = r;
00997 __imag__ _M_value = i;
00998 }
00999
01000
inline complex<float>&
01001 complex<float>::operator=(
float __f)
01002 {
01003 __real__ _M_value = __f;
01004 __imag__ _M_value = 0.0f;
01005
return *
this;
01006 }
01007
01008
inline complex<float>&
01009 complex<float>::operator+=(
float __f)
01010 {
01011 __real__ _M_value += __f;
01012
return *
this;
01013 }
01014
01015
inline complex<float>&
01016 complex<float>::operator-=(
float __f)
01017 {
01018 __real__ _M_value -= __f;
01019
return *
this;
01020 }
01021
01022
inline complex<float>&
01023 complex<float>::operator*=(
float __f)
01024 {
01025 _M_value *= __f;
01026
return *
this;
01027 }
01028
01029
inline complex<float>&
01030 complex<float>::operator/=(
float __f)
01031 {
01032 _M_value /= __f;
01033
return *
this;
01034 }
01035
01036
template<
typename _Tp>
01037
inline complex<float>&
01038 complex<float>::operator=(
const complex<_Tp>& __z)
01039 {
01040 __real__ _M_value = __z.real();
01041 __imag__ _M_value = __z.imag();
01042
return *
this;
01043 }
01044
01045
template<
typename _Tp>
01046
inline complex<float>&
01047 complex<float>::operator+=(
const complex<_Tp>& __z)
01048 {
01049 __real__ _M_value += __z.real();
01050 __imag__ _M_value += __z.imag();
01051
return *
this;
01052 }
01053
01054
template<
typename _Tp>
01055
inline complex<float>&
01056 complex<float>::operator-=(
const complex<_Tp>& __z)
01057 {
01058 __real__ _M_value -= __z.real();
01059 __imag__ _M_value -= __z.imag();
01060
return *
this;
01061 }
01062
01063
template<
typename _Tp>
01064
inline complex<float>&
01065 complex<float>::operator*=(
const complex<_Tp>& __z)
01066 {
01067 _ComplexT __t;
01068 __real__ __t = __z.real();
01069 __imag__ __t = __z.imag();
01070 _M_value *= __t;
01071
return *
this;
01072 }
01073
01074
template<
typename _Tp>
01075
inline complex<float>&
01076 complex<float>::operator/=(
const complex<_Tp>& __z)
01077 {
01078 _ComplexT __t;
01079 __real__ __t = __z.real();
01080 __imag__ __t = __z.imag();
01081 _M_value /= __t;
01082
return *
this;
01083 }
01084
01085
01086
01087
template<>
01088
struct complex<double>
01089 {
01090
typedef double value_type;
01091
typedef __complex__
double _ComplexT;
01092
01093 complex(_ComplexT __z) : _M_value(__z) { }
01094
01095 complex(
double = 0.0,
double = 0.0);
01096
#ifdef _GLIBCXX_BUGGY_COMPLEX
01097
complex(
const complex& __z) : _M_value(__z._M_value) { }
01098
#endif
01099
complex(
const complex<float>&);
01100
explicit complex(
const complex<long double>&);
01101
01102
double& real();
01103
const double& real() const;
01104
double& imag();
01105 const
double& imag() const;
01106
01107 complex<
double>& operator=(
double);
01108 complex<
double>& operator+=(
double);
01109 complex<
double>& operator-=(
double);
01110 complex<
double>& operator*=(
double);
01111 complex<
double>& operator/=(
double);
01112
01113
01114
01115 template<typename _Tp>
01116 complex<
double>& operator=(const complex<_Tp>&);
01117 template<typename _Tp>
01118 complex<
double>& operator+=(const complex<_Tp>&);
01119 template<typename _Tp>
01120 complex<
double>& operator-=(const complex<_Tp>&);
01121 template<typename _Tp>
01122 complex<
double>& operator*=(const complex<_Tp>&);
01123 template<typename _Tp>
01124 complex<
double>& operator/=(const complex<_Tp>&);
01125
01126 const _ComplexT& __rep()
const {
return _M_value; }
01127
01128
private:
01129 _ComplexT _M_value;
01130 };
01131
01132
inline double&
01133 complex<double>::real()
01134 {
return __real__ _M_value; }
01135
01136
inline const double&
01137 complex<double>::real()
const
01138
{
return __real__ _M_value; }
01139
01140
inline double&
01141 complex<double>::imag()
01142 {
return __imag__ _M_value; }
01143
01144
inline const double&
01145 complex<double>::imag()
const
01146
{
return __imag__ _M_value; }
01147
01148
inline
01149 complex<double>::complex(
double __r,
double __i)
01150 {
01151 __real__ _M_value = __r;
01152 __imag__ _M_value = __i;
01153 }
01154
01155
inline complex<double>&
01156 complex<double>::operator=(
double __d)
01157 {
01158 __real__ _M_value = __d;
01159 __imag__ _M_value = 0.0;
01160
return *
this;
01161 }
01162
01163
inline complex<double>&
01164 complex<double>::operator+=(
double __d)
01165 {
01166 __real__ _M_value += __d;
01167
return *
this;
01168 }
01169
01170
inline complex<double>&
01171 complex<double>::operator-=(
double __d)
01172 {
01173 __real__ _M_value -= __d;
01174
return *
this;
01175 }
01176
01177
inline complex<double>&
01178 complex<double>::operator*=(
double __d)
01179 {
01180 _M_value *= __d;
01181
return *
this;
01182 }
01183
01184
inline complex<double>&
01185 complex<double>::operator/=(
double __d)
01186 {
01187 _M_value /= __d;
01188
return *
this;
01189 }
01190
01191
template<
typename _Tp>
01192
inline complex<double>&
01193 complex<double>::operator=(
const complex<_Tp>& __z)
01194 {
01195 __real__ _M_value = __z.real();
01196 __imag__ _M_value = __z.imag();
01197
return *
this;
01198 }
01199
01200
template<
typename _Tp>
01201
inline complex<double>&
01202 complex<double>::operator+=(
const complex<_Tp>& __z)
01203 {
01204 __real__ _M_value += __z.real();
01205 __imag__ _M_value += __z.imag();
01206
return *
this;
01207 }
01208
01209
template<
typename _Tp>
01210
inline complex<double>&
01211 complex<double>::operator-=(
const complex<_Tp>& __z)
01212 {
01213 __real__ _M_value -= __z.real();
01214 __imag__ _M_value -= __z.imag();
01215
return *
this;
01216 }
01217
01218
template<
typename _Tp>
01219
inline complex<double>&
01220 complex<double>::operator*=(
const complex<_Tp>& __z)
01221 {
01222 _ComplexT __t;
01223 __real__ __t = __z.real();
01224 __imag__ __t = __z.imag();
01225 _M_value *= __t;
01226
return *
this;
01227 }
01228
01229
template<
typename _Tp>
01230
inline complex<double>&
01231 complex<double>::operator/=(
const complex<_Tp>& __z)
01232 {
01233 _ComplexT __t;
01234 __real__ __t = __z.real();
01235 __imag__ __t = __z.imag();
01236 _M_value /= __t;
01237
return *
this;
01238 }
01239
01240
01241
01242
template<>
01243
struct complex<long double>
01244 {
01245
typedef long double value_type;
01246
typedef __complex__
long double _ComplexT;
01247
01248 complex(_ComplexT __z) : _M_value(__z) { }
01249
01250 complex(
long double = 0.0L,
long double = 0.0L);
01251
#ifdef _GLIBCXX_BUGGY_COMPLEX
01252
complex(
const complex& __z) : _M_value(__z._M_value) { }
01253
#endif
01254
complex(
const complex<float>&);
01255 complex(
const complex<double>&);
01256
01257
long double& real();
01258
const long double& real() const;
01259
long double& imag();
01260 const
long double& imag() const;
01261
01262 complex<
long double>& operator= (
long double);
01263 complex<
long double>& operator+= (
long double);
01264 complex<
long double>& operator-= (
long double);
01265 complex<
long double>& operator*= (
long double);
01266 complex<
long double>& operator/= (
long double);
01267
01268
01269
01270 template<typename _Tp>
01271 complex<
long double>& operator=(const complex<_Tp>&);
01272 template<typename _Tp>
01273 complex<
long double>& operator+=(const complex<_Tp>&);
01274 template<typename _Tp>
01275 complex<
long double>& operator-=(const complex<_Tp>&);
01276 template<typename _Tp>
01277 complex<
long double>& operator*=(const complex<_Tp>&);
01278 template<typename _Tp>
01279 complex<
long double>& operator/=(const complex<_Tp>&);
01280
01281 const _ComplexT& __rep()
const {
return _M_value; }
01282
01283
private:
01284 _ComplexT _M_value;
01285 };
01286
01287
inline
01288 complex<long double>::complex(
long double __r,
long double __i)
01289 {
01290 __real__ _M_value = __r;
01291 __imag__ _M_value = __i;
01292 }
01293
01294
inline long double&
01295 complex<long double>::real()
01296 {
return __real__ _M_value; }
01297
01298
inline const long double&
01299 complex<long double>::real()
const
01300
{
return __real__ _M_value; }
01301
01302
inline long double&
01303 complex<long double>::imag()
01304 {
return __imag__ _M_value; }
01305
01306
inline const long double&
01307 complex<long double>::imag()
const
01308
{
return __imag__ _M_value; }
01309
01310
inline complex<long double>&
01311 complex<long double>::operator=(
long double __r)
01312 {
01313 __real__ _M_value = __r;
01314 __imag__ _M_value = 0.0L;
01315
return *
this;
01316 }
01317
01318
inline complex<long double>&
01319 complex<long double>::operator+=(
long double __r)
01320 {
01321 __real__ _M_value += __r;
01322
return *
this;
01323 }
01324
01325
inline complex<long double>&
01326 complex<long double>::operator-=(
long double __r)
01327 {
01328 __real__ _M_value -= __r;
01329
return *
this;
01330 }
01331
01332
inline complex<long double>&
01333 complex<long double>::operator*=(
long double __r)
01334 {
01335 _M_value *= __r;
01336
return *
this;
01337 }
01338
01339
inline complex<long double>&
01340 complex<long double>::operator/=(
long double __r)
01341 {
01342 _M_value /= __r;
01343
return *
this;
01344 }
01345
01346
template<
typename _Tp>
01347
inline complex<long double>&
01348 complex<long double>::operator=(
const complex<_Tp>& __z)
01349 {
01350 __real__ _M_value = __z.real();
01351 __imag__ _M_value = __z.imag();
01352
return *
this;
01353 }
01354
01355
template<
typename _Tp>
01356
inline complex<long double>&
01357 complex<long double>::operator+=(
const complex<_Tp>& __z)
01358 {
01359 __real__ _M_value += __z.real();
01360 __imag__ _M_value += __z.imag();
01361
return *
this;
01362 }
01363
01364
template<
typename _Tp>
01365
inline complex<long double>&
01366 complex<long double>::operator-=(
const complex<_Tp>& __z)
01367 {
01368 __real__ _M_value -= __z.real();
01369 __imag__ _M_value -= __z.imag();
01370
return *
this;
01371 }
01372
01373
template<
typename _Tp>
01374
inline complex<long double>&
01375 complex<long double>::operator*=(
const complex<_Tp>& __z)
01376 {
01377 _ComplexT __t;
01378 __real__ __t = __z.real();
01379 __imag__ __t = __z.imag();
01380 _M_value *= __t;
01381
return *
this;
01382 }
01383
01384
template<
typename _Tp>
01385
inline complex<long double>&
01386 complex<long double>::operator/=(
const complex<_Tp>& __z)
01387 {
01388 _ComplexT __t;
01389 __real__ __t = __z.real();
01390 __imag__ __t = __z.imag();
01391 _M_value /= __t;
01392
return *
this;
01393 }
01394
01395
01396
01397
01398
01399
inline
01400 complex<float>::complex(
const complex<double>& __z)
01401 : _M_value(__z.__rep()) { }
01402
01403
inline
01404 complex<float>::complex(
const complex<long double>& __z)
01405 : _M_value(__z.__rep()) { }
01406
01407
inline
01408 complex<double>::complex(
const complex<float>& __z)
01409 : _M_value(__z.__rep()) { }
01410
01411
inline
01412 complex<double>::complex(
const complex<long double>& __z)
01413 : _M_value(__z.__rep()) { }
01414
01415
inline
01416 complex<long double>::complex(
const complex<float>& __z)
01417 : _M_value(__z.__rep()) { }
01418
01419
inline
01420 complex<long double>::complex(
const complex<double>& __z)
01421 : _M_value(__z.__rep()) { }
01422 }
01423
01424
#endif