libstdc++
tr1/complex
Go to the documentation of this file.
1 // TR1 complex -*- C++ -*-
2 
3 // Copyright (C) 2006-2025 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 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
34 #endif
35 
36 #include <bits/requires_hosted.h> // TR1
37 
38 #include <complex>
39 
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44 namespace tr1
45 {
46  /**
47  * @addtogroup complex_numbers
48  * @{
49  */
50 
51 #if __cplusplus >= 201103L
52  using std::acos;
53  using std::asin;
54  using std::atan;
55  using std::acosh;
56  using std::asinh;
57  using std::atanh;
58 #else
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>&);
65 #endif
66 
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>&);
69 
70 #if __cplusplus < 201103L
71  template<typename _Tp>
72  inline std::complex<_Tp>
73  __complex_acos(const std::complex<_Tp>& __z)
74  {
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());
78  }
79 
80 #if _GLIBCXX_USE_C99_COMPLEX_TR1
81  inline __complex__ float
82  __complex_acos(__complex__ float __z)
83  { return __builtin_cacosf(__z); }
84 
85  inline __complex__ double
86  __complex_acos(__complex__ double __z)
87  { return __builtin_cacos(__z); }
88 
89  inline __complex__ long double
90  __complex_acos(const __complex__ long double& __z)
91  { return __builtin_cacosl(__z); }
92 
93  template<typename _Tp>
94  inline std::complex<_Tp>
95  acos(const std::complex<_Tp>& __z)
96  { return __complex_acos(__z.__rep()); }
97 #else
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); }
105 #endif
106 
107  template<typename _Tp>
108  inline std::complex<_Tp>
109  __complex_asin(const std::complex<_Tp>& __z)
110  {
111  std::complex<_Tp> __t(-__z.imag(), __z.real());
112  __t = std::tr1::asinh(__t);
113  return std::complex<_Tp>(__t.imag(), -__t.real());
114  }
115 
116 #if _GLIBCXX_USE_C99_COMPLEX_TR1
117  inline __complex__ float
118  __complex_asin(__complex__ float __z)
119  { return __builtin_casinf(__z); }
120 
121  inline __complex__ double
122  __complex_asin(__complex__ double __z)
123  { return __builtin_casin(__z); }
124 
125  inline __complex__ long double
126  __complex_asin(const __complex__ long double& __z)
127  { return __builtin_casinl(__z); }
128 
129  template<typename _Tp>
130  inline std::complex<_Tp>
131  asin(const std::complex<_Tp>& __z)
132  { return __complex_asin(__z.__rep()); }
133 #else
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); }
141 #endif
142 
143  template<typename _Tp>
144  std::complex<_Tp>
145  __complex_atan(const std::complex<_Tp>& __z)
146  {
147  const _Tp __r2 = __z.real() * __z.real();
148  const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
149 
150  _Tp __num = __z.imag() + _Tp(1.0);
151  _Tp __den = __z.imag() - _Tp(1.0);
152 
153  __num = __r2 + __num * __num;
154  __den = __r2 + __den * __den;
155 
156  return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
157  _Tp(0.25) * log(__num / __den));
158  }
159 
160 #if _GLIBCXX_USE_C99_COMPLEX_TR1
161  inline __complex__ float
162  __complex_atan(__complex__ float __z)
163  { return __builtin_catanf(__z); }
164 
165  inline __complex__ double
166  __complex_atan(__complex__ double __z)
167  { return __builtin_catan(__z); }
168 
169  inline __complex__ long double
170  __complex_atan(const __complex__ long double& __z)
171  { return __builtin_catanl(__z); }
172 
173  template<typename _Tp>
174  inline std::complex<_Tp>
175  atan(const std::complex<_Tp>& __z)
176  { return __complex_atan(__z.__rep()); }
177 #else
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); }
185 #endif
186 
187  template<typename _Tp>
188  std::complex<_Tp>
189  __complex_acosh(const std::complex<_Tp>& __z)
190  {
191  // Kahan's formula.
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))));
194  }
195 
196 #if _GLIBCXX_USE_C99_COMPLEX_TR1
197  inline __complex__ float
198  __complex_acosh(__complex__ float __z)
199  { return __builtin_cacoshf(__z); }
200 
201  inline __complex__ double
202  __complex_acosh(__complex__ double __z)
203  { return __builtin_cacosh(__z); }
204 
205  inline __complex__ long double
206  __complex_acosh(const __complex__ long double& __z)
207  { return __builtin_cacoshl(__z); }
208 
209  template<typename _Tp>
210  inline std::complex<_Tp>
211  acosh(const std::complex<_Tp>& __z)
212  { return __complex_acosh(__z.__rep()); }
213 #else
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); }
221 #endif
222 
223  template<typename _Tp>
224  std::complex<_Tp>
225  __complex_asinh(const std::complex<_Tp>& __z)
226  {
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);
231 
232  return std::log(__t + __z);
233  }
234 
235 #if _GLIBCXX_USE_C99_COMPLEX_TR1
236  inline __complex__ float
237  __complex_asinh(__complex__ float __z)
238  { return __builtin_casinhf(__z); }
239 
240  inline __complex__ double
241  __complex_asinh(__complex__ double __z)
242  { return __builtin_casinh(__z); }
243 
244  inline __complex__ long double
245  __complex_asinh(const __complex__ long double& __z)
246  { return __builtin_casinhl(__z); }
247 
248  template<typename _Tp>
249  inline std::complex<_Tp>
250  asinh(const std::complex<_Tp>& __z)
251  { return __complex_asinh(__z.__rep()); }
252 #else
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); }
260 #endif
261 
262  template<typename _Tp>
263  std::complex<_Tp>
264  __complex_atanh(const std::complex<_Tp>& __z)
265  {
266  const _Tp __i2 = __z.imag() * __z.imag();
267  const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
268 
269  _Tp __num = _Tp(1.0) + __z.real();
270  _Tp __den = _Tp(1.0) - __z.real();
271 
272  __num = __i2 + __num * __num;
273  __den = __i2 + __den * __den;
274 
275  return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
276  _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
277  }
278 
279 #if _GLIBCXX_USE_C99_COMPLEX_TR1
280  inline __complex__ float
281  __complex_atanh(__complex__ float __z)
282  { return __builtin_catanhf(__z); }
283 
284  inline __complex__ double
285  __complex_atanh(__complex__ double __z)
286  { return __builtin_catanh(__z); }
287 
288  inline __complex__ long double
289  __complex_atanh(const __complex__ long double& __z)
290  { return __builtin_catanhl(__z); }
291 
292  template<typename _Tp>
293  inline std::complex<_Tp>
294  atanh(const std::complex<_Tp>& __z)
295  { return __complex_atanh(__z.__rep()); }
296 #else
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); }
304 #endif
305 
306 #endif // C++11
307 
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); }
315 
316  /// Additional overloads [8.1.9].
317 #if __cplusplus < 201103L
318 
319  template<typename _Tp>
320  inline typename __gnu_cxx::__promote<_Tp>::__type
321  arg(_Tp __x)
322  {
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)
326  : __type();
327 #else
328  return std::arg(std::complex<__type>(__x));
329 #endif
330  }
331 
332  template<typename _Tp>
333  inline typename __gnu_cxx::__promote<_Tp>::__type
334  imag(_Tp)
335  { return _Tp(); }
336 
337  template<typename _Tp>
338  inline typename __gnu_cxx::__promote<_Tp>::__type
339  norm(_Tp __x)
340  {
341  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
342  return __type(__x) * __type(__x);
343  }
344 
345  template<typename _Tp>
346  inline typename __gnu_cxx::__promote<_Tp>::__type
347  real(_Tp __x)
348  { return __x; }
349 
350 #endif
351 
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)
355  {
356  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
357  return std::pow(std::complex<__type>(__x), __type(__y));
358  }
359 
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)
363  {
364  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
365  return std::pow(__type(__x), std::complex<__type>(__y));
366  }
367 
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)
371  {
372  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
373  return std::pow(std::complex<__type>(__x),
374  std::complex<__type>(__y));
375  }
376 
377  using std::arg;
378 
379  template<typename _Tp>
380  inline std::complex<_Tp>
381  conj(const std::complex<_Tp>& __z)
382  { return std::conj(__z); }
383 
384  template<typename _Tp>
385  inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
386  conj(_Tp __x)
387  { return __x; }
388 
389  using std::imag;
390  using std::norm;
391  using std::polar;
392 
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)
396  {
397  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
398  return std::polar(__type(__rho), __type(__theta));
399  }
400 
401  using std::real;
402 
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); }
407 
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); }
412 
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); }
417 
418 /// @} group complex_numbers
419 }
420 
421 _GLIBCXX_END_NAMESPACE_VERSION
422 }
423 
424 #endif // _GLIBCXX_TR1_COMPLEX