OSDN Git Service

2011-01-30 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / complex
1 // TR1 complex -*- C++ -*-
2
3 // Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file tr1/complex
26  *  This is a TR1 C++ Library header. 
27  */
28
29 #ifndef _GLIBCXX_TR1_COMPLEX
30 #define _GLIBCXX_TR1_COMPLEX 1
31
32 #pragma GCC system_header
33
34 #include <complex>
35
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 namespace tr1
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42   /**
43    * @addtogroup complex_numbers
44    * @{
45    */
46
47   // Forward declarations.
48   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
49   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
50   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
51
52   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
53   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
54   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
55   template<typename _Tp> std::complex<_Tp> fabs(const std::complex<_Tp>&);
56
57   template<typename _Tp>
58     inline std::complex<_Tp>
59     __complex_acos(const std::complex<_Tp>& __z)
60     {
61       const std::complex<_Tp> __t = std::tr1::asin(__z);
62       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
63       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
64     }
65
66 #if _GLIBCXX_USE_C99_COMPLEX_TR1
67   inline __complex__ float
68   __complex_acos(__complex__ float __z)
69   { return __builtin_cacosf(__z); }
70
71   inline __complex__ double
72   __complex_acos(__complex__ double __z)
73   { return __builtin_cacos(__z); }
74
75   inline __complex__ long double
76   __complex_acos(const __complex__ long double& __z)
77   { return __builtin_cacosl(__z); }
78
79   template<typename _Tp>
80     inline std::complex<_Tp>
81     acos(const std::complex<_Tp>& __z)
82     { return __complex_acos(__z.__rep()); }
83 #else
84   /// acos(__z) [8.1.2].
85   //  Effects:  Behaves the same as C99 function cacos, defined
86   //            in subclause 7.3.5.1.
87   template<typename _Tp>
88     inline std::complex<_Tp>
89     acos(const std::complex<_Tp>& __z)
90     { return __complex_acos(__z); }
91 #endif
92
93   template<typename _Tp>
94     inline std::complex<_Tp>
95     __complex_asin(const std::complex<_Tp>& __z)
96     {
97       std::complex<_Tp> __t(-__z.imag(), __z.real());
98       __t = std::tr1::asinh(__t);
99       return std::complex<_Tp>(__t.imag(), -__t.real());
100     }
101
102 #if _GLIBCXX_USE_C99_COMPLEX_TR1
103   inline __complex__ float
104   __complex_asin(__complex__ float __z)
105   { return __builtin_casinf(__z); }
106
107   inline __complex__ double
108   __complex_asin(__complex__ double __z)
109   { return __builtin_casin(__z); }
110
111   inline __complex__ long double
112   __complex_asin(const __complex__ long double& __z)
113   { return __builtin_casinl(__z); }
114
115   template<typename _Tp>
116     inline std::complex<_Tp>
117     asin(const std::complex<_Tp>& __z)
118     { return __complex_asin(__z.__rep()); }
119 #else
120   /// asin(__z) [8.1.3].
121   //  Effects:  Behaves the same as C99 function casin, defined
122   //            in subclause 7.3.5.2.
123   template<typename _Tp>
124     inline std::complex<_Tp>
125     asin(const std::complex<_Tp>& __z)
126     { return __complex_asin(__z); }
127 #endif
128   
129   template<typename _Tp>
130     std::complex<_Tp>
131     __complex_atan(const std::complex<_Tp>& __z)
132     {
133       const _Tp __r2 = __z.real() * __z.real();
134       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
135
136       _Tp __num = __z.imag() + _Tp(1.0);
137       _Tp __den = __z.imag() - _Tp(1.0);
138
139       __num = __r2 + __num * __num;
140       __den = __r2 + __den * __den;
141
142       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
143                                _Tp(0.25) * log(__num / __den));
144     }
145
146 #if _GLIBCXX_USE_C99_COMPLEX_TR1
147   inline __complex__ float
148   __complex_atan(__complex__ float __z)
149   { return __builtin_catanf(__z); }
150
151   inline __complex__ double
152   __complex_atan(__complex__ double __z)
153   { return __builtin_catan(__z); }
154
155   inline __complex__ long double
156   __complex_atan(const __complex__ long double& __z)
157   { return __builtin_catanl(__z); }
158
159   template<typename _Tp>
160     inline std::complex<_Tp>
161     atan(const std::complex<_Tp>& __z)
162     { return __complex_atan(__z.__rep()); }
163 #else
164   /// atan(__z) [8.1.4].
165   //  Effects:  Behaves the same as C99 function catan, defined
166   //            in subclause 7.3.5.3.
167   template<typename _Tp>
168     inline std::complex<_Tp>
169     atan(const std::complex<_Tp>& __z)
170     { return __complex_atan(__z); }
171 #endif
172
173   template<typename _Tp>
174     std::complex<_Tp>
175     __complex_acosh(const std::complex<_Tp>& __z)
176     {
177       std::complex<_Tp> __t((__z.real() - __z.imag())
178                             * (__z.real() + __z.imag()) - _Tp(1.0),
179                             _Tp(2.0) * __z.real() * __z.imag());
180       __t = std::sqrt(__t);
181
182       return std::log(__t + __z);
183     }
184
185 #if _GLIBCXX_USE_C99_COMPLEX_TR1
186   inline __complex__ float
187   __complex_acosh(__complex__ float __z)
188   { return __builtin_cacoshf(__z); }
189
190   inline __complex__ double
191   __complex_acosh(__complex__ double __z)
192   { return __builtin_cacosh(__z); }
193
194   inline __complex__ long double
195   __complex_acosh(const __complex__ long double& __z)
196   { return __builtin_cacoshl(__z); }
197
198   template<typename _Tp>
199     inline std::complex<_Tp>
200     acosh(const std::complex<_Tp>& __z)
201     { return __complex_acosh(__z.__rep()); }
202 #else
203   /// acosh(__z) [8.1.5].
204   //  Effects:  Behaves the same as C99 function cacosh, defined
205   //            in subclause 7.3.6.1.
206   template<typename _Tp>
207     inline std::complex<_Tp>
208     acosh(const std::complex<_Tp>& __z)
209     { return __complex_acosh(__z); }
210 #endif
211
212   template<typename _Tp>
213     std::complex<_Tp>
214     __complex_asinh(const std::complex<_Tp>& __z)
215     {
216       std::complex<_Tp> __t((__z.real() - __z.imag())
217                             * (__z.real() + __z.imag()) + _Tp(1.0),
218                             _Tp(2.0) * __z.real() * __z.imag());
219       __t = std::sqrt(__t);
220
221       return std::log(__t + __z);
222     }
223
224 #if _GLIBCXX_USE_C99_COMPLEX_TR1
225   inline __complex__ float
226   __complex_asinh(__complex__ float __z)
227   { return __builtin_casinhf(__z); }
228
229   inline __complex__ double
230   __complex_asinh(__complex__ double __z)
231   { return __builtin_casinh(__z); }
232
233   inline __complex__ long double
234   __complex_asinh(const __complex__ long double& __z)
235   { return __builtin_casinhl(__z); }
236
237   template<typename _Tp>
238     inline std::complex<_Tp>
239     asinh(const std::complex<_Tp>& __z)
240     { return __complex_asinh(__z.__rep()); }
241 #else
242   /// asinh(__z) [8.1.6].
243   //  Effects:  Behaves the same as C99 function casin, defined
244   //            in subclause 7.3.6.2.
245   template<typename _Tp>
246     inline std::complex<_Tp>
247     asinh(const std::complex<_Tp>& __z)
248     { return __complex_asinh(__z); }
249 #endif
250
251   template<typename _Tp>
252     std::complex<_Tp>
253     __complex_atanh(const std::complex<_Tp>& __z)
254     {
255       const _Tp __i2 = __z.imag() * __z.imag();
256       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
257
258       _Tp __num = _Tp(1.0) + __z.real();
259       _Tp __den = _Tp(1.0) - __z.real();
260
261       __num = __i2 + __num * __num;
262       __den = __i2 + __den * __den;
263
264       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
265                                _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
266     }
267
268 #if _GLIBCXX_USE_C99_COMPLEX_TR1
269   inline __complex__ float
270   __complex_atanh(__complex__ float __z)
271   { return __builtin_catanhf(__z); }
272
273   inline __complex__ double
274   __complex_atanh(__complex__ double __z)
275   { return __builtin_catanh(__z); }
276
277   inline __complex__ long double
278   __complex_atanh(const __complex__ long double& __z)
279   { return __builtin_catanhl(__z); }
280
281   template<typename _Tp>
282     inline std::complex<_Tp>
283     atanh(const std::complex<_Tp>& __z)
284     { return __complex_atanh(__z.__rep()); }
285 #else
286   /// atanh(__z) [8.1.7].
287   //  Effects:  Behaves the same as C99 function catanh, defined
288   //            in subclause 7.3.6.3.
289   template<typename _Tp>
290     inline std::complex<_Tp>
291     atanh(const std::complex<_Tp>& __z)
292     { return __complex_atanh(__z); }
293 #endif
294
295   template<typename _Tp>
296     inline std::complex<_Tp>
297     /// fabs(__z) [8.1.8].
298     //  Effects:  Behaves the same as C99 function cabs, defined
299     //            in subclause 7.3.8.1.
300     fabs(const std::complex<_Tp>& __z)
301     { return std::abs(__z); }
302
303   /// Additional overloads [8.1.9].
304 #ifndef __GXX_EXPERIMENTAL_CXX0X__
305
306   template<typename _Tp>
307     inline typename __gnu_cxx::__promote<_Tp>::__type
308     arg(_Tp __x)
309     {
310       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
311 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
312       return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
313                                : __type();
314 #else
315       return std::arg(std::complex<__type>(__x));
316 #endif
317     }
318
319   template<typename _Tp>
320     inline typename __gnu_cxx::__promote<_Tp>::__type
321     imag(_Tp)
322     { return _Tp(); }
323
324   template<typename _Tp>
325     inline typename __gnu_cxx::__promote<_Tp>::__type
326     norm(_Tp __x)
327     {
328       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
329       return __type(__x) * __type(__x);
330     }
331
332   template<typename _Tp>
333     inline typename __gnu_cxx::__promote<_Tp>::__type
334     real(_Tp __x)
335     { return __x; }
336
337 #endif
338
339   template<typename _Tp, typename _Up>
340     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
341     pow(const std::complex<_Tp>& __x, const _Up& __y)
342     {
343       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
344       return std::pow(std::complex<__type>(__x), __type(__y));
345     }
346
347   template<typename _Tp, typename _Up>
348     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
349     pow(const _Tp& __x, const std::complex<_Up>& __y)
350     {
351       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
352       return std::pow(__type(__x), std::complex<__type>(__y));
353     }
354
355   template<typename _Tp, typename _Up>
356     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
357     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
358     {
359       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
360       return std::pow(std::complex<__type>(__x),
361                       std::complex<__type>(__y));
362     }
363
364   using std::arg;
365
366   template<typename _Tp>
367     inline std::complex<_Tp>
368     conj(const std::complex<_Tp>& __z)
369     { return std::conj(__z); }  
370
371   template<typename _Tp>
372     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
373     conj(_Tp __x)
374     { return __x; }
375
376   using std::imag;
377   using std::norm;
378   using std::polar;
379
380   template<typename _Tp, typename _Up>
381     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
382     polar(const _Tp& __rho, const _Up& __theta)
383     {
384       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
385       return std::polar(__type(__rho), __type(__theta));
386     }
387
388   using std::real;
389
390   template<typename _Tp>
391     inline std::complex<_Tp>
392     pow(const std::complex<_Tp>& __x, const _Tp& __y)
393     { return std::pow(__x, __y); }
394
395   template<typename _Tp>
396     inline std::complex<_Tp>
397     pow(const _Tp& __x, const std::complex<_Tp>& __y)
398     { return std::pow(__x, __y); }
399
400   template<typename _Tp>
401     inline std::complex<_Tp>
402     pow(const std::complex<_Tp>& __x, const std::complex<_Tp>& __y)
403     { return std::pow(__x, __y); }
404
405 // @} group complex_numbers
406
407 _GLIBCXX_END_NAMESPACE_VERSION
408 }
409 }
410
411 #endif // _GLIBCXX_TR1_COMPLEX