| 1 |
#ifndef _complex_h_ |
| 2 |
#define _complex_h_ |
| 3 |
/* complex.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2005,2011,2016-2018,2020,2021 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
//struct dcomplex { double re, im; }; |
| 22 |
#include <complex> |
| 23 |
using dcomplex = std::complex <double>; |
| 24 |
|
| 25 |
inline dcomplex dcomplex_add (dcomplex a, dcomplex b) { |
| 26 |
dcomplex result; |
| 27 |
result. real (a.real() + b.real()); |
| 28 |
result. imag (a.imag() + b.imag()); |
| 29 |
return result; |
| 30 |
} |
| 31 |
|
| 32 |
inline dcomplex dcomplex_sub (dcomplex a, dcomplex b) { |
| 33 |
dcomplex result; |
| 34 |
result. real (a.real() - b.real()); |
| 35 |
result. imag (a.imag() - b.imag()); |
| 36 |
return result; |
| 37 |
} |
| 38 |
|
| 39 |
inline dcomplex dcomplex_mul (dcomplex a, dcomplex b) { |
| 40 |
dcomplex result; |
| 41 |
result. real (a.real() * b.real() - a.imag() * b.imag()); |
| 42 |
result. imag (a.imag() * b.real() + a.real() * b.imag()); |
| 43 |
return result; |
| 44 |
} |
| 45 |
|
| 46 |
inline dcomplex dcomplex_conjugate (dcomplex z) { |
| 47 |
dcomplex result; |
| 48 |
result. real (z.real()); |
| 49 |
result. imag (- z.imag()); |
| 50 |
return result; |
| 51 |
} |
| 52 |
|
| 53 |
inline dcomplex dcomplex_div (dcomplex a, dcomplex b) { |
| 54 |
dcomplex result; |
| 55 |
if (fabs (b.real()) >= fabs (b.imag())) { |
| 56 |
const double r = b.imag() / b.real(); |
| 57 |
const double den = b.real() + r * b.imag(); |
| 58 |
result. real ((a.real() + r * a.imag()) / den); |
| 59 |
result. imag ((a.imag() - r * a.real()) / den); |
| 60 |
} else { |
| 61 |
const double r = b.real() / b.imag(); |
| 62 |
const double den = b.imag() + r * b.real(); |
| 63 |
result. real ((a.real() * r + a.imag()) / den); |
| 64 |
result. imag ((a.imag() * r - a.real()) / den); |
| 65 |
} |
| 66 |
return result; |
| 67 |
} |
| 68 |
|
| 69 |
inline double dcomplex_abs (dcomplex z) { |
| 70 |
return hypot (z.real(), z.imag()); |
| 71 |
} |
| 72 |
|
| 73 |
inline dcomplex dcomplex_rmul (double x, dcomplex a) { |
| 74 |
dcomplex result; |
| 75 |
result. real (x * a.real()); |
| 76 |
result. imag (x * a.imag()); |
| 77 |
return result; |
| 78 |
} |
| 79 |
|
| 80 |
inline dcomplex dcomplex_exp (dcomplex z) { |
| 81 |
dcomplex result; |
| 82 |
const double size = exp (z.real()); |
| 83 |
result. real (size * cos (z.imag())); |
| 84 |
result. imag (size * sin (z.imag())); |
| 85 |
return result; |
| 86 |
} |
| 87 |
|
| 88 |
dcomplex dcomplex_sqrt (dcomplex z); |
| 89 |
|
| 90 |
/* End of file complex.h */ |
| 91 |
#endif |