blob: 273a807583b62bc032fcecb5ea6999156116c1bc [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- complex ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_COMPLEX
12#define _LIBCPP_COMPLEX
13
14/*
15 complex synopsis
16
17namespace std
18{
19
20template<class T>
21class complex
22{
23public:
24 typedef T value_type;
25
26 complex(const T& re = T(), const T& im = T());
27 complex(const complex&);
28 template<class X> complex(const complex<X>&);
29
30 T real() const;
31 T imag() const;
32
33 void real(T);
34 void imag(T);
35
36 complex<T>& operator= (const T&);
37 complex<T>& operator+=(const T&);
38 complex<T>& operator-=(const T&);
39 complex<T>& operator*=(const T&);
40 complex<T>& operator/=(const T&);
41
42 complex& operator=(const complex&);
43 template<class X> complex<T>& operator= (const complex<X>&);
44 template<class X> complex<T>& operator+=(const complex<X>&);
45 template<class X> complex<T>& operator-=(const complex<X>&);
46 template<class X> complex<T>& operator*=(const complex<X>&);
47 template<class X> complex<T>& operator/=(const complex<X>&);
48};
49
50template<>
51class complex<float>
Howard Hinnant324bb032010-08-22 00:02:43 +000052{
53public:
54 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055
Howard Hinnant324bb032010-08-22 00:02:43 +000056 constexpr complex(float re = 0.0f, float im = 0.0f);
57 explicit constexpr complex(const complex<double>&);
58 explicit constexpr complex(const complex<long double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059
Howard Hinnant324bb032010-08-22 00:02:43 +000060 constexpr float real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061 void real(float);
Howard Hinnant324bb032010-08-22 00:02:43 +000062 constexpr float imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 void imag(float);
64
Howard Hinnant324bb032010-08-22 00:02:43 +000065 complex<float>& operator= (float);
66 complex<float>& operator+=(float);
67 complex<float>& operator-=(float);
68 complex<float>& operator*=(float);
69 complex<float>& operator/=(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070
Howard Hinnant324bb032010-08-22 00:02:43 +000071 complex<float>& operator=(const complex<float>&);
72 template<class X> complex<float>& operator= (const complex<X>&);
73 template<class X> complex<float>& operator+=(const complex<X>&);
74 template<class X> complex<float>& operator-=(const complex<X>&);
75 template<class X> complex<float>& operator*=(const complex<X>&);
76 template<class X> complex<float>& operator/=(const complex<X>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077};
78
79template<>
80class complex<double>
Howard Hinnant324bb032010-08-22 00:02:43 +000081{
82public:
83 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
Howard Hinnant324bb032010-08-22 00:02:43 +000085 constexpr complex(double re = 0.0, double im = 0.0);
86 constexpr complex(const complex<float>&);
87 explicit constexpr complex(const complex<long double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088
Howard Hinnant324bb032010-08-22 00:02:43 +000089 constexpr double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000090 void real(double);
Howard Hinnant324bb032010-08-22 00:02:43 +000091 constexpr double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 void imag(double);
93
Howard Hinnant324bb032010-08-22 00:02:43 +000094 complex<double>& operator= (double);
95 complex<double>& operator+=(double);
96 complex<double>& operator-=(double);
97 complex<double>& operator*=(double);
98 complex<double>& operator/=(double);
99 complex<double>& operator=(const complex<double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100
Howard Hinnant324bb032010-08-22 00:02:43 +0000101 template<class X> complex<double>& operator= (const complex<X>&);
102 template<class X> complex<double>& operator+=(const complex<X>&);
103 template<class X> complex<double>& operator-=(const complex<X>&);
104 template<class X> complex<double>& operator*=(const complex<X>&);
105 template<class X> complex<double>& operator/=(const complex<X>&);
106};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107
108template<>
109class complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000110{
111public:
112 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113
Howard Hinnant324bb032010-08-22 00:02:43 +0000114 constexpr complex(long double re = 0.0L, long double im = 0.0L);
115 constexpr complex(const complex<float>&);
116 constexpr complex(const complex<double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
Howard Hinnant324bb032010-08-22 00:02:43 +0000118 constexpr long double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119 void real(long double);
Howard Hinnant324bb032010-08-22 00:02:43 +0000120 constexpr long double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121 void imag(long double);
122
Howard Hinnant324bb032010-08-22 00:02:43 +0000123 complex<long double>& operator=(const complex<long double>&);
124 complex<long double>& operator= (long double);
125 complex<long double>& operator+=(long double);
126 complex<long double>& operator-=(long double);
127 complex<long double>& operator*=(long double);
128 complex<long double>& operator/=(long double);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129
Howard Hinnant324bb032010-08-22 00:02:43 +0000130 template<class X> complex<long double>& operator= (const complex<X>&);
131 template<class X> complex<long double>& operator+=(const complex<X>&);
132 template<class X> complex<long double>& operator-=(const complex<X>&);
133 template<class X> complex<long double>& operator*=(const complex<X>&);
134 template<class X> complex<long double>& operator/=(const complex<X>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135};
136
137// 26.3.6 operators:
138template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139template<class T> complex<T> operator+(const complex<T>&, const T&);
140template<class T> complex<T> operator+(const T&, const complex<T>&);
141template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142template<class T> complex<T> operator-(const complex<T>&, const T&);
143template<class T> complex<T> operator-(const T&, const complex<T>&);
144template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145template<class T> complex<T> operator*(const complex<T>&, const T&);
146template<class T> complex<T> operator*(const T&, const complex<T>&);
147template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148template<class T> complex<T> operator/(const complex<T>&, const T&);
149template<class T> complex<T> operator/(const T&, const complex<T>&);
150template<class T> complex<T> operator+(const complex<T>&);
151template<class T> complex<T> operator-(const complex<T>&);
152template<class T> bool operator==(const complex<T>&, const complex<T>&);
153template<class T> bool operator==(const complex<T>&, const T&);
154template<class T> bool operator==(const T&, const complex<T>&);
155template<class T> bool operator!=(const complex<T>&, const complex<T>&);
156template<class T> bool operator!=(const complex<T>&, const T&);
157template<class T> bool operator!=(const T&, const complex<T>&);
158
159template<class T, class charT, class traits>
160 basic_istream<charT, traits>&
161 operator>>(basic_istream<charT, traits>&, complex<T>&);
162template<class T, class charT, class traits>
163 basic_ostream<charT, traits>&
164 operator<<(basic_ostream<charT, traits>&, const complex<T>&);
165
166// 26.3.7 values:
167
168template<class T> T real(const complex<T>&);
169 long double real(long double);
170 double real(double);
171template<Integral T> double real(T);
172 float real(float);
173
174template<class T> T imag(const complex<T>&);
175 long double imag(long double);
176 double imag(double);
177template<Integral T> double imag(T);
178 float imag(float);
179
180template<class T> T abs(const complex<T>&);
181
182template<class T> T arg(const complex<T>&);
183 long double arg(long double);
184 double arg(double);
185template<Integral T> double arg(T);
186 float arg(float);
187
188template<class T> T norm(const complex<T>&);
189 long double norm(long double);
190 double norm(double);
191template<Integral T> double norm(T);
192 float norm(float);
193
Howard Hinnant995676a2010-11-18 17:34:48 +0000194template<class T> complex<T> conj(const complex<T>&);
195 complex<long double> conj(long double);
196 complex<double> conj(double);
197template<Integral T> complex<double> conj(T);
198 complex<float> conj(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199
Howard Hinnant995676a2010-11-18 17:34:48 +0000200template<class T> complex<T> proj(const complex<T>&);
201 complex<long double> proj(long double);
202 complex<double> proj(double);
203template<Integral T> complex<double> proj(T);
204 complex<float> proj(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205
206template<class T> complex<T> polar(const T&, const T& = 0);
207
208// 26.3.8 transcendentals:
209template<class T> complex<T> acos(const complex<T>&);
210template<class T> complex<T> asin(const complex<T>&);
211template<class T> complex<T> atan(const complex<T>&);
212template<class T> complex<T> acosh(const complex<T>&);
213template<class T> complex<T> asinh(const complex<T>&);
214template<class T> complex<T> atanh(const complex<T>&);
215template<class T> complex<T> cos (const complex<T>&);
216template<class T> complex<T> cosh (const complex<T>&);
217template<class T> complex<T> exp (const complex<T>&);
218template<class T> complex<T> log (const complex<T>&);
219template<class T> complex<T> log10(const complex<T>&);
220
221template<class T> complex<T> pow(const complex<T>&, const T&);
222template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223template<class T> complex<T> pow(const T&, const complex<T>&);
224
225template<class T> complex<T> sin (const complex<T>&);
226template<class T> complex<T> sinh (const complex<T>&);
227template<class T> complex<T> sqrt (const complex<T>&);
228template<class T> complex<T> tan (const complex<T>&);
229template<class T> complex<T> tanh (const complex<T>&);
230
231template<class T, class charT, class traits>
232 basic_istream<charT, traits>&
233 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234
235template<class T, class charT, class traits>
236 basic_ostream<charT, traits>&
237 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238
239} // std
240
241*/
242
243#include <__config>
244#include <type_traits>
245#include <stdexcept>
246#include <cmath>
247#include <sstream>
248#if defined(_LIBCPP_NO_EXCEPTIONS)
249 #include <cassert>
250#endif
251
252#pragma GCC system_header
253
254_LIBCPP_BEGIN_NAMESPACE_STD
255
Howard Hinnant422a53f2010-09-21 21:28:23 +0000256template<class _Tp> class _LIBCPP_VISIBLE complex;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257
258template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
259template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
260
261template<class _Tp>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000262class _LIBCPP_VISIBLE complex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263{
264public:
265 typedef _Tp value_type;
266private:
267 value_type __re_;
268 value_type __im_;
269public:
270 _LIBCPP_INLINE_VISIBILITY
271 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
272 : __re_(__re), __im_(__im) {}
273 template<class _Xp> _LIBCPP_INLINE_VISIBILITY
274 complex(const complex<_Xp>& __c)
275 : __re_(__c.real()), __im_(__c.imag()) {}
276
277 _LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;}
278 _LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;}
279
280 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
281 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
282
283 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re) {__re_ = __re; return *this;}
284 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
285 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
286 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
287 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
288
289 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
290 {
291 __re_ = __c.real();
292 __im_ = __c.imag();
293 return *this;
294 }
295 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
296 {
297 __re_ += __c.real();
298 __im_ += __c.imag();
299 return *this;
300 }
301 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
302 {
303 __re_ -= __c.real();
304 __im_ -= __c.imag();
305 return *this;
306 }
307 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
308 {
309 *this = *this * __c;
310 return *this;
311 }
312 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
313 {
314 *this = *this / __c;
315 return *this;
316 }
317};
318
Howard Hinnant422a53f2010-09-21 21:28:23 +0000319template<> class _LIBCPP_VISIBLE complex<double>;
320template<> class _LIBCPP_VISIBLE complex<long double>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321
322template<>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000323class _LIBCPP_VISIBLE complex<float>
Howard Hinnant324bb032010-08-22 00:02:43 +0000324{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 float __re_;
326 float __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000327public:
328 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329
330 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(float __re = 0.0f, float __im = 0.0f)
331 : __re_(__re), __im_(__im) {}
332 explicit /*constexpr*/ complex(const complex<double>& __c);
333 explicit /*constexpr*/ complex(const complex<long double>& __c);
334
335 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float real() const {return __re_;}
336 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float imag() const {return __im_;}
337
338 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
339 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
340
341 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) {__re_ = __re; return *this;}
342 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
343 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
344 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
345 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
346
347 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
348 {
349 __re_ = __c.real();
350 __im_ = __c.imag();
351 return *this;
352 }
353 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
354 {
355 __re_ += __c.real();
356 __im_ += __c.imag();
357 return *this;
358 }
359 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
360 {
361 __re_ -= __c.real();
362 __im_ -= __c.imag();
363 return *this;
364 }
365 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
366 {
367 *this = *this * __c;
368 return *this;
369 }
370 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
371 {
372 *this = *this / __c;
373 return *this;
374 }
375};
376
377template<>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000378class _LIBCPP_VISIBLE complex<double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000379{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380 double __re_;
381 double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000382public:
383 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(double __re = 0.0, double __im = 0.0)
386 : __re_(__re), __im_(__im) {}
387 /*constexpr*/ complex(const complex<float>& __c);
388 explicit /*constexpr*/ complex(const complex<long double>& __c);
389
390 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double real() const {return __re_;}
391 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double imag() const {return __im_;}
392
393 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
394 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
395
396 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) {__re_ = __re; return *this;}
397 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
398 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
399 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
400 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
401
402 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
403 {
404 __re_ = __c.real();
405 __im_ = __c.imag();
406 return *this;
407 }
408 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
409 {
410 __re_ += __c.real();
411 __im_ += __c.imag();
412 return *this;
413 }
414 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
415 {
416 __re_ -= __c.real();
417 __im_ -= __c.imag();
418 return *this;
419 }
420 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
421 {
422 *this = *this * __c;
423 return *this;
424 }
425 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
426 {
427 *this = *this / __c;
428 return *this;
429 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000430};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431
432template<>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000433class _LIBCPP_VISIBLE complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000434{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 long double __re_;
436 long double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000437public:
438 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439
440 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(long double __re = 0.0L, long double __im = 0.0L)
441 : __re_(__re), __im_(__im) {}
442 /*constexpr*/ complex(const complex<float>& __c);
443 /*constexpr*/ complex(const complex<double>& __c);
444
445 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double real() const {return __re_;}
446 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double imag() const {return __im_;}
447
448 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
449 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
450
451 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re) {__re_ = __re; return *this;}
452 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
453 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
454 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
455 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
456
457 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
458 {
459 __re_ = __c.real();
460 __im_ = __c.imag();
461 return *this;
462 }
463 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
464 {
465 __re_ += __c.real();
466 __im_ += __c.imag();
467 return *this;
468 }
469 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
470 {
471 __re_ -= __c.real();
472 __im_ -= __c.imag();
473 return *this;
474 }
475 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
476 {
477 *this = *this * __c;
478 return *this;
479 }
480 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
481 {
482 *this = *this / __c;
483 return *this;
484 }
485};
486
487//constexpr
488inline _LIBCPP_INLINE_VISIBILITY
489complex<float>::complex(const complex<double>& __c)
490 : __re_(__c.real()), __im_(__c.imag()) {}
491
492//constexpr
493inline _LIBCPP_INLINE_VISIBILITY
494complex<float>::complex(const complex<long double>& __c)
495 : __re_(__c.real()), __im_(__c.imag()) {}
496
497//constexpr
498inline _LIBCPP_INLINE_VISIBILITY
499complex<double>::complex(const complex<float>& __c)
500 : __re_(__c.real()), __im_(__c.imag()) {}
501
502//constexpr
503inline _LIBCPP_INLINE_VISIBILITY
504complex<double>::complex(const complex<long double>& __c)
505 : __re_(__c.real()), __im_(__c.imag()) {}
506
507//constexpr
508inline _LIBCPP_INLINE_VISIBILITY
509complex<long double>::complex(const complex<float>& __c)
510 : __re_(__c.real()), __im_(__c.imag()) {}
511
512//constexpr
513inline _LIBCPP_INLINE_VISIBILITY
514complex<long double>::complex(const complex<double>& __c)
515 : __re_(__c.real()), __im_(__c.imag()) {}
516
517// 26.3.6 operators:
518
519template<class _Tp>
520inline _LIBCPP_INLINE_VISIBILITY
521complex<_Tp>
522operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
523{
524 complex<_Tp> __t(__x);
525 __t += __y;
526 return __t;
527}
528
529template<class _Tp>
530inline _LIBCPP_INLINE_VISIBILITY
531complex<_Tp>
532operator+(const complex<_Tp>& __x, const _Tp& __y)
533{
534 complex<_Tp> __t(__x);
535 __t += __y;
536 return __t;
537}
538
539template<class _Tp>
540inline _LIBCPP_INLINE_VISIBILITY
541complex<_Tp>
542operator+(const _Tp& __x, const complex<_Tp>& __y)
543{
544 complex<_Tp> __t(__y);
545 __t += __x;
546 return __t;
547}
548
549template<class _Tp>
550inline _LIBCPP_INLINE_VISIBILITY
551complex<_Tp>
552operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
553{
554 complex<_Tp> __t(__x);
555 __t -= __y;
556 return __t;
557}
558
559template<class _Tp>
560inline _LIBCPP_INLINE_VISIBILITY
561complex<_Tp>
562operator-(const complex<_Tp>& __x, const _Tp& __y)
563{
564 complex<_Tp> __t(__x);
565 __t -= __y;
566 return __t;
567}
568
569template<class _Tp>
570inline _LIBCPP_INLINE_VISIBILITY
571complex<_Tp>
572operator-(const _Tp& __x, const complex<_Tp>& __y)
573{
574 complex<_Tp> __t(-__y);
575 __t += __x;
576 return __t;
577}
578
579template<class _Tp>
580complex<_Tp>
581operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
582{
583 _Tp __a = __z.real();
584 _Tp __b = __z.imag();
585 _Tp __c = __w.real();
586 _Tp __d = __w.imag();
587 _Tp __ac = __a * __c;
588 _Tp __bd = __b * __d;
589 _Tp __ad = __a * __d;
590 _Tp __bc = __b * __c;
591 _Tp __x = __ac - __bd;
592 _Tp __y = __ad + __bc;
593 if (isnan(__x) && isnan(__y))
594 {
595 bool __recalc = false;
596 if (isinf(__a) || isinf(__b))
597 {
598 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
599 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
600 if (isnan(__c))
601 __c = copysign(_Tp(0), __c);
602 if (isnan(__d))
603 __d = copysign(_Tp(0), __d);
604 __recalc = true;
605 }
606 if (isinf(__c) || isinf(__d))
607 {
608 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
609 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
610 if (isnan(__a))
611 __a = copysign(_Tp(0), __a);
612 if (isnan(__b))
613 __b = copysign(_Tp(0), __b);
614 __recalc = true;
615 }
616 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
617 isinf(__ad) || isinf(__bc)))
618 {
619 if (isnan(__a))
620 __a = copysign(_Tp(0), __a);
621 if (isnan(__b))
622 __b = copysign(_Tp(0), __b);
623 if (isnan(__c))
624 __c = copysign(_Tp(0), __c);
625 if (isnan(__d))
626 __d = copysign(_Tp(0), __d);
627 __recalc = true;
628 }
629 if (__recalc)
630 {
631 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
632 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
633 }
634 }
635 return complex<_Tp>(__x, __y);
636}
637
638template<class _Tp>
639inline _LIBCPP_INLINE_VISIBILITY
640complex<_Tp>
641operator*(const complex<_Tp>& __x, const _Tp& __y)
642{
643 complex<_Tp> __t(__x);
644 __t *= __y;
645 return __t;
646}
647
648template<class _Tp>
649inline _LIBCPP_INLINE_VISIBILITY
650complex<_Tp>
651operator*(const _Tp& __x, const complex<_Tp>& __y)
652{
653 complex<_Tp> __t(__y);
654 __t *= __x;
655 return __t;
656}
657
658template<class _Tp>
659complex<_Tp>
660operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
661{
662 int __ilogbw = 0;
663 _Tp __a = __z.real();
664 _Tp __b = __z.imag();
665 _Tp __c = __w.real();
666 _Tp __d = __w.imag();
667 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
668 if (isfinite(__logbw))
669 {
670 __ilogbw = static_cast<int>(__logbw);
671 __c = scalbn(__c, -__ilogbw);
672 __d = scalbn(__d, -__ilogbw);
673 }
674 _Tp __denom = __c * __c + __d * __d;
675 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
676 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
677 if (isnan(__x) && isnan(__y))
678 {
679 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
680 {
681 __x = copysign(_Tp(INFINITY), __c) * __a;
682 __y = copysign(_Tp(INFINITY), __c) * __b;
683 }
684 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
685 {
686 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
687 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
688 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
689 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
690 }
691 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
692 {
693 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
694 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
695 __x = _Tp(0) * (__a * __c + __b * __d);
696 __y = _Tp(0) * (__b * __c - __a * __d);
697 }
698 }
699 return complex<_Tp>(__x, __y);
700}
701
702template<class _Tp>
703inline _LIBCPP_INLINE_VISIBILITY
704complex<_Tp>
705operator/(const complex<_Tp>& __x, const _Tp& __y)
706{
707 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
708}
709
710template<class _Tp>
711inline _LIBCPP_INLINE_VISIBILITY
712complex<_Tp>
713operator/(const _Tp& __x, const complex<_Tp>& __y)
714{
715 complex<_Tp> __t(__x);
716 __t /= __y;
717 return __t;
718}
719
720template<class _Tp>
721inline _LIBCPP_INLINE_VISIBILITY
722complex<_Tp>
723operator+(const complex<_Tp>& __x)
724{
725 return __x;
726}
727
728template<class _Tp>
729inline _LIBCPP_INLINE_VISIBILITY
730complex<_Tp>
731operator-(const complex<_Tp>& __x)
732{
733 return complex<_Tp>(-__x.real(), -__x.imag());
734}
735
736template<class _Tp>
737inline _LIBCPP_INLINE_VISIBILITY
738bool
739operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
740{
741 return __x.real() == __y.real() && __x.imag() == __y.imag();
742}
743
744template<class _Tp>
745inline _LIBCPP_INLINE_VISIBILITY
746bool
747operator==(const complex<_Tp>& __x, const _Tp& __y)
748{
749 return __x.real() == __y && __x.imag() == 0;
750}
751
752template<class _Tp>
753inline _LIBCPP_INLINE_VISIBILITY
754bool
755operator==(const _Tp& __x, const complex<_Tp>& __y)
756{
757 return __x == __y.real() && 0 == __y.imag();
758}
759
760template<class _Tp>
761inline _LIBCPP_INLINE_VISIBILITY
762bool
763operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
764{
765 return !(__x == __y);
766}
767
768template<class _Tp>
769inline _LIBCPP_INLINE_VISIBILITY
770bool
771operator!=(const complex<_Tp>& __x, const _Tp& __y)
772{
773 return !(__x == __y);
774}
775
776template<class _Tp>
777inline _LIBCPP_INLINE_VISIBILITY
778bool
779operator!=(const _Tp& __x, const complex<_Tp>& __y)
780{
781 return !(__x == __y);
782}
783
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784// 26.3.7 values:
785
786// real
787
788template<class _Tp>
789inline _LIBCPP_INLINE_VISIBILITY
790_Tp
791real(const complex<_Tp>& __c)
792{
793 return __c.real();
794}
795
796inline _LIBCPP_INLINE_VISIBILITY
797long double
798real(long double __re)
799{
800 return __re;
801}
802
803inline _LIBCPP_INLINE_VISIBILITY
804double
805real(double __re)
806{
807 return __re;
808}
809
810template<class _Tp>
811inline _LIBCPP_INLINE_VISIBILITY
812typename enable_if
813<
814 is_integral<_Tp>::value,
815 double
816>::type
817real(_Tp __re)
818{
819 return __re;
820}
821
822inline _LIBCPP_INLINE_VISIBILITY
823float
824real(float __re)
825{
826 return __re;
827}
828
829// imag
830
831template<class _Tp>
832inline _LIBCPP_INLINE_VISIBILITY
833_Tp
834imag(const complex<_Tp>& __c)
835{
836 return __c.imag();
837}
838
839inline _LIBCPP_INLINE_VISIBILITY
840long double
841imag(long double __re)
842{
843 return 0;
844}
845
846inline _LIBCPP_INLINE_VISIBILITY
847double
848imag(double __re)
849{
850 return 0;
851}
852
853template<class _Tp>
854inline _LIBCPP_INLINE_VISIBILITY
855typename enable_if
856<
857 is_integral<_Tp>::value,
858 double
859>::type
860imag(_Tp __re)
861{
862 return 0;
863}
864
865inline _LIBCPP_INLINE_VISIBILITY
866float
867imag(float __re)
868{
869 return 0;
870}
871
872// abs
873
874template<class _Tp>
875inline _LIBCPP_INLINE_VISIBILITY
876_Tp
877abs(const complex<_Tp>& __c)
878{
879 return hypot(__c.real(), __c.imag());
880}
881
882// arg
883
884template<class _Tp>
885inline _LIBCPP_INLINE_VISIBILITY
886_Tp
887arg(const complex<_Tp>& __c)
888{
889 return atan2(__c.imag(), __c.real());
890}
891
892inline _LIBCPP_INLINE_VISIBILITY
893long double
894arg(long double __re)
895{
896 return atan2l(0.L, __re);
897}
898
899inline _LIBCPP_INLINE_VISIBILITY
900double
901arg(double __re)
902{
903 return atan2(0., __re);
904}
905
906template<class _Tp>
907inline _LIBCPP_INLINE_VISIBILITY
908typename enable_if
909<
910 is_integral<_Tp>::value,
911 double
912>::type
913arg(_Tp __re)
914{
915 return atan2(0., __re);
916}
917
918inline _LIBCPP_INLINE_VISIBILITY
919float
920arg(float __re)
921{
922 return atan2f(0.F, __re);
923}
924
925// norm
926
927template<class _Tp>
928inline _LIBCPP_INLINE_VISIBILITY
929_Tp
930norm(const complex<_Tp>& __c)
931{
932 if (isinf(__c.real()))
933 return abs(__c.real());
934 if (isinf(__c.imag()))
935 return abs(__c.imag());
936 return __c.real() * __c.real() + __c.imag() * __c.imag();
937}
938
939inline _LIBCPP_INLINE_VISIBILITY
940long double
941norm(long double __re)
942{
943 return __re * __re;
944}
945
946inline _LIBCPP_INLINE_VISIBILITY
947double
948norm(double __re)
949{
950 return __re * __re;
951}
952
953template<class _Tp>
954inline _LIBCPP_INLINE_VISIBILITY
955typename enable_if
956<
957 is_integral<_Tp>::value,
958 double
959>::type
960norm(_Tp __re)
961{
962 return (double)__re * __re;
963}
964
965inline _LIBCPP_INLINE_VISIBILITY
966float
967norm(float __re)
968{
969 return __re * __re;
970}
971
972// conj
973
974template<class _Tp>
975inline _LIBCPP_INLINE_VISIBILITY
976complex<_Tp>
977conj(const complex<_Tp>& __c)
978{
979 return complex<_Tp>(__c.real(), -__c.imag());
980}
981
982inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +0000983complex<long double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984conj(long double __re)
985{
Howard Hinnant995676a2010-11-18 17:34:48 +0000986 return complex<long double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987}
988
989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +0000990complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991conj(double __re)
992{
Howard Hinnant995676a2010-11-18 17:34:48 +0000993 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994}
995
996template<class _Tp>
997inline _LIBCPP_INLINE_VISIBILITY
998typename enable_if
999<
1000 is_integral<_Tp>::value,
Howard Hinnant995676a2010-11-18 17:34:48 +00001001 complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002>::type
1003conj(_Tp __re)
1004{
Howard Hinnant995676a2010-11-18 17:34:48 +00001005 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006}
1007
1008inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001009complex<float>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010conj(float __re)
1011{
Howard Hinnant995676a2010-11-18 17:34:48 +00001012 return complex<float>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013}
1014
1015// proj
1016
1017template<class _Tp>
1018inline _LIBCPP_INLINE_VISIBILITY
1019complex<_Tp>
1020proj(const complex<_Tp>& __c)
1021{
1022 std::complex<_Tp> __r = __c;
1023 if (isinf(__c.real()) || isinf(__c.imag()))
1024 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1025 return __r;
1026}
1027
1028inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001029complex<long double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030proj(long double __re)
1031{
1032 if (isinf(__re))
1033 __re = abs(__re);
Howard Hinnant995676a2010-11-18 17:34:48 +00001034 return complex<long double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035}
1036
1037inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001038complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039proj(double __re)
1040{
1041 if (isinf(__re))
1042 __re = abs(__re);
Howard Hinnant995676a2010-11-18 17:34:48 +00001043 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044}
1045
1046template<class _Tp>
1047inline _LIBCPP_INLINE_VISIBILITY
1048typename enable_if
1049<
1050 is_integral<_Tp>::value,
Howard Hinnant995676a2010-11-18 17:34:48 +00001051 complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052>::type
1053proj(_Tp __re)
1054{
Howard Hinnant995676a2010-11-18 17:34:48 +00001055 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056}
1057
1058inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001059complex<float>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060proj(float __re)
1061{
1062 if (isinf(__re))
1063 __re = abs(__re);
Howard Hinnant995676a2010-11-18 17:34:48 +00001064 return complex<float>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065}
1066
1067// polar
1068
1069template<class _Tp>
1070complex<_Tp>
1071polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1072{
1073 if (isnan(__rho) || signbit(__rho))
1074 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1075 if (isnan(__theta))
1076 {
1077 if (isinf(__rho))
1078 return complex<_Tp>(__rho, __theta);
1079 return complex<_Tp>(__theta, __theta);
1080 }
1081 if (isinf(__theta))
1082 {
1083 if (isinf(__rho))
1084 return complex<_Tp>(__rho, _Tp(NAN));
1085 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1086 }
1087 _Tp __x = __rho * cos(__theta);
1088 if (isnan(__x))
1089 __x = 0;
1090 _Tp __y = __rho * sin(__theta);
1091 if (isnan(__y))
1092 __y = 0;
1093 return complex<_Tp>(__x, __y);
1094}
1095
1096// log
1097
1098template<class _Tp>
1099inline _LIBCPP_INLINE_VISIBILITY
1100complex<_Tp>
1101log(const complex<_Tp>& __x)
1102{
1103 return complex<_Tp>(log(abs(__x)), arg(__x));
1104}
1105
1106// log10
1107
1108template<class _Tp>
1109inline _LIBCPP_INLINE_VISIBILITY
1110complex<_Tp>
1111log10(const complex<_Tp>& __x)
1112{
1113 return log(__x) / log(_Tp(10));
1114}
1115
1116// sqrt
1117
1118template<class _Tp>
1119complex<_Tp>
1120sqrt(const complex<_Tp>& __x)
1121{
1122 if (isinf(__x.imag()))
1123 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1124 if (isinf(__x.real()))
1125 {
1126 if (__x.real() > _Tp(0))
1127 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1128 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1129 }
1130 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1131}
1132
1133// exp
1134
1135template<class _Tp>
1136complex<_Tp>
1137exp(const complex<_Tp>& __x)
1138{
1139 _Tp __i = __x.imag();
1140 if (isinf(__x.real()))
1141 {
1142 if (__x.real() < _Tp(0))
1143 {
1144 if (!isfinite(__i))
1145 __i = _Tp(1);
1146 }
1147 else if (__i == 0 || !isfinite(__i))
1148 {
1149 if (isinf(__i))
1150 __i = _Tp(NAN);
1151 return complex<_Tp>(__x.real(), __i);
1152 }
1153 }
1154 else if (isnan(__x.real()) && __x.imag() == 0)
1155 return __x;
1156 _Tp __e = exp(__x.real());
1157 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1158}
1159
1160// pow
1161
1162template<class _Tp>
1163inline _LIBCPP_INLINE_VISIBILITY
1164complex<_Tp>
1165pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1166{
1167 return exp(__y * log(__x));
1168}
1169
1170template<class _Tp, class _Up>
1171inline _LIBCPP_INLINE_VISIBILITY
1172complex<typename __promote<_Tp, _Up>::type>
1173pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1174{
1175 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1176 return _STD::pow(result_type(__x), result_type(__y));
1177}
1178
1179template<class _Tp, class _Up>
1180inline _LIBCPP_INLINE_VISIBILITY
1181typename enable_if
1182<
1183 is_arithmetic<_Up>::value,
1184 complex<typename __promote<_Tp, _Up>::type>
1185>::type
1186pow(const complex<_Tp>& __x, const _Up& __y)
1187{
1188 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1189 return _STD::pow(result_type(__x), result_type(__y));
1190}
1191
1192template<class _Tp, class _Up>
1193inline _LIBCPP_INLINE_VISIBILITY
1194typename enable_if
1195<
1196 is_arithmetic<_Tp>::value,
1197 complex<typename __promote<_Tp, _Up>::type>
1198>::type
1199pow(const _Tp& __x, const complex<_Up>& __y)
1200{
1201 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1202 return _STD::pow(result_type(__x), result_type(__y));
1203}
1204
1205// asinh
1206
1207template<class _Tp>
1208complex<_Tp>
1209asinh(const complex<_Tp>& __x)
1210{
1211 const _Tp __pi(atan2(+0., -0.));
1212 if (isinf(__x.real()))
1213 {
1214 if (isnan(__x.imag()))
1215 return __x;
1216 if (isinf(__x.imag()))
1217 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1218 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1219 }
1220 if (isnan(__x.real()))
1221 {
1222 if (isinf(__x.imag()))
1223 return complex<_Tp>(__x.imag(), __x.real());
1224 if (__x.imag() == 0)
1225 return __x;
1226 return complex<_Tp>(__x.real(), __x.real());
1227 }
1228 if (isinf(__x.imag()))
1229 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1230 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1231 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1232}
1233
1234// acosh
1235
1236template<class _Tp>
1237complex<_Tp>
1238acosh(const complex<_Tp>& __x)
1239{
1240 const _Tp __pi(atan2(+0., -0.));
1241 if (isinf(__x.real()))
1242 {
1243 if (isnan(__x.imag()))
1244 return complex<_Tp>(abs(__x.real()), __x.imag());
1245 if (isinf(__x.imag()))
1246 if (__x.real() > 0)
1247 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1248 else
1249 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1250 if (__x.real() < 0)
1251 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1252 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1253 }
1254 if (isnan(__x.real()))
1255 {
1256 if (isinf(__x.imag()))
1257 return complex<_Tp>(abs(__x.imag()), __x.real());
1258 return complex<_Tp>(__x.real(), __x.real());
1259 }
1260 if (isinf(__x.imag()))
1261 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1262 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1263 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1264}
1265
1266// atanh
1267
1268template<class _Tp>
1269complex<_Tp>
1270atanh(const complex<_Tp>& __x)
1271{
1272 const _Tp __pi(atan2(+0., -0.));
1273 if (isinf(__x.imag()))
1274 {
1275 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1276 }
1277 if (isnan(__x.imag()))
1278 {
1279 if (isinf(__x.real()) || __x.real() == 0)
1280 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1281 return complex<_Tp>(__x.imag(), __x.imag());
1282 }
1283 if (isnan(__x.real()))
1284 {
1285 return complex<_Tp>(__x.real(), __x.real());
1286 }
1287 if (isinf(__x.real()))
1288 {
1289 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1290 }
1291 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1292 {
1293 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1294 }
1295 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1296 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1297}
1298
1299// sinh
1300
1301template<class _Tp>
1302complex<_Tp>
1303sinh(const complex<_Tp>& __x)
1304{
1305 if (isinf(__x.real()) && !isfinite(__x.imag()))
1306 return complex<_Tp>(__x.real(), _Tp(NAN));
1307 if (__x.real() == 0 && !isfinite(__x.imag()))
1308 return complex<_Tp>(__x.real(), _Tp(NAN));
1309 if (__x.imag() == 0 && !isfinite(__x.real()))
1310 return __x;
1311 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1312}
1313
1314// cosh
1315
1316template<class _Tp>
1317complex<_Tp>
1318cosh(const complex<_Tp>& __x)
1319{
1320 if (isinf(__x.real()) && !isfinite(__x.imag()))
1321 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1322 if (__x.real() == 0 && !isfinite(__x.imag()))
1323 return complex<_Tp>(_Tp(NAN), __x.real());
1324 if (__x.real() == 0 && __x.imag() == 0)
1325 return complex<_Tp>(_Tp(1), __x.imag());
1326 if (__x.imag() == 0 && !isfinite(__x.real()))
1327 return complex<_Tp>(abs(__x.real()), __x.imag());
1328 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1329}
1330
1331// tanh
1332
1333template<class _Tp>
1334complex<_Tp>
1335tanh(const complex<_Tp>& __x)
1336{
1337 if (isinf(__x.real()))
1338 {
1339 if (!isfinite(__x.imag()))
1340 return complex<_Tp>(_Tp(1), _Tp(0));
1341 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1342 }
1343 if (isnan(__x.real()) && __x.imag() == 0)
1344 return __x;
1345 _Tp __2r(_Tp(2) * __x.real());
1346 _Tp __2i(_Tp(2) * __x.imag());
1347 _Tp __d(cosh(__2r) + cos(__2i));
1348 return complex<_Tp>(sinh(__2r)/__d, sin(__2i)/__d);
1349}
1350
1351// asin
1352
1353template<class _Tp>
1354complex<_Tp>
1355asin(const complex<_Tp>& __x)
1356{
1357 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1358 return complex<_Tp>(__z.imag(), -__z.real());
1359}
1360
1361// acos
1362
1363template<class _Tp>
1364complex<_Tp>
1365acos(const complex<_Tp>& __x)
1366{
1367 const _Tp __pi(atan2(+0., -0.));
1368 if (isinf(__x.real()))
1369 {
1370 if (isnan(__x.imag()))
1371 return complex<_Tp>(__x.imag(), __x.real());
1372 if (isinf(__x.imag()))
1373 {
1374 if (__x.real() < _Tp(0))
1375 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1376 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1377 }
1378 if (__x.real() < _Tp(0))
1379 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1380 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1381 }
1382 if (isnan(__x.real()))
1383 {
1384 if (isinf(__x.imag()))
1385 return complex<_Tp>(__x.real(), -__x.imag());
1386 return complex<_Tp>(__x.real(), __x.real());
1387 }
1388 if (isinf(__x.imag()))
1389 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1390 if (__x.real() == 0)
1391 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1392 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1393 if (signbit(__x.imag()))
1394 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1395 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1396}
1397
1398// atan
1399
1400template<class _Tp>
1401complex<_Tp>
1402atan(const complex<_Tp>& __x)
1403{
1404 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1405 return complex<_Tp>(__z.imag(), -__z.real());
1406}
1407
1408// sin
1409
1410template<class _Tp>
1411complex<_Tp>
1412sin(const complex<_Tp>& __x)
1413{
1414 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1415 return complex<_Tp>(__z.imag(), -__z.real());
1416}
1417
1418// cos
1419
1420template<class _Tp>
1421inline _LIBCPP_INLINE_VISIBILITY
1422complex<_Tp>
1423cos(const complex<_Tp>& __x)
1424{
1425 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1426}
1427
1428// tan
1429
1430template<class _Tp>
1431complex<_Tp>
1432tan(const complex<_Tp>& __x)
1433{
1434 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1435 return complex<_Tp>(__z.imag(), -__z.real());
1436}
1437
1438template<class _Tp, class _CharT, class _Traits>
1439basic_istream<_CharT, _Traits>&
1440operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1441{
1442 if (__is.good())
1443 {
1444 ws(__is);
1445 if (__is.peek() == _CharT('('))
1446 {
1447 __is.get();
1448 _Tp __r;
1449 __is >> __r;
1450 if (!__is.fail())
1451 {
1452 ws(__is);
1453 _CharT __c = __is.peek();
1454 if (__c == _CharT(','))
1455 {
1456 __is.get();
1457 _Tp __i;
1458 __is >> __i;
1459 if (!__is.fail())
1460 {
1461 ws(__is);
1462 __c = __is.peek();
1463 if (__c == _CharT(')'))
1464 {
1465 __is.get();
1466 __x = complex<_Tp>(__r, __i);
1467 }
1468 else
1469 __is.setstate(ios_base::failbit);
1470 }
1471 else
1472 __is.setstate(ios_base::failbit);
1473 }
1474 else if (__c == _CharT(')'))
1475 {
1476 __is.get();
1477 __x = complex<_Tp>(__r, _Tp(0));
1478 }
1479 else
1480 __is.setstate(ios_base::failbit);
1481 }
1482 else
1483 __is.setstate(ios_base::failbit);
1484 }
1485 else
1486 {
1487 _Tp __r;
1488 __is >> __r;
1489 if (!__is.fail())
1490 __x = complex<_Tp>(__r, _Tp(0));
1491 else
1492 __is.setstate(ios_base::failbit);
1493 }
1494 }
1495 else
1496 __is.setstate(ios_base::failbit);
1497 return __is;
1498}
1499
1500template<class _Tp, class _CharT, class _Traits>
1501basic_ostream<_CharT, _Traits>&
1502operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1503{
1504 basic_ostringstream<_CharT, _Traits> __s;
1505 __s.flags(__os.flags());
1506 __s.imbue(__os.getloc());
1507 __s.precision(__os.precision());
1508 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1509 return __os << __s.str();
1510}
1511
1512_LIBCPP_END_NAMESPACE_STD
1513
1514#endif // _LIBCPP_COMPLEX