blob: 3b660a35a9ab840a386bfdeb1e2615ac6ba5d16b [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
Howard Hinnant08e17472011-10-17 20:05:10 +0000252#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255
256_LIBCPP_BEGIN_NAMESPACE_STD
257
Howard Hinnant422a53f2010-09-21 21:28:23 +0000258template<class _Tp> class _LIBCPP_VISIBLE complex;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259
260template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262
263template<class _Tp>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000264class _LIBCPP_VISIBLE complex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265{
266public:
267 typedef _Tp value_type;
268private:
269 value_type __re_;
270 value_type __im_;
271public:
272 _LIBCPP_INLINE_VISIBILITY
273 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274 : __re_(__re), __im_(__im) {}
275 template<class _Xp> _LIBCPP_INLINE_VISIBILITY
276 complex(const complex<_Xp>& __c)
277 : __re_(__c.real()), __im_(__c.imag()) {}
278
279 _LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;}
280 _LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;}
281
282 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
284
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000285 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
286 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
288 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
289 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
290 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
291
292 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
293 {
294 __re_ = __c.real();
295 __im_ = __c.imag();
296 return *this;
297 }
298 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
299 {
300 __re_ += __c.real();
301 __im_ += __c.imag();
302 return *this;
303 }
304 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
305 {
306 __re_ -= __c.real();
307 __im_ -= __c.imag();
308 return *this;
309 }
310 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
311 {
312 *this = *this * __c;
313 return *this;
314 }
315 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
316 {
317 *this = *this / __c;
318 return *this;
319 }
320};
321
Howard Hinnant422a53f2010-09-21 21:28:23 +0000322template<> class _LIBCPP_VISIBLE complex<double>;
323template<> class _LIBCPP_VISIBLE complex<long double>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000324
325template<>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000326class _LIBCPP_VISIBLE complex<float>
Howard Hinnant324bb032010-08-22 00:02:43 +0000327{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 float __re_;
329 float __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000330public:
331 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332
333 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(float __re = 0.0f, float __im = 0.0f)
334 : __re_(__re), __im_(__im) {}
335 explicit /*constexpr*/ complex(const complex<double>& __c);
336 explicit /*constexpr*/ complex(const complex<long double>& __c);
337
338 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float real() const {return __re_;}
339 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float imag() const {return __im_;}
340
341 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
342 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
343
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000344 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
345 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
347 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
348 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
349 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
350
351 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
352 {
353 __re_ = __c.real();
354 __im_ = __c.imag();
355 return *this;
356 }
357 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
358 {
359 __re_ += __c.real();
360 __im_ += __c.imag();
361 return *this;
362 }
363 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
364 {
365 __re_ -= __c.real();
366 __im_ -= __c.imag();
367 return *this;
368 }
369 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
370 {
371 *this = *this * __c;
372 return *this;
373 }
374 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
375 {
376 *this = *this / __c;
377 return *this;
378 }
379};
380
381template<>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000382class _LIBCPP_VISIBLE complex<double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000383{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 double __re_;
385 double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000386public:
387 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388
389 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(double __re = 0.0, double __im = 0.0)
390 : __re_(__re), __im_(__im) {}
391 /*constexpr*/ complex(const complex<float>& __c);
392 explicit /*constexpr*/ complex(const complex<long double>& __c);
393
394 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double real() const {return __re_;}
395 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double imag() const {return __im_;}
396
397 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
398 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
399
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000400 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
401 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
403 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
405 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
406
407 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
408 {
409 __re_ = __c.real();
410 __im_ = __c.imag();
411 return *this;
412 }
413 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
414 {
415 __re_ += __c.real();
416 __im_ += __c.imag();
417 return *this;
418 }
419 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
420 {
421 __re_ -= __c.real();
422 __im_ -= __c.imag();
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 }
430 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
431 {
432 *this = *this / __c;
433 return *this;
434 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000435};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436
437template<>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000438class _LIBCPP_VISIBLE complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000439{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440 long double __re_;
441 long double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000442public:
443 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444
445 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(long double __re = 0.0L, long double __im = 0.0L)
446 : __re_(__re), __im_(__im) {}
447 /*constexpr*/ complex(const complex<float>& __c);
448 /*constexpr*/ complex(const complex<double>& __c);
449
450 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double real() const {return __re_;}
451 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double imag() const {return __im_;}
452
453 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
454 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
455
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000456 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
457 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
459 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
460 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
461 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; 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 __re_ -= __c.real();
478 __im_ -= __c.imag();
479 return *this;
480 }
481 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
482 {
483 *this = *this * __c;
484 return *this;
485 }
486 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
487 {
488 *this = *this / __c;
489 return *this;
490 }
491};
492
493//constexpr
494inline _LIBCPP_INLINE_VISIBILITY
495complex<float>::complex(const complex<double>& __c)
496 : __re_(__c.real()), __im_(__c.imag()) {}
497
498//constexpr
499inline _LIBCPP_INLINE_VISIBILITY
500complex<float>::complex(const complex<long double>& __c)
501 : __re_(__c.real()), __im_(__c.imag()) {}
502
503//constexpr
504inline _LIBCPP_INLINE_VISIBILITY
505complex<double>::complex(const complex<float>& __c)
506 : __re_(__c.real()), __im_(__c.imag()) {}
507
508//constexpr
509inline _LIBCPP_INLINE_VISIBILITY
510complex<double>::complex(const complex<long double>& __c)
511 : __re_(__c.real()), __im_(__c.imag()) {}
512
513//constexpr
514inline _LIBCPP_INLINE_VISIBILITY
515complex<long double>::complex(const complex<float>& __c)
516 : __re_(__c.real()), __im_(__c.imag()) {}
517
518//constexpr
519inline _LIBCPP_INLINE_VISIBILITY
520complex<long double>::complex(const complex<double>& __c)
521 : __re_(__c.real()), __im_(__c.imag()) {}
522
523// 26.3.6 operators:
524
525template<class _Tp>
526inline _LIBCPP_INLINE_VISIBILITY
527complex<_Tp>
528operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
529{
530 complex<_Tp> __t(__x);
531 __t += __y;
532 return __t;
533}
534
535template<class _Tp>
536inline _LIBCPP_INLINE_VISIBILITY
537complex<_Tp>
538operator+(const complex<_Tp>& __x, const _Tp& __y)
539{
540 complex<_Tp> __t(__x);
541 __t += __y;
542 return __t;
543}
544
545template<class _Tp>
546inline _LIBCPP_INLINE_VISIBILITY
547complex<_Tp>
548operator+(const _Tp& __x, const complex<_Tp>& __y)
549{
550 complex<_Tp> __t(__y);
551 __t += __x;
552 return __t;
553}
554
555template<class _Tp>
556inline _LIBCPP_INLINE_VISIBILITY
557complex<_Tp>
558operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
559{
560 complex<_Tp> __t(__x);
561 __t -= __y;
562 return __t;
563}
564
565template<class _Tp>
566inline _LIBCPP_INLINE_VISIBILITY
567complex<_Tp>
568operator-(const complex<_Tp>& __x, const _Tp& __y)
569{
570 complex<_Tp> __t(__x);
571 __t -= __y;
572 return __t;
573}
574
575template<class _Tp>
576inline _LIBCPP_INLINE_VISIBILITY
577complex<_Tp>
578operator-(const _Tp& __x, const complex<_Tp>& __y)
579{
580 complex<_Tp> __t(-__y);
581 __t += __x;
582 return __t;
583}
584
585template<class _Tp>
586complex<_Tp>
587operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
588{
589 _Tp __a = __z.real();
590 _Tp __b = __z.imag();
591 _Tp __c = __w.real();
592 _Tp __d = __w.imag();
593 _Tp __ac = __a * __c;
594 _Tp __bd = __b * __d;
595 _Tp __ad = __a * __d;
596 _Tp __bc = __b * __c;
597 _Tp __x = __ac - __bd;
598 _Tp __y = __ad + __bc;
599 if (isnan(__x) && isnan(__y))
600 {
601 bool __recalc = false;
602 if (isinf(__a) || isinf(__b))
603 {
604 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
605 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
606 if (isnan(__c))
607 __c = copysign(_Tp(0), __c);
608 if (isnan(__d))
609 __d = copysign(_Tp(0), __d);
610 __recalc = true;
611 }
612 if (isinf(__c) || isinf(__d))
613 {
614 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
615 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
616 if (isnan(__a))
617 __a = copysign(_Tp(0), __a);
618 if (isnan(__b))
619 __b = copysign(_Tp(0), __b);
620 __recalc = true;
621 }
622 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
623 isinf(__ad) || isinf(__bc)))
624 {
625 if (isnan(__a))
626 __a = copysign(_Tp(0), __a);
627 if (isnan(__b))
628 __b = copysign(_Tp(0), __b);
629 if (isnan(__c))
630 __c = copysign(_Tp(0), __c);
631 if (isnan(__d))
632 __d = copysign(_Tp(0), __d);
633 __recalc = true;
634 }
635 if (__recalc)
636 {
637 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
638 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
639 }
640 }
641 return complex<_Tp>(__x, __y);
642}
643
644template<class _Tp>
645inline _LIBCPP_INLINE_VISIBILITY
646complex<_Tp>
647operator*(const complex<_Tp>& __x, const _Tp& __y)
648{
649 complex<_Tp> __t(__x);
650 __t *= __y;
651 return __t;
652}
653
654template<class _Tp>
655inline _LIBCPP_INLINE_VISIBILITY
656complex<_Tp>
657operator*(const _Tp& __x, const complex<_Tp>& __y)
658{
659 complex<_Tp> __t(__y);
660 __t *= __x;
661 return __t;
662}
663
664template<class _Tp>
665complex<_Tp>
666operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
667{
668 int __ilogbw = 0;
669 _Tp __a = __z.real();
670 _Tp __b = __z.imag();
671 _Tp __c = __w.real();
672 _Tp __d = __w.imag();
673 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
674 if (isfinite(__logbw))
675 {
676 __ilogbw = static_cast<int>(__logbw);
677 __c = scalbn(__c, -__ilogbw);
678 __d = scalbn(__d, -__ilogbw);
679 }
680 _Tp __denom = __c * __c + __d * __d;
681 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
682 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
683 if (isnan(__x) && isnan(__y))
684 {
685 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
686 {
687 __x = copysign(_Tp(INFINITY), __c) * __a;
688 __y = copysign(_Tp(INFINITY), __c) * __b;
689 }
690 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
691 {
692 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
693 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
694 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
695 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
696 }
697 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
698 {
699 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
700 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
701 __x = _Tp(0) * (__a * __c + __b * __d);
702 __y = _Tp(0) * (__b * __c - __a * __d);
703 }
704 }
705 return complex<_Tp>(__x, __y);
706}
707
708template<class _Tp>
709inline _LIBCPP_INLINE_VISIBILITY
710complex<_Tp>
711operator/(const complex<_Tp>& __x, const _Tp& __y)
712{
713 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
714}
715
716template<class _Tp>
717inline _LIBCPP_INLINE_VISIBILITY
718complex<_Tp>
719operator/(const _Tp& __x, const complex<_Tp>& __y)
720{
721 complex<_Tp> __t(__x);
722 __t /= __y;
723 return __t;
724}
725
726template<class _Tp>
727inline _LIBCPP_INLINE_VISIBILITY
728complex<_Tp>
729operator+(const complex<_Tp>& __x)
730{
731 return __x;
732}
733
734template<class _Tp>
735inline _LIBCPP_INLINE_VISIBILITY
736complex<_Tp>
737operator-(const complex<_Tp>& __x)
738{
739 return complex<_Tp>(-__x.real(), -__x.imag());
740}
741
742template<class _Tp>
743inline _LIBCPP_INLINE_VISIBILITY
744bool
745operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
746{
747 return __x.real() == __y.real() && __x.imag() == __y.imag();
748}
749
750template<class _Tp>
751inline _LIBCPP_INLINE_VISIBILITY
752bool
753operator==(const complex<_Tp>& __x, const _Tp& __y)
754{
755 return __x.real() == __y && __x.imag() == 0;
756}
757
758template<class _Tp>
759inline _LIBCPP_INLINE_VISIBILITY
760bool
761operator==(const _Tp& __x, const complex<_Tp>& __y)
762{
763 return __x == __y.real() && 0 == __y.imag();
764}
765
766template<class _Tp>
767inline _LIBCPP_INLINE_VISIBILITY
768bool
769operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
770{
771 return !(__x == __y);
772}
773
774template<class _Tp>
775inline _LIBCPP_INLINE_VISIBILITY
776bool
777operator!=(const complex<_Tp>& __x, const _Tp& __y)
778{
779 return !(__x == __y);
780}
781
782template<class _Tp>
783inline _LIBCPP_INLINE_VISIBILITY
784bool
785operator!=(const _Tp& __x, const complex<_Tp>& __y)
786{
787 return !(__x == __y);
788}
789
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790// 26.3.7 values:
791
792// real
793
794template<class _Tp>
795inline _LIBCPP_INLINE_VISIBILITY
796_Tp
797real(const complex<_Tp>& __c)
798{
799 return __c.real();
800}
801
802inline _LIBCPP_INLINE_VISIBILITY
803long double
804real(long double __re)
805{
806 return __re;
807}
808
809inline _LIBCPP_INLINE_VISIBILITY
810double
811real(double __re)
812{
813 return __re;
814}
815
816template<class _Tp>
817inline _LIBCPP_INLINE_VISIBILITY
818typename enable_if
819<
820 is_integral<_Tp>::value,
821 double
822>::type
823real(_Tp __re)
824{
825 return __re;
826}
827
828inline _LIBCPP_INLINE_VISIBILITY
829float
830real(float __re)
831{
832 return __re;
833}
834
835// imag
836
837template<class _Tp>
838inline _LIBCPP_INLINE_VISIBILITY
839_Tp
840imag(const complex<_Tp>& __c)
841{
842 return __c.imag();
843}
844
845inline _LIBCPP_INLINE_VISIBILITY
846long double
847imag(long double __re)
848{
849 return 0;
850}
851
852inline _LIBCPP_INLINE_VISIBILITY
853double
854imag(double __re)
855{
856 return 0;
857}
858
859template<class _Tp>
860inline _LIBCPP_INLINE_VISIBILITY
861typename enable_if
862<
863 is_integral<_Tp>::value,
864 double
865>::type
866imag(_Tp __re)
867{
868 return 0;
869}
870
871inline _LIBCPP_INLINE_VISIBILITY
872float
873imag(float __re)
874{
875 return 0;
876}
877
878// abs
879
880template<class _Tp>
881inline _LIBCPP_INLINE_VISIBILITY
882_Tp
883abs(const complex<_Tp>& __c)
884{
885 return hypot(__c.real(), __c.imag());
886}
887
888// arg
889
890template<class _Tp>
891inline _LIBCPP_INLINE_VISIBILITY
892_Tp
893arg(const complex<_Tp>& __c)
894{
895 return atan2(__c.imag(), __c.real());
896}
897
898inline _LIBCPP_INLINE_VISIBILITY
899long double
900arg(long double __re)
901{
902 return atan2l(0.L, __re);
903}
904
905inline _LIBCPP_INLINE_VISIBILITY
906double
907arg(double __re)
908{
909 return atan2(0., __re);
910}
911
912template<class _Tp>
913inline _LIBCPP_INLINE_VISIBILITY
914typename enable_if
915<
916 is_integral<_Tp>::value,
917 double
918>::type
919arg(_Tp __re)
920{
921 return atan2(0., __re);
922}
923
924inline _LIBCPP_INLINE_VISIBILITY
925float
926arg(float __re)
927{
928 return atan2f(0.F, __re);
929}
930
931// norm
932
933template<class _Tp>
934inline _LIBCPP_INLINE_VISIBILITY
935_Tp
936norm(const complex<_Tp>& __c)
937{
938 if (isinf(__c.real()))
939 return abs(__c.real());
940 if (isinf(__c.imag()))
941 return abs(__c.imag());
942 return __c.real() * __c.real() + __c.imag() * __c.imag();
943}
944
945inline _LIBCPP_INLINE_VISIBILITY
946long double
947norm(long double __re)
948{
949 return __re * __re;
950}
951
952inline _LIBCPP_INLINE_VISIBILITY
953double
954norm(double __re)
955{
956 return __re * __re;
957}
958
959template<class _Tp>
960inline _LIBCPP_INLINE_VISIBILITY
961typename enable_if
962<
963 is_integral<_Tp>::value,
964 double
965>::type
966norm(_Tp __re)
967{
968 return (double)__re * __re;
969}
970
971inline _LIBCPP_INLINE_VISIBILITY
972float
973norm(float __re)
974{
975 return __re * __re;
976}
977
978// conj
979
980template<class _Tp>
981inline _LIBCPP_INLINE_VISIBILITY
982complex<_Tp>
983conj(const complex<_Tp>& __c)
984{
985 return complex<_Tp>(__c.real(), -__c.imag());
986}
987
988inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +0000989complex<long double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990conj(long double __re)
991{
Howard Hinnant995676a2010-11-18 17:34:48 +0000992 return complex<long double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993}
994
995inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +0000996complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997conj(double __re)
998{
Howard Hinnant995676a2010-11-18 17:34:48 +0000999 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000}
1001
1002template<class _Tp>
1003inline _LIBCPP_INLINE_VISIBILITY
1004typename enable_if
1005<
1006 is_integral<_Tp>::value,
Howard Hinnant995676a2010-11-18 17:34:48 +00001007 complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008>::type
1009conj(_Tp __re)
1010{
Howard Hinnant995676a2010-11-18 17:34:48 +00001011 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012}
1013
1014inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001015complex<float>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016conj(float __re)
1017{
Howard Hinnant995676a2010-11-18 17:34:48 +00001018 return complex<float>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019}
1020
1021// proj
1022
1023template<class _Tp>
1024inline _LIBCPP_INLINE_VISIBILITY
1025complex<_Tp>
1026proj(const complex<_Tp>& __c)
1027{
1028 std::complex<_Tp> __r = __c;
1029 if (isinf(__c.real()) || isinf(__c.imag()))
1030 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1031 return __r;
1032}
1033
1034inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001035complex<long double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036proj(long double __re)
1037{
1038 if (isinf(__re))
1039 __re = abs(__re);
Howard Hinnant995676a2010-11-18 17:34:48 +00001040 return complex<long double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041}
1042
1043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001044complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045proj(double __re)
1046{
1047 if (isinf(__re))
1048 __re = abs(__re);
Howard Hinnant995676a2010-11-18 17:34:48 +00001049 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050}
1051
1052template<class _Tp>
1053inline _LIBCPP_INLINE_VISIBILITY
1054typename enable_if
1055<
1056 is_integral<_Tp>::value,
Howard Hinnant995676a2010-11-18 17:34:48 +00001057 complex<double>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058>::type
1059proj(_Tp __re)
1060{
Howard Hinnant995676a2010-11-18 17:34:48 +00001061 return complex<double>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062}
1063
1064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant995676a2010-11-18 17:34:48 +00001065complex<float>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066proj(float __re)
1067{
1068 if (isinf(__re))
1069 __re = abs(__re);
Howard Hinnant995676a2010-11-18 17:34:48 +00001070 return complex<float>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071}
1072
1073// polar
1074
1075template<class _Tp>
1076complex<_Tp>
1077polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1078{
1079 if (isnan(__rho) || signbit(__rho))
1080 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1081 if (isnan(__theta))
1082 {
1083 if (isinf(__rho))
1084 return complex<_Tp>(__rho, __theta);
1085 return complex<_Tp>(__theta, __theta);
1086 }
1087 if (isinf(__theta))
1088 {
1089 if (isinf(__rho))
1090 return complex<_Tp>(__rho, _Tp(NAN));
1091 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1092 }
1093 _Tp __x = __rho * cos(__theta);
1094 if (isnan(__x))
1095 __x = 0;
1096 _Tp __y = __rho * sin(__theta);
1097 if (isnan(__y))
1098 __y = 0;
1099 return complex<_Tp>(__x, __y);
1100}
1101
1102// log
1103
1104template<class _Tp>
1105inline _LIBCPP_INLINE_VISIBILITY
1106complex<_Tp>
1107log(const complex<_Tp>& __x)
1108{
1109 return complex<_Tp>(log(abs(__x)), arg(__x));
1110}
1111
1112// log10
1113
1114template<class _Tp>
1115inline _LIBCPP_INLINE_VISIBILITY
1116complex<_Tp>
1117log10(const complex<_Tp>& __x)
1118{
1119 return log(__x) / log(_Tp(10));
1120}
1121
1122// sqrt
1123
1124template<class _Tp>
1125complex<_Tp>
1126sqrt(const complex<_Tp>& __x)
1127{
1128 if (isinf(__x.imag()))
1129 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1130 if (isinf(__x.real()))
1131 {
1132 if (__x.real() > _Tp(0))
1133 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1134 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1135 }
1136 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1137}
1138
1139// exp
1140
1141template<class _Tp>
1142complex<_Tp>
1143exp(const complex<_Tp>& __x)
1144{
1145 _Tp __i = __x.imag();
1146 if (isinf(__x.real()))
1147 {
1148 if (__x.real() < _Tp(0))
1149 {
1150 if (!isfinite(__i))
1151 __i = _Tp(1);
1152 }
1153 else if (__i == 0 || !isfinite(__i))
1154 {
1155 if (isinf(__i))
1156 __i = _Tp(NAN);
1157 return complex<_Tp>(__x.real(), __i);
1158 }
1159 }
1160 else if (isnan(__x.real()) && __x.imag() == 0)
1161 return __x;
1162 _Tp __e = exp(__x.real());
1163 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1164}
1165
1166// pow
1167
1168template<class _Tp>
1169inline _LIBCPP_INLINE_VISIBILITY
1170complex<_Tp>
1171pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1172{
1173 return exp(__y * log(__x));
1174}
1175
1176template<class _Tp, class _Up>
1177inline _LIBCPP_INLINE_VISIBILITY
1178complex<typename __promote<_Tp, _Up>::type>
1179pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1180{
1181 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001182 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183}
1184
1185template<class _Tp, class _Up>
1186inline _LIBCPP_INLINE_VISIBILITY
1187typename enable_if
1188<
1189 is_arithmetic<_Up>::value,
1190 complex<typename __promote<_Tp, _Up>::type>
1191>::type
1192pow(const complex<_Tp>& __x, const _Up& __y)
1193{
1194 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001195 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196}
1197
1198template<class _Tp, class _Up>
1199inline _LIBCPP_INLINE_VISIBILITY
1200typename enable_if
1201<
1202 is_arithmetic<_Tp>::value,
1203 complex<typename __promote<_Tp, _Up>::type>
1204>::type
1205pow(const _Tp& __x, const complex<_Up>& __y)
1206{
1207 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001208 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209}
1210
1211// asinh
1212
1213template<class _Tp>
1214complex<_Tp>
1215asinh(const complex<_Tp>& __x)
1216{
1217 const _Tp __pi(atan2(+0., -0.));
1218 if (isinf(__x.real()))
1219 {
1220 if (isnan(__x.imag()))
1221 return __x;
1222 if (isinf(__x.imag()))
1223 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1224 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1225 }
1226 if (isnan(__x.real()))
1227 {
1228 if (isinf(__x.imag()))
1229 return complex<_Tp>(__x.imag(), __x.real());
1230 if (__x.imag() == 0)
1231 return __x;
1232 return complex<_Tp>(__x.real(), __x.real());
1233 }
1234 if (isinf(__x.imag()))
1235 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1236 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1237 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1238}
1239
1240// acosh
1241
1242template<class _Tp>
1243complex<_Tp>
1244acosh(const complex<_Tp>& __x)
1245{
1246 const _Tp __pi(atan2(+0., -0.));
1247 if (isinf(__x.real()))
1248 {
1249 if (isnan(__x.imag()))
1250 return complex<_Tp>(abs(__x.real()), __x.imag());
1251 if (isinf(__x.imag()))
1252 if (__x.real() > 0)
1253 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1254 else
1255 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1256 if (__x.real() < 0)
1257 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1258 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1259 }
1260 if (isnan(__x.real()))
1261 {
1262 if (isinf(__x.imag()))
1263 return complex<_Tp>(abs(__x.imag()), __x.real());
1264 return complex<_Tp>(__x.real(), __x.real());
1265 }
1266 if (isinf(__x.imag()))
1267 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1268 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1269 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1270}
1271
1272// atanh
1273
1274template<class _Tp>
1275complex<_Tp>
1276atanh(const complex<_Tp>& __x)
1277{
1278 const _Tp __pi(atan2(+0., -0.));
1279 if (isinf(__x.imag()))
1280 {
1281 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1282 }
1283 if (isnan(__x.imag()))
1284 {
1285 if (isinf(__x.real()) || __x.real() == 0)
1286 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1287 return complex<_Tp>(__x.imag(), __x.imag());
1288 }
1289 if (isnan(__x.real()))
1290 {
1291 return complex<_Tp>(__x.real(), __x.real());
1292 }
1293 if (isinf(__x.real()))
1294 {
1295 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1296 }
1297 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1298 {
1299 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1300 }
1301 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1302 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1303}
1304
1305// sinh
1306
1307template<class _Tp>
1308complex<_Tp>
1309sinh(const complex<_Tp>& __x)
1310{
1311 if (isinf(__x.real()) && !isfinite(__x.imag()))
1312 return complex<_Tp>(__x.real(), _Tp(NAN));
1313 if (__x.real() == 0 && !isfinite(__x.imag()))
1314 return complex<_Tp>(__x.real(), _Tp(NAN));
1315 if (__x.imag() == 0 && !isfinite(__x.real()))
1316 return __x;
1317 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1318}
1319
1320// cosh
1321
1322template<class _Tp>
1323complex<_Tp>
1324cosh(const complex<_Tp>& __x)
1325{
1326 if (isinf(__x.real()) && !isfinite(__x.imag()))
1327 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1328 if (__x.real() == 0 && !isfinite(__x.imag()))
1329 return complex<_Tp>(_Tp(NAN), __x.real());
1330 if (__x.real() == 0 && __x.imag() == 0)
1331 return complex<_Tp>(_Tp(1), __x.imag());
1332 if (__x.imag() == 0 && !isfinite(__x.real()))
1333 return complex<_Tp>(abs(__x.real()), __x.imag());
1334 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1335}
1336
1337// tanh
1338
1339template<class _Tp>
1340complex<_Tp>
1341tanh(const complex<_Tp>& __x)
1342{
1343 if (isinf(__x.real()))
1344 {
1345 if (!isfinite(__x.imag()))
1346 return complex<_Tp>(_Tp(1), _Tp(0));
1347 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1348 }
1349 if (isnan(__x.real()) && __x.imag() == 0)
1350 return __x;
1351 _Tp __2r(_Tp(2) * __x.real());
1352 _Tp __2i(_Tp(2) * __x.imag());
1353 _Tp __d(cosh(__2r) + cos(__2i));
1354 return complex<_Tp>(sinh(__2r)/__d, sin(__2i)/__d);
1355}
1356
1357// asin
1358
1359template<class _Tp>
1360complex<_Tp>
1361asin(const complex<_Tp>& __x)
1362{
1363 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1364 return complex<_Tp>(__z.imag(), -__z.real());
1365}
1366
1367// acos
1368
1369template<class _Tp>
1370complex<_Tp>
1371acos(const complex<_Tp>& __x)
1372{
1373 const _Tp __pi(atan2(+0., -0.));
1374 if (isinf(__x.real()))
1375 {
1376 if (isnan(__x.imag()))
1377 return complex<_Tp>(__x.imag(), __x.real());
1378 if (isinf(__x.imag()))
1379 {
1380 if (__x.real() < _Tp(0))
1381 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1382 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1383 }
1384 if (__x.real() < _Tp(0))
1385 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1386 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1387 }
1388 if (isnan(__x.real()))
1389 {
1390 if (isinf(__x.imag()))
1391 return complex<_Tp>(__x.real(), -__x.imag());
1392 return complex<_Tp>(__x.real(), __x.real());
1393 }
1394 if (isinf(__x.imag()))
1395 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1396 if (__x.real() == 0)
1397 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1398 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1399 if (signbit(__x.imag()))
1400 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1401 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1402}
1403
1404// atan
1405
1406template<class _Tp>
1407complex<_Tp>
1408atan(const complex<_Tp>& __x)
1409{
1410 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1411 return complex<_Tp>(__z.imag(), -__z.real());
1412}
1413
1414// sin
1415
1416template<class _Tp>
1417complex<_Tp>
1418sin(const complex<_Tp>& __x)
1419{
1420 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1421 return complex<_Tp>(__z.imag(), -__z.real());
1422}
1423
1424// cos
1425
1426template<class _Tp>
1427inline _LIBCPP_INLINE_VISIBILITY
1428complex<_Tp>
1429cos(const complex<_Tp>& __x)
1430{
1431 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1432}
1433
1434// tan
1435
1436template<class _Tp>
1437complex<_Tp>
1438tan(const complex<_Tp>& __x)
1439{
1440 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1441 return complex<_Tp>(__z.imag(), -__z.real());
1442}
1443
1444template<class _Tp, class _CharT, class _Traits>
1445basic_istream<_CharT, _Traits>&
1446operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1447{
1448 if (__is.good())
1449 {
1450 ws(__is);
1451 if (__is.peek() == _CharT('('))
1452 {
1453 __is.get();
1454 _Tp __r;
1455 __is >> __r;
1456 if (!__is.fail())
1457 {
1458 ws(__is);
1459 _CharT __c = __is.peek();
1460 if (__c == _CharT(','))
1461 {
1462 __is.get();
1463 _Tp __i;
1464 __is >> __i;
1465 if (!__is.fail())
1466 {
1467 ws(__is);
1468 __c = __is.peek();
1469 if (__c == _CharT(')'))
1470 {
1471 __is.get();
1472 __x = complex<_Tp>(__r, __i);
1473 }
1474 else
1475 __is.setstate(ios_base::failbit);
1476 }
1477 else
1478 __is.setstate(ios_base::failbit);
1479 }
1480 else if (__c == _CharT(')'))
1481 {
1482 __is.get();
1483 __x = complex<_Tp>(__r, _Tp(0));
1484 }
1485 else
1486 __is.setstate(ios_base::failbit);
1487 }
1488 else
1489 __is.setstate(ios_base::failbit);
1490 }
1491 else
1492 {
1493 _Tp __r;
1494 __is >> __r;
1495 if (!__is.fail())
1496 __x = complex<_Tp>(__r, _Tp(0));
1497 else
1498 __is.setstate(ios_base::failbit);
1499 }
1500 }
1501 else
1502 __is.setstate(ios_base::failbit);
1503 return __is;
1504}
1505
1506template<class _Tp, class _CharT, class _Traits>
1507basic_ostream<_CharT, _Traits>&
1508operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1509{
1510 basic_ostringstream<_CharT, _Traits> __s;
1511 __s.flags(__os.flags());
1512 __s.imbue(__os.getloc());
1513 __s.precision(__os.precision());
1514 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1515 return __os << __s.str();
1516}
1517
1518_LIBCPP_END_NAMESPACE_STD
1519
1520#endif // _LIBCPP_COMPLEX