1 // TR1 complex -*- C++ -*-
3 // Copyright (C) 2006-2024 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 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
36 #include <bits/requires_hosted.h> // TR1
40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 * @addtogroup complex_numbers
51 #if __cplusplus >= 201103L
59 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
60 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
61 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
62 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
63 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
64 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
67 // The std::fabs return type in C++11 mode is different (just _Tp).
68 template<typename _Tp> std::complex<_Tp> fabs(const std::complex<_Tp>&);
70 #if __cplusplus < 201103L
71 template<typename _Tp>
72 inline std::complex<_Tp>
73 __complex_acos(const std::complex<_Tp>& __z)
75 const std::complex<_Tp> __t = std::tr1::asin(__z);
76 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
77 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
80 #if _GLIBCXX_USE_C99_COMPLEX_TR1
81 inline __complex__ float
82 __complex_acos(__complex__ float __z)
83 { return __builtin_cacosf(__z); }
85 inline __complex__ double
86 __complex_acos(__complex__ double __z)
87 { return __builtin_cacos(__z); }
89 inline __complex__ long double
90 __complex_acos(const __complex__ long double& __z)
91 { return __builtin_cacosl(__z); }
93 template<typename _Tp>
94 inline std::complex<_Tp>
95 acos(const std::complex<_Tp>& __z)
96 { return __complex_acos(__z.__rep()); }
98 /// acos(__z) [8.1.2].
99 // Effects: Behaves the same as C99 function cacos, defined
100 // in subclause 7.3.5.1.
101 template<typename _Tp>
102 inline std::complex<_Tp>
103 acos(const std::complex<_Tp>& __z)
104 { return __complex_acos(__z); }
107 template<typename _Tp>
108 inline std::complex<_Tp>
109 __complex_asin(const std::complex<_Tp>& __z)
111 std::complex<_Tp> __t(-__z.imag(), __z.real());
112 __t = std::tr1::asinh(__t);
113 return std::complex<_Tp>(__t.imag(), -__t.real());
116 #if _GLIBCXX_USE_C99_COMPLEX_TR1
117 inline __complex__ float
118 __complex_asin(__complex__ float __z)
119 { return __builtin_casinf(__z); }
121 inline __complex__ double
122 __complex_asin(__complex__ double __z)
123 { return __builtin_casin(__z); }
125 inline __complex__ long double
126 __complex_asin(const __complex__ long double& __z)
127 { return __builtin_casinl(__z); }
129 template<typename _Tp>
130 inline std::complex<_Tp>
131 asin(const std::complex<_Tp>& __z)
132 { return __complex_asin(__z.__rep()); }
134 /// asin(__z) [8.1.3].
135 // Effects: Behaves the same as C99 function casin, defined
136 // in subclause 7.3.5.2.
137 template<typename _Tp>
138 inline std::complex<_Tp>
139 asin(const std::complex<_Tp>& __z)
140 { return __complex_asin(__z); }
143 template<typename _Tp>
145 __complex_atan(const std::complex<_Tp>& __z)
147 const _Tp __r2 = __z.real() * __z.real();
148 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
150 _Tp __num = __z.imag() + _Tp(1.0);
151 _Tp __den = __z.imag() - _Tp(1.0);
153 __num = __r2 + __num * __num;
154 __den = __r2 + __den * __den;
156 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
157 _Tp(0.25) * log(__num / __den));
160 #if _GLIBCXX_USE_C99_COMPLEX_TR1
161 inline __complex__ float
162 __complex_atan(__complex__ float __z)
163 { return __builtin_catanf(__z); }
165 inline __complex__ double
166 __complex_atan(__complex__ double __z)
167 { return __builtin_catan(__z); }
169 inline __complex__ long double
170 __complex_atan(const __complex__ long double& __z)
171 { return __builtin_catanl(__z); }
173 template<typename _Tp>
174 inline std::complex<_Tp>
175 atan(const std::complex<_Tp>& __z)
176 { return __complex_atan(__z.__rep()); }
178 /// atan(__z) [8.1.4].
179 // Effects: Behaves the same as C99 function catan, defined
180 // in subclause 7.3.5.3.
181 template<typename _Tp>
182 inline std::complex<_Tp>
183 atan(const std::complex<_Tp>& __z)
184 { return __complex_atan(__z); }
187 template<typename _Tp>
189 __complex_acosh(const std::complex<_Tp>& __z)
192 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
193 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
196 #if _GLIBCXX_USE_C99_COMPLEX_TR1
197 inline __complex__ float
198 __complex_acosh(__complex__ float __z)
199 { return __builtin_cacoshf(__z); }
201 inline __complex__ double
202 __complex_acosh(__complex__ double __z)
203 { return __builtin_cacosh(__z); }
205 inline __complex__ long double
206 __complex_acosh(const __complex__ long double& __z)
207 { return __builtin_cacoshl(__z); }
209 template<typename _Tp>
210 inline std::complex<_Tp>
211 acosh(const std::complex<_Tp>& __z)
212 { return __complex_acosh(__z.__rep()); }
214 /// acosh(__z) [8.1.5].
215 // Effects: Behaves the same as C99 function cacosh, defined
216 // in subclause 7.3.6.1.
217 template<typename _Tp>
218 inline std::complex<_Tp>
219 acosh(const std::complex<_Tp>& __z)
220 { return __complex_acosh(__z); }
223 template<typename _Tp>
225 __complex_asinh(const std::complex<_Tp>& __z)
227 std::complex<_Tp> __t((__z.real() - __z.imag())
228 * (__z.real() + __z.imag()) + _Tp(1.0),
229 _Tp(2.0) * __z.real() * __z.imag());
230 __t = std::sqrt(__t);
232 return std::log(__t + __z);
235 #if _GLIBCXX_USE_C99_COMPLEX_TR1
236 inline __complex__ float
237 __complex_asinh(__complex__ float __z)
238 { return __builtin_casinhf(__z); }
240 inline __complex__ double
241 __complex_asinh(__complex__ double __z)
242 { return __builtin_casinh(__z); }
244 inline __complex__ long double
245 __complex_asinh(const __complex__ long double& __z)
246 { return __builtin_casinhl(__z); }
248 template<typename _Tp>
249 inline std::complex<_Tp>
250 asinh(const std::complex<_Tp>& __z)
251 { return __complex_asinh(__z.__rep()); }
253 /// asinh(__z) [8.1.6].
254 // Effects: Behaves the same as C99 function casin, defined
255 // in subclause 7.3.6.2.
256 template<typename _Tp>
257 inline std::complex<_Tp>
258 asinh(const std::complex<_Tp>& __z)
259 { return __complex_asinh(__z); }
262 template<typename _Tp>
264 __complex_atanh(const std::complex<_Tp>& __z)
266 const _Tp __i2 = __z.imag() * __z.imag();
267 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
269 _Tp __num = _Tp(1.0) + __z.real();
270 _Tp __den = _Tp(1.0) - __z.real();
272 __num = __i2 + __num * __num;
273 __den = __i2 + __den * __den;
275 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
276 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
279 #if _GLIBCXX_USE_C99_COMPLEX_TR1
280 inline __complex__ float
281 __complex_atanh(__complex__ float __z)
282 { return __builtin_catanhf(__z); }
284 inline __complex__ double
285 __complex_atanh(__complex__ double __z)
286 { return __builtin_catanh(__z); }
288 inline __complex__ long double
289 __complex_atanh(const __complex__ long double& __z)
290 { return __builtin_catanhl(__z); }
292 template<typename _Tp>
293 inline std::complex<_Tp>
294 atanh(const std::complex<_Tp>& __z)
295 { return __complex_atanh(__z.__rep()); }
297 /// atanh(__z) [8.1.7].
298 // Effects: Behaves the same as C99 function catanh, defined
299 // in subclause 7.3.6.3.
300 template<typename _Tp>
301 inline std::complex<_Tp>
302 atanh(const std::complex<_Tp>& __z)
303 { return __complex_atanh(__z); }
308 template<typename _Tp>
309 inline std::complex<_Tp>
310 /// fabs(__z) [8.1.8].
311 // Effects: Behaves the same as C99 function cabs, defined
312 // in subclause 7.3.8.1.
313 fabs(const std::complex<_Tp>& __z)
314 { return std::abs(__z); }
316 /// Additional overloads [8.1.9].
317 #if __cplusplus < 201103L
319 template<typename _Tp>
320 inline typename __gnu_cxx::__promote<_Tp>::__type
323 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
324 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
325 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
328 return std::arg(std::complex<__type>(__x));
332 template<typename _Tp>
333 inline typename __gnu_cxx::__promote<_Tp>::__type
337 template<typename _Tp>
338 inline typename __gnu_cxx::__promote<_Tp>::__type
341 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
342 return __type(__x) * __type(__x);
345 template<typename _Tp>
346 inline typename __gnu_cxx::__promote<_Tp>::__type
352 template<typename _Tp, typename _Up>
353 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
354 pow(const std::complex<_Tp>& __x, const _Up& __y)
356 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
357 return std::pow(std::complex<__type>(__x), __type(__y));
360 template<typename _Tp, typename _Up>
361 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
362 pow(const _Tp& __x, const std::complex<_Up>& __y)
364 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
365 return std::pow(__type(__x), std::complex<__type>(__y));
368 template<typename _Tp, typename _Up>
369 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
370 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
372 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
373 return std::pow(std::complex<__type>(__x),
374 std::complex<__type>(__y));
379 template<typename _Tp>
380 inline std::complex<_Tp>
381 conj(const std::complex<_Tp>& __z)
382 { return std::conj(__z); }
384 template<typename _Tp>
385 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
393 template<typename _Tp, typename _Up>
394 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
395 polar(const _Tp& __rho, const _Up& __theta)
397 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
398 return std::polar(__type(__rho), __type(__theta));
403 template<typename _Tp>
404 inline std::complex<_Tp>
405 pow(const std::complex<_Tp>& __x, const _Tp& __y)
406 { return std::pow(__x, __y); }
408 template<typename _Tp>
409 inline std::complex<_Tp>
410 pow(const _Tp& __x, const std::complex<_Tp>& __y)
411 { return std::pow(__x, __y); }
413 template<typename _Tp>
414 inline std::complex<_Tp>
415 pow(const std::complex<_Tp>& __x, const std::complex<_Tp>& __y)
416 { return std::pow(__x, __y); }
418 /// @} group complex_numbers
421 _GLIBCXX_END_NAMESPACE_VERSION
424 #endif // _GLIBCXX_TR1_COMPLEX