complex revision 242945
1219019Sgabor// -*- C++ -*-
2219019Sgabor//===--------------------------- complex ----------------------------------===//
3219019Sgabor//
4219019Sgabor//                     The LLVM Compiler Infrastructure
5219019Sgabor//
6219019Sgabor// This file is dual licensed under the MIT and the University of Illinois Open
7219019Sgabor// Source Licenses. See LICENSE.TXT for details.
8219019Sgabor//
9219019Sgabor//===----------------------------------------------------------------------===//
10219019Sgabor
11219019Sgabor#ifndef _LIBCPP_COMPLEX
12219019Sgabor#define _LIBCPP_COMPLEX
13219019Sgabor
14219019Sgabor/*
15219019Sgabor    complex synopsis
16219019Sgabor
17219019Sgabornamespace std
18219019Sgabor{
19219019Sgabor
20219019Sgabortemplate<class T>
21219019Sgaborclass complex
22219019Sgabor{
23219019Sgaborpublic:
24219019Sgabor    typedef T value_type;
25219019Sgabor
26219019Sgabor    complex(const T& re = T(), const T& im = T());
27219019Sgabor    complex(const complex&);
28219019Sgabor    template<class X> complex(const complex<X>&);
29219019Sgabor
30219019Sgabor    T real() const;
31219019Sgabor    T imag() const;
32219019Sgabor
33219019Sgabor    void real(T);
34219019Sgabor    void imag(T);
35219019Sgabor
36219019Sgabor    complex<T>& operator= (const T&);
37219019Sgabor    complex<T>& operator+=(const T&);
38219019Sgabor    complex<T>& operator-=(const T&);
39219019Sgabor    complex<T>& operator*=(const T&);
40219019Sgabor    complex<T>& operator/=(const T&);
41219019Sgabor
42219019Sgabor    complex& operator=(const complex&);
43219019Sgabor    template<class X> complex<T>& operator= (const complex<X>&);
44219019Sgabor    template<class X> complex<T>& operator+=(const complex<X>&);
45219019Sgabor    template<class X> complex<T>& operator-=(const complex<X>&);
46219019Sgabor    template<class X> complex<T>& operator*=(const complex<X>&);
47219019Sgabor    template<class X> complex<T>& operator/=(const complex<X>&);
48219019Sgabor};
49219019Sgabor
50219019Sgabortemplate<>
51219019Sgaborclass complex<float>
52219019Sgabor{
53219019Sgaborpublic:
54219019Sgabor    typedef float value_type;
55219019Sgabor
56219019Sgabor    constexpr complex(float re = 0.0f, float im = 0.0f);
57219019Sgabor    explicit constexpr complex(const complex<double>&);
58219019Sgabor    explicit constexpr complex(const complex<long double>&);
59219019Sgabor
60219019Sgabor    constexpr float real() const;
61219019Sgabor    void real(float);
62219019Sgabor    constexpr float imag() const;
63219019Sgabor    void imag(float);
64219019Sgabor
65219019Sgabor    complex<float>& operator= (float);
66219019Sgabor    complex<float>& operator+=(float);
67219019Sgabor    complex<float>& operator-=(float);
68219019Sgabor    complex<float>& operator*=(float);
69219019Sgabor    complex<float>& operator/=(float);
70219019Sgabor
71219019Sgabor    complex<float>& operator=(const complex<float>&);
72219019Sgabor    template<class X> complex<float>& operator= (const complex<X>&);
73219019Sgabor    template<class X> complex<float>& operator+=(const complex<X>&);
74219019Sgabor    template<class X> complex<float>& operator-=(const complex<X>&);
75219019Sgabor    template<class X> complex<float>& operator*=(const complex<X>&);
76219019Sgabor    template<class X> complex<float>& operator/=(const complex<X>&);
77219019Sgabor};
78219019Sgabor
79219019Sgabortemplate<>
80219019Sgaborclass complex<double>
81219019Sgabor{
82219019Sgaborpublic:
83219019Sgabor    typedef double value_type;
84219019Sgabor
85219019Sgabor    constexpr complex(double re = 0.0, double im = 0.0);
86219019Sgabor    constexpr complex(const complex<float>&);
87219019Sgabor    explicit constexpr complex(const complex<long double>&);
88219019Sgabor
89219019Sgabor    constexpr double real() const;
90219019Sgabor    void real(double);
91219019Sgabor    constexpr double imag() const;
92219019Sgabor    void imag(double);
93219019Sgabor
94219019Sgabor    complex<double>& operator= (double);
95219019Sgabor    complex<double>& operator+=(double);
96219019Sgabor    complex<double>& operator-=(double);
97219019Sgabor    complex<double>& operator*=(double);
98219019Sgabor    complex<double>& operator/=(double);
99219019Sgabor    complex<double>& operator=(const complex<double>&);
100219019Sgabor
101219019Sgabor    template<class X> complex<double>& operator= (const complex<X>&);
102219019Sgabor    template<class X> complex<double>& operator+=(const complex<X>&);
103219019Sgabor    template<class X> complex<double>& operator-=(const complex<X>&);
104219019Sgabor    template<class X> complex<double>& operator*=(const complex<X>&);
105219019Sgabor    template<class X> complex<double>& operator/=(const complex<X>&);
106219019Sgabor};
107219019Sgabor
108219019Sgabortemplate<>
109219019Sgaborclass complex<long double>
110219019Sgabor{
111219019Sgaborpublic:
112219019Sgabor    typedef long double value_type;
113219019Sgabor
114219019Sgabor    constexpr complex(long double re = 0.0L, long double im = 0.0L);
115219019Sgabor    constexpr complex(const complex<float>&);
116219019Sgabor    constexpr complex(const complex<double>&);
117219019Sgabor
118219019Sgabor    constexpr long double real() const;
119219019Sgabor    void real(long double);
120219019Sgabor    constexpr long double imag() const;
121219019Sgabor    void imag(long double);
122219019Sgabor
123219019Sgabor    complex<long double>& operator=(const complex<long double>&);
124219019Sgabor    complex<long double>& operator= (long double);
125219019Sgabor    complex<long double>& operator+=(long double);
126219019Sgabor    complex<long double>& operator-=(long double);
127219019Sgabor    complex<long double>& operator*=(long double);
128219019Sgabor    complex<long double>& operator/=(long double);
129219019Sgabor
130219019Sgabor    template<class X> complex<long double>& operator= (const complex<X>&);
131219019Sgabor    template<class X> complex<long double>& operator+=(const complex<X>&);
132219019Sgabor    template<class X> complex<long double>& operator-=(const complex<X>&);
133219019Sgabor    template<class X> complex<long double>& operator*=(const complex<X>&);
134219019Sgabor    template<class X> complex<long double>& operator/=(const complex<X>&);
135219019Sgabor};
136219019Sgabor
137219019Sgabor// 26.3.6 operators:
138219019Sgabortemplate<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139219019Sgabortemplate<class T> complex<T> operator+(const complex<T>&, const T&);
140219019Sgabortemplate<class T> complex<T> operator+(const T&, const complex<T>&);
141219019Sgabortemplate<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142219019Sgabortemplate<class T> complex<T> operator-(const complex<T>&, const T&);
143219019Sgabortemplate<class T> complex<T> operator-(const T&, const complex<T>&);
144219019Sgabortemplate<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145219019Sgabortemplate<class T> complex<T> operator*(const complex<T>&, const T&);
146219019Sgabortemplate<class T> complex<T> operator*(const T&, const complex<T>&);
147219019Sgabortemplate<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148219019Sgabortemplate<class T> complex<T> operator/(const complex<T>&, const T&);
149219019Sgabortemplate<class T> complex<T> operator/(const T&, const complex<T>&);
150219019Sgabortemplate<class T> complex<T> operator+(const complex<T>&);
151219019Sgabortemplate<class T> complex<T> operator-(const complex<T>&);
152219019Sgabortemplate<class T> bool operator==(const complex<T>&, const complex<T>&);
153219019Sgabortemplate<class T> bool operator==(const complex<T>&, const T&);
154219019Sgabortemplate<class T> bool operator==(const T&, const complex<T>&);
155219019Sgabortemplate<class T> bool operator!=(const complex<T>&, const complex<T>&);
156219019Sgabortemplate<class T> bool operator!=(const complex<T>&, const T&);
157219019Sgabortemplate<class T> bool operator!=(const T&, const complex<T>&);
158219019Sgabor
159219019Sgabortemplate<class T, class charT, class traits>
160219019Sgabor  basic_istream<charT, traits>&
161219019Sgabor  operator>>(basic_istream<charT, traits>&, complex<T>&);
162219019Sgabortemplate<class T, class charT, class traits>
163219019Sgabor  basic_ostream<charT, traits>&
164219019Sgabor  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
165219019Sgabor
166219019Sgabor// 26.3.7 values:
167219019Sgabor
168219019Sgabortemplate<class T>              T real(const complex<T>&);
169219019Sgabor                     long double real(long double);
170219019Sgabor                          double real(double);
171219019Sgabortemplate<Integral T>      double real(T);
172219019Sgabor                          float  real(float);
173219019Sgabor
174219019Sgabortemplate<class T>              T imag(const complex<T>&);
175219019Sgabor                     long double imag(long double);
176219019Sgabor                          double imag(double);
177219019Sgabortemplate<Integral T>      double imag(T);
178219019Sgabor                          float  imag(float);
179219019Sgabor
180219019Sgabortemplate<class T> T abs(const complex<T>&);
181219019Sgabor
182219019Sgabortemplate<class T>              T arg(const complex<T>&);
183219019Sgabor                     long double arg(long double);
184219019Sgabor                          double arg(double);
185219019Sgabortemplate<Integral T>      double arg(T);
186219019Sgabor                          float  arg(float);
187219019Sgabor
188219019Sgabortemplate<class T>              T norm(const complex<T>&);
189219019Sgabor                     long double norm(long double);
190219019Sgabor                          double norm(double);
191219019Sgabortemplate<Integral T>      double norm(T);
192219019Sgabor                          float  norm(float);
193219019Sgabor
194219019Sgabortemplate<class T>      complex<T>           conj(const complex<T>&);
195219019Sgabor                       complex<long double> conj(long double);
196219019Sgabor                       complex<double>      conj(double);
197219019Sgabortemplate<Integral T>   complex<double>      conj(T);
198219019Sgabor                       complex<float>       conj(float);
199219019Sgabor
200219019Sgabortemplate<class T>    complex<T>           proj(const complex<T>&);
201219019Sgabor                     complex<long double> proj(long double);
202219019Sgabor                     complex<double>      proj(double);
203219019Sgabortemplate<Integral T> complex<double>      proj(T);
204219019Sgabor                     complex<float>       proj(float);
205219019Sgabor
206219019Sgabortemplate<class T> complex<T> polar(const T&, const T& = 0);
207219019Sgabor
208219019Sgabor// 26.3.8 transcendentals:
209219019Sgabortemplate<class T> complex<T> acos(const complex<T>&);
210219019Sgabortemplate<class T> complex<T> asin(const complex<T>&);
211219019Sgabortemplate<class T> complex<T> atan(const complex<T>&);
212219019Sgabortemplate<class T> complex<T> acosh(const complex<T>&);
213219019Sgabortemplate<class T> complex<T> asinh(const complex<T>&);
214219019Sgabortemplate<class T> complex<T> atanh(const complex<T>&);
215219019Sgabortemplate<class T> complex<T> cos (const complex<T>&);
216219019Sgabortemplate<class T> complex<T> cosh (const complex<T>&);
217219019Sgabortemplate<class T> complex<T> exp (const complex<T>&);
218219019Sgabortemplate<class T> complex<T> log (const complex<T>&);
219219019Sgabortemplate<class T> complex<T> log10(const complex<T>&);
220219019Sgabor
221219019Sgabortemplate<class T> complex<T> pow(const complex<T>&, const T&);
222219019Sgabortemplate<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223219019Sgabortemplate<class T> complex<T> pow(const T&, const complex<T>&);
224219019Sgabor
225219019Sgabortemplate<class T> complex<T> sin (const complex<T>&);
226219019Sgabortemplate<class T> complex<T> sinh (const complex<T>&);
227219019Sgabortemplate<class T> complex<T> sqrt (const complex<T>&);
228219019Sgabortemplate<class T> complex<T> tan (const complex<T>&);
229219019Sgabortemplate<class T> complex<T> tanh (const complex<T>&);
230219019Sgabor
231219019Sgabortemplate<class T, class charT, class traits>
232219019Sgabor  basic_istream<charT, traits>&
233219019Sgabor  operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234219019Sgabor
235219019Sgabortemplate<class T, class charT, class traits>
236219019Sgabor  basic_ostream<charT, traits>&
237219019Sgabor  operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238219019Sgabor
239219019Sgabor}  // std
240219019Sgabor
241219019Sgabor*/
242219019Sgabor
243219019Sgabor#include <__config>
244219019Sgabor#include <type_traits>
245219019Sgabor#include <stdexcept>
246219019Sgabor#include <cmath>
247219019Sgabor#include <sstream>
248219019Sgabor#if defined(_LIBCPP_NO_EXCEPTIONS)
249219019Sgabor    #include <cassert>
250219019Sgabor#endif
251219019Sgabor
252219019Sgabor#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
253219019Sgabor#pragma GCC system_header
254219019Sgabor#endif
255219019Sgabor
256219019Sgabor_LIBCPP_BEGIN_NAMESPACE_STD
257219019Sgabor
258219019Sgabortemplate<class _Tp> class _LIBCPP_VISIBLE complex;
259219019Sgabor
260219019Sgabortemplate<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261219019Sgabortemplate<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262219019Sgabor
263219019Sgabortemplate<class _Tp>
264219019Sgaborclass _LIBCPP_VISIBLE complex
265219019Sgabor{
266219019Sgaborpublic:
267219019Sgabor    typedef _Tp value_type;
268219019Sgaborprivate:
269219019Sgabor    value_type __re_;
270219019Sgabor    value_type __im_;
271219019Sgaborpublic:
272219019Sgabor    _LIBCPP_INLINE_VISIBILITY
273219019Sgabor    complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274219019Sgabor        : __re_(__re), __im_(__im) {}
275219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY
276219019Sgabor    complex(const complex<_Xp>& __c)
277219019Sgabor        : __re_(__c.real()), __im_(__c.imag()) {}
278219019Sgabor
279219019Sgabor    _LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;}
280219019Sgabor    _LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;}
281219019Sgabor
282219019Sgabor    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283219019Sgabor    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
284219019Sgabor
285219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
286219019Sgabor        {__re_ = __re; __im_ = value_type(); return *this;}
287219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
288219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
289219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
290219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
291219019Sgabor
292219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
293219019Sgabor        {
294219019Sgabor            __re_ = __c.real();
295219019Sgabor            __im_ = __c.imag();
296219019Sgabor            return *this;
297219019Sgabor        }
298219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
299219019Sgabor        {
300219019Sgabor            __re_ += __c.real();
301219019Sgabor            __im_ += __c.imag();
302219019Sgabor            return *this;
303219019Sgabor        }
304219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
305219019Sgabor        {
306219019Sgabor            __re_ -= __c.real();
307219019Sgabor            __im_ -= __c.imag();
308219019Sgabor            return *this;
309219019Sgabor        }
310219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
311219019Sgabor        {
312219019Sgabor            *this = *this * __c;
313219019Sgabor            return *this;
314219019Sgabor        }
315219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
316219019Sgabor        {
317219019Sgabor            *this = *this / __c;
318219019Sgabor            return *this;
319219019Sgabor        }
320219019Sgabor};
321219019Sgabor
322219019Sgabortemplate<> class _LIBCPP_VISIBLE complex<double>;
323219019Sgabortemplate<> class _LIBCPP_VISIBLE complex<long double>;
324219019Sgabor
325219019Sgabortemplate<>
326219019Sgaborclass _LIBCPP_VISIBLE complex<float>
327219019Sgabor{
328219019Sgabor    float __re_;
329219019Sgabor    float __im_;
330219019Sgaborpublic:
331219019Sgabor    typedef float value_type;
332219019Sgabor
333219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
334219019Sgabor        : __re_(__re), __im_(__im) {}
335219019Sgabor    explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
336219019Sgabor    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
337219019Sgabor
338219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
339219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
340219019Sgabor
341219019Sgabor    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
342219019Sgabor    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
343219019Sgabor
344219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
345219019Sgabor        {__re_ = __re; __im_ = value_type(); return *this;}
346219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
347219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
348219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
349219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
350219019Sgabor
351219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
352219019Sgabor        {
353219019Sgabor            __re_ = __c.real();
354219019Sgabor            __im_ = __c.imag();
355219019Sgabor            return *this;
356219019Sgabor        }
357219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
358219019Sgabor        {
359219019Sgabor            __re_ += __c.real();
360219019Sgabor            __im_ += __c.imag();
361219019Sgabor            return *this;
362219019Sgabor        }
363219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
364219019Sgabor        {
365219019Sgabor            __re_ -= __c.real();
366219019Sgabor            __im_ -= __c.imag();
367219019Sgabor            return *this;
368219019Sgabor        }
369219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
370219019Sgabor        {
371219019Sgabor            *this = *this * __c;
372219019Sgabor            return *this;
373219019Sgabor        }
374219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
375219019Sgabor        {
376219019Sgabor            *this = *this / __c;
377219019Sgabor            return *this;
378219019Sgabor        }
379219019Sgabor};
380219019Sgabor
381219019Sgabortemplate<>
382219019Sgaborclass _LIBCPP_VISIBLE complex<double>
383219019Sgabor{
384219019Sgabor    double __re_;
385219019Sgabor    double __im_;
386219019Sgaborpublic:
387219019Sgabor    typedef double value_type;
388219019Sgabor
389219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
390219019Sgabor        : __re_(__re), __im_(__im) {}
391219019Sgabor    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
392219019Sgabor    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
393219019Sgabor
394219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
395219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
396219019Sgabor
397219019Sgabor    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
398219019Sgabor    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
399219019Sgabor
400219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
401219019Sgabor        {__re_ = __re; __im_ = value_type(); return *this;}
402219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
403219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
404219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
405219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
406219019Sgabor
407219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
408219019Sgabor        {
409219019Sgabor            __re_ = __c.real();
410219019Sgabor            __im_ = __c.imag();
411219019Sgabor            return *this;
412219019Sgabor        }
413219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
414219019Sgabor        {
415219019Sgabor            __re_ += __c.real();
416219019Sgabor            __im_ += __c.imag();
417219019Sgabor            return *this;
418219019Sgabor        }
419219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
420219019Sgabor        {
421219019Sgabor            __re_ -= __c.real();
422219019Sgabor            __im_ -= __c.imag();
423219019Sgabor            return *this;
424219019Sgabor        }
425219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
426219019Sgabor        {
427219019Sgabor            *this = *this * __c;
428219019Sgabor            return *this;
429219019Sgabor        }
430219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
431219019Sgabor        {
432219019Sgabor            *this = *this / __c;
433219019Sgabor            return *this;
434219019Sgabor        }
435219019Sgabor};
436219019Sgabor
437219019Sgabortemplate<>
438219019Sgaborclass _LIBCPP_VISIBLE complex<long double>
439219019Sgabor{
440219019Sgabor    long double __re_;
441219019Sgabor    long double __im_;
442219019Sgaborpublic:
443219019Sgabor    typedef long double value_type;
444219019Sgabor
445219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
446219019Sgabor        : __re_(__re), __im_(__im) {}
447219019Sgabor    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
448219019Sgabor    _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
449219019Sgabor
450219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
451219019Sgabor    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
452219019Sgabor
453219019Sgabor    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
454219019Sgabor    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
455219019Sgabor
456219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
457219019Sgabor        {__re_ = __re; __im_ = value_type(); return *this;}
458219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
459219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
460219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
461219019Sgabor    _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
462219019Sgabor
463219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
464219019Sgabor        {
465219019Sgabor            __re_ = __c.real();
466219019Sgabor            __im_ = __c.imag();
467219019Sgabor            return *this;
468219019Sgabor        }
469219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
470219019Sgabor        {
471219019Sgabor            __re_ += __c.real();
472219019Sgabor            __im_ += __c.imag();
473219019Sgabor            return *this;
474219019Sgabor        }
475219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
476219019Sgabor        {
477219019Sgabor            __re_ -= __c.real();
478219019Sgabor            __im_ -= __c.imag();
479219019Sgabor            return *this;
480219019Sgabor        }
481219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
482219019Sgabor        {
483219019Sgabor            *this = *this * __c;
484219019Sgabor            return *this;
485219019Sgabor        }
486219019Sgabor    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
487219019Sgabor        {
488219019Sgabor            *this = *this / __c;
489219019Sgabor            return *this;
490219019Sgabor        }
491219019Sgabor};
492219019Sgabor
493219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
494219019Sgabor_LIBCPP_CONSTEXPR
495219019Sgaborcomplex<float>::complex(const complex<double>& __c)
496219019Sgabor    : __re_(__c.real()), __im_(__c.imag()) {}
497219019Sgabor
498219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
499219019Sgabor_LIBCPP_CONSTEXPR
500219019Sgaborcomplex<float>::complex(const complex<long double>& __c)
501219019Sgabor    : __re_(__c.real()), __im_(__c.imag()) {}
502219019Sgabor
503219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
504219019Sgabor_LIBCPP_CONSTEXPR
505219019Sgaborcomplex<double>::complex(const complex<float>& __c)
506219019Sgabor    : __re_(__c.real()), __im_(__c.imag()) {}
507219019Sgabor
508219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
509219019Sgabor_LIBCPP_CONSTEXPR
510219019Sgaborcomplex<double>::complex(const complex<long double>& __c)
511219019Sgabor    : __re_(__c.real()), __im_(__c.imag()) {}
512219019Sgabor
513219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
514219019Sgabor_LIBCPP_CONSTEXPR
515219019Sgaborcomplex<long double>::complex(const complex<float>& __c)
516219019Sgabor    : __re_(__c.real()), __im_(__c.imag()) {}
517219019Sgabor
518219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
519219019Sgabor_LIBCPP_CONSTEXPR
520219019Sgaborcomplex<long double>::complex(const complex<double>& __c)
521219019Sgabor    : __re_(__c.real()), __im_(__c.imag()) {}
522219019Sgabor
523219019Sgabor// 26.3.6 operators:
524219019Sgabor
525219019Sgabortemplate<class _Tp>
526219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
527219019Sgaborcomplex<_Tp>
528219019Sgaboroperator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
529219019Sgabor{
530219019Sgabor    complex<_Tp> __t(__x);
531219019Sgabor    __t += __y;
532219019Sgabor    return __t;
533219019Sgabor}
534219019Sgabor
535219019Sgabortemplate<class _Tp>
536219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
537219019Sgaborcomplex<_Tp>
538219019Sgaboroperator+(const complex<_Tp>& __x, const _Tp& __y)
539219019Sgabor{
540219019Sgabor    complex<_Tp> __t(__x);
541219019Sgabor    __t += __y;
542219019Sgabor    return __t;
543219019Sgabor}
544219019Sgabor
545219019Sgabortemplate<class _Tp>
546219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
547219019Sgaborcomplex<_Tp>
548219019Sgaboroperator+(const _Tp& __x, const complex<_Tp>& __y)
549219019Sgabor{
550219019Sgabor    complex<_Tp> __t(__y);
551219019Sgabor    __t += __x;
552219019Sgabor    return __t;
553219019Sgabor}
554219019Sgabor
555219019Sgabortemplate<class _Tp>
556219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
557219019Sgaborcomplex<_Tp>
558219019Sgaboroperator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
559219019Sgabor{
560219019Sgabor    complex<_Tp> __t(__x);
561219019Sgabor    __t -= __y;
562219019Sgabor    return __t;
563219019Sgabor}
564219019Sgabor
565219019Sgabortemplate<class _Tp>
566219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
567219019Sgaborcomplex<_Tp>
568219019Sgaboroperator-(const complex<_Tp>& __x, const _Tp& __y)
569219019Sgabor{
570219019Sgabor    complex<_Tp> __t(__x);
571219019Sgabor    __t -= __y;
572219019Sgabor    return __t;
573219019Sgabor}
574219019Sgabor
575219019Sgabortemplate<class _Tp>
576219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
577219019Sgaborcomplex<_Tp>
578219019Sgaboroperator-(const _Tp& __x, const complex<_Tp>& __y)
579219019Sgabor{
580219019Sgabor    complex<_Tp> __t(-__y);
581219019Sgabor    __t += __x;
582219019Sgabor    return __t;
583219019Sgabor}
584219019Sgabor
585219019Sgabortemplate<class _Tp>
586219019Sgaborcomplex<_Tp>
587219019Sgaboroperator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
588219019Sgabor{
589219019Sgabor    _Tp __a = __z.real();
590219019Sgabor    _Tp __b = __z.imag();
591219019Sgabor    _Tp __c = __w.real();
592219019Sgabor    _Tp __d = __w.imag();
593219019Sgabor    _Tp __ac = __a * __c;
594219019Sgabor    _Tp __bd = __b * __d;
595219019Sgabor    _Tp __ad = __a * __d;
596219019Sgabor    _Tp __bc = __b * __c;
597219019Sgabor    _Tp __x = __ac - __bd;
598219019Sgabor    _Tp __y = __ad + __bc;
599219019Sgabor    if (isnan(__x) && isnan(__y))
600219019Sgabor    {
601219019Sgabor        bool __recalc = false;
602219019Sgabor        if (isinf(__a) || isinf(__b))
603219019Sgabor        {
604219019Sgabor            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
605219019Sgabor            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
606219019Sgabor            if (isnan(__c))
607219019Sgabor                __c = copysign(_Tp(0), __c);
608219019Sgabor            if (isnan(__d))
609219019Sgabor                __d = copysign(_Tp(0), __d);
610219019Sgabor            __recalc = true;
611219019Sgabor        }
612219019Sgabor        if (isinf(__c) || isinf(__d))
613219019Sgabor        {
614219019Sgabor            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
615219019Sgabor            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
616219019Sgabor            if (isnan(__a))
617219019Sgabor                __a = copysign(_Tp(0), __a);
618219019Sgabor            if (isnan(__b))
619219019Sgabor                __b = copysign(_Tp(0), __b);
620219019Sgabor            __recalc = true;
621219019Sgabor        }
622219019Sgabor        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
623219019Sgabor                          isinf(__ad) || isinf(__bc)))
624219019Sgabor        {
625219019Sgabor            if (isnan(__a))
626219019Sgabor                __a = copysign(_Tp(0), __a);
627219019Sgabor            if (isnan(__b))
628219019Sgabor                __b = copysign(_Tp(0), __b);
629219019Sgabor            if (isnan(__c))
630219019Sgabor                __c = copysign(_Tp(0), __c);
631219019Sgabor            if (isnan(__d))
632219019Sgabor                __d = copysign(_Tp(0), __d);
633219019Sgabor            __recalc = true;
634219019Sgabor        }
635219019Sgabor        if (__recalc)
636219019Sgabor        {
637219019Sgabor            __x = _Tp(INFINITY) * (__a * __c - __b * __d);
638219019Sgabor            __y = _Tp(INFINITY) * (__a * __d + __b * __c);
639219019Sgabor        }
640219019Sgabor    }
641219019Sgabor    return complex<_Tp>(__x, __y);
642219019Sgabor}
643219019Sgabor
644219019Sgabortemplate<class _Tp>
645219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
646219019Sgaborcomplex<_Tp>
647219019Sgaboroperator*(const complex<_Tp>& __x, const _Tp& __y)
648219019Sgabor{
649219019Sgabor    complex<_Tp> __t(__x);
650219019Sgabor    __t *= __y;
651219019Sgabor    return __t;
652219019Sgabor}
653219019Sgabor
654219019Sgabortemplate<class _Tp>
655219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
656219019Sgaborcomplex<_Tp>
657219019Sgaboroperator*(const _Tp& __x, const complex<_Tp>& __y)
658219019Sgabor{
659219019Sgabor    complex<_Tp> __t(__y);
660219019Sgabor    __t *= __x;
661219019Sgabor    return __t;
662219019Sgabor}
663219019Sgabor
664219019Sgabortemplate<class _Tp>
665219019Sgaborcomplex<_Tp>
666219019Sgaboroperator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
667219019Sgabor{
668219019Sgabor    int __ilogbw = 0;
669219019Sgabor    _Tp __a = __z.real();
670219019Sgabor    _Tp __b = __z.imag();
671219019Sgabor    _Tp __c = __w.real();
672219019Sgabor    _Tp __d = __w.imag();
673219019Sgabor    _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
674219019Sgabor    if (isfinite(__logbw))
675219019Sgabor    {
676219019Sgabor        __ilogbw = static_cast<int>(__logbw);
677219019Sgabor        __c = scalbn(__c, -__ilogbw);
678219019Sgabor        __d = scalbn(__d, -__ilogbw);
679219019Sgabor    }
680219019Sgabor    _Tp __denom = __c * __c + __d * __d;
681219019Sgabor    _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
682219019Sgabor    _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
683219019Sgabor    if (isnan(__x) && isnan(__y))
684219019Sgabor    {
685219019Sgabor        if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
686219019Sgabor        {
687219019Sgabor            __x = copysign(_Tp(INFINITY), __c) * __a;
688219019Sgabor            __y = copysign(_Tp(INFINITY), __c) * __b;
689219019Sgabor        }
690219019Sgabor        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
691219019Sgabor        {
692219019Sgabor            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
693219019Sgabor            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
694219019Sgabor            __x = _Tp(INFINITY) * (__a * __c + __b * __d);
695219019Sgabor            __y = _Tp(INFINITY) * (__b * __c - __a * __d);
696219019Sgabor        }
697219019Sgabor        else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
698219019Sgabor        {
699219019Sgabor            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
700219019Sgabor            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
701219019Sgabor            __x = _Tp(0) * (__a * __c + __b * __d);
702219019Sgabor            __y = _Tp(0) * (__b * __c - __a * __d);
703219019Sgabor        }
704219019Sgabor    }
705219019Sgabor    return complex<_Tp>(__x, __y);
706219019Sgabor}
707219019Sgabor
708219019Sgabortemplate<class _Tp>
709219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
710219019Sgaborcomplex<_Tp>
711219019Sgaboroperator/(const complex<_Tp>& __x, const _Tp& __y)
712219019Sgabor{
713219019Sgabor    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
714219019Sgabor}
715219019Sgabor
716219019Sgabortemplate<class _Tp>
717219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
718219019Sgaborcomplex<_Tp>
719219019Sgaboroperator/(const _Tp& __x, const complex<_Tp>& __y)
720219019Sgabor{
721219019Sgabor    complex<_Tp> __t(__x);
722219019Sgabor    __t /= __y;
723219019Sgabor    return __t;
724219019Sgabor}
725219019Sgabor
726219019Sgabortemplate<class _Tp>
727219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
728219019Sgaborcomplex<_Tp>
729219019Sgaboroperator+(const complex<_Tp>& __x)
730219019Sgabor{
731219019Sgabor    return __x;
732219019Sgabor}
733219019Sgabor
734219019Sgabortemplate<class _Tp>
735219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
736219019Sgaborcomplex<_Tp>
737219019Sgaboroperator-(const complex<_Tp>& __x)
738219019Sgabor{
739219019Sgabor    return complex<_Tp>(-__x.real(), -__x.imag());
740219019Sgabor}
741219019Sgabor
742219019Sgabortemplate<class _Tp>
743219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
744219019Sgaborbool
745219019Sgaboroperator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
746219019Sgabor{
747219019Sgabor    return __x.real() == __y.real() && __x.imag() == __y.imag();
748219019Sgabor}
749219019Sgabor
750219019Sgabortemplate<class _Tp>
751219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
752219019Sgaborbool
753219019Sgaboroperator==(const complex<_Tp>& __x, const _Tp& __y)
754219019Sgabor{
755219019Sgabor    return __x.real() == __y && __x.imag() == 0;
756219019Sgabor}
757219019Sgabor
758219019Sgabortemplate<class _Tp>
759219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
760219019Sgaborbool
761219019Sgaboroperator==(const _Tp& __x, const complex<_Tp>& __y)
762219019Sgabor{
763219019Sgabor    return __x == __y.real() && 0 == __y.imag();
764219019Sgabor}
765219019Sgabor
766219019Sgabortemplate<class _Tp>
767219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
768219019Sgaborbool
769219019Sgaboroperator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
770219019Sgabor{
771219019Sgabor    return !(__x == __y);
772219019Sgabor}
773219019Sgabor
774219019Sgabortemplate<class _Tp>
775219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
776219019Sgaborbool
777219019Sgaboroperator!=(const complex<_Tp>& __x, const _Tp& __y)
778219019Sgabor{
779219019Sgabor    return !(__x == __y);
780219019Sgabor}
781219019Sgabor
782219019Sgabortemplate<class _Tp>
783219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
784219019Sgaborbool
785219019Sgaboroperator!=(const _Tp& __x, const complex<_Tp>& __y)
786219019Sgabor{
787219019Sgabor    return !(__x == __y);
788219019Sgabor}
789219019Sgabor
790219019Sgabor// 26.3.7 values:
791219019Sgabor
792219019Sgabor// real
793219019Sgabor
794219019Sgabortemplate<class _Tp>
795219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
796219019Sgabor_Tp
797219019Sgaborreal(const complex<_Tp>& __c)
798219019Sgabor{
799219019Sgabor    return __c.real();
800219019Sgabor}
801219019Sgabor
802219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
803219019Sgaborlong double
804219019Sgaborreal(long double __re)
805219019Sgabor{
806219019Sgabor    return __re;
807219019Sgabor}
808219019Sgabor
809219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
810219019Sgabordouble
811219019Sgaborreal(double __re)
812219019Sgabor{
813219019Sgabor    return __re;
814219019Sgabor}
815219019Sgabor
816219019Sgabortemplate<class _Tp>
817219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
818219019Sgabortypename enable_if
819219019Sgabor<
820219019Sgabor    is_integral<_Tp>::value,
821219019Sgabor    double
822219019Sgabor>::type
823219019Sgaborreal(_Tp  __re)
824219019Sgabor{
825219019Sgabor    return __re;
826219019Sgabor}
827219019Sgabor
828219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
829219019Sgaborfloat
830219019Sgaborreal(float  __re)
831219019Sgabor{
832219019Sgabor    return __re;
833219019Sgabor}
834219019Sgabor
835219019Sgabor// imag
836219019Sgabor
837219019Sgabortemplate<class _Tp>
838219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
839219019Sgabor_Tp
840219019Sgaborimag(const complex<_Tp>& __c)
841219019Sgabor{
842219019Sgabor    return __c.imag();
843219019Sgabor}
844219019Sgabor
845219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
846219019Sgaborlong double
847219019Sgaborimag(long double __re)
848219019Sgabor{
849219019Sgabor    return 0;
850219019Sgabor}
851219019Sgabor
852219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
853219019Sgabordouble
854219019Sgaborimag(double __re)
855219019Sgabor{
856219019Sgabor    return 0;
857219019Sgabor}
858219019Sgabor
859219019Sgabortemplate<class _Tp>
860219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
861219019Sgabortypename enable_if
862219019Sgabor<
863219019Sgabor    is_integral<_Tp>::value,
864219019Sgabor    double
865219019Sgabor>::type
866219019Sgaborimag(_Tp  __re)
867219019Sgabor{
868219019Sgabor    return 0;
869219019Sgabor}
870219019Sgabor
871219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
872219019Sgaborfloat
873219019Sgaborimag(float  __re)
874219019Sgabor{
875219019Sgabor    return 0;
876219019Sgabor}
877219019Sgabor
878219019Sgabor// abs
879219019Sgabor
880219019Sgabortemplate<class _Tp>
881219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
882219019Sgabor_Tp
883219019Sgaborabs(const complex<_Tp>& __c)
884219019Sgabor{
885219019Sgabor    return hypot(__c.real(), __c.imag());
886219019Sgabor}
887219019Sgabor
888219019Sgabor// arg
889219019Sgabor
890219019Sgabortemplate<class _Tp>
891219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
892219019Sgabor_Tp
893219019Sgaborarg(const complex<_Tp>& __c)
894219019Sgabor{
895219019Sgabor    return atan2(__c.imag(), __c.real());
896219019Sgabor}
897219019Sgabor
898219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
899219019Sgaborlong double
900219019Sgaborarg(long double __re)
901219019Sgabor{
902219019Sgabor    return atan2l(0.L, __re);
903219019Sgabor}
904219019Sgabor
905219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
906219019Sgabordouble
907219019Sgaborarg(double __re)
908219019Sgabor{
909219019Sgabor    return atan2(0., __re);
910219019Sgabor}
911219019Sgabor
912219019Sgabortemplate<class _Tp>
913219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
914219019Sgabortypename enable_if
915219019Sgabor<
916219019Sgabor    is_integral<_Tp>::value,
917219019Sgabor    double
918219019Sgabor>::type
919219019Sgaborarg(_Tp __re)
920219019Sgabor{
921219019Sgabor    return atan2(0., __re);
922219019Sgabor}
923219019Sgabor
924219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
925219019Sgaborfloat
926219019Sgaborarg(float __re)
927219019Sgabor{
928219019Sgabor    return atan2f(0.F, __re);
929219019Sgabor}
930219019Sgabor
931219019Sgabor// norm
932219019Sgabor
933219019Sgabortemplate<class _Tp>
934219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
935219019Sgabor_Tp
936219019Sgabornorm(const complex<_Tp>& __c)
937219019Sgabor{
938219019Sgabor    if (isinf(__c.real()))
939219019Sgabor        return abs(__c.real());
940219019Sgabor    if (isinf(__c.imag()))
941219019Sgabor        return abs(__c.imag());
942219019Sgabor    return __c.real() * __c.real() + __c.imag() * __c.imag();
943219019Sgabor}
944219019Sgabor
945219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
946219019Sgaborlong double
947219019Sgabornorm(long double __re)
948219019Sgabor{
949219019Sgabor    return __re * __re;
950219019Sgabor}
951219019Sgabor
952219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
953219019Sgabordouble
954219019Sgabornorm(double __re)
955219019Sgabor{
956219019Sgabor    return __re * __re;
957219019Sgabor}
958219019Sgabor
959219019Sgabortemplate<class _Tp>
960219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
961219019Sgabortypename enable_if
962219019Sgabor<
963219019Sgabor    is_integral<_Tp>::value,
964219019Sgabor    double
965219019Sgabor>::type
966219019Sgabornorm(_Tp __re)
967219019Sgabor{
968219019Sgabor    return (double)__re * __re;
969219019Sgabor}
970219019Sgabor
971219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
972219019Sgaborfloat
973219019Sgabornorm(float __re)
974219019Sgabor{
975219019Sgabor    return __re * __re;
976219019Sgabor}
977219019Sgabor
978219019Sgabor// conj
979219019Sgabor
980219019Sgabortemplate<class _Tp>
981219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
982219019Sgaborcomplex<_Tp>
983219019Sgaborconj(const complex<_Tp>& __c)
984219019Sgabor{
985219019Sgabor    return complex<_Tp>(__c.real(), -__c.imag());
986219019Sgabor}
987219019Sgabor
988219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
989219019Sgaborcomplex<long double>
990219019Sgaborconj(long double __re)
991219019Sgabor{
992219019Sgabor    return complex<long double>(__re);
993219019Sgabor}
994219019Sgabor
995219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
996219019Sgaborcomplex<double>
997219019Sgaborconj(double __re)
998219019Sgabor{
999219019Sgabor    return complex<double>(__re);
1000219019Sgabor}
1001219019Sgabor
1002219019Sgabortemplate<class _Tp>
1003219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1004219019Sgabortypename enable_if
1005219019Sgabor<
1006219019Sgabor    is_integral<_Tp>::value,
1007219019Sgabor    complex<double>
1008219019Sgabor>::type
1009219019Sgaborconj(_Tp __re)
1010219019Sgabor{
1011219019Sgabor    return complex<double>(__re);
1012219019Sgabor}
1013219019Sgabor
1014219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1015219019Sgaborcomplex<float>
1016219019Sgaborconj(float __re)
1017219019Sgabor{
1018219019Sgabor    return complex<float>(__re);
1019219019Sgabor}
1020219019Sgabor
1021219019Sgabor// proj
1022219019Sgabor
1023219019Sgabortemplate<class _Tp>
1024219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1025219019Sgaborcomplex<_Tp>
1026219019Sgaborproj(const complex<_Tp>& __c)
1027219019Sgabor{
1028219019Sgabor    std::complex<_Tp> __r = __c;
1029219019Sgabor    if (isinf(__c.real()) || isinf(__c.imag()))
1030219019Sgabor        __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1031219019Sgabor    return __r;
1032219019Sgabor}
1033219019Sgabor
1034219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1035219019Sgaborcomplex<long double>
1036219019Sgaborproj(long double __re)
1037219019Sgabor{
1038219019Sgabor    if (isinf(__re))
1039219019Sgabor        __re = abs(__re);
1040219019Sgabor    return complex<long double>(__re);
1041219019Sgabor}
1042219019Sgabor
1043219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1044219019Sgaborcomplex<double>
1045219019Sgaborproj(double __re)
1046219019Sgabor{
1047219019Sgabor    if (isinf(__re))
1048219019Sgabor        __re = abs(__re);
1049219019Sgabor    return complex<double>(__re);
1050219019Sgabor}
1051219019Sgabor
1052219019Sgabortemplate<class _Tp>
1053219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1054219019Sgabortypename enable_if
1055219019Sgabor<
1056219019Sgabor    is_integral<_Tp>::value,
1057219019Sgabor    complex<double>
1058219019Sgabor>::type
1059219019Sgaborproj(_Tp __re)
1060219019Sgabor{
1061219019Sgabor    return complex<double>(__re);
1062219019Sgabor}
1063219019Sgabor
1064219019Sgaborinline _LIBCPP_INLINE_VISIBILITY
1065219019Sgaborcomplex<float>
1066219019Sgaborproj(float __re)
1067219019Sgabor{
1068219019Sgabor    if (isinf(__re))
1069219019Sgabor        __re = abs(__re);
1070219019Sgabor    return complex<float>(__re);
1071219019Sgabor}
1072219019Sgabor
1073219019Sgabor// polar
1074219019Sgabor
1075219019Sgabortemplate<class _Tp>
1076219019Sgaborcomplex<_Tp>
1077219019Sgaborpolar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1078219019Sgabor{
1079219019Sgabor    if (isnan(__rho) || signbit(__rho))
1080219019Sgabor        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1081219019Sgabor    if (isnan(__theta))
1082219019Sgabor    {
1083219019Sgabor        if (isinf(__rho))
1084219019Sgabor            return complex<_Tp>(__rho, __theta);
1085219019Sgabor        return complex<_Tp>(__theta, __theta);
1086219019Sgabor    }
1087219019Sgabor    if (isinf(__theta))
1088219019Sgabor    {
1089219019Sgabor        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;
1182    return _VSTD::pow(result_type(__x), result_type(__y));
1183}
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;
1195    return _VSTD::pow(result_type(__x), result_type(__y));
1196}
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;
1208    return _VSTD::pow(result_type(__x), result_type(__y));
1209}
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        {
1253            if (__x.real() > 0)
1254                return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1255            else
1256                return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1257        }
1258        if (__x.real() < 0)
1259            return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1260        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1261    }
1262    if (isnan(__x.real()))
1263    {
1264        if (isinf(__x.imag()))
1265            return complex<_Tp>(abs(__x.imag()), __x.real());
1266        return complex<_Tp>(__x.real(), __x.real());
1267    }
1268    if (isinf(__x.imag()))
1269        return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1270    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1271    return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1272}
1273
1274// atanh
1275
1276template<class _Tp>
1277complex<_Tp>
1278atanh(const complex<_Tp>& __x)
1279{
1280    const _Tp __pi(atan2(+0., -0.));
1281    if (isinf(__x.imag()))
1282    {
1283        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1284    }
1285    if (isnan(__x.imag()))
1286    {
1287        if (isinf(__x.real()) || __x.real() == 0)
1288            return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1289        return complex<_Tp>(__x.imag(), __x.imag());
1290    }
1291    if (isnan(__x.real()))
1292    {
1293        return complex<_Tp>(__x.real(), __x.real());
1294    }
1295    if (isinf(__x.real()))
1296    {
1297        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1298    }
1299    if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1300    {
1301        return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1302    }
1303    complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1304    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1305}
1306
1307// sinh
1308
1309template<class _Tp>
1310complex<_Tp>
1311sinh(const complex<_Tp>& __x)
1312{
1313    if (isinf(__x.real()) && !isfinite(__x.imag()))
1314        return complex<_Tp>(__x.real(), _Tp(NAN));
1315    if (__x.real() == 0 && !isfinite(__x.imag()))
1316        return complex<_Tp>(__x.real(), _Tp(NAN));
1317    if (__x.imag() == 0 && !isfinite(__x.real()))
1318        return __x;
1319    return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1320}
1321
1322// cosh
1323
1324template<class _Tp>
1325complex<_Tp>
1326cosh(const complex<_Tp>& __x)
1327{
1328    if (isinf(__x.real()) && !isfinite(__x.imag()))
1329        return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1330    if (__x.real() == 0 && !isfinite(__x.imag()))
1331        return complex<_Tp>(_Tp(NAN), __x.real());
1332    if (__x.real() == 0 && __x.imag() == 0)
1333        return complex<_Tp>(_Tp(1), __x.imag());
1334    if (__x.imag() == 0 && !isfinite(__x.real()))
1335        return complex<_Tp>(abs(__x.real()), __x.imag());
1336    return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1337}
1338
1339// tanh
1340
1341template<class _Tp>
1342complex<_Tp>
1343tanh(const complex<_Tp>& __x)
1344{
1345    if (isinf(__x.real()))
1346    {
1347        if (!isfinite(__x.imag()))
1348            return complex<_Tp>(_Tp(1), _Tp(0));
1349        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1350    }
1351    if (isnan(__x.real()) && __x.imag() == 0)
1352        return __x;
1353    _Tp __2r(_Tp(2) * __x.real());
1354    _Tp __2i(_Tp(2) * __x.imag());
1355    _Tp __d(cosh(__2r) + cos(__2i));
1356    _Tp __2rsh(sinh(__2r));
1357    if (isinf(__2rsh) && isinf(__d))
1358        return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1359                            __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1360    return  complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1361}
1362
1363// asin
1364
1365template<class _Tp>
1366complex<_Tp>
1367asin(const complex<_Tp>& __x)
1368{
1369    complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1370    return complex<_Tp>(__z.imag(), -__z.real());
1371}
1372
1373// acos
1374
1375template<class _Tp>
1376complex<_Tp>
1377acos(const complex<_Tp>& __x)
1378{
1379    const _Tp __pi(atan2(+0., -0.));
1380    if (isinf(__x.real()))
1381    {
1382        if (isnan(__x.imag()))
1383            return complex<_Tp>(__x.imag(), __x.real());
1384        if (isinf(__x.imag()))
1385        {
1386            if (__x.real() < _Tp(0))
1387                return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1388            return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1389        }
1390        if (__x.real() < _Tp(0))
1391            return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1392        return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1393    }
1394    if (isnan(__x.real()))
1395    {
1396        if (isinf(__x.imag()))
1397            return complex<_Tp>(__x.real(), -__x.imag());
1398        return complex<_Tp>(__x.real(), __x.real());
1399    }
1400    if (isinf(__x.imag()))
1401        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1402    if (__x.real() == 0)
1403        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1404    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1405    if (signbit(__x.imag()))
1406        return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1407    return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1408}
1409
1410// atan
1411
1412template<class _Tp>
1413complex<_Tp>
1414atan(const complex<_Tp>& __x)
1415{
1416    complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1417    return complex<_Tp>(__z.imag(), -__z.real());
1418}
1419
1420// sin
1421
1422template<class _Tp>
1423complex<_Tp>
1424sin(const complex<_Tp>& __x)
1425{
1426    complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1427    return complex<_Tp>(__z.imag(), -__z.real());
1428}
1429
1430// cos
1431
1432template<class _Tp>
1433inline _LIBCPP_INLINE_VISIBILITY
1434complex<_Tp>
1435cos(const complex<_Tp>& __x)
1436{
1437    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1438}
1439
1440// tan
1441
1442template<class _Tp>
1443complex<_Tp>
1444tan(const complex<_Tp>& __x)
1445{
1446    complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1447    return complex<_Tp>(__z.imag(), -__z.real());
1448}
1449
1450template<class _Tp, class _CharT, class _Traits>
1451basic_istream<_CharT, _Traits>&
1452operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1453{
1454    if (__is.good())
1455    {
1456        ws(__is);
1457        if (__is.peek() == _CharT('('))
1458        {
1459            __is.get();
1460            _Tp __r;
1461            __is >> __r;
1462            if (!__is.fail())
1463            {
1464                ws(__is);
1465                _CharT __c = __is.peek();
1466                if (__c == _CharT(','))
1467                {
1468                    __is.get();
1469                    _Tp __i;
1470                    __is >> __i;
1471                    if (!__is.fail())
1472                    {
1473                        ws(__is);
1474                        __c = __is.peek();
1475                        if (__c == _CharT(')'))
1476                        {
1477                            __is.get();
1478                            __x = complex<_Tp>(__r, __i);
1479                        }
1480                        else
1481                            __is.setstate(ios_base::failbit);
1482                    }
1483                    else
1484                        __is.setstate(ios_base::failbit);
1485                }
1486                else if (__c == _CharT(')'))
1487                {
1488                    __is.get();
1489                    __x = complex<_Tp>(__r, _Tp(0));
1490                }
1491                else
1492                    __is.setstate(ios_base::failbit);
1493            }
1494            else
1495                __is.setstate(ios_base::failbit);
1496        }
1497        else
1498        {
1499            _Tp __r;
1500            __is >> __r;
1501            if (!__is.fail())
1502                __x = complex<_Tp>(__r, _Tp(0));
1503            else
1504                __is.setstate(ios_base::failbit);
1505        }
1506    }
1507    else
1508        __is.setstate(ios_base::failbit);
1509    return __is;
1510}
1511
1512template<class _Tp, class _CharT, class _Traits>
1513basic_ostream<_CharT, _Traits>&
1514operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1515{
1516    basic_ostringstream<_CharT, _Traits> __s;
1517    __s.flags(__os.flags());
1518    __s.imbue(__os.getloc());
1519    __s.precision(__os.precision());
1520    __s << '(' << __x.real() << ',' << __x.imag() << ')';
1521    return __os << __s.str();
1522}
1523
1524_LIBCPP_END_NAMESPACE_STD
1525
1526#endif  // _LIBCPP_COMPLEX
1527