blob: c8261e2efffcc262f458249b0aec4d97e11a29f6 [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
Marshall Clowa61e6f82013-07-31 21:02:34 +000026 complex(const T& re = T(), const T& im = T()); // constexpr in C++14
27 complex(const complex&); // constexpr in C++14
28 template<class X> complex(const complex<X>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029
Marshall Clowa61e6f82013-07-31 21:02:34 +000030 T real() const; // constexpr in C++14
31 T imag() const; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032
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>&);
Marshall Clowa61e6f82013-07-31 21:02:34 +0000152template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
153template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
154template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
155template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
156template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
157template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000158
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
Marshall Clowa61e6f82013-07-31 21:02:34 +0000168template<class T> T real(const complex<T>&); // constexpr in C++14
169 long double real(long double); // constexpr in C++14
170 double real(double); // constexpr in C++14
171template<Integral T> double real(T); // constexpr in C++14
172 float real(float); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173
Marshall Clowa61e6f82013-07-31 21:02:34 +0000174template<class T> T imag(const complex<T>&); // constexpr in C++14
175 long double imag(long double); // constexpr in C++14
176 double imag(double); // constexpr in C++14
177template<Integral T> double imag(T); // constexpr in C++14
178 float imag(float); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179
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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248
Howard Hinnant08e17472011-10-17 20:05:10 +0000249#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000251#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
253_LIBCPP_BEGIN_NAMESPACE_STD
254
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000255template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY complex;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256
257template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
258template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
259
260template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000261class _LIBCPP_TYPE_VIS_ONLY complex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262{
263public:
264 typedef _Tp value_type;
265private:
266 value_type __re_;
267 value_type __im_;
268public:
Marshall Clowa61e6f82013-07-31 21:02:34 +0000269 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
271 : __re_(__re), __im_(__im) {}
Marshall Clowa61e6f82013-07-31 21:02:34 +0000272 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273 complex(const complex<_Xp>& __c)
274 : __re_(__c.real()), __im_(__c.imag()) {}
275
Marshall Clowa61e6f82013-07-31 21:02:34 +0000276 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
279 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
280 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
281
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000282 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
283 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284 _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 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000309 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310 return *this;
311 }
312 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
313 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000314 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000315 return *this;
316 }
317};
318
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000319template<> class _LIBCPP_TYPE_VIS_ONLY complex<double>;
320template<> class _LIBCPP_TYPE_VIS_ONLY complex<long double>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321
322template<>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000323class _LIBCPP_TYPE_VIS_ONLY 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
Howard Hinnant410f2de2012-07-20 22:18:27 +0000330 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000333 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000335 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336
Howard Hinnant410f2de2012-07-20 22:18:27 +0000337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339
340 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
341 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
342
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000343 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
344 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
346 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
347 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
348 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
349
350 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
351 {
352 __re_ = __c.real();
353 __im_ = __c.imag();
354 return *this;
355 }
356 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
357 {
358 __re_ += __c.real();
359 __im_ += __c.imag();
360 return *this;
361 }
362 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
363 {
364 __re_ -= __c.real();
365 __im_ -= __c.imag();
366 return *this;
367 }
368 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
369 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000370 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371 return *this;
372 }
373 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
374 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000375 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376 return *this;
377 }
378};
379
380template<>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000381class _LIBCPP_TYPE_VIS_ONLY complex<double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000382{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 double __re_;
384 double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000385public:
386 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
Howard Hinnant410f2de2012-07-20 22:18:27 +0000388 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000391 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000393 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394
Howard Hinnant410f2de2012-07-20 22:18:27 +0000395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
396 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397
398 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
399 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
400
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000401 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
402 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
405 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
406 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; 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 __re_ -= __c.real();
423 __im_ -= __c.imag();
424 return *this;
425 }
426 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
427 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000428 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 return *this;
430 }
431 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
432 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000433 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 return *this;
435 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000436};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437
438template<>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000439class _LIBCPP_TYPE_VIS_ONLY complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000440{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000441 long double __re_;
442 long double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000443public:
444 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445
Howard Hinnant410f2de2012-07-20 22:18:27 +0000446 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000449 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000451 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452
Howard Hinnant410f2de2012-07-20 22:18:27 +0000453 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455
456 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
457 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
458
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000459 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
460 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
462 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
463 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
464 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
465
466 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
467 {
468 __re_ = __c.real();
469 __im_ = __c.imag();
470 return *this;
471 }
472 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
473 {
474 __re_ += __c.real();
475 __im_ += __c.imag();
476 return *this;
477 }
478 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
479 {
480 __re_ -= __c.real();
481 __im_ -= __c.imag();
482 return *this;
483 }
484 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
485 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000486 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487 return *this;
488 }
489 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
490 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000491 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492 return *this;
493 }
494};
495
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000496inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000497_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498complex<float>::complex(const complex<double>& __c)
499 : __re_(__c.real()), __im_(__c.imag()) {}
500
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000501inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000502_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503complex<float>::complex(const complex<long double>& __c)
504 : __re_(__c.real()), __im_(__c.imag()) {}
505
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000506inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000507_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508complex<double>::complex(const complex<float>& __c)
509 : __re_(__c.real()), __im_(__c.imag()) {}
510
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000511inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000512_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513complex<double>::complex(const complex<long double>& __c)
514 : __re_(__c.real()), __im_(__c.imag()) {}
515
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000516inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000517_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518complex<long double>::complex(const complex<float>& __c)
519 : __re_(__c.real()), __im_(__c.imag()) {}
520
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000521inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000522_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523complex<long double>::complex(const complex<double>& __c)
524 : __re_(__c.real()), __im_(__c.imag()) {}
525
526// 26.3.6 operators:
527
528template<class _Tp>
529inline _LIBCPP_INLINE_VISIBILITY
530complex<_Tp>
531operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
532{
533 complex<_Tp> __t(__x);
534 __t += __y;
535 return __t;
536}
537
538template<class _Tp>
539inline _LIBCPP_INLINE_VISIBILITY
540complex<_Tp>
541operator+(const complex<_Tp>& __x, const _Tp& __y)
542{
543 complex<_Tp> __t(__x);
544 __t += __y;
545 return __t;
546}
547
548template<class _Tp>
549inline _LIBCPP_INLINE_VISIBILITY
550complex<_Tp>
551operator+(const _Tp& __x, const complex<_Tp>& __y)
552{
553 complex<_Tp> __t(__y);
554 __t += __x;
555 return __t;
556}
557
558template<class _Tp>
559inline _LIBCPP_INLINE_VISIBILITY
560complex<_Tp>
561operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
562{
563 complex<_Tp> __t(__x);
564 __t -= __y;
565 return __t;
566}
567
568template<class _Tp>
569inline _LIBCPP_INLINE_VISIBILITY
570complex<_Tp>
571operator-(const complex<_Tp>& __x, const _Tp& __y)
572{
573 complex<_Tp> __t(__x);
574 __t -= __y;
575 return __t;
576}
577
578template<class _Tp>
579inline _LIBCPP_INLINE_VISIBILITY
580complex<_Tp>
581operator-(const _Tp& __x, const complex<_Tp>& __y)
582{
583 complex<_Tp> __t(-__y);
584 __t += __x;
585 return __t;
586}
587
588template<class _Tp>
589complex<_Tp>
590operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
591{
592 _Tp __a = __z.real();
593 _Tp __b = __z.imag();
594 _Tp __c = __w.real();
595 _Tp __d = __w.imag();
596 _Tp __ac = __a * __c;
597 _Tp __bd = __b * __d;
598 _Tp __ad = __a * __d;
599 _Tp __bc = __b * __c;
600 _Tp __x = __ac - __bd;
601 _Tp __y = __ad + __bc;
602 if (isnan(__x) && isnan(__y))
603 {
604 bool __recalc = false;
605 if (isinf(__a) || isinf(__b))
606 {
607 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
608 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
609 if (isnan(__c))
610 __c = copysign(_Tp(0), __c);
611 if (isnan(__d))
612 __d = copysign(_Tp(0), __d);
613 __recalc = true;
614 }
615 if (isinf(__c) || isinf(__d))
616 {
617 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
618 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
619 if (isnan(__a))
620 __a = copysign(_Tp(0), __a);
621 if (isnan(__b))
622 __b = copysign(_Tp(0), __b);
623 __recalc = true;
624 }
625 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
626 isinf(__ad) || isinf(__bc)))
627 {
628 if (isnan(__a))
629 __a = copysign(_Tp(0), __a);
630 if (isnan(__b))
631 __b = copysign(_Tp(0), __b);
632 if (isnan(__c))
633 __c = copysign(_Tp(0), __c);
634 if (isnan(__d))
635 __d = copysign(_Tp(0), __d);
636 __recalc = true;
637 }
638 if (__recalc)
639 {
640 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
641 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
642 }
643 }
644 return complex<_Tp>(__x, __y);
645}
646
647template<class _Tp>
648inline _LIBCPP_INLINE_VISIBILITY
649complex<_Tp>
650operator*(const complex<_Tp>& __x, const _Tp& __y)
651{
652 complex<_Tp> __t(__x);
653 __t *= __y;
654 return __t;
655}
656
657template<class _Tp>
658inline _LIBCPP_INLINE_VISIBILITY
659complex<_Tp>
660operator*(const _Tp& __x, const complex<_Tp>& __y)
661{
662 complex<_Tp> __t(__y);
663 __t *= __x;
664 return __t;
665}
666
667template<class _Tp>
668complex<_Tp>
669operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
670{
671 int __ilogbw = 0;
672 _Tp __a = __z.real();
673 _Tp __b = __z.imag();
674 _Tp __c = __w.real();
675 _Tp __d = __w.imag();
676 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
677 if (isfinite(__logbw))
678 {
679 __ilogbw = static_cast<int>(__logbw);
680 __c = scalbn(__c, -__ilogbw);
681 __d = scalbn(__d, -__ilogbw);
682 }
683 _Tp __denom = __c * __c + __d * __d;
684 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
685 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
686 if (isnan(__x) && isnan(__y))
687 {
688 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
689 {
690 __x = copysign(_Tp(INFINITY), __c) * __a;
691 __y = copysign(_Tp(INFINITY), __c) * __b;
692 }
693 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
694 {
695 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
696 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
697 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
698 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
699 }
700 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
701 {
702 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
703 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
704 __x = _Tp(0) * (__a * __c + __b * __d);
705 __y = _Tp(0) * (__b * __c - __a * __d);
706 }
707 }
708 return complex<_Tp>(__x, __y);
709}
710
711template<class _Tp>
712inline _LIBCPP_INLINE_VISIBILITY
713complex<_Tp>
714operator/(const complex<_Tp>& __x, const _Tp& __y)
715{
716 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
717}
718
719template<class _Tp>
720inline _LIBCPP_INLINE_VISIBILITY
721complex<_Tp>
722operator/(const _Tp& __x, const complex<_Tp>& __y)
723{
724 complex<_Tp> __t(__x);
725 __t /= __y;
726 return __t;
727}
728
729template<class _Tp>
730inline _LIBCPP_INLINE_VISIBILITY
731complex<_Tp>
732operator+(const complex<_Tp>& __x)
733{
734 return __x;
735}
736
737template<class _Tp>
738inline _LIBCPP_INLINE_VISIBILITY
739complex<_Tp>
740operator-(const complex<_Tp>& __x)
741{
742 return complex<_Tp>(-__x.real(), -__x.imag());
743}
744
745template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000746inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747bool
748operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
749{
750 return __x.real() == __y.real() && __x.imag() == __y.imag();
751}
752
753template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000754inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755bool
756operator==(const complex<_Tp>& __x, const _Tp& __y)
757{
758 return __x.real() == __y && __x.imag() == 0;
759}
760
761template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000762inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763bool
764operator==(const _Tp& __x, const complex<_Tp>& __y)
765{
766 return __x == __y.real() && 0 == __y.imag();
767}
768
769template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000770inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771bool
772operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
773{
774 return !(__x == __y);
775}
776
777template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000778inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779bool
780operator!=(const complex<_Tp>& __x, const _Tp& __y)
781{
782 return !(__x == __y);
783}
784
785template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000786inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787bool
788operator!=(const _Tp& __x, const complex<_Tp>& __y)
789{
790 return !(__x == __y);
791}
792
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793// 26.3.7 values:
794
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000795template <class _Tp, bool = is_integral<_Tp>::value,
796 bool = is_floating_point<_Tp>::value
797 >
798struct __libcpp_complex_overload_traits {};
799
800// Integral Types
801template <class _Tp>
802struct __libcpp_complex_overload_traits<_Tp, true, false>
803{
804 typedef double _ValueType;
805 typedef complex<double> _ComplexType;
806};
807
808// Floating point types
809template <class _Tp>
810struct __libcpp_complex_overload_traits<_Tp, false, true>
811{
812 typedef _Tp _ValueType;
813 typedef complex<_Tp> _ComplexType;
814};
815
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816// real
817
818template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000819inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000820_Tp
821real(const complex<_Tp>& __c)
822{
823 return __c.real();
824}
825
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000826template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000827inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000828typename __libcpp_complex_overload_traits<_Tp>::_ValueType
829real(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830{
831 return __re;
832}
833
834// imag
835
836template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000837inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838_Tp
839imag(const complex<_Tp>& __c)
840{
841 return __c.imag();
842}
843
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000844template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000845inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000846typename __libcpp_complex_overload_traits<_Tp>::_ValueType
847imag(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848{
849 return 0;
850}
851
852// abs
853
854template<class _Tp>
855inline _LIBCPP_INLINE_VISIBILITY
856_Tp
857abs(const complex<_Tp>& __c)
858{
859 return hypot(__c.real(), __c.imag());
860}
861
862// arg
863
864template<class _Tp>
865inline _LIBCPP_INLINE_VISIBILITY
866_Tp
867arg(const complex<_Tp>& __c)
868{
869 return atan2(__c.imag(), __c.real());
870}
871
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000872template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000874typename enable_if<
875 is_same<_Tp, long double>::value,
876 long double
877>::type
878arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879{
880 return atan2l(0.L, __re);
881}
882
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883template<class _Tp>
884inline _LIBCPP_INLINE_VISIBILITY
885typename enable_if
886<
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000887 is_integral<_Tp>::value || is_same<_Tp, double>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888 double
889>::type
890arg(_Tp __re)
891{
892 return atan2(0., __re);
893}
894
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000895template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000897typename enable_if<
898 is_same<_Tp, float>::value,
899 float
900>::type
901arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902{
903 return atan2f(0.F, __re);
904}
905
906// norm
907
908template<class _Tp>
909inline _LIBCPP_INLINE_VISIBILITY
910_Tp
911norm(const complex<_Tp>& __c)
912{
913 if (isinf(__c.real()))
914 return abs(__c.real());
915 if (isinf(__c.imag()))
916 return abs(__c.imag());
917 return __c.real() * __c.real() + __c.imag() * __c.imag();
918}
919
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000920template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000922typename __libcpp_complex_overload_traits<_Tp>::_ValueType
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923norm(_Tp __re)
924{
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000925 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
926 return static_cast<_ValueType>(__re) * __re;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927}
928
929// conj
930
931template<class _Tp>
932inline _LIBCPP_INLINE_VISIBILITY
933complex<_Tp>
934conj(const complex<_Tp>& __c)
935{
936 return complex<_Tp>(__c.real(), -__c.imag());
937}
938
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000939template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000941typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942conj(_Tp __re)
943{
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000944 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
945 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946}
947
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000948
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949
950// proj
951
952template<class _Tp>
953inline _LIBCPP_INLINE_VISIBILITY
954complex<_Tp>
955proj(const complex<_Tp>& __c)
956{
957 std::complex<_Tp> __r = __c;
958 if (isinf(__c.real()) || isinf(__c.imag()))
959 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
960 return __r;
961}
962
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000963template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000965typename enable_if
966<
967 is_floating_point<_Tp>::value,
968 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
969>::type
970proj(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971{
972 if (isinf(__re))
973 __re = abs(__re);
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000974 return complex<_Tp>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975}
976
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000977template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978inline _LIBCPP_INLINE_VISIBILITY
979typename enable_if
980<
981 is_integral<_Tp>::value,
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000982 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983>::type
984proj(_Tp __re)
985{
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000986 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
987 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988}
989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990
991// polar
992
993template<class _Tp>
994complex<_Tp>
995polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
996{
997 if (isnan(__rho) || signbit(__rho))
998 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
999 if (isnan(__theta))
1000 {
1001 if (isinf(__rho))
1002 return complex<_Tp>(__rho, __theta);
1003 return complex<_Tp>(__theta, __theta);
1004 }
1005 if (isinf(__theta))
1006 {
1007 if (isinf(__rho))
1008 return complex<_Tp>(__rho, _Tp(NAN));
1009 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1010 }
1011 _Tp __x = __rho * cos(__theta);
1012 if (isnan(__x))
1013 __x = 0;
1014 _Tp __y = __rho * sin(__theta);
1015 if (isnan(__y))
1016 __y = 0;
1017 return complex<_Tp>(__x, __y);
1018}
1019
1020// log
1021
1022template<class _Tp>
1023inline _LIBCPP_INLINE_VISIBILITY
1024complex<_Tp>
1025log(const complex<_Tp>& __x)
1026{
1027 return complex<_Tp>(log(abs(__x)), arg(__x));
1028}
1029
1030// log10
1031
1032template<class _Tp>
1033inline _LIBCPP_INLINE_VISIBILITY
1034complex<_Tp>
1035log10(const complex<_Tp>& __x)
1036{
1037 return log(__x) / log(_Tp(10));
1038}
1039
1040// sqrt
1041
1042template<class _Tp>
1043complex<_Tp>
1044sqrt(const complex<_Tp>& __x)
1045{
1046 if (isinf(__x.imag()))
1047 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1048 if (isinf(__x.real()))
1049 {
1050 if (__x.real() > _Tp(0))
1051 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1052 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1053 }
1054 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1055}
1056
1057// exp
1058
1059template<class _Tp>
1060complex<_Tp>
1061exp(const complex<_Tp>& __x)
1062{
1063 _Tp __i = __x.imag();
1064 if (isinf(__x.real()))
1065 {
1066 if (__x.real() < _Tp(0))
1067 {
1068 if (!isfinite(__i))
1069 __i = _Tp(1);
1070 }
1071 else if (__i == 0 || !isfinite(__i))
1072 {
1073 if (isinf(__i))
1074 __i = _Tp(NAN);
1075 return complex<_Tp>(__x.real(), __i);
1076 }
1077 }
1078 else if (isnan(__x.real()) && __x.imag() == 0)
1079 return __x;
1080 _Tp __e = exp(__x.real());
1081 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1082}
1083
1084// pow
1085
1086template<class _Tp>
1087inline _LIBCPP_INLINE_VISIBILITY
1088complex<_Tp>
1089pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1090{
1091 return exp(__y * log(__x));
1092}
1093
1094template<class _Tp, class _Up>
1095inline _LIBCPP_INLINE_VISIBILITY
1096complex<typename __promote<_Tp, _Up>::type>
1097pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1098{
1099 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001100 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101}
1102
1103template<class _Tp, class _Up>
1104inline _LIBCPP_INLINE_VISIBILITY
1105typename enable_if
1106<
1107 is_arithmetic<_Up>::value,
1108 complex<typename __promote<_Tp, _Up>::type>
1109>::type
1110pow(const complex<_Tp>& __x, const _Up& __y)
1111{
1112 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001113 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114}
1115
1116template<class _Tp, class _Up>
1117inline _LIBCPP_INLINE_VISIBILITY
1118typename enable_if
1119<
1120 is_arithmetic<_Tp>::value,
1121 complex<typename __promote<_Tp, _Up>::type>
1122>::type
1123pow(const _Tp& __x, const complex<_Up>& __y)
1124{
1125 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001126 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127}
1128
1129// asinh
1130
1131template<class _Tp>
1132complex<_Tp>
1133asinh(const complex<_Tp>& __x)
1134{
1135 const _Tp __pi(atan2(+0., -0.));
1136 if (isinf(__x.real()))
1137 {
1138 if (isnan(__x.imag()))
1139 return __x;
1140 if (isinf(__x.imag()))
1141 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1142 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1143 }
1144 if (isnan(__x.real()))
1145 {
1146 if (isinf(__x.imag()))
1147 return complex<_Tp>(__x.imag(), __x.real());
1148 if (__x.imag() == 0)
1149 return __x;
1150 return complex<_Tp>(__x.real(), __x.real());
1151 }
1152 if (isinf(__x.imag()))
1153 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1154 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1155 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1156}
1157
1158// acosh
1159
1160template<class _Tp>
1161complex<_Tp>
1162acosh(const complex<_Tp>& __x)
1163{
1164 const _Tp __pi(atan2(+0., -0.));
1165 if (isinf(__x.real()))
1166 {
1167 if (isnan(__x.imag()))
1168 return complex<_Tp>(abs(__x.real()), __x.imag());
1169 if (isinf(__x.imag()))
Howard Hinnant0919dba2012-11-06 21:55:44 +00001170 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 if (__x.real() > 0)
1172 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1173 else
1174 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
Howard Hinnant0919dba2012-11-06 21:55:44 +00001175 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176 if (__x.real() < 0)
1177 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1178 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1179 }
1180 if (isnan(__x.real()))
1181 {
1182 if (isinf(__x.imag()))
1183 return complex<_Tp>(abs(__x.imag()), __x.real());
1184 return complex<_Tp>(__x.real(), __x.real());
1185 }
1186 if (isinf(__x.imag()))
1187 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1188 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1189 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1190}
1191
1192// atanh
1193
1194template<class _Tp>
1195complex<_Tp>
1196atanh(const complex<_Tp>& __x)
1197{
1198 const _Tp __pi(atan2(+0., -0.));
1199 if (isinf(__x.imag()))
1200 {
1201 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1202 }
1203 if (isnan(__x.imag()))
1204 {
1205 if (isinf(__x.real()) || __x.real() == 0)
1206 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1207 return complex<_Tp>(__x.imag(), __x.imag());
1208 }
1209 if (isnan(__x.real()))
1210 {
1211 return complex<_Tp>(__x.real(), __x.real());
1212 }
1213 if (isinf(__x.real()))
1214 {
1215 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1216 }
1217 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1218 {
1219 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1220 }
1221 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1222 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1223}
1224
1225// sinh
1226
1227template<class _Tp>
1228complex<_Tp>
1229sinh(const complex<_Tp>& __x)
1230{
1231 if (isinf(__x.real()) && !isfinite(__x.imag()))
1232 return complex<_Tp>(__x.real(), _Tp(NAN));
1233 if (__x.real() == 0 && !isfinite(__x.imag()))
1234 return complex<_Tp>(__x.real(), _Tp(NAN));
1235 if (__x.imag() == 0 && !isfinite(__x.real()))
1236 return __x;
1237 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1238}
1239
1240// cosh
1241
1242template<class _Tp>
1243complex<_Tp>
1244cosh(const complex<_Tp>& __x)
1245{
1246 if (isinf(__x.real()) && !isfinite(__x.imag()))
1247 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1248 if (__x.real() == 0 && !isfinite(__x.imag()))
1249 return complex<_Tp>(_Tp(NAN), __x.real());
1250 if (__x.real() == 0 && __x.imag() == 0)
1251 return complex<_Tp>(_Tp(1), __x.imag());
1252 if (__x.imag() == 0 && !isfinite(__x.real()))
1253 return complex<_Tp>(abs(__x.real()), __x.imag());
1254 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1255}
1256
1257// tanh
1258
1259template<class _Tp>
1260complex<_Tp>
1261tanh(const complex<_Tp>& __x)
1262{
1263 if (isinf(__x.real()))
1264 {
1265 if (!isfinite(__x.imag()))
1266 return complex<_Tp>(_Tp(1), _Tp(0));
1267 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1268 }
1269 if (isnan(__x.real()) && __x.imag() == 0)
1270 return __x;
1271 _Tp __2r(_Tp(2) * __x.real());
1272 _Tp __2i(_Tp(2) * __x.imag());
1273 _Tp __d(cosh(__2r) + cos(__2i));
Howard Hinnant2d3f4ee2012-09-19 23:51:47 +00001274 _Tp __2rsh(sinh(__2r));
1275 if (isinf(__2rsh) && isinf(__d))
1276 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1277 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1278 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001279}
1280
1281// asin
1282
1283template<class _Tp>
1284complex<_Tp>
1285asin(const complex<_Tp>& __x)
1286{
1287 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1288 return complex<_Tp>(__z.imag(), -__z.real());
1289}
1290
1291// acos
1292
1293template<class _Tp>
1294complex<_Tp>
1295acos(const complex<_Tp>& __x)
1296{
1297 const _Tp __pi(atan2(+0., -0.));
1298 if (isinf(__x.real()))
1299 {
1300 if (isnan(__x.imag()))
1301 return complex<_Tp>(__x.imag(), __x.real());
1302 if (isinf(__x.imag()))
1303 {
1304 if (__x.real() < _Tp(0))
1305 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1306 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1307 }
1308 if (__x.real() < _Tp(0))
1309 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1310 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1311 }
1312 if (isnan(__x.real()))
1313 {
1314 if (isinf(__x.imag()))
1315 return complex<_Tp>(__x.real(), -__x.imag());
1316 return complex<_Tp>(__x.real(), __x.real());
1317 }
1318 if (isinf(__x.imag()))
1319 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
Marshall Clowa2778112016-04-04 16:08:54 +00001320 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1322 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1323 if (signbit(__x.imag()))
1324 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1325 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1326}
1327
1328// atan
1329
1330template<class _Tp>
1331complex<_Tp>
1332atan(const complex<_Tp>& __x)
1333{
1334 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1335 return complex<_Tp>(__z.imag(), -__z.real());
1336}
1337
1338// sin
1339
1340template<class _Tp>
1341complex<_Tp>
1342sin(const complex<_Tp>& __x)
1343{
1344 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1345 return complex<_Tp>(__z.imag(), -__z.real());
1346}
1347
1348// cos
1349
1350template<class _Tp>
1351inline _LIBCPP_INLINE_VISIBILITY
1352complex<_Tp>
1353cos(const complex<_Tp>& __x)
1354{
1355 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1356}
1357
1358// tan
1359
1360template<class _Tp>
1361complex<_Tp>
1362tan(const complex<_Tp>& __x)
1363{
1364 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1365 return complex<_Tp>(__z.imag(), -__z.real());
1366}
1367
1368template<class _Tp, class _CharT, class _Traits>
1369basic_istream<_CharT, _Traits>&
1370operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1371{
1372 if (__is.good())
1373 {
1374 ws(__is);
1375 if (__is.peek() == _CharT('('))
1376 {
1377 __is.get();
1378 _Tp __r;
1379 __is >> __r;
1380 if (!__is.fail())
1381 {
1382 ws(__is);
1383 _CharT __c = __is.peek();
1384 if (__c == _CharT(','))
1385 {
1386 __is.get();
1387 _Tp __i;
1388 __is >> __i;
1389 if (!__is.fail())
1390 {
1391 ws(__is);
1392 __c = __is.peek();
1393 if (__c == _CharT(')'))
1394 {
1395 __is.get();
1396 __x = complex<_Tp>(__r, __i);
1397 }
1398 else
1399 __is.setstate(ios_base::failbit);
1400 }
1401 else
1402 __is.setstate(ios_base::failbit);
1403 }
1404 else if (__c == _CharT(')'))
1405 {
1406 __is.get();
1407 __x = complex<_Tp>(__r, _Tp(0));
1408 }
1409 else
1410 __is.setstate(ios_base::failbit);
1411 }
1412 else
1413 __is.setstate(ios_base::failbit);
1414 }
1415 else
1416 {
1417 _Tp __r;
1418 __is >> __r;
1419 if (!__is.fail())
1420 __x = complex<_Tp>(__r, _Tp(0));
1421 else
1422 __is.setstate(ios_base::failbit);
1423 }
1424 }
1425 else
1426 __is.setstate(ios_base::failbit);
1427 return __is;
1428}
1429
1430template<class _Tp, class _CharT, class _Traits>
1431basic_ostream<_CharT, _Traits>&
1432operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1433{
1434 basic_ostringstream<_CharT, _Traits> __s;
1435 __s.flags(__os.flags());
1436 __s.imbue(__os.getloc());
1437 __s.precision(__os.precision());
1438 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1439 return __os << __s.str();
1440}
1441
Marshall Clow320c80f2013-10-05 21:19:49 +00001442#if _LIBCPP_STD_VER > 11
1443// Literal suffix for complex number literals [complex.literals]
1444inline namespace literals
1445{
1446 inline namespace complex_literals
1447 {
1448 constexpr complex<long double> operator""il(long double __im)
1449 {
1450 return { 0.0l, __im };
1451 }
1452
1453 constexpr complex<long double> operator""il(unsigned long long __im)
1454 {
1455 return { 0.0l, static_cast<long double>(__im) };
1456 }
1457
1458
1459 constexpr complex<double> operator""i(long double __im)
1460 {
1461 return { 0.0, static_cast<double>(__im) };
1462 }
1463
1464 constexpr complex<double> operator""i(unsigned long long __im)
1465 {
1466 return { 0.0, static_cast<double>(__im) };
1467 }
1468
1469
1470 constexpr complex<float> operator""if(long double __im)
1471 {
1472 return { 0.0f, static_cast<float>(__im) };
1473 }
1474
1475 constexpr complex<float> operator""if(unsigned long long __im)
1476 {
1477 return { 0.0f, static_cast<float>(__im) };
1478 }
1479 }
1480}
1481#endif
1482
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483_LIBCPP_END_NAMESPACE_STD
1484
1485#endif // _LIBCPP_COMPLEX