1// TR1 complex -*- C++ -*-
3// Copyright (C) 2006-2023 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
34#include <bits/requires_hosted.h> // TR1
38namespace std _GLIBCXX_VISIBILITY(default)
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
45 * @addtogroup complex_numbers
49#if __cplusplus >= 201103L
57 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
58 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
59 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
60 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
61 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
62 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
65 // The std::fabs return type in C++11 mode is different (just _Tp).
66 template<typename _Tp> std::complex<_Tp> fabs(const std::complex<_Tp>&);
68#if __cplusplus < 201103L
69 template<typename _Tp>
70 inline std::complex<_Tp>
71 __complex_acos(const std::complex<_Tp>& __z)
73 const std::complex<_Tp> __t = std::tr1::asin(__z);
74 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
75 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
78#if _GLIBCXX_USE_C99_COMPLEX_TR1
79 inline __complex__ float
80 __complex_acos(__complex__ float __z)
81 { return __builtin_cacosf(__z); }
83 inline __complex__ double
84 __complex_acos(__complex__ double __z)
85 { return __builtin_cacos(__z); }
87 inline __complex__ long double
88 __complex_acos(const __complex__ long double& __z)
89 { return __builtin_cacosl(__z); }
91 template<typename _Tp>
92 inline std::complex<_Tp>
93 acos(const std::complex<_Tp>& __z)
94 { return __complex_acos(__z.__rep()); }
96 /// acos(__z) [8.1.2].
97 // Effects: Behaves the same as C99 function cacos, defined
98 // in subclause 7.3.5.1.
99 template<typename _Tp>
100 inline std::complex<_Tp>
101 acos(const std::complex<_Tp>& __z)
102 { return __complex_acos(__z); }
105 template<typename _Tp>
106 inline std::complex<_Tp>
107 __complex_asin(const std::complex<_Tp>& __z)
109 std::complex<_Tp> __t(-__z.imag(), __z.real());
110 __t = std::tr1::asinh(__t);
111 return std::complex<_Tp>(__t.imag(), -__t.real());
114#if _GLIBCXX_USE_C99_COMPLEX_TR1
115 inline __complex__ float
116 __complex_asin(__complex__ float __z)
117 { return __builtin_casinf(__z); }
119 inline __complex__ double
120 __complex_asin(__complex__ double __z)
121 { return __builtin_casin(__z); }
123 inline __complex__ long double
124 __complex_asin(const __complex__ long double& __z)
125 { return __builtin_casinl(__z); }
127 template<typename _Tp>
128 inline std::complex<_Tp>
129 asin(const std::complex<_Tp>& __z)
130 { return __complex_asin(__z.__rep()); }
132 /// asin(__z) [8.1.3].
133 // Effects: Behaves the same as C99 function casin, defined
134 // in subclause 7.3.5.2.
135 template<typename _Tp>
136 inline std::complex<_Tp>
137 asin(const std::complex<_Tp>& __z)
138 { return __complex_asin(__z); }
141 template<typename _Tp>
143 __complex_atan(const std::complex<_Tp>& __z)
145 const _Tp __r2 = __z.real() * __z.real();
146 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
148 _Tp __num = __z.imag() + _Tp(1.0);
149 _Tp __den = __z.imag() - _Tp(1.0);
151 __num = __r2 + __num * __num;
152 __den = __r2 + __den * __den;
154 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
155 _Tp(0.25) * log(__num / __den));
158#if _GLIBCXX_USE_C99_COMPLEX_TR1
159 inline __complex__ float
160 __complex_atan(__complex__ float __z)
161 { return __builtin_catanf(__z); }
163 inline __complex__ double
164 __complex_atan(__complex__ double __z)
165 { return __builtin_catan(__z); }
167 inline __complex__ long double
168 __complex_atan(const __complex__ long double& __z)
169 { return __builtin_catanl(__z); }
171 template<typename _Tp>
172 inline std::complex<_Tp>
173 atan(const std::complex<_Tp>& __z)
174 { return __complex_atan(__z.__rep()); }
176 /// atan(__z) [8.1.4].
177 // Effects: Behaves the same as C99 function catan, defined
178 // in subclause 7.3.5.3.
179 template<typename _Tp>
180 inline std::complex<_Tp>
181 atan(const std::complex<_Tp>& __z)
182 { return __complex_atan(__z); }
185 template<typename _Tp>
187 __complex_acosh(const std::complex<_Tp>& __z)
190 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
191 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
194#if _GLIBCXX_USE_C99_COMPLEX_TR1
195 inline __complex__ float
196 __complex_acosh(__complex__ float __z)
197 { return __builtin_cacoshf(__z); }
199 inline __complex__ double
200 __complex_acosh(__complex__ double __z)
201 { return __builtin_cacosh(__z); }
203 inline __complex__ long double
204 __complex_acosh(const __complex__ long double& __z)
205 { return __builtin_cacoshl(__z); }
207 template<typename _Tp>
208 inline std::complex<_Tp>
209 acosh(const std::complex<_Tp>& __z)
210 { return __complex_acosh(__z.__rep()); }
212 /// acosh(__z) [8.1.5].
213 // Effects: Behaves the same as C99 function cacosh, defined
214 // in subclause 7.3.6.1.
215 template<typename _Tp>
216 inline std::complex<_Tp>
217 acosh(const std::complex<_Tp>& __z)
218 { return __complex_acosh(__z); }
221 template<typename _Tp>
223 __complex_asinh(const std::complex<_Tp>& __z)
225 std::complex<_Tp> __t((__z.real() - __z.imag())
226 * (__z.real() + __z.imag()) + _Tp(1.0),
227 _Tp(2.0) * __z.real() * __z.imag());
228 __t = std::sqrt(__t);
230 return std::log(__t + __z);
233#if _GLIBCXX_USE_C99_COMPLEX_TR1
234 inline __complex__ float
235 __complex_asinh(__complex__ float __z)
236 { return __builtin_casinhf(__z); }
238 inline __complex__ double
239 __complex_asinh(__complex__ double __z)
240 { return __builtin_casinh(__z); }
242 inline __complex__ long double
243 __complex_asinh(const __complex__ long double& __z)
244 { return __builtin_casinhl(__z); }
246 template<typename _Tp>
247 inline std::complex<_Tp>
248 asinh(const std::complex<_Tp>& __z)
249 { return __complex_asinh(__z.__rep()); }
251 /// asinh(__z) [8.1.6].
252 // Effects: Behaves the same as C99 function casin, defined
253 // in subclause 7.3.6.2.
254 template<typename _Tp>
255 inline std::complex<_Tp>
256 asinh(const std::complex<_Tp>& __z)
257 { return __complex_asinh(__z); }
260 template<typename _Tp>
262 __complex_atanh(const std::complex<_Tp>& __z)
264 const _Tp __i2 = __z.imag() * __z.imag();
265 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
267 _Tp __num = _Tp(1.0) + __z.real();
268 _Tp __den = _Tp(1.0) - __z.real();
270 __num = __i2 + __num * __num;
271 __den = __i2 + __den * __den;
273 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
274 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
277#if _GLIBCXX_USE_C99_COMPLEX_TR1
278 inline __complex__ float
279 __complex_atanh(__complex__ float __z)
280 { return __builtin_catanhf(__z); }
282 inline __complex__ double
283 __complex_atanh(__complex__ double __z)
284 { return __builtin_catanh(__z); }
286 inline __complex__ long double
287 __complex_atanh(const __complex__ long double& __z)
288 { return __builtin_catanhl(__z); }
290 template<typename _Tp>
291 inline std::complex<_Tp>
292 atanh(const std::complex<_Tp>& __z)
293 { return __complex_atanh(__z.__rep()); }
295 /// atanh(__z) [8.1.7].
296 // Effects: Behaves the same as C99 function catanh, defined
297 // in subclause 7.3.6.3.
298 template<typename _Tp>
299 inline std::complex<_Tp>
300 atanh(const std::complex<_Tp>& __z)
301 { return __complex_atanh(__z); }
306 template<typename _Tp>
307 inline std::complex<_Tp>
308 /// fabs(__z) [8.1.8].
309 // Effects: Behaves the same as C99 function cabs, defined
310 // in subclause 7.3.8.1.
311 fabs(const std::complex<_Tp>& __z)
312 { return std::abs(__z); }
314 /// Additional overloads [8.1.9].
315#if __cplusplus < 201103L
317 template<typename _Tp>
318 inline typename __gnu_cxx::__promote<_Tp>::__type
321 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
322#if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
323 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
326 return std::arg(std::complex<__type>(__x));
330 template<typename _Tp>
331 inline typename __gnu_cxx::__promote<_Tp>::__type
335 template<typename _Tp>
336 inline typename __gnu_cxx::__promote<_Tp>::__type
339 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
340 return __type(__x) * __type(__x);
343 template<typename _Tp>
344 inline typename __gnu_cxx::__promote<_Tp>::__type
350 template<typename _Tp, typename _Up>
351 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
352 pow(const std::complex<_Tp>& __x, const _Up& __y)
354 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
355 return std::pow(std::complex<__type>(__x), __type(__y));
358 template<typename _Tp, typename _Up>
359 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
360 pow(const _Tp& __x, const std::complex<_Up>& __y)
362 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
363 return std::pow(__type(__x), std::complex<__type>(__y));
366 template<typename _Tp, typename _Up>
367 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
368 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
370 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
371 return std::pow(std::complex<__type>(__x),
372 std::complex<__type>(__y));
377 template<typename _Tp>
378 inline std::complex<_Tp>
379 conj(const std::complex<_Tp>& __z)
380 { return std::conj(__z); }
382 template<typename _Tp>
383 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
391 template<typename _Tp, typename _Up>
392 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
393 polar(const _Tp& __rho, const _Up& __theta)
395 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
396 return std::polar(__type(__rho), __type(__theta));
401 template<typename _Tp>
402 inline std::complex<_Tp>
403 pow(const std::complex<_Tp>& __x, const _Tp& __y)
404 { return std::pow(__x, __y); }
406 template<typename _Tp>
407 inline std::complex<_Tp>
408 pow(const _Tp& __x, const std::complex<_Tp>& __y)
409 { return std::pow(__x, __y); }
411 template<typename _Tp>
412 inline std::complex<_Tp>
413 pow(const std::complex<_Tp>& __x, const std::complex<_Tp>& __y)
414 { return std::pow(__x, __y); }
416/// @} group complex_numbers
419_GLIBCXX_END_NAMESPACE_VERSION
422#endif // _GLIBCXX_TR1_COMPLEX