complex revision 227825
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===--------------------------- complex ----------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_COMPLEX
12227825Stheraven#define _LIBCPP_COMPLEX
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    complex synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheraventemplate<class T>
21227825Stheravenclass complex
22227825Stheraven{
23227825Stheravenpublic:
24227825Stheraven    typedef T value_type;
25227825Stheraven
26227825Stheraven    complex(const T& re = T(), const T& im = T());
27227825Stheraven    complex(const complex&);
28227825Stheraven    template<class X> complex(const complex<X>&);
29227825Stheraven
30227825Stheraven    T real() const;
31227825Stheraven    T imag() const;
32227825Stheraven
33227825Stheraven    void real(T);
34227825Stheraven    void imag(T);
35227825Stheraven
36227825Stheraven    complex<T>& operator= (const T&);
37227825Stheraven    complex<T>& operator+=(const T&);
38227825Stheraven    complex<T>& operator-=(const T&);
39227825Stheraven    complex<T>& operator*=(const T&);
40227825Stheraven    complex<T>& operator/=(const T&);
41227825Stheraven
42227825Stheraven    complex& operator=(const complex&);
43227825Stheraven    template<class X> complex<T>& operator= (const complex<X>&);
44227825Stheraven    template<class X> complex<T>& operator+=(const complex<X>&);
45227825Stheraven    template<class X> complex<T>& operator-=(const complex<X>&);
46227825Stheraven    template<class X> complex<T>& operator*=(const complex<X>&);
47227825Stheraven    template<class X> complex<T>& operator/=(const complex<X>&);
48227825Stheraven};
49227825Stheraven
50227825Stheraventemplate<>
51227825Stheravenclass complex<float>
52227825Stheraven{
53227825Stheravenpublic:
54227825Stheraven    typedef float value_type;
55227825Stheraven
56227825Stheraven    constexpr complex(float re = 0.0f, float im = 0.0f);
57227825Stheraven    explicit constexpr complex(const complex<double>&);
58227825Stheraven    explicit constexpr complex(const complex<long double>&);
59227825Stheraven
60227825Stheraven    constexpr float real() const;
61227825Stheraven    void real(float);
62227825Stheraven    constexpr float imag() const;
63227825Stheraven    void imag(float);
64227825Stheraven
65227825Stheraven    complex<float>& operator= (float);
66227825Stheraven    complex<float>& operator+=(float);
67227825Stheraven    complex<float>& operator-=(float);
68227825Stheraven    complex<float>& operator*=(float);
69227825Stheraven    complex<float>& operator/=(float);
70227825Stheraven
71227825Stheraven    complex<float>& operator=(const complex<float>&);
72227825Stheraven    template<class X> complex<float>& operator= (const complex<X>&);
73227825Stheraven    template<class X> complex<float>& operator+=(const complex<X>&);
74227825Stheraven    template<class X> complex<float>& operator-=(const complex<X>&);
75227825Stheraven    template<class X> complex<float>& operator*=(const complex<X>&);
76227825Stheraven    template<class X> complex<float>& operator/=(const complex<X>&);
77227825Stheraven};
78227825Stheraven
79227825Stheraventemplate<>
80227825Stheravenclass complex<double>
81227825Stheraven{
82227825Stheravenpublic:
83227825Stheraven    typedef double value_type;
84227825Stheraven
85227825Stheraven    constexpr complex(double re = 0.0, double im = 0.0);
86227825Stheraven    constexpr complex(const complex<float>&);
87227825Stheraven    explicit constexpr complex(const complex<long double>&);
88227825Stheraven
89227825Stheraven    constexpr double real() const;
90227825Stheraven    void real(double);
91227825Stheraven    constexpr double imag() const;
92227825Stheraven    void imag(double);
93227825Stheraven
94227825Stheraven    complex<double>& operator= (double);
95227825Stheraven    complex<double>& operator+=(double);
96227825Stheraven    complex<double>& operator-=(double);
97227825Stheraven    complex<double>& operator*=(double);
98227825Stheraven    complex<double>& operator/=(double);
99227825Stheraven    complex<double>& operator=(const complex<double>&);
100227825Stheraven
101227825Stheraven    template<class X> complex<double>& operator= (const complex<X>&);
102227825Stheraven    template<class X> complex<double>& operator+=(const complex<X>&);
103227825Stheraven    template<class X> complex<double>& operator-=(const complex<X>&);
104227825Stheraven    template<class X> complex<double>& operator*=(const complex<X>&);
105227825Stheraven    template<class X> complex<double>& operator/=(const complex<X>&);
106227825Stheraven};
107227825Stheraven
108227825Stheraventemplate<>
109227825Stheravenclass complex<long double>
110227825Stheraven{
111227825Stheravenpublic:
112227825Stheraven    typedef long double value_type;
113227825Stheraven
114227825Stheraven    constexpr complex(long double re = 0.0L, long double im = 0.0L);
115227825Stheraven    constexpr complex(const complex<float>&);
116227825Stheraven    constexpr complex(const complex<double>&);
117227825Stheraven
118227825Stheraven    constexpr long double real() const;
119227825Stheraven    void real(long double);
120227825Stheraven    constexpr long double imag() const;
121227825Stheraven    void imag(long double);
122227825Stheraven
123227825Stheraven    complex<long double>& operator=(const complex<long double>&);
124227825Stheraven    complex<long double>& operator= (long double);
125227825Stheraven    complex<long double>& operator+=(long double);
126227825Stheraven    complex<long double>& operator-=(long double);
127227825Stheraven    complex<long double>& operator*=(long double);
128227825Stheraven    complex<long double>& operator/=(long double);
129227825Stheraven
130227825Stheraven    template<class X> complex<long double>& operator= (const complex<X>&);
131227825Stheraven    template<class X> complex<long double>& operator+=(const complex<X>&);
132227825Stheraven    template<class X> complex<long double>& operator-=(const complex<X>&);
133227825Stheraven    template<class X> complex<long double>& operator*=(const complex<X>&);
134227825Stheraven    template<class X> complex<long double>& operator/=(const complex<X>&);
135227825Stheraven};
136227825Stheraven
137227825Stheraven// 26.3.6 operators:
138227825Stheraventemplate<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139227825Stheraventemplate<class T> complex<T> operator+(const complex<T>&, const T&);
140227825Stheraventemplate<class T> complex<T> operator+(const T&, const complex<T>&);
141227825Stheraventemplate<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142227825Stheraventemplate<class T> complex<T> operator-(const complex<T>&, const T&);
143227825Stheraventemplate<class T> complex<T> operator-(const T&, const complex<T>&);
144227825Stheraventemplate<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145227825Stheraventemplate<class T> complex<T> operator*(const complex<T>&, const T&);
146227825Stheraventemplate<class T> complex<T> operator*(const T&, const complex<T>&);
147227825Stheraventemplate<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148227825Stheraventemplate<class T> complex<T> operator/(const complex<T>&, const T&);
149227825Stheraventemplate<class T> complex<T> operator/(const T&, const complex<T>&);
150227825Stheraventemplate<class T> complex<T> operator+(const complex<T>&);
151227825Stheraventemplate<class T> complex<T> operator-(const complex<T>&);
152227825Stheraventemplate<class T> bool operator==(const complex<T>&, const complex<T>&);
153227825Stheraventemplate<class T> bool operator==(const complex<T>&, const T&);
154227825Stheraventemplate<class T> bool operator==(const T&, const complex<T>&);
155227825Stheraventemplate<class T> bool operator!=(const complex<T>&, const complex<T>&);
156227825Stheraventemplate<class T> bool operator!=(const complex<T>&, const T&);
157227825Stheraventemplate<class T> bool operator!=(const T&, const complex<T>&);
158227825Stheraven
159227825Stheraventemplate<class T, class charT, class traits>
160227825Stheraven  basic_istream<charT, traits>&
161227825Stheraven  operator>>(basic_istream<charT, traits>&, complex<T>&);
162227825Stheraventemplate<class T, class charT, class traits>
163227825Stheraven  basic_ostream<charT, traits>&
164227825Stheraven  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
165227825Stheraven
166227825Stheraven// 26.3.7 values:
167227825Stheraven
168227825Stheraventemplate<class T>              T real(const complex<T>&);
169227825Stheraven                     long double real(long double);
170227825Stheraven                          double real(double);
171227825Stheraventemplate<Integral T>      double real(T);
172227825Stheraven                          float  real(float);
173227825Stheraven
174227825Stheraventemplate<class T>              T imag(const complex<T>&);
175227825Stheraven                     long double imag(long double);
176227825Stheraven                          double imag(double);
177227825Stheraventemplate<Integral T>      double imag(T);
178227825Stheraven                          float  imag(float);
179227825Stheraven
180227825Stheraventemplate<class T> T abs(const complex<T>&);
181227825Stheraven
182227825Stheraventemplate<class T>              T arg(const complex<T>&);
183227825Stheraven                     long double arg(long double);
184227825Stheraven                          double arg(double);
185227825Stheraventemplate<Integral T>      double arg(T);
186227825Stheraven                          float  arg(float);
187227825Stheraven
188227825Stheraventemplate<class T>              T norm(const complex<T>&);
189227825Stheraven                     long double norm(long double);
190227825Stheraven                          double norm(double);
191227825Stheraventemplate<Integral T>      double norm(T);
192227825Stheraven                          float  norm(float);
193227825Stheraven
194227825Stheraventemplate<class T>      complex<T>           conj(const complex<T>&);
195227825Stheraven                       complex<long double> conj(long double);
196227825Stheraven                       complex<double>      conj(double);
197227825Stheraventemplate<Integral T>   complex<double>      conj(T);
198227825Stheraven                       complex<float>       conj(float);
199227825Stheraven
200227825Stheraventemplate<class T>    complex<T>           proj(const complex<T>&);
201227825Stheraven                     complex<long double> proj(long double);
202227825Stheraven                     complex<double>      proj(double);
203227825Stheraventemplate<Integral T> complex<double>      proj(T);
204227825Stheraven                     complex<float>       proj(float);
205227825Stheraven
206227825Stheraventemplate<class T> complex<T> polar(const T&, const T& = 0);
207227825Stheraven
208227825Stheraven// 26.3.8 transcendentals:
209227825Stheraventemplate<class T> complex<T> acos(const complex<T>&);
210227825Stheraventemplate<class T> complex<T> asin(const complex<T>&);
211227825Stheraventemplate<class T> complex<T> atan(const complex<T>&);
212227825Stheraventemplate<class T> complex<T> acosh(const complex<T>&);
213227825Stheraventemplate<class T> complex<T> asinh(const complex<T>&);
214227825Stheraventemplate<class T> complex<T> atanh(const complex<T>&);
215227825Stheraventemplate<class T> complex<T> cos (const complex<T>&);
216227825Stheraventemplate<class T> complex<T> cosh (const complex<T>&);
217227825Stheraventemplate<class T> complex<T> exp (const complex<T>&);
218227825Stheraventemplate<class T> complex<T> log (const complex<T>&);
219227825Stheraventemplate<class T> complex<T> log10(const complex<T>&);
220227825Stheraven
221227825Stheraventemplate<class T> complex<T> pow(const complex<T>&, const T&);
222227825Stheraventemplate<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223227825Stheraventemplate<class T> complex<T> pow(const T&, const complex<T>&);
224227825Stheraven
225227825Stheraventemplate<class T> complex<T> sin (const complex<T>&);
226227825Stheraventemplate<class T> complex<T> sinh (const complex<T>&);
227227825Stheraventemplate<class T> complex<T> sqrt (const complex<T>&);
228227825Stheraventemplate<class T> complex<T> tan (const complex<T>&);
229227825Stheraventemplate<class T> complex<T> tanh (const complex<T>&);
230227825Stheraven
231227825Stheraventemplate<class T, class charT, class traits>
232227825Stheraven  basic_istream<charT, traits>&
233227825Stheraven  operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234227825Stheraven
235227825Stheraventemplate<class T, class charT, class traits>
236227825Stheraven  basic_ostream<charT, traits>&
237227825Stheraven  operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238227825Stheraven
239227825Stheraven}  // std
240227825Stheraven
241227825Stheraven*/
242227825Stheraven
243227825Stheraven#include <__config>
244227825Stheraven#include <type_traits>
245227825Stheraven#include <stdexcept>
246227825Stheraven#include <cmath>
247227825Stheraven#include <sstream>
248227825Stheraven#if defined(_LIBCPP_NO_EXCEPTIONS)
249227825Stheraven    #include <cassert>
250227825Stheraven#endif
251227825Stheraven
252227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
253227825Stheraven#pragma GCC system_header
254227825Stheraven#endif
255227825Stheraven
256227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
257227825Stheraven
258227825Stheraventemplate<class _Tp> class _LIBCPP_VISIBLE complex;
259227825Stheraven
260227825Stheraventemplate<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261227825Stheraventemplate<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262227825Stheraven
263227825Stheraventemplate<class _Tp>
264227825Stheravenclass _LIBCPP_VISIBLE complex
265227825Stheraven{
266227825Stheravenpublic:
267227825Stheraven    typedef _Tp value_type;
268227825Stheravenprivate:
269227825Stheraven    value_type __re_;
270227825Stheraven    value_type __im_;
271227825Stheravenpublic:
272227825Stheraven    _LIBCPP_INLINE_VISIBILITY
273227825Stheraven    complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274227825Stheraven        : __re_(__re), __im_(__im) {}
275227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY
276227825Stheraven    complex(const complex<_Xp>& __c)
277227825Stheraven        : __re_(__c.real()), __im_(__c.imag()) {}
278227825Stheraven
279227825Stheraven    _LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;}
280227825Stheraven    _LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;}
281227825Stheraven
282227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
284227825Stheraven
285227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re) {__re_ = __re; return *this;}
286227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
287227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
288227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
289227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
290227825Stheraven
291227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
292227825Stheraven        {
293227825Stheraven            __re_ = __c.real();
294227825Stheraven            __im_ = __c.imag();
295227825Stheraven            return *this;
296227825Stheraven        }
297227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
298227825Stheraven        {
299227825Stheraven            __re_ += __c.real();
300227825Stheraven            __im_ += __c.imag();
301227825Stheraven            return *this;
302227825Stheraven        }
303227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
304227825Stheraven        {
305227825Stheraven            __re_ -= __c.real();
306227825Stheraven            __im_ -= __c.imag();
307227825Stheraven            return *this;
308227825Stheraven        }
309227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
310227825Stheraven        {
311227825Stheraven            *this = *this * __c;
312227825Stheraven            return *this;
313227825Stheraven        }
314227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
315227825Stheraven        {
316227825Stheraven            *this = *this / __c;
317227825Stheraven            return *this;
318227825Stheraven        }
319227825Stheraven};
320227825Stheraven
321227825Stheraventemplate<> class _LIBCPP_VISIBLE complex<double>;
322227825Stheraventemplate<> class _LIBCPP_VISIBLE complex<long double>;
323227825Stheraven
324227825Stheraventemplate<>
325227825Stheravenclass _LIBCPP_VISIBLE complex<float>
326227825Stheraven{
327227825Stheraven    float __re_;
328227825Stheraven    float __im_;
329227825Stheravenpublic:
330227825Stheraven    typedef float value_type;
331227825Stheraven
332227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(float __re = 0.0f, float __im = 0.0f)
333227825Stheraven        : __re_(__re), __im_(__im) {}
334227825Stheraven    explicit /*constexpr*/ complex(const complex<double>& __c);
335227825Stheraven    explicit /*constexpr*/ complex(const complex<long double>& __c);
336227825Stheraven
337227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float real() const {return __re_;}
338227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float imag() const {return __im_;}
339227825Stheraven
340227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
341227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
342227825Stheraven
343227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) {__re_ = __re; return *this;}
344227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
345227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
346227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
347227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
348227825Stheraven
349227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
350227825Stheraven        {
351227825Stheraven            __re_ = __c.real();
352227825Stheraven            __im_ = __c.imag();
353227825Stheraven            return *this;
354227825Stheraven        }
355227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
356227825Stheraven        {
357227825Stheraven            __re_ += __c.real();
358227825Stheraven            __im_ += __c.imag();
359227825Stheraven            return *this;
360227825Stheraven        }
361227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
362227825Stheraven        {
363227825Stheraven            __re_ -= __c.real();
364227825Stheraven            __im_ -= __c.imag();
365227825Stheraven            return *this;
366227825Stheraven        }
367227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
368227825Stheraven        {
369227825Stheraven            *this = *this * __c;
370227825Stheraven            return *this;
371227825Stheraven        }
372227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
373227825Stheraven        {
374227825Stheraven            *this = *this / __c;
375227825Stheraven            return *this;
376227825Stheraven        }
377227825Stheraven};
378227825Stheraven
379227825Stheraventemplate<>
380227825Stheravenclass _LIBCPP_VISIBLE complex<double>
381227825Stheraven{
382227825Stheraven    double __re_;
383227825Stheraven    double __im_;
384227825Stheravenpublic:
385227825Stheraven    typedef double value_type;
386227825Stheraven
387227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(double __re = 0.0, double __im = 0.0)
388227825Stheraven        : __re_(__re), __im_(__im) {}
389227825Stheraven    /*constexpr*/ complex(const complex<float>& __c);
390227825Stheraven    explicit /*constexpr*/ complex(const complex<long double>& __c);
391227825Stheraven
392227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double real() const {return __re_;}
393227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double imag() const {return __im_;}
394227825Stheraven
395227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
396227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
397227825Stheraven
398227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) {__re_ = __re; return *this;}
399227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
400227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
401227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
402227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
403227825Stheraven
404227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
405227825Stheraven        {
406227825Stheraven            __re_ = __c.real();
407227825Stheraven            __im_ = __c.imag();
408227825Stheraven            return *this;
409227825Stheraven        }
410227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
411227825Stheraven        {
412227825Stheraven            __re_ += __c.real();
413227825Stheraven            __im_ += __c.imag();
414227825Stheraven            return *this;
415227825Stheraven        }
416227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
417227825Stheraven        {
418227825Stheraven            __re_ -= __c.real();
419227825Stheraven            __im_ -= __c.imag();
420227825Stheraven            return *this;
421227825Stheraven        }
422227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
423227825Stheraven        {
424227825Stheraven            *this = *this * __c;
425227825Stheraven            return *this;
426227825Stheraven        }
427227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
428227825Stheraven        {
429227825Stheraven            *this = *this / __c;
430227825Stheraven            return *this;
431227825Stheraven        }
432227825Stheraven};
433227825Stheraven
434227825Stheraventemplate<>
435227825Stheravenclass _LIBCPP_VISIBLE complex<long double>
436227825Stheraven{
437227825Stheraven    long double __re_;
438227825Stheraven    long double __im_;
439227825Stheravenpublic:
440227825Stheraven    typedef long double value_type;
441227825Stheraven
442227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(long double __re = 0.0L, long double __im = 0.0L)
443227825Stheraven        : __re_(__re), __im_(__im) {}
444227825Stheraven    /*constexpr*/ complex(const complex<float>& __c);
445227825Stheraven    /*constexpr*/ complex(const complex<double>& __c);
446227825Stheraven
447227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double real() const {return __re_;}
448227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double imag() const {return __im_;}
449227825Stheraven
450227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
451227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
452227825Stheraven
453227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re) {__re_ = __re; return *this;}
454227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
455227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
456227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
457227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
458227825Stheraven
459227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
460227825Stheraven        {
461227825Stheraven            __re_ = __c.real();
462227825Stheraven            __im_ = __c.imag();
463227825Stheraven            return *this;
464227825Stheraven        }
465227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
466227825Stheraven        {
467227825Stheraven            __re_ += __c.real();
468227825Stheraven            __im_ += __c.imag();
469227825Stheraven            return *this;
470227825Stheraven        }
471227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
472227825Stheraven        {
473227825Stheraven            __re_ -= __c.real();
474227825Stheraven            __im_ -= __c.imag();
475227825Stheraven            return *this;
476227825Stheraven        }
477227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
478227825Stheraven        {
479227825Stheraven            *this = *this * __c;
480227825Stheraven            return *this;
481227825Stheraven        }
482227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
483227825Stheraven        {
484227825Stheraven            *this = *this / __c;
485227825Stheraven            return *this;
486227825Stheraven        }
487227825Stheraven};
488227825Stheraven
489227825Stheraven//constexpr
490227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
491227825Stheravencomplex<float>::complex(const complex<double>& __c)
492227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
493227825Stheraven
494227825Stheraven//constexpr
495227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
496227825Stheravencomplex<float>::complex(const complex<long double>& __c)
497227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
498227825Stheraven
499227825Stheraven//constexpr
500227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
501227825Stheravencomplex<double>::complex(const complex<float>& __c)
502227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
503227825Stheraven
504227825Stheraven//constexpr
505227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
506227825Stheravencomplex<double>::complex(const complex<long double>& __c)
507227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
508227825Stheraven
509227825Stheraven//constexpr
510227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
511227825Stheravencomplex<long double>::complex(const complex<float>& __c)
512227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
513227825Stheraven
514227825Stheraven//constexpr
515227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
516227825Stheravencomplex<long double>::complex(const complex<double>& __c)
517227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
518227825Stheraven
519227825Stheraven// 26.3.6 operators:
520227825Stheraven
521227825Stheraventemplate<class _Tp>
522227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
523227825Stheravencomplex<_Tp>
524227825Stheravenoperator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
525227825Stheraven{
526227825Stheraven    complex<_Tp> __t(__x);
527227825Stheraven    __t += __y;
528227825Stheraven    return __t;
529227825Stheraven}
530227825Stheraven
531227825Stheraventemplate<class _Tp>
532227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
533227825Stheravencomplex<_Tp>
534227825Stheravenoperator+(const complex<_Tp>& __x, const _Tp& __y)
535227825Stheraven{
536227825Stheraven    complex<_Tp> __t(__x);
537227825Stheraven    __t += __y;
538227825Stheraven    return __t;
539227825Stheraven}
540227825Stheraven
541227825Stheraventemplate<class _Tp>
542227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
543227825Stheravencomplex<_Tp>
544227825Stheravenoperator+(const _Tp& __x, const complex<_Tp>& __y)
545227825Stheraven{
546227825Stheraven    complex<_Tp> __t(__y);
547227825Stheraven    __t += __x;
548227825Stheraven    return __t;
549227825Stheraven}
550227825Stheraven
551227825Stheraventemplate<class _Tp>
552227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
553227825Stheravencomplex<_Tp>
554227825Stheravenoperator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
555227825Stheraven{
556227825Stheraven    complex<_Tp> __t(__x);
557227825Stheraven    __t -= __y;
558227825Stheraven    return __t;
559227825Stheraven}
560227825Stheraven
561227825Stheraventemplate<class _Tp>
562227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
563227825Stheravencomplex<_Tp>
564227825Stheravenoperator-(const complex<_Tp>& __x, const _Tp& __y)
565227825Stheraven{
566227825Stheraven    complex<_Tp> __t(__x);
567227825Stheraven    __t -= __y;
568227825Stheraven    return __t;
569227825Stheraven}
570227825Stheraven
571227825Stheraventemplate<class _Tp>
572227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
573227825Stheravencomplex<_Tp>
574227825Stheravenoperator-(const _Tp& __x, const complex<_Tp>& __y)
575227825Stheraven{
576227825Stheraven    complex<_Tp> __t(-__y);
577227825Stheraven    __t += __x;
578227825Stheraven    return __t;
579227825Stheraven}
580227825Stheraven
581227825Stheraventemplate<class _Tp>
582227825Stheravencomplex<_Tp>
583227825Stheravenoperator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
584227825Stheraven{
585227825Stheraven    _Tp __a = __z.real();
586227825Stheraven    _Tp __b = __z.imag();
587227825Stheraven    _Tp __c = __w.real();
588227825Stheraven    _Tp __d = __w.imag();
589227825Stheraven    _Tp __ac = __a * __c;
590227825Stheraven    _Tp __bd = __b * __d;
591227825Stheraven    _Tp __ad = __a * __d;
592227825Stheraven    _Tp __bc = __b * __c;
593227825Stheraven    _Tp __x = __ac - __bd;
594227825Stheraven    _Tp __y = __ad + __bc;
595227825Stheraven    if (isnan(__x) && isnan(__y))
596227825Stheraven    {
597227825Stheraven        bool __recalc = false;
598227825Stheraven        if (isinf(__a) || isinf(__b))
599227825Stheraven        {
600227825Stheraven            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
601227825Stheraven            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
602227825Stheraven            if (isnan(__c))
603227825Stheraven                __c = copysign(_Tp(0), __c);
604227825Stheraven            if (isnan(__d))
605227825Stheraven                __d = copysign(_Tp(0), __d);
606227825Stheraven            __recalc = true;
607227825Stheraven        }
608227825Stheraven        if (isinf(__c) || isinf(__d))
609227825Stheraven        {
610227825Stheraven            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
611227825Stheraven            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
612227825Stheraven            if (isnan(__a))
613227825Stheraven                __a = copysign(_Tp(0), __a);
614227825Stheraven            if (isnan(__b))
615227825Stheraven                __b = copysign(_Tp(0), __b);
616227825Stheraven            __recalc = true;
617227825Stheraven        }
618227825Stheraven        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
619227825Stheraven                          isinf(__ad) || isinf(__bc)))
620227825Stheraven        {
621227825Stheraven            if (isnan(__a))
622227825Stheraven                __a = copysign(_Tp(0), __a);
623227825Stheraven            if (isnan(__b))
624227825Stheraven                __b = copysign(_Tp(0), __b);
625227825Stheraven            if (isnan(__c))
626227825Stheraven                __c = copysign(_Tp(0), __c);
627227825Stheraven            if (isnan(__d))
628227825Stheraven                __d = copysign(_Tp(0), __d);
629227825Stheraven            __recalc = true;
630227825Stheraven        }
631227825Stheraven        if (__recalc)
632227825Stheraven        {
633227825Stheraven            __x = _Tp(INFINITY) * (__a * __c - __b * __d);
634227825Stheraven            __y = _Tp(INFINITY) * (__a * __d + __b * __c);
635227825Stheraven        }
636227825Stheraven    }
637227825Stheraven    return complex<_Tp>(__x, __y);
638227825Stheraven}
639227825Stheraven
640227825Stheraventemplate<class _Tp>
641227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
642227825Stheravencomplex<_Tp>
643227825Stheravenoperator*(const complex<_Tp>& __x, const _Tp& __y)
644227825Stheraven{
645227825Stheraven    complex<_Tp> __t(__x);
646227825Stheraven    __t *= __y;
647227825Stheraven    return __t;
648227825Stheraven}
649227825Stheraven
650227825Stheraventemplate<class _Tp>
651227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
652227825Stheravencomplex<_Tp>
653227825Stheravenoperator*(const _Tp& __x, const complex<_Tp>& __y)
654227825Stheraven{
655227825Stheraven    complex<_Tp> __t(__y);
656227825Stheraven    __t *= __x;
657227825Stheraven    return __t;
658227825Stheraven}
659227825Stheraven
660227825Stheraventemplate<class _Tp>
661227825Stheravencomplex<_Tp>
662227825Stheravenoperator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
663227825Stheraven{
664227825Stheraven    int __ilogbw = 0;
665227825Stheraven    _Tp __a = __z.real();
666227825Stheraven    _Tp __b = __z.imag();
667227825Stheraven    _Tp __c = __w.real();
668227825Stheraven    _Tp __d = __w.imag();
669227825Stheraven    _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
670227825Stheraven    if (isfinite(__logbw))
671227825Stheraven    {
672227825Stheraven        __ilogbw = static_cast<int>(__logbw);
673227825Stheraven        __c = scalbn(__c, -__ilogbw);
674227825Stheraven        __d = scalbn(__d, -__ilogbw);
675227825Stheraven    }
676227825Stheraven    _Tp __denom = __c * __c + __d * __d;
677227825Stheraven    _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
678227825Stheraven    _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
679227825Stheraven    if (isnan(__x) && isnan(__y))
680227825Stheraven    {
681227825Stheraven        if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
682227825Stheraven        {
683227825Stheraven            __x = copysign(_Tp(INFINITY), __c) * __a;
684227825Stheraven            __y = copysign(_Tp(INFINITY), __c) * __b;
685227825Stheraven        }
686227825Stheraven        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
687227825Stheraven        {
688227825Stheraven            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
689227825Stheraven            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
690227825Stheraven            __x = _Tp(INFINITY) * (__a * __c + __b * __d);
691227825Stheraven            __y = _Tp(INFINITY) * (__b * __c - __a * __d);
692227825Stheraven        }
693227825Stheraven        else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
694227825Stheraven        {
695227825Stheraven            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
696227825Stheraven            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
697227825Stheraven            __x = _Tp(0) * (__a * __c + __b * __d);
698227825Stheraven            __y = _Tp(0) * (__b * __c - __a * __d);
699227825Stheraven        }
700227825Stheraven    }
701227825Stheraven    return complex<_Tp>(__x, __y);
702227825Stheraven}
703227825Stheraven
704227825Stheraventemplate<class _Tp>
705227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
706227825Stheravencomplex<_Tp>
707227825Stheravenoperator/(const complex<_Tp>& __x, const _Tp& __y)
708227825Stheraven{
709227825Stheraven    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
710227825Stheraven}
711227825Stheraven
712227825Stheraventemplate<class _Tp>
713227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
714227825Stheravencomplex<_Tp>
715227825Stheravenoperator/(const _Tp& __x, const complex<_Tp>& __y)
716227825Stheraven{
717227825Stheraven    complex<_Tp> __t(__x);
718227825Stheraven    __t /= __y;
719227825Stheraven    return __t;
720227825Stheraven}
721227825Stheraven
722227825Stheraventemplate<class _Tp>
723227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
724227825Stheravencomplex<_Tp>
725227825Stheravenoperator+(const complex<_Tp>& __x)
726227825Stheraven{
727227825Stheraven    return __x;
728227825Stheraven}
729227825Stheraven
730227825Stheraventemplate<class _Tp>
731227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
732227825Stheravencomplex<_Tp>
733227825Stheravenoperator-(const complex<_Tp>& __x)
734227825Stheraven{
735227825Stheraven    return complex<_Tp>(-__x.real(), -__x.imag());
736227825Stheraven}
737227825Stheraven
738227825Stheraventemplate<class _Tp>
739227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
740227825Stheravenbool
741227825Stheravenoperator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
742227825Stheraven{
743227825Stheraven    return __x.real() == __y.real() && __x.imag() == __y.imag();
744227825Stheraven}
745227825Stheraven
746227825Stheraventemplate<class _Tp>
747227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
748227825Stheravenbool
749227825Stheravenoperator==(const complex<_Tp>& __x, const _Tp& __y)
750227825Stheraven{
751227825Stheraven    return __x.real() == __y && __x.imag() == 0;
752227825Stheraven}
753227825Stheraven
754227825Stheraventemplate<class _Tp>
755227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
756227825Stheravenbool
757227825Stheravenoperator==(const _Tp& __x, const complex<_Tp>& __y)
758227825Stheraven{
759227825Stheraven    return __x == __y.real() && 0 == __y.imag();
760227825Stheraven}
761227825Stheraven
762227825Stheraventemplate<class _Tp>
763227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
764227825Stheravenbool
765227825Stheravenoperator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
766227825Stheraven{
767227825Stheraven    return !(__x == __y);
768227825Stheraven}
769227825Stheraven
770227825Stheraventemplate<class _Tp>
771227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
772227825Stheravenbool
773227825Stheravenoperator!=(const complex<_Tp>& __x, const _Tp& __y)
774227825Stheraven{
775227825Stheraven    return !(__x == __y);
776227825Stheraven}
777227825Stheraven
778227825Stheraventemplate<class _Tp>
779227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
780227825Stheravenbool
781227825Stheravenoperator!=(const _Tp& __x, const complex<_Tp>& __y)
782227825Stheraven{
783227825Stheraven    return !(__x == __y);
784227825Stheraven}
785227825Stheraven
786227825Stheraven// 26.3.7 values:
787227825Stheraven
788227825Stheraven// real
789227825Stheraven
790227825Stheraventemplate<class _Tp>
791227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
792227825Stheraven_Tp
793227825Stheravenreal(const complex<_Tp>& __c)
794227825Stheraven{
795227825Stheraven    return __c.real();
796227825Stheraven}
797227825Stheraven
798227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
799227825Stheravenlong double
800227825Stheravenreal(long double __re)
801227825Stheraven{
802227825Stheraven    return __re;
803227825Stheraven}
804227825Stheraven
805227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
806227825Stheravendouble
807227825Stheravenreal(double __re)
808227825Stheraven{
809227825Stheraven    return __re;
810227825Stheraven}
811227825Stheraven
812227825Stheraventemplate<class _Tp>
813227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
814227825Stheraventypename enable_if
815227825Stheraven<
816227825Stheraven    is_integral<_Tp>::value,
817227825Stheraven    double
818227825Stheraven>::type
819227825Stheravenreal(_Tp  __re)
820227825Stheraven{
821227825Stheraven    return __re;
822227825Stheraven}
823227825Stheraven
824227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
825227825Stheravenfloat
826227825Stheravenreal(float  __re)
827227825Stheraven{
828227825Stheraven    return __re;
829227825Stheraven}
830227825Stheraven
831227825Stheraven// imag
832227825Stheraven
833227825Stheraventemplate<class _Tp>
834227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
835227825Stheraven_Tp
836227825Stheravenimag(const complex<_Tp>& __c)
837227825Stheraven{
838227825Stheraven    return __c.imag();
839227825Stheraven}
840227825Stheraven
841227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
842227825Stheravenlong double
843227825Stheravenimag(long double __re)
844227825Stheraven{
845227825Stheraven    return 0;
846227825Stheraven}
847227825Stheraven
848227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
849227825Stheravendouble
850227825Stheravenimag(double __re)
851227825Stheraven{
852227825Stheraven    return 0;
853227825Stheraven}
854227825Stheraven
855227825Stheraventemplate<class _Tp>
856227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
857227825Stheraventypename enable_if
858227825Stheraven<
859227825Stheraven    is_integral<_Tp>::value,
860227825Stheraven    double
861227825Stheraven>::type
862227825Stheravenimag(_Tp  __re)
863227825Stheraven{
864227825Stheraven    return 0;
865227825Stheraven}
866227825Stheraven
867227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
868227825Stheravenfloat
869227825Stheravenimag(float  __re)
870227825Stheraven{
871227825Stheraven    return 0;
872227825Stheraven}
873227825Stheraven
874227825Stheraven// abs
875227825Stheraven
876227825Stheraventemplate<class _Tp>
877227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
878227825Stheraven_Tp
879227825Stheravenabs(const complex<_Tp>& __c)
880227825Stheraven{
881227825Stheraven    return hypot(__c.real(), __c.imag());
882227825Stheraven}
883227825Stheraven
884227825Stheraven// arg
885227825Stheraven
886227825Stheraventemplate<class _Tp>
887227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
888227825Stheraven_Tp
889227825Stheravenarg(const complex<_Tp>& __c)
890227825Stheraven{
891227825Stheraven    return atan2(__c.imag(), __c.real());
892227825Stheraven}
893227825Stheraven
894227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
895227825Stheravenlong double
896227825Stheravenarg(long double __re)
897227825Stheraven{
898227825Stheraven    return atan2l(0.L, __re);
899227825Stheraven}
900227825Stheraven
901227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
902227825Stheravendouble
903227825Stheravenarg(double __re)
904227825Stheraven{
905227825Stheraven    return atan2(0., __re);
906227825Stheraven}
907227825Stheraven
908227825Stheraventemplate<class _Tp>
909227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
910227825Stheraventypename enable_if
911227825Stheraven<
912227825Stheraven    is_integral<_Tp>::value,
913227825Stheraven    double
914227825Stheraven>::type
915227825Stheravenarg(_Tp __re)
916227825Stheraven{
917227825Stheraven    return atan2(0., __re);
918227825Stheraven}
919227825Stheraven
920227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
921227825Stheravenfloat
922227825Stheravenarg(float __re)
923227825Stheraven{
924227825Stheraven    return atan2f(0.F, __re);
925227825Stheraven}
926227825Stheraven
927227825Stheraven// norm
928227825Stheraven
929227825Stheraventemplate<class _Tp>
930227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
931227825Stheraven_Tp
932227825Stheravennorm(const complex<_Tp>& __c)
933227825Stheraven{
934227825Stheraven    if (isinf(__c.real()))
935227825Stheraven        return abs(__c.real());
936227825Stheraven    if (isinf(__c.imag()))
937227825Stheraven        return abs(__c.imag());
938227825Stheraven    return __c.real() * __c.real() + __c.imag() * __c.imag();
939227825Stheraven}
940227825Stheraven
941227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
942227825Stheravenlong double
943227825Stheravennorm(long double __re)
944227825Stheraven{
945227825Stheraven    return __re * __re;
946227825Stheraven}
947227825Stheraven
948227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
949227825Stheravendouble
950227825Stheravennorm(double __re)
951227825Stheraven{
952227825Stheraven    return __re * __re;
953227825Stheraven}
954227825Stheraven
955227825Stheraventemplate<class _Tp>
956227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
957227825Stheraventypename enable_if
958227825Stheraven<
959227825Stheraven    is_integral<_Tp>::value,
960227825Stheraven    double
961227825Stheraven>::type
962227825Stheravennorm(_Tp __re)
963227825Stheraven{
964227825Stheraven    return (double)__re * __re;
965227825Stheraven}
966227825Stheraven
967227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
968227825Stheravenfloat
969227825Stheravennorm(float __re)
970227825Stheraven{
971227825Stheraven    return __re * __re;
972227825Stheraven}
973227825Stheraven
974227825Stheraven// conj
975227825Stheraven
976227825Stheraventemplate<class _Tp>
977227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
978227825Stheravencomplex<_Tp>
979227825Stheravenconj(const complex<_Tp>& __c)
980227825Stheraven{
981227825Stheraven    return complex<_Tp>(__c.real(), -__c.imag());
982227825Stheraven}
983227825Stheraven
984227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
985227825Stheravencomplex<long double>
986227825Stheravenconj(long double __re)
987227825Stheraven{
988227825Stheraven    return complex<long double>(__re);
989227825Stheraven}
990227825Stheraven
991227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
992227825Stheravencomplex<double>
993227825Stheravenconj(double __re)
994227825Stheraven{
995227825Stheraven    return complex<double>(__re);
996227825Stheraven}
997227825Stheraven
998227825Stheraventemplate<class _Tp>
999227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1000227825Stheraventypename enable_if
1001227825Stheraven<
1002227825Stheraven    is_integral<_Tp>::value,
1003227825Stheraven    complex<double>
1004227825Stheraven>::type
1005227825Stheravenconj(_Tp __re)
1006227825Stheraven{
1007227825Stheraven    return complex<double>(__re);
1008227825Stheraven}
1009227825Stheraven
1010227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1011227825Stheravencomplex<float>
1012227825Stheravenconj(float __re)
1013227825Stheraven{
1014227825Stheraven    return complex<float>(__re);
1015227825Stheraven}
1016227825Stheraven
1017227825Stheraven// proj
1018227825Stheraven
1019227825Stheraventemplate<class _Tp>
1020227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1021227825Stheravencomplex<_Tp>
1022227825Stheravenproj(const complex<_Tp>& __c)
1023227825Stheraven{
1024227825Stheraven    std::complex<_Tp> __r = __c;
1025227825Stheraven    if (isinf(__c.real()) || isinf(__c.imag()))
1026227825Stheraven        __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1027227825Stheraven    return __r;
1028227825Stheraven}
1029227825Stheraven
1030227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1031227825Stheravencomplex<long double>
1032227825Stheravenproj(long double __re)
1033227825Stheraven{
1034227825Stheraven    if (isinf(__re))
1035227825Stheraven        __re = abs(__re);
1036227825Stheraven    return complex<long double>(__re);
1037227825Stheraven}
1038227825Stheraven
1039227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1040227825Stheravencomplex<double>
1041227825Stheravenproj(double __re)
1042227825Stheraven{
1043227825Stheraven    if (isinf(__re))
1044227825Stheraven        __re = abs(__re);
1045227825Stheraven    return complex<double>(__re);
1046227825Stheraven}
1047227825Stheraven
1048227825Stheraventemplate<class _Tp>
1049227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1050227825Stheraventypename enable_if
1051227825Stheraven<
1052227825Stheraven    is_integral<_Tp>::value,
1053227825Stheraven    complex<double>
1054227825Stheraven>::type
1055227825Stheravenproj(_Tp __re)
1056227825Stheraven{
1057227825Stheraven    return complex<double>(__re);
1058227825Stheraven}
1059227825Stheraven
1060227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1061227825Stheravencomplex<float>
1062227825Stheravenproj(float __re)
1063227825Stheraven{
1064227825Stheraven    if (isinf(__re))
1065227825Stheraven        __re = abs(__re);
1066227825Stheraven    return complex<float>(__re);
1067227825Stheraven}
1068227825Stheraven
1069227825Stheraven// polar
1070227825Stheraven
1071227825Stheraventemplate<class _Tp>
1072227825Stheravencomplex<_Tp>
1073227825Stheravenpolar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1074227825Stheraven{
1075227825Stheraven    if (isnan(__rho) || signbit(__rho))
1076227825Stheraven        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1077227825Stheraven    if (isnan(__theta))
1078227825Stheraven    {
1079227825Stheraven        if (isinf(__rho))
1080227825Stheraven            return complex<_Tp>(__rho, __theta);
1081227825Stheraven        return complex<_Tp>(__theta, __theta);
1082227825Stheraven    }
1083227825Stheraven    if (isinf(__theta))
1084227825Stheraven    {
1085227825Stheraven        if (isinf(__rho))
1086227825Stheraven            return complex<_Tp>(__rho, _Tp(NAN));
1087227825Stheraven        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1088227825Stheraven    }
1089227825Stheraven    _Tp __x = __rho * cos(__theta);
1090227825Stheraven    if (isnan(__x))
1091227825Stheraven        __x = 0;
1092227825Stheraven    _Tp __y = __rho * sin(__theta);
1093227825Stheraven    if (isnan(__y))
1094227825Stheraven        __y = 0;
1095227825Stheraven    return complex<_Tp>(__x, __y);
1096227825Stheraven}
1097227825Stheraven
1098227825Stheraven// log
1099227825Stheraven
1100227825Stheraventemplate<class _Tp>
1101227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1102227825Stheravencomplex<_Tp>
1103227825Stheravenlog(const complex<_Tp>& __x)
1104227825Stheraven{
1105227825Stheraven    return complex<_Tp>(log(abs(__x)), arg(__x));
1106227825Stheraven}
1107227825Stheraven
1108227825Stheraven// log10
1109227825Stheraven
1110227825Stheraventemplate<class _Tp>
1111227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1112227825Stheravencomplex<_Tp>
1113227825Stheravenlog10(const complex<_Tp>& __x)
1114227825Stheraven{
1115227825Stheraven    return log(__x) / log(_Tp(10));
1116227825Stheraven}
1117227825Stheraven
1118227825Stheraven// sqrt
1119227825Stheraven
1120227825Stheraventemplate<class _Tp>
1121227825Stheravencomplex<_Tp>
1122227825Stheravensqrt(const complex<_Tp>& __x)
1123227825Stheraven{
1124227825Stheraven    if (isinf(__x.imag()))
1125227825Stheraven        return complex<_Tp>(_Tp(INFINITY), __x.imag());
1126227825Stheraven    if (isinf(__x.real()))
1127227825Stheraven    {
1128227825Stheraven        if (__x.real() > _Tp(0))
1129227825Stheraven            return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1130227825Stheraven        return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1131227825Stheraven    }
1132227825Stheraven    return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1133227825Stheraven}
1134227825Stheraven
1135227825Stheraven// exp
1136227825Stheraven
1137227825Stheraventemplate<class _Tp>
1138227825Stheravencomplex<_Tp>
1139227825Stheravenexp(const complex<_Tp>& __x)
1140227825Stheraven{
1141227825Stheraven    _Tp __i = __x.imag();
1142227825Stheraven    if (isinf(__x.real()))
1143227825Stheraven    {
1144227825Stheraven        if (__x.real() < _Tp(0))
1145227825Stheraven        {
1146227825Stheraven            if (!isfinite(__i))
1147227825Stheraven                __i = _Tp(1);
1148227825Stheraven        }
1149227825Stheraven        else if (__i == 0 || !isfinite(__i))
1150227825Stheraven        {
1151227825Stheraven            if (isinf(__i))
1152227825Stheraven                __i = _Tp(NAN);
1153227825Stheraven            return complex<_Tp>(__x.real(), __i);
1154227825Stheraven        }
1155227825Stheraven    }
1156227825Stheraven    else if (isnan(__x.real()) && __x.imag() == 0)
1157227825Stheraven        return __x;
1158227825Stheraven    _Tp __e = exp(__x.real());
1159227825Stheraven    return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1160227825Stheraven}
1161227825Stheraven
1162227825Stheraven// pow
1163227825Stheraven
1164227825Stheraventemplate<class _Tp>
1165227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1166227825Stheravencomplex<_Tp>
1167227825Stheravenpow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1168227825Stheraven{
1169227825Stheraven    return exp(__y * log(__x));
1170227825Stheraven}
1171227825Stheraven
1172227825Stheraventemplate<class _Tp, class _Up>
1173227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1174227825Stheravencomplex<typename __promote<_Tp, _Up>::type>
1175227825Stheravenpow(const complex<_Tp>& __x, const complex<_Up>& __y)
1176227825Stheraven{
1177227825Stheraven    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1178227825Stheraven    return _VSTD::pow(result_type(__x), result_type(__y));
1179227825Stheraven}
1180227825Stheraven
1181227825Stheraventemplate<class _Tp, class _Up>
1182227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1183227825Stheraventypename enable_if
1184227825Stheraven<
1185227825Stheraven    is_arithmetic<_Up>::value,
1186227825Stheraven    complex<typename __promote<_Tp, _Up>::type>
1187227825Stheraven>::type
1188227825Stheravenpow(const complex<_Tp>& __x, const _Up& __y)
1189227825Stheraven{
1190227825Stheraven    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1191227825Stheraven    return _VSTD::pow(result_type(__x), result_type(__y));
1192227825Stheraven}
1193227825Stheraven
1194227825Stheraventemplate<class _Tp, class _Up>
1195227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1196227825Stheraventypename enable_if
1197227825Stheraven<
1198227825Stheraven    is_arithmetic<_Tp>::value,
1199227825Stheraven    complex<typename __promote<_Tp, _Up>::type>
1200227825Stheraven>::type
1201227825Stheravenpow(const _Tp& __x, const complex<_Up>& __y)
1202227825Stheraven{
1203227825Stheraven    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1204227825Stheraven    return _VSTD::pow(result_type(__x), result_type(__y));
1205227825Stheraven}
1206227825Stheraven
1207227825Stheraven// asinh
1208227825Stheraven
1209227825Stheraventemplate<class _Tp>
1210227825Stheravencomplex<_Tp>
1211227825Stheravenasinh(const complex<_Tp>& __x)
1212227825Stheraven{
1213227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1214227825Stheraven    if (isinf(__x.real()))
1215227825Stheraven    {
1216227825Stheraven        if (isnan(__x.imag()))
1217227825Stheraven            return __x;
1218227825Stheraven        if (isinf(__x.imag()))
1219227825Stheraven            return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1220227825Stheraven        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1221227825Stheraven    }
1222227825Stheraven    if (isnan(__x.real()))
1223227825Stheraven    {
1224227825Stheraven        if (isinf(__x.imag()))
1225227825Stheraven            return complex<_Tp>(__x.imag(), __x.real());
1226227825Stheraven        if (__x.imag() == 0)
1227227825Stheraven            return __x;
1228227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1229227825Stheraven    }
1230227825Stheraven    if (isinf(__x.imag()))
1231227825Stheraven        return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1232227825Stheraven    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1233227825Stheraven    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1234227825Stheraven}
1235227825Stheraven
1236227825Stheraven// acosh
1237227825Stheraven
1238227825Stheraventemplate<class _Tp>
1239227825Stheravencomplex<_Tp>
1240227825Stheravenacosh(const complex<_Tp>& __x)
1241227825Stheraven{
1242227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1243227825Stheraven    if (isinf(__x.real()))
1244227825Stheraven    {
1245227825Stheraven        if (isnan(__x.imag()))
1246227825Stheraven            return complex<_Tp>(abs(__x.real()), __x.imag());
1247227825Stheraven        if (isinf(__x.imag()))
1248227825Stheraven            if (__x.real() > 0)
1249227825Stheraven                return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1250227825Stheraven            else
1251227825Stheraven                return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1252227825Stheraven        if (__x.real() < 0)
1253227825Stheraven            return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1254227825Stheraven        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1255227825Stheraven    }
1256227825Stheraven    if (isnan(__x.real()))
1257227825Stheraven    {
1258227825Stheraven        if (isinf(__x.imag()))
1259227825Stheraven            return complex<_Tp>(abs(__x.imag()), __x.real());
1260227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1261227825Stheraven    }
1262227825Stheraven    if (isinf(__x.imag()))
1263227825Stheraven        return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1264227825Stheraven    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1265227825Stheraven    return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1266227825Stheraven}
1267227825Stheraven
1268227825Stheraven// atanh
1269227825Stheraven
1270227825Stheraventemplate<class _Tp>
1271227825Stheravencomplex<_Tp>
1272227825Stheravenatanh(const complex<_Tp>& __x)
1273227825Stheraven{
1274227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1275227825Stheraven    if (isinf(__x.imag()))
1276227825Stheraven    {
1277227825Stheraven        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1278227825Stheraven    }
1279227825Stheraven    if (isnan(__x.imag()))
1280227825Stheraven    {
1281227825Stheraven        if (isinf(__x.real()) || __x.real() == 0)
1282227825Stheraven            return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1283227825Stheraven        return complex<_Tp>(__x.imag(), __x.imag());
1284227825Stheraven    }
1285227825Stheraven    if (isnan(__x.real()))
1286227825Stheraven    {
1287227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1288227825Stheraven    }
1289227825Stheraven    if (isinf(__x.real()))
1290227825Stheraven    {
1291227825Stheraven        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1292227825Stheraven    }
1293227825Stheraven    if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1294227825Stheraven    {
1295227825Stheraven        return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1296227825Stheraven    }
1297227825Stheraven    complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1298227825Stheraven    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1299227825Stheraven}
1300227825Stheraven
1301227825Stheraven// sinh
1302227825Stheraven
1303227825Stheraventemplate<class _Tp>
1304227825Stheravencomplex<_Tp>
1305227825Stheravensinh(const complex<_Tp>& __x)
1306227825Stheraven{
1307227825Stheraven    if (isinf(__x.real()) && !isfinite(__x.imag()))
1308227825Stheraven        return complex<_Tp>(__x.real(), _Tp(NAN));
1309227825Stheraven    if (__x.real() == 0 && !isfinite(__x.imag()))
1310227825Stheraven        return complex<_Tp>(__x.real(), _Tp(NAN));
1311227825Stheraven    if (__x.imag() == 0 && !isfinite(__x.real()))
1312227825Stheraven        return __x;
1313227825Stheraven    return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1314227825Stheraven}
1315227825Stheraven
1316227825Stheraven// cosh
1317227825Stheraven
1318227825Stheraventemplate<class _Tp>
1319227825Stheravencomplex<_Tp>
1320227825Stheravencosh(const complex<_Tp>& __x)
1321227825Stheraven{
1322227825Stheraven    if (isinf(__x.real()) && !isfinite(__x.imag()))
1323227825Stheraven        return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1324227825Stheraven    if (__x.real() == 0 && !isfinite(__x.imag()))
1325227825Stheraven        return complex<_Tp>(_Tp(NAN), __x.real());
1326227825Stheraven    if (__x.real() == 0 && __x.imag() == 0)
1327227825Stheraven        return complex<_Tp>(_Tp(1), __x.imag());
1328227825Stheraven    if (__x.imag() == 0 && !isfinite(__x.real()))
1329227825Stheraven        return complex<_Tp>(abs(__x.real()), __x.imag());
1330227825Stheraven    return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1331227825Stheraven}
1332227825Stheraven
1333227825Stheraven// tanh
1334227825Stheraven
1335227825Stheraventemplate<class _Tp>
1336227825Stheravencomplex<_Tp>
1337227825Stheraventanh(const complex<_Tp>& __x)
1338227825Stheraven{
1339227825Stheraven    if (isinf(__x.real()))
1340227825Stheraven    {
1341227825Stheraven        if (!isfinite(__x.imag()))
1342227825Stheraven            return complex<_Tp>(_Tp(1), _Tp(0));
1343227825Stheraven        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1344227825Stheraven    }
1345227825Stheraven    if (isnan(__x.real()) && __x.imag() == 0)
1346227825Stheraven        return __x;
1347227825Stheraven    _Tp __2r(_Tp(2) * __x.real());
1348227825Stheraven    _Tp __2i(_Tp(2) * __x.imag());
1349227825Stheraven    _Tp __d(cosh(__2r) + cos(__2i));
1350227825Stheraven    return  complex<_Tp>(sinh(__2r)/__d, sin(__2i)/__d);
1351227825Stheraven}
1352227825Stheraven
1353227825Stheraven// asin
1354227825Stheraven
1355227825Stheraventemplate<class _Tp>
1356227825Stheravencomplex<_Tp>
1357227825Stheravenasin(const complex<_Tp>& __x)
1358227825Stheraven{
1359227825Stheraven    complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1360227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1361227825Stheraven}
1362227825Stheraven
1363227825Stheraven// acos
1364227825Stheraven
1365227825Stheraventemplate<class _Tp>
1366227825Stheravencomplex<_Tp>
1367227825Stheravenacos(const complex<_Tp>& __x)
1368227825Stheraven{
1369227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1370227825Stheraven    if (isinf(__x.real()))
1371227825Stheraven    {
1372227825Stheraven        if (isnan(__x.imag()))
1373227825Stheraven            return complex<_Tp>(__x.imag(), __x.real());
1374227825Stheraven        if (isinf(__x.imag()))
1375227825Stheraven        {
1376227825Stheraven            if (__x.real() < _Tp(0))
1377227825Stheraven                return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1378227825Stheraven            return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1379227825Stheraven        }
1380227825Stheraven        if (__x.real() < _Tp(0))
1381227825Stheraven            return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1382227825Stheraven        return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1383227825Stheraven    }
1384227825Stheraven    if (isnan(__x.real()))
1385227825Stheraven    {
1386227825Stheraven        if (isinf(__x.imag()))
1387227825Stheraven            return complex<_Tp>(__x.real(), -__x.imag());
1388227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1389227825Stheraven    }
1390227825Stheraven    if (isinf(__x.imag()))
1391227825Stheraven        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1392227825Stheraven    if (__x.real() == 0)
1393227825Stheraven        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1394227825Stheraven    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1395227825Stheraven    if (signbit(__x.imag()))
1396227825Stheraven        return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1397227825Stheraven    return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1398227825Stheraven}
1399227825Stheraven
1400227825Stheraven// atan
1401227825Stheraven
1402227825Stheraventemplate<class _Tp>
1403227825Stheravencomplex<_Tp>
1404227825Stheravenatan(const complex<_Tp>& __x)
1405227825Stheraven{
1406227825Stheraven    complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1407227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1408227825Stheraven}
1409227825Stheraven
1410227825Stheraven// sin
1411227825Stheraven
1412227825Stheraventemplate<class _Tp>
1413227825Stheravencomplex<_Tp>
1414227825Stheravensin(const complex<_Tp>& __x)
1415227825Stheraven{
1416227825Stheraven    complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1417227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1418227825Stheraven}
1419227825Stheraven
1420227825Stheraven// cos
1421227825Stheraven
1422227825Stheraventemplate<class _Tp>
1423227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1424227825Stheravencomplex<_Tp>
1425227825Stheravencos(const complex<_Tp>& __x)
1426227825Stheraven{
1427227825Stheraven    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1428227825Stheraven}
1429227825Stheraven
1430227825Stheraven// tan
1431227825Stheraven
1432227825Stheraventemplate<class _Tp>
1433227825Stheravencomplex<_Tp>
1434227825Stheraventan(const complex<_Tp>& __x)
1435227825Stheraven{
1436227825Stheraven    complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1437227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1438227825Stheraven}
1439227825Stheraven
1440227825Stheraventemplate<class _Tp, class _CharT, class _Traits>
1441227825Stheravenbasic_istream<_CharT, _Traits>&
1442227825Stheravenoperator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1443227825Stheraven{
1444227825Stheraven    if (__is.good())
1445227825Stheraven    {
1446227825Stheraven        ws(__is);
1447227825Stheraven        if (__is.peek() == _CharT('('))
1448227825Stheraven        {
1449227825Stheraven            __is.get();
1450227825Stheraven            _Tp __r;
1451227825Stheraven            __is >> __r;
1452227825Stheraven            if (!__is.fail())
1453227825Stheraven            {
1454227825Stheraven                ws(__is);
1455227825Stheraven                _CharT __c = __is.peek();
1456227825Stheraven                if (__c == _CharT(','))
1457227825Stheraven                {
1458227825Stheraven                    __is.get();
1459227825Stheraven                    _Tp __i;
1460227825Stheraven                    __is >> __i;
1461227825Stheraven                    if (!__is.fail())
1462227825Stheraven                    {
1463227825Stheraven                        ws(__is);
1464227825Stheraven                        __c = __is.peek();
1465227825Stheraven                        if (__c == _CharT(')'))
1466227825Stheraven                        {
1467227825Stheraven                            __is.get();
1468227825Stheraven                            __x = complex<_Tp>(__r, __i);
1469227825Stheraven                        }
1470227825Stheraven                        else
1471227825Stheraven                            __is.setstate(ios_base::failbit);
1472227825Stheraven                    }
1473227825Stheraven                    else
1474227825Stheraven                        __is.setstate(ios_base::failbit);
1475227825Stheraven                }
1476227825Stheraven                else if (__c == _CharT(')'))
1477227825Stheraven                {
1478227825Stheraven                    __is.get();
1479227825Stheraven                    __x = complex<_Tp>(__r, _Tp(0));
1480227825Stheraven                }
1481227825Stheraven                else
1482227825Stheraven                    __is.setstate(ios_base::failbit);
1483227825Stheraven            }
1484227825Stheraven            else
1485227825Stheraven                __is.setstate(ios_base::failbit);
1486227825Stheraven        }
1487227825Stheraven        else
1488227825Stheraven        {
1489227825Stheraven            _Tp __r;
1490227825Stheraven            __is >> __r;
1491227825Stheraven            if (!__is.fail())
1492227825Stheraven                __x = complex<_Tp>(__r, _Tp(0));
1493227825Stheraven            else
1494227825Stheraven                __is.setstate(ios_base::failbit);
1495227825Stheraven        }
1496227825Stheraven    }
1497227825Stheraven    else
1498227825Stheraven        __is.setstate(ios_base::failbit);
1499227825Stheraven    return __is;
1500227825Stheraven}
1501227825Stheraven
1502227825Stheraventemplate<class _Tp, class _CharT, class _Traits>
1503227825Stheravenbasic_ostream<_CharT, _Traits>&
1504227825Stheravenoperator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1505227825Stheraven{
1506227825Stheraven    basic_ostringstream<_CharT, _Traits> __s;
1507227825Stheraven    __s.flags(__os.flags());
1508227825Stheraven    __s.imbue(__os.getloc());
1509227825Stheraven    __s.precision(__os.precision());
1510227825Stheraven    __s << '(' << __x.real() << ',' << __x.imag() << ')';
1511227825Stheraven    return __os << __s.str();
1512227825Stheraven}
1513227825Stheraven
1514227825Stheraven_LIBCPP_END_NAMESPACE_STD
1515227825Stheraven
1516227825Stheraven#endif  // _LIBCPP_COMPLEX
1517