2009-03-10 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libstdc++-v3 / include / std / complex
blobe5c583554c178dabda22195111800909542d66b5
1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING.  If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction.  Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License.  This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file include/complex
33  *  This is a Standard C++ Library header.
34  */
37 // ISO C++ 14882: 26.2  Complex Numbers
38 // Note: this is not a conforming implementation.
39 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
40 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
43 #ifndef _GLIBCXX_COMPLEX
44 #define _GLIBCXX_COMPLEX 1
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
51 #include <cmath>
52 #include <sstream>
54 _GLIBCXX_BEGIN_NAMESPACE(std)
56   /**
57    * @defgroup complex_numbers Complex Numbers
58    * @ingroup numerics
59    *
60    * Classes and functions for complex numbers.
61    * @{
62    */
64   // Forward declarations.
65   template<typename _Tp> class complex;
66   template<> class complex<float>;
67   template<> class complex<double>;
68   template<> class complex<long double>;
70   ///  Return magnitude of @a z.
71   template<typename _Tp> _Tp abs(const complex<_Tp>&);
72   ///  Return phase angle of @a z.
73   template<typename _Tp> _Tp arg(const complex<_Tp>&);
74   ///  Return @a z magnitude squared.
75   template<typename _Tp> _Tp norm(const complex<_Tp>&);
77   ///  Return complex conjugate of @a z.
78   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
79   ///  Return complex with magnitude @a rho and angle @a theta.
80   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
82   // Transcendentals:
83   /// Return complex cosine of @a z.
84   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
85   /// Return complex hyperbolic cosine of @a z.
86   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
87   /// Return complex base e exponential of @a z.
88   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
89   /// Return complex natural logarithm of @a z.
90   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
91   /// Return complex base 10 logarithm of @a z.
92   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
93 #ifndef __GXX_EXPERIMENTAL_CXX0X__
94   // DR 844.
95   /// Return @a x to the @a y'th power.
96   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
97 #endif
98   /// Return @a x to the @a y'th power.
99   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
100   /// Return @a x to the @a y'th power.
101   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
102                                           const complex<_Tp>&);
103   /// Return @a x to the @a y'th power.
104   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
105   /// Return complex sine of @a z.
106   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
107   /// Return complex hyperbolic sine of @a z.
108   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
109   /// Return complex square root of @a z.
110   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
111   /// Return complex tangent of @a z.
112   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
113   /// Return complex hyperbolic tangent of @a z.
114   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
115     
116     
117   // 26.2.2  Primary template class complex
118   /**
119    *  Template to represent complex numbers.
120    *
121    *  Specializations for float, double, and long double are part of the
122    *  library.  Results with any other type are not guaranteed.
123    *
124    *  @param  Tp  Type of real and imaginary values.
125   */
126   template<typename _Tp>
127     struct complex
128     {
129       /// Value typedef.
130       typedef _Tp value_type;
131       
132       ///  Default constructor.  First parameter is x, second parameter is y.
133       ///  Unspecified parameters default to 0.
134       complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
135       : _M_real(__r), _M_imag(__i) { }
137       // Lets the compiler synthesize the copy constructor   
138       // complex (const complex<_Tp>&);
139       ///  Copy constructor.
140       template<typename _Up>
141         complex(const complex<_Up>& __z)
142         : _M_real(__z.real()), _M_imag(__z.imag()) { }
144 #ifdef __GXX_EXPERIMENTAL_CXX0X__
145       // _GLIBCXX_RESOLVE_LIB_DEFECTS
146       // DR 387. std::complex over-encapsulated.
147       _Tp real() const
148       { return _M_real; }
150       _Tp imag() const
151       { return _M_imag; }
152 #else
153       ///  Return real part of complex number.
154       _Tp& real()
155       { return _M_real; }
157       ///  Return real part of complex number.
158       const _Tp& real() const
159       { return _M_real; }
161       ///  Return imaginary part of complex number.
162       _Tp& imag()
163       { return _M_imag; }
165       ///  Return imaginary part of complex number.
166       const _Tp& imag() const
167       { return _M_imag; }
168 #endif
170       // _GLIBCXX_RESOLVE_LIB_DEFECTS
171       // DR 387. std::complex over-encapsulated.
172       void real(_Tp __val)
173       { _M_real = __val; }
175       void imag(_Tp __val)
176       { _M_imag = __val; }
178       /// Assign this complex number to scalar @a t.
179       complex<_Tp>& operator=(const _Tp&);
180       
181       /// Add @a t to this complex number.
182       // 26.2.5/1
183       complex<_Tp>&
184       operator+=(const _Tp& __t)
185       {
186         _M_real += __t;
187         return *this;
188       }
190       /// Subtract @a t from this complex number.
191       // 26.2.5/3
192       complex<_Tp>&
193       operator-=(const _Tp& __t)
194       {
195         _M_real -= __t;
196         return *this;
197       }
199       /// Multiply this complex number by @a t.
200       complex<_Tp>& operator*=(const _Tp&);
201       /// Divide this complex number by @a t.
202       complex<_Tp>& operator/=(const _Tp&);
204       // Lets the compiler synthesize the
205       // copy and assignment operator
206       // complex<_Tp>& operator= (const complex<_Tp>&);
207       /// Assign this complex number to complex @a z.
208       template<typename _Up>
209         complex<_Tp>& operator=(const complex<_Up>&);
210       /// Add @a z to this complex number.
211       template<typename _Up>
212         complex<_Tp>& operator+=(const complex<_Up>&);
213       /// Subtract @a z from this complex number.
214       template<typename _Up>
215         complex<_Tp>& operator-=(const complex<_Up>&);
216       /// Multiply this complex number by @a z.
217       template<typename _Up>
218         complex<_Tp>& operator*=(const complex<_Up>&);
219       /// Divide this complex number by @a z.
220       template<typename _Up>
221         complex<_Tp>& operator/=(const complex<_Up>&);
223       const complex& __rep() const
224       { return *this; }
226     private:
227       _Tp _M_real;
228       _Tp _M_imag;
229     };
231   template<typename _Tp>
232     complex<_Tp>&
233     complex<_Tp>::operator=(const _Tp& __t)
234     {
235      _M_real = __t;
236      _M_imag = _Tp();
237      return *this;
238     } 
240   // 26.2.5/5
241   template<typename _Tp>
242     complex<_Tp>&
243     complex<_Tp>::operator*=(const _Tp& __t)
244     {
245       _M_real *= __t;
246       _M_imag *= __t;
247       return *this;
248     }
250   // 26.2.5/7
251   template<typename _Tp>
252     complex<_Tp>&
253     complex<_Tp>::operator/=(const _Tp& __t)
254     {
255       _M_real /= __t;
256       _M_imag /= __t;
257       return *this;
258     }
260   template<typename _Tp>
261     template<typename _Up>
262     complex<_Tp>&
263     complex<_Tp>::operator=(const complex<_Up>& __z)
264     {
265       _M_real = __z.real();
266       _M_imag = __z.imag();
267       return *this;
268     }
270   // 26.2.5/9
271   template<typename _Tp>
272     template<typename _Up>
273     complex<_Tp>&
274     complex<_Tp>::operator+=(const complex<_Up>& __z)
275     {
276       _M_real += __z.real();
277       _M_imag += __z.imag();
278       return *this;
279     }
281   // 26.2.5/11
282   template<typename _Tp>
283     template<typename _Up>
284     complex<_Tp>&
285     complex<_Tp>::operator-=(const complex<_Up>& __z)
286     {
287       _M_real -= __z.real();
288       _M_imag -= __z.imag();
289       return *this;
290     }
292   // 26.2.5/13
293   // XXX: This is a grammar school implementation.
294   template<typename _Tp>
295     template<typename _Up>
296     complex<_Tp>&
297     complex<_Tp>::operator*=(const complex<_Up>& __z)
298     {
299       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
300       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
301       _M_real = __r;
302       return *this;
303     }
305   // 26.2.5/15
306   // XXX: This is a grammar school implementation.
307   template<typename _Tp>
308     template<typename _Up>
309     complex<_Tp>&
310     complex<_Tp>::operator/=(const complex<_Up>& __z)
311     {
312       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
313       const _Tp __n = std::norm(__z);
314       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
315       _M_real = __r / __n;
316       return *this;
317     }
318     
319   // Operators:
320   //@{
321   ///  Return new complex value @a x plus @a y.
322   template<typename _Tp>
323     inline complex<_Tp>
324     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
325     {
326       complex<_Tp> __r = __x;
327       __r += __y;
328       return __r;
329     }
331   template<typename _Tp>
332     inline complex<_Tp>
333     operator+(const complex<_Tp>& __x, const _Tp& __y)
334     {
335       complex<_Tp> __r = __x;
336       __r += __y;
337       return __r;
338     }
340   template<typename _Tp>
341     inline complex<_Tp>
342     operator+(const _Tp& __x, const complex<_Tp>& __y)
343     {
344       complex<_Tp> __r = __y;
345       __r += __x;
346       return __r;
347     }
348   //@}
350   //@{
351   ///  Return new complex value @a x minus @a y.
352   template<typename _Tp>
353     inline complex<_Tp>
354     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
355     {
356       complex<_Tp> __r = __x;
357       __r -= __y;
358       return __r;
359     }
360     
361   template<typename _Tp>
362     inline complex<_Tp>
363     operator-(const complex<_Tp>& __x, const _Tp& __y)
364     {
365       complex<_Tp> __r = __x;
366       __r -= __y;
367       return __r;
368     }
370   template<typename _Tp>
371     inline complex<_Tp>
372     operator-(const _Tp& __x, const complex<_Tp>& __y)
373     {
374       complex<_Tp> __r(__x, -__y.imag());
375       __r -= __y.real();
376       return __r;
377     }
378   //@}
380   //@{
381   ///  Return new complex value @a x times @a y.
382   template<typename _Tp>
383     inline complex<_Tp>
384     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
385     {
386       complex<_Tp> __r = __x;
387       __r *= __y;
388       return __r;
389     }
391   template<typename _Tp>
392     inline complex<_Tp>
393     operator*(const complex<_Tp>& __x, const _Tp& __y)
394     {
395       complex<_Tp> __r = __x;
396       __r *= __y;
397       return __r;
398     }
400   template<typename _Tp>
401     inline complex<_Tp>
402     operator*(const _Tp& __x, const complex<_Tp>& __y)
403     {
404       complex<_Tp> __r = __y;
405       __r *= __x;
406       return __r;
407     }
408   //@}
410   //@{
411   ///  Return new complex value @a x divided by @a y.
412   template<typename _Tp>
413     inline complex<_Tp>
414     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
415     {
416       complex<_Tp> __r = __x;
417       __r /= __y;
418       return __r;
419     }
420     
421   template<typename _Tp>
422     inline complex<_Tp>
423     operator/(const complex<_Tp>& __x, const _Tp& __y)
424     {
425       complex<_Tp> __r = __x;
426       __r /= __y;
427       return __r;
428     }
430   template<typename _Tp>
431     inline complex<_Tp>
432     operator/(const _Tp& __x, const complex<_Tp>& __y)
433     {
434       complex<_Tp> __r = __x;
435       __r /= __y;
436       return __r;
437     }
438   //@}
440   ///  Return @a x.
441   template<typename _Tp>
442     inline complex<_Tp>
443     operator+(const complex<_Tp>& __x)
444     { return __x; }
446   ///  Return complex negation of @a x.
447   template<typename _Tp>
448     inline complex<_Tp>
449     operator-(const complex<_Tp>& __x)
450     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
452   //@{
453   ///  Return true if @a x is equal to @a y.
454   template<typename _Tp>
455     inline bool
456     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
457     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
459   template<typename _Tp>
460     inline bool
461     operator==(const complex<_Tp>& __x, const _Tp& __y)
462     { return __x.real() == __y && __x.imag() == _Tp(); }
464   template<typename _Tp>
465     inline bool
466     operator==(const _Tp& __x, const complex<_Tp>& __y)
467     { return __x == __y.real() && _Tp() == __y.imag(); }
468   //@}
470   //@{
471   ///  Return false if @a x is equal to @a y.
472   template<typename _Tp>
473     inline bool
474     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
475     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
477   template<typename _Tp>
478     inline bool
479     operator!=(const complex<_Tp>& __x, const _Tp& __y)
480     { return __x.real() != __y || __x.imag() != _Tp(); }
482   template<typename _Tp>
483     inline bool
484     operator!=(const _Tp& __x, const complex<_Tp>& __y)
485     { return __x != __y.real() || _Tp() != __y.imag(); }
486   //@}
488   ///  Extraction operator for complex values.
489   template<typename _Tp, typename _CharT, class _Traits>
490     basic_istream<_CharT, _Traits>&
491     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
492     {
493       _Tp __re_x, __im_x;
494       _CharT __ch;
495       __is >> __ch;
496       if (__ch == '(') 
497         {
498           __is >> __re_x >> __ch;
499           if (__ch == ',') 
500             {
501               __is >> __im_x >> __ch;
502               if (__ch == ')') 
503                 __x = complex<_Tp>(__re_x, __im_x);
504               else
505                 __is.setstate(ios_base::failbit);
506             }
507           else if (__ch == ')') 
508             __x = __re_x;
509           else
510             __is.setstate(ios_base::failbit);
511         }
512       else 
513         {
514           __is.putback(__ch);
515           __is >> __re_x;
516           __x = __re_x;
517         }
518       return __is;
519     }
521   ///  Insertion operator for complex values.
522   template<typename _Tp, typename _CharT, class _Traits>
523     basic_ostream<_CharT, _Traits>&
524     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
525     {
526       basic_ostringstream<_CharT, _Traits> __s;
527       __s.flags(__os.flags());
528       __s.imbue(__os.getloc());
529       __s.precision(__os.precision());
530       __s << '(' << __x.real() << ',' << __x.imag() << ')';
531       return __os << __s.str();
532     }
534   // Values
535 #ifdef __GXX_EXPERIMENTAL_CXX0X__
536   template<typename _Tp>
537     inline _Tp
538     real(const complex<_Tp>& __z)
539     { return __z.real(); }
540     
541   template<typename _Tp>
542     inline _Tp
543     imag(const complex<_Tp>& __z)
544     { return __z.imag(); }
545 #else
546   template<typename _Tp>
547     inline _Tp&
548     real(complex<_Tp>& __z)
549     { return __z.real(); }
550     
551   template<typename _Tp>
552     inline const _Tp&
553     real(const complex<_Tp>& __z)
554     { return __z.real(); }
555     
556   template<typename _Tp>
557     inline _Tp&
558     imag(complex<_Tp>& __z)
559     { return __z.imag(); }
560     
561   template<typename _Tp>
562     inline const _Tp&
563     imag(const complex<_Tp>& __z)
564     { return __z.imag(); }
565 #endif
567   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
568   template<typename _Tp>
569     inline _Tp
570     __complex_abs(const complex<_Tp>& __z)
571     {
572       _Tp __x = __z.real();
573       _Tp __y = __z.imag();
574       const _Tp __s = std::max(abs(__x), abs(__y));
575       if (__s == _Tp())  // well ...
576         return __s;
577       __x /= __s; 
578       __y /= __s;
579       return __s * sqrt(__x * __x + __y * __y);
580     }
582 #if _GLIBCXX_USE_C99_COMPLEX
583   inline float
584   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
586   inline double
587   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
589   inline long double
590   __complex_abs(const __complex__ long double& __z)
591   { return __builtin_cabsl(__z); }
593   template<typename _Tp>
594     inline _Tp
595     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
596 #else
597   template<typename _Tp>
598     inline _Tp
599     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
600 #endif  
603   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
604   template<typename _Tp>
605     inline _Tp
606     __complex_arg(const complex<_Tp>& __z)
607     { return  atan2(__z.imag(), __z.real()); }
609 #if _GLIBCXX_USE_C99_COMPLEX
610   inline float
611   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
613   inline double
614   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
616   inline long double
617   __complex_arg(const __complex__ long double& __z)
618   { return __builtin_cargl(__z); }
620   template<typename _Tp>
621     inline _Tp
622     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
623 #else
624   template<typename _Tp>
625     inline _Tp
626     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
627 #endif
629   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
630   //     As defined, norm() is -not- a norm is the common mathematical
631   //     sens used in numerics.  The helper class _Norm_helper<> tries to
632   //     distinguish between builtin floating point and the rest, so as
633   //     to deliver an answer as close as possible to the real value.
634   template<bool>
635     struct _Norm_helper
636     {
637       template<typename _Tp>
638         static inline _Tp _S_do_it(const complex<_Tp>& __z)
639         {
640           const _Tp __x = __z.real();
641           const _Tp __y = __z.imag();
642           return __x * __x + __y * __y;
643         }
644     };
646   template<>
647     struct _Norm_helper<true>
648     {
649       template<typename _Tp>
650         static inline _Tp _S_do_it(const complex<_Tp>& __z)
651         {
652           _Tp __res = std::abs(__z);
653           return __res * __res;
654         }
655     };
656   
657   template<typename _Tp>
658     inline _Tp
659     norm(const complex<_Tp>& __z)
660     {
661       return _Norm_helper<__is_floating<_Tp>::__value 
662         && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
663     }
665   template<typename _Tp>
666     inline complex<_Tp>
667     polar(const _Tp& __rho, const _Tp& __theta)
668     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
670   template<typename _Tp>
671     inline complex<_Tp>
672     conj(const complex<_Tp>& __z)
673     { return complex<_Tp>(__z.real(), -__z.imag()); }
674   
675   // Transcendentals
677   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
678   template<typename _Tp>
679     inline complex<_Tp>
680     __complex_cos(const complex<_Tp>& __z)
681     {
682       const _Tp __x = __z.real();
683       const _Tp __y = __z.imag();
684       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
685     }
687 #if _GLIBCXX_USE_C99_COMPLEX
688   inline __complex__ float
689   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
691   inline __complex__ double
692   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
694   inline __complex__ long double
695   __complex_cos(const __complex__ long double& __z)
696   { return __builtin_ccosl(__z); }
698   template<typename _Tp>
699     inline complex<_Tp>
700     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
701 #else
702   template<typename _Tp>
703     inline complex<_Tp>
704     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
705 #endif
707   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
708   template<typename _Tp>
709     inline complex<_Tp>
710     __complex_cosh(const complex<_Tp>& __z)
711     {
712       const _Tp __x = __z.real();
713       const _Tp __y = __z.imag();
714       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
715     }
717 #if _GLIBCXX_USE_C99_COMPLEX
718   inline __complex__ float
719   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
721   inline __complex__ double
722   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
724   inline __complex__ long double
725   __complex_cosh(const __complex__ long double& __z)
726   { return __builtin_ccoshl(__z); }
728   template<typename _Tp>
729     inline complex<_Tp>
730     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
731 #else
732   template<typename _Tp>
733     inline complex<_Tp>
734     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
735 #endif
737   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
738   template<typename _Tp>
739     inline complex<_Tp>
740     __complex_exp(const complex<_Tp>& __z)
741     { return std::polar(exp(__z.real()), __z.imag()); }
743 #if _GLIBCXX_USE_C99_COMPLEX
744   inline __complex__ float
745   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
747   inline __complex__ double
748   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
750   inline __complex__ long double
751   __complex_exp(const __complex__ long double& __z)
752   { return __builtin_cexpl(__z); }
754   template<typename _Tp>
755     inline complex<_Tp>
756     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
757 #else
758   template<typename _Tp>
759     inline complex<_Tp>
760     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
761 #endif
763   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
764   //                    The branch cut is along the negative axis.
765   template<typename _Tp>
766     inline complex<_Tp>
767     __complex_log(const complex<_Tp>& __z)
768     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
770 #if _GLIBCXX_USE_C99_COMPLEX
771   inline __complex__ float
772   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
774   inline __complex__ double
775   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
777   inline __complex__ long double
778   __complex_log(const __complex__ long double& __z)
779   { return __builtin_clogl(__z); }
781   template<typename _Tp>
782     inline complex<_Tp>
783     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
784 #else
785   template<typename _Tp>
786     inline complex<_Tp>
787     log(const complex<_Tp>& __z) { return __complex_log(__z); }
788 #endif
790   template<typename _Tp>
791     inline complex<_Tp>
792     log10(const complex<_Tp>& __z)
793     { return std::log(__z) / log(_Tp(10.0)); }
795   // 26.2.8/10 sin(__z): Returns the sine of __z.
796   template<typename _Tp>
797     inline complex<_Tp>
798     __complex_sin(const complex<_Tp>& __z)
799     {
800       const _Tp __x = __z.real();
801       const _Tp __y = __z.imag();
802       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
803     }
805 #if _GLIBCXX_USE_C99_COMPLEX
806   inline __complex__ float
807   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
809   inline __complex__ double
810   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
812   inline __complex__ long double
813   __complex_sin(const __complex__ long double& __z)
814   { return __builtin_csinl(__z); }
816   template<typename _Tp>
817     inline complex<_Tp>
818     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
819 #else
820   template<typename _Tp>
821     inline complex<_Tp>
822     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
823 #endif
825   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
826   template<typename _Tp>
827     inline complex<_Tp>
828     __complex_sinh(const complex<_Tp>& __z)
829     {
830       const _Tp __x = __z.real();
831       const _Tp  __y = __z.imag();
832       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
833     }
835 #if _GLIBCXX_USE_C99_COMPLEX
836   inline __complex__ float
837   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
839   inline __complex__ double
840   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
842   inline __complex__ long double
843   __complex_sinh(const __complex__ long double& __z)
844   { return __builtin_csinhl(__z); }      
846   template<typename _Tp>
847     inline complex<_Tp>
848     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
849 #else
850   template<typename _Tp>
851     inline complex<_Tp>
852     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
853 #endif
855   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
856   //                     The branch cut is on the negative axis.
857   template<typename _Tp>
858     complex<_Tp>
859     __complex_sqrt(const complex<_Tp>& __z)
860     {
861       _Tp __x = __z.real();
862       _Tp __y = __z.imag();
864       if (__x == _Tp())
865         {
866           _Tp __t = sqrt(abs(__y) / 2);
867           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
868         }
869       else
870         {
871           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
872           _Tp __u = __t / 2;
873           return __x > _Tp()
874             ? complex<_Tp>(__u, __y / __t)
875             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
876         }
877     }
879 #if _GLIBCXX_USE_C99_COMPLEX
880   inline __complex__ float
881   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
883   inline __complex__ double
884   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
886   inline __complex__ long double
887   __complex_sqrt(const __complex__ long double& __z)
888   { return __builtin_csqrtl(__z); }
890   template<typename _Tp>
891     inline complex<_Tp>
892     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
893 #else
894   template<typename _Tp>
895     inline complex<_Tp>
896     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
897 #endif
899   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
900   
901   template<typename _Tp>
902     inline complex<_Tp>
903     __complex_tan(const complex<_Tp>& __z)
904     { return std::sin(__z) / std::cos(__z); }
906 #if _GLIBCXX_USE_C99_COMPLEX
907   inline __complex__ float
908   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
910   inline __complex__ double
911   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
913   inline __complex__ long double
914   __complex_tan(const __complex__ long double& __z)
915   { return __builtin_ctanl(__z); }
917   template<typename _Tp>
918     inline complex<_Tp>
919     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
920 #else
921   template<typename _Tp>
922     inline complex<_Tp>
923     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
924 #endif
927   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
928   
929   template<typename _Tp>
930     inline complex<_Tp>
931     __complex_tanh(const complex<_Tp>& __z)
932     { return std::sinh(__z) / std::cosh(__z); }
934 #if _GLIBCXX_USE_C99_COMPLEX
935   inline __complex__ float
936   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
938   inline __complex__ double
939   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
941   inline __complex__ long double
942   __complex_tanh(const __complex__ long double& __z)
943   { return __builtin_ctanhl(__z); }
945   template<typename _Tp>
946     inline complex<_Tp>
947     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
948 #else
949   template<typename _Tp>
950     inline complex<_Tp>
951     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
952 #endif
955   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
956   //                          raised to the __y-th power.  The branch
957   //                          cut is on the negative axis.
958 #ifndef __GXX_EXPERIMENTAL_CXX0X__
959   // _GLIBCXX_RESOLVE_LIB_DEFECTS
960   // DR 844. complex pow return type is ambiguous.
961   template<typename _Tp>
962     inline complex<_Tp>
963     pow(const complex<_Tp>& __z, int __n)
964     { return std::__pow_helper(__z, __n); }
965 #endif
967   template<typename _Tp>
968     complex<_Tp>
969     pow(const complex<_Tp>& __x, const _Tp& __y)
970     {
971 #ifndef _GLIBCXX_USE_C99_COMPLEX
972       if (__x == _Tp())
973         return _Tp();
974 #endif
975       if (__x.imag() == _Tp() && __x.real() > _Tp())
976         return pow(__x.real(), __y);
978       complex<_Tp> __t = std::log(__x);
979       return std::polar(exp(__y * __t.real()), __y * __t.imag());
980     }
982   template<typename _Tp>
983     inline complex<_Tp>
984     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
985     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
987 #if _GLIBCXX_USE_C99_COMPLEX
988   inline __complex__ float
989   __complex_pow(__complex__ float __x, __complex__ float __y)
990   { return __builtin_cpowf(__x, __y); }
992   inline __complex__ double
993   __complex_pow(__complex__ double __x, __complex__ double __y)
994   { return __builtin_cpow(__x, __y); }
996   inline __complex__ long double
997   __complex_pow(const __complex__ long double& __x,
998                 const __complex__ long double& __y)
999   { return __builtin_cpowl(__x, __y); }
1001   template<typename _Tp>
1002     inline complex<_Tp>
1003     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1004     { return __complex_pow(__x.__rep(), __y.__rep()); }
1005 #else
1006   template<typename _Tp>
1007     inline complex<_Tp>
1008     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1009     { return __complex_pow(__x, __y); }
1010 #endif
1012   template<typename _Tp>
1013     inline complex<_Tp>
1014     pow(const _Tp& __x, const complex<_Tp>& __y)
1015     {
1016       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1017                                       __y.imag() * log(__x))
1018                          : std::pow(complex<_Tp>(__x), __y);
1019     }
1021   // 26.2.3  complex specializations
1022   // complex<float> specialization
1023   template<>
1024     struct complex<float>
1025     {
1026       typedef float value_type;
1027       typedef __complex__ float _ComplexT;
1029       complex(_ComplexT __z) : _M_value(__z) { }
1031       complex(float __r = 0.0f, float __i = 0.0f)
1032       {
1033         __real__ _M_value = __r;
1034         __imag__ _M_value = __i;
1035       }
1037       explicit complex(const complex<double>&);
1038       explicit complex(const complex<long double>&);    
1040 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1041       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1042       // DR 387. std::complex over-encapsulated.
1043       float real() const
1044       { return __real__ _M_value; }
1046       float imag() const
1047       { return __imag__ _M_value; }
1048 #else
1049       float& real()
1050       { return __real__ _M_value; }
1052       const float& real() const
1053       { return __real__ _M_value; }      
1055       float& imag()
1056       { return __imag__ _M_value; }
1058       const float& imag() const
1059       { return __imag__ _M_value; }
1060 #endif
1062       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1063       // DR 387. std::complex over-encapsulated.
1064       void real(float __val)
1065       { __real__ _M_value = __val; }
1067       void imag(float __val)
1068       { __imag__ _M_value = __val; }
1070       complex<float>&
1071       operator=(float __f)
1072       {
1073         __real__ _M_value = __f;
1074         __imag__ _M_value = 0.0f;
1075         return *this;
1076       }
1078       complex<float>&
1079       operator+=(float __f)
1080       {
1081         __real__ _M_value += __f;
1082         return *this;
1083       }
1085       complex<float>&
1086       operator-=(float __f)
1087       {
1088         __real__ _M_value -= __f;
1089         return *this;
1090       }
1092       complex<float>&
1093       operator*=(float __f)
1094       {
1095         _M_value *= __f;
1096         return *this;
1097       }
1099       complex<float>&
1100       operator/=(float __f)
1101       {
1102         _M_value /= __f;
1103         return *this;
1104       }
1106       // Let the compiler synthesize the copy and assignment
1107       // operator.  It always does a pretty good job.
1108       // complex& operator=(const complex&);
1110       template<typename _Tp>
1111         complex<float>&
1112         operator=(const complex<_Tp>&  __z)
1113         {
1114           __real__ _M_value = __z.real();
1115           __imag__ _M_value = __z.imag();
1116           return *this;
1117         }
1119       template<typename _Tp>
1120         complex<float>&
1121         operator+=(const complex<_Tp>& __z)
1122         {
1123           __real__ _M_value += __z.real();
1124           __imag__ _M_value += __z.imag();
1125           return *this;
1126         }
1128       template<class _Tp>
1129         complex<float>&
1130         operator-=(const complex<_Tp>& __z)
1131         {
1132           __real__ _M_value -= __z.real();
1133           __imag__ _M_value -= __z.imag();
1134           return *this;
1135         }
1137       template<class _Tp>
1138         complex<float>&
1139         operator*=(const complex<_Tp>& __z)
1140         {
1141           _ComplexT __t;
1142           __real__ __t = __z.real();
1143           __imag__ __t = __z.imag();
1144           _M_value *= __t;
1145           return *this;
1146         }
1148       template<class _Tp>
1149         complex<float>&
1150         operator/=(const complex<_Tp>& __z)
1151         {
1152           _ComplexT __t;
1153           __real__ __t = __z.real();
1154           __imag__ __t = __z.imag();
1155           _M_value /= __t;
1156           return *this;
1157         }
1159       const _ComplexT& __rep() const { return _M_value; }
1161     private:
1162       _ComplexT _M_value;
1163     };
1165   // 26.2.3  complex specializations
1166   // complex<double> specialization
1167   template<>
1168     struct complex<double>
1169     {
1170       typedef double value_type;
1171       typedef __complex__ double _ComplexT;
1173       complex(_ComplexT __z) : _M_value(__z) { }
1175       complex(double __r = 0.0, double __i = 0.0)
1176       {
1177         __real__ _M_value = __r;
1178         __imag__ _M_value = __i;
1179       }
1181       complex(const complex<float>& __z)
1182       : _M_value(__z.__rep()) { }
1184       explicit complex(const complex<long double>&);    
1186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1187       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1188       // DR 387. std::complex over-encapsulated.
1189       double real() const
1190       { return __real__ _M_value; }
1192       double imag() const
1193       { return __imag__ _M_value; }
1194 #else
1195       double& real()
1196       { return __real__ _M_value; }
1198       const double& real() const
1199       { return __real__ _M_value; }
1201       double& imag()
1202       { return __imag__ _M_value; }
1204       const double& imag() const
1205       { return __imag__ _M_value; }
1206 #endif
1208       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1209       // DR 387. std::complex over-encapsulated.
1210       void real(double __val)
1211       { __real__ _M_value = __val; }
1213       void imag(double __val)
1214       { __imag__ _M_value = __val; }
1216       complex<double>&
1217       operator=(double __d)
1218       {
1219         __real__ _M_value = __d;
1220         __imag__ _M_value = 0.0;
1221         return *this;
1222       }
1224       complex<double>&
1225       operator+=(double __d)
1226       {
1227         __real__ _M_value += __d;
1228         return *this;
1229       }
1230         
1231       complex<double>&
1232       operator-=(double __d)
1233       {
1234         __real__ _M_value -= __d;
1235         return *this;
1236       }
1238       complex<double>&
1239       operator*=(double __d)
1240       {
1241         _M_value *= __d;
1242         return *this;
1243       }
1245       complex<double>&
1246       operator/=(double __d)
1247       {
1248         _M_value /= __d;
1249         return *this;
1250       }
1252       // The compiler will synthesize this, efficiently.
1253       // complex& operator=(const complex&);
1255       template<typename _Tp>
1256         complex<double>&
1257         operator=(const complex<_Tp>& __z)
1258         {
1259           __real__ _M_value = __z.real();
1260           __imag__ _M_value = __z.imag();
1261           return *this;
1262         }
1264       template<typename _Tp>
1265         complex<double>&
1266         operator+=(const complex<_Tp>& __z)
1267         {
1268           __real__ _M_value += __z.real();
1269           __imag__ _M_value += __z.imag();
1270           return *this;
1271         }
1273       template<typename _Tp>
1274         complex<double>&
1275         operator-=(const complex<_Tp>& __z)
1276         {
1277           __real__ _M_value -= __z.real();
1278           __imag__ _M_value -= __z.imag();
1279           return *this;
1280         }
1282       template<typename _Tp>
1283         complex<double>&
1284         operator*=(const complex<_Tp>& __z)
1285         {
1286           _ComplexT __t;
1287           __real__ __t = __z.real();
1288           __imag__ __t = __z.imag();
1289           _M_value *= __t;
1290           return *this;
1291         }
1293       template<typename _Tp>
1294         complex<double>&
1295         operator/=(const complex<_Tp>& __z)
1296         {
1297           _ComplexT __t;
1298           __real__ __t = __z.real();
1299           __imag__ __t = __z.imag();
1300           _M_value /= __t;
1301           return *this;
1302         }
1304       const _ComplexT& __rep() const { return _M_value; }
1306     private:
1307       _ComplexT _M_value;
1308     };
1310   // 26.2.3  complex specializations
1311   // complex<long double> specialization
1312   template<>
1313     struct complex<long double>
1314     {
1315       typedef long double value_type;
1316       typedef __complex__ long double _ComplexT;
1318       complex(_ComplexT __z) : _M_value(__z) { }
1320       complex(long double __r = 0.0L, long double __i = 0.0L)
1321       {
1322         __real__ _M_value = __r;
1323         __imag__ _M_value = __i;
1324       }
1326       complex(const complex<float>& __z)
1327       : _M_value(__z.__rep()) { }
1329       complex(const complex<double>& __z)
1330       : _M_value(__z.__rep()) { }
1332 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1333       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1334       // DR 387. std::complex over-encapsulated.
1335       long double real() const
1336       { return __real__ _M_value; }
1338       long double imag() const
1339       { return __imag__ _M_value; }
1340 #else
1341       long double& real()
1342       { return __real__ _M_value; }
1344       const long double& real() const
1345       { return __real__ _M_value; }
1347       long double& imag()
1348       { return __imag__ _M_value; }
1350       const long double& imag() const
1351       { return __imag__ _M_value; }
1352 #endif
1354       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1355       // DR 387. std::complex over-encapsulated.
1356       void real(long double __val)
1357       { __real__ _M_value = __val; }
1359       void imag(long double __val)
1360       { __imag__ _M_value = __val; }
1362       complex<long double>&
1363       operator=(long double __r)
1364       {
1365         __real__ _M_value = __r;
1366         __imag__ _M_value = 0.0L;
1367         return *this;
1368       }
1370       complex<long double>&
1371       operator+=(long double __r)
1372       {
1373         __real__ _M_value += __r;
1374         return *this;
1375       }
1377       complex<long double>&
1378       operator-=(long double __r)
1379       {
1380         __real__ _M_value -= __r;
1381         return *this;
1382       }
1384       complex<long double>&
1385       operator*=(long double __r)
1386       {
1387         _M_value *= __r;
1388         return *this;
1389       }
1391       complex<long double>&
1392       operator/=(long double __r)
1393       {
1394         _M_value /= __r;
1395         return *this;
1396       }
1398       // The compiler knows how to do this efficiently
1399       // complex& operator=(const complex&);
1401       template<typename _Tp>
1402         complex<long double>&
1403         operator=(const complex<_Tp>& __z)
1404         {
1405           __real__ _M_value = __z.real();
1406           __imag__ _M_value = __z.imag();
1407           return *this;
1408         }
1410       template<typename _Tp>
1411         complex<long double>&
1412         operator+=(const complex<_Tp>& __z)
1413         {
1414           __real__ _M_value += __z.real();
1415           __imag__ _M_value += __z.imag();
1416           return *this;
1417         }
1419       template<typename _Tp>
1420         complex<long double>&
1421         operator-=(const complex<_Tp>& __z)
1422         {
1423           __real__ _M_value -= __z.real();
1424           __imag__ _M_value -= __z.imag();
1425           return *this;
1426         }
1428       template<typename _Tp>
1429         complex<long double>&
1430         operator*=(const complex<_Tp>& __z)
1431         {
1432           _ComplexT __t;
1433           __real__ __t = __z.real();
1434           __imag__ __t = __z.imag();
1435           _M_value *= __t;
1436           return *this;
1437         }
1439       template<typename _Tp>
1440         complex<long double>&
1441         operator/=(const complex<_Tp>& __z)
1442         {
1443           _ComplexT __t;
1444           __real__ __t = __z.real();
1445           __imag__ __t = __z.imag();
1446           _M_value /= __t;
1447           return *this;
1448         }
1450       const _ComplexT& __rep() const { return _M_value; }
1452     private:
1453       _ComplexT _M_value;
1454     };
1456   // These bits have to be at the end of this file, so that the
1457   // specializations have all been defined.
1458   inline
1459   complex<float>::complex(const complex<double>& __z)
1460   : _M_value(__z.__rep()) { }
1462   inline
1463   complex<float>::complex(const complex<long double>& __z)
1464   : _M_value(__z.__rep()) { }
1466   inline
1467   complex<double>::complex(const complex<long double>& __z)
1468   : _M_value(__z.__rep()) { }
1470   // Inhibit implicit instantiations for required instantiations,
1471   // which are defined via explicit instantiations elsewhere.
1472   // NB:  This syntax is a GNU extension.
1473 #if _GLIBCXX_EXTERN_TEMPLATE
1474   extern template istream& operator>>(istream&, complex<float>&);
1475   extern template ostream& operator<<(ostream&, const complex<float>&);
1476   extern template istream& operator>>(istream&, complex<double>&);
1477   extern template ostream& operator<<(ostream&, const complex<double>&);
1478   extern template istream& operator>>(istream&, complex<long double>&);
1479   extern template ostream& operator<<(ostream&, const complex<long double>&);
1481 #ifdef _GLIBCXX_USE_WCHAR_T
1482   extern template wistream& operator>>(wistream&, complex<float>&);
1483   extern template wostream& operator<<(wostream&, const complex<float>&);
1484   extern template wistream& operator>>(wistream&, complex<double>&);
1485   extern template wostream& operator<<(wostream&, const complex<double>&);
1486   extern template wistream& operator>>(wistream&, complex<long double>&);
1487   extern template wostream& operator<<(wostream&, const complex<long double>&);
1488 #endif
1489 #endif
1491   // @} group complex_numbers
1493 _GLIBCXX_END_NAMESPACE
1495 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1497   // See ext/type_traits.h for the primary template.
1498   template<typename _Tp, typename _Up>
1499     struct __promote_2<std::complex<_Tp>, _Up>
1500     {
1501     public:
1502       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1503     };
1505   template<typename _Tp, typename _Up>
1506     struct __promote_2<_Tp, std::complex<_Up> >
1507     {
1508     public:
1509       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1510     };
1511   
1512   template<typename _Tp, typename _Up>
1513     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1514     {
1515     public:
1516       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1517     };
1519 _GLIBCXX_END_NAMESPACE
1521 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1522 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
1523 #    error C++0x header cannot be included from TR1 header
1524 #  endif
1525 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1526 #    include <tr1_impl/complex>
1527 #  else
1528 #    define _GLIBCXX_INCLUDE_AS_CXX0X
1529 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
1530 #    define _GLIBCXX_END_NAMESPACE_TR1
1531 #    define _GLIBCXX_TR1
1532 #    include <tr1_impl/complex>
1533 #    undef _GLIBCXX_TR1
1534 #    undef _GLIBCXX_END_NAMESPACE_TR1
1535 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1536 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
1537 #  endif
1539 _GLIBCXX_BEGIN_NAMESPACE(std)
1541   // Forward declarations.
1542   // DR 781.
1543   template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1545   template<typename _Tp>
1546     std::complex<_Tp>
1547     __complex_proj(const std::complex<_Tp>& __z)
1548     {
1549       const _Tp __den = (__z.real() * __z.real()
1550                          + __z.imag() * __z.imag() + _Tp(1.0));
1552       return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1553                                (_Tp(2.0) * __z.imag()) / __den);
1554     }
1556 #if _GLIBCXX_USE_C99_COMPLEX
1557   inline __complex__ float
1558   __complex_proj(__complex__ float __z)
1559   { return __builtin_cprojf(__z); }
1561   inline __complex__ double
1562   __complex_proj(__complex__ double __z)
1563   { return __builtin_cproj(__z); }
1565   inline __complex__ long double
1566   __complex_proj(const __complex__ long double& __z)
1567   { return __builtin_cprojl(__z); }
1569   template<typename _Tp>
1570     inline std::complex<_Tp>
1571     proj(const std::complex<_Tp>& __z)
1572     { return __complex_proj(__z.__rep()); }
1573 #else
1574   template<typename _Tp>
1575     inline std::complex<_Tp>
1576     proj(const std::complex<_Tp>& __z)
1577     { return __complex_proj(__z); }
1578 #endif
1580   template<typename _Tp>
1581     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1582     proj(_Tp __x)
1583     {
1584       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1585       return std::proj(std::complex<__type>(__x));
1586     }
1588 _GLIBCXX_END_NAMESPACE
1590 #endif
1592 #endif  /* _GLIBCXX_COMPLEX */