1 // TR1 complex -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 * This is a TR1 C++ Library header.
29 #ifndef _GLIBCXX_TR1_COMPLEX
30 #define _GLIBCXX_TR1_COMPLEX 1
32 #pragma GCC system_header
36 namespace std _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 * @addtogroup complex_numbers
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>&);
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>&);
57 template<typename _Tp>
58 inline std::complex<_Tp>
59 __complex_acos(const std::complex<_Tp>& __z)
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());
66 #if _GLIBCXX_USE_C99_COMPLEX_TR1
67 inline __complex__ float
68 __complex_acos(__complex__ float __z)
69 { return __builtin_cacosf(__z); }
71 inline __complex__ double
72 __complex_acos(__complex__ double __z)
73 { return __builtin_cacos(__z); }
75 inline __complex__ long double
76 __complex_acos(const __complex__ long double& __z)
77 { return __builtin_cacosl(__z); }
79 template<typename _Tp>
80 inline std::complex<_Tp>
81 acos(const std::complex<_Tp>& __z)
82 { return __complex_acos(__z.__rep()); }
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); }
93 template<typename _Tp>
94 inline std::complex<_Tp>
95 __complex_asin(const std::complex<_Tp>& __z)
97 std::complex<_Tp> __t(-__z.imag(), __z.real());
98 __t = std::tr1::asinh(__t);
99 return std::complex<_Tp>(__t.imag(), -__t.real());
102 #if _GLIBCXX_USE_C99_COMPLEX_TR1
103 inline __complex__ float
104 __complex_asin(__complex__ float __z)
105 { return __builtin_casinf(__z); }
107 inline __complex__ double
108 __complex_asin(__complex__ double __z)
109 { return __builtin_casin(__z); }
111 inline __complex__ long double
112 __complex_asin(const __complex__ long double& __z)
113 { return __builtin_casinl(__z); }
115 template<typename _Tp>
116 inline std::complex<_Tp>
117 asin(const std::complex<_Tp>& __z)
118 { return __complex_asin(__z.__rep()); }
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); }
129 template<typename _Tp>
131 __complex_atan(const std::complex<_Tp>& __z)
133 const _Tp __r2 = __z.real() * __z.real();
134 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
136 _Tp __num = __z.imag() + _Tp(1.0);
137 _Tp __den = __z.imag() - _Tp(1.0);
139 __num = __r2 + __num * __num;
140 __den = __r2 + __den * __den;
142 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
143 _Tp(0.25) * log(__num / __den));
146 #if _GLIBCXX_USE_C99_COMPLEX_TR1
147 inline __complex__ float
148 __complex_atan(__complex__ float __z)
149 { return __builtin_catanf(__z); }
151 inline __complex__ double
152 __complex_atan(__complex__ double __z)
153 { return __builtin_catan(__z); }
155 inline __complex__ long double
156 __complex_atan(const __complex__ long double& __z)
157 { return __builtin_catanl(__z); }
159 template<typename _Tp>
160 inline std::complex<_Tp>
161 atan(const std::complex<_Tp>& __z)
162 { return __complex_atan(__z.__rep()); }
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); }
173 template<typename _Tp>
175 __complex_acosh(const std::complex<_Tp>& __z)
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);
182 return std::log(__t + __z);
185 #if _GLIBCXX_USE_C99_COMPLEX_TR1
186 inline __complex__ float
187 __complex_acosh(__complex__ float __z)
188 { return __builtin_cacoshf(__z); }
190 inline __complex__ double
191 __complex_acosh(__complex__ double __z)
192 { return __builtin_cacosh(__z); }
194 inline __complex__ long double
195 __complex_acosh(const __complex__ long double& __z)
196 { return __builtin_cacoshl(__z); }
198 template<typename _Tp>
199 inline std::complex<_Tp>
200 acosh(const std::complex<_Tp>& __z)
201 { return __complex_acosh(__z.__rep()); }
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); }
212 template<typename _Tp>
214 __complex_asinh(const std::complex<_Tp>& __z)
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);
221 return std::log(__t + __z);
224 #if _GLIBCXX_USE_C99_COMPLEX_TR1
225 inline __complex__ float
226 __complex_asinh(__complex__ float __z)
227 { return __builtin_casinhf(__z); }
229 inline __complex__ double
230 __complex_asinh(__complex__ double __z)
231 { return __builtin_casinh(__z); }
233 inline __complex__ long double
234 __complex_asinh(const __complex__ long double& __z)
235 { return __builtin_casinhl(__z); }
237 template<typename _Tp>
238 inline std::complex<_Tp>
239 asinh(const std::complex<_Tp>& __z)
240 { return __complex_asinh(__z.__rep()); }
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); }
251 template<typename _Tp>
253 __complex_atanh(const std::complex<_Tp>& __z)
255 const _Tp __i2 = __z.imag() * __z.imag();
256 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
258 _Tp __num = _Tp(1.0) + __z.real();
259 _Tp __den = _Tp(1.0) - __z.real();
261 __num = __i2 + __num * __num;
262 __den = __i2 + __den * __den;
264 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
265 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
268 #if _GLIBCXX_USE_C99_COMPLEX_TR1
269 inline __complex__ float
270 __complex_atanh(__complex__ float __z)
271 { return __builtin_catanhf(__z); }
273 inline __complex__ double
274 __complex_atanh(__complex__ double __z)
275 { return __builtin_catanh(__z); }
277 inline __complex__ long double
278 __complex_atanh(const __complex__ long double& __z)
279 { return __builtin_catanhl(__z); }
281 template<typename _Tp>
282 inline std::complex<_Tp>
283 atanh(const std::complex<_Tp>& __z)
284 { return __complex_atanh(__z.__rep()); }
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); }
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); }
303 /// Additional overloads [8.1.9].
304 #ifndef __GXX_EXPERIMENTAL_CXX0X__
306 template<typename _Tp>
307 inline typename __gnu_cxx::__promote<_Tp>::__type
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)
315 return std::arg(std::complex<__type>(__x));
319 template<typename _Tp>
320 inline typename __gnu_cxx::__promote<_Tp>::__type
324 template<typename _Tp>
325 inline typename __gnu_cxx::__promote<_Tp>::__type
328 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
329 return __type(__x) * __type(__x);
332 template<typename _Tp>
333 inline typename __gnu_cxx::__promote<_Tp>::__type
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)
343 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
344 return std::pow(std::complex<__type>(__x), __type(__y));
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)
351 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
352 return std::pow(__type(__x), std::complex<__type>(__y));
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)
359 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
360 return std::pow(std::complex<__type>(__x),
361 std::complex<__type>(__y));
366 template<typename _Tp>
367 inline std::complex<_Tp>
368 conj(const std::complex<_Tp>& __z)
369 { return std::conj(__z); }
371 template<typename _Tp>
372 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
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)
384 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
385 return std::polar(__type(__rho), __type(__theta));
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); }
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); }
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); }
405 // @} group complex_numbers
407 _GLIBCXX_END_NAMESPACE_VERSION
411 #endif // _GLIBCXX_TR1_COMPLEX