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