Debian Codegraph @

melder/complex.cpp

raw
1 /* complex.cpp
2 *
3 * Copyright (C) 1992-2005,2011,2016-2018,2020 Paul Boersma
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "melder.h"
20
21 dcomplex dcomplex_sqrt (dcomplex z) {
22 dcomplex result;
23 double x, y, w, r;
24 if (z.real() == 0 && z.imag() == 0) {
25 result. real (0.0);
26 result. imag (0.0);
27 return result;
28 }
29 x = fabs (z.real());
30 y = fabs (z.imag());
31 if (x >= y) {
32 r = y / x;
33 w = sqrt (x) * sqrt (0.5 * (1.0 + sqrt (1.0 + r * r)));
34 } else {
35 r = x / y;
36 w = sqrt (y) * sqrt (0.5 * (r + sqrt (1.0 + r * r)));
37 }
38 if (z.real() >= 0.0) {
39 result. real (w);
40 result. imag (z.imag() / (2.0 * w));
41 } else {
42 result. imag ((z.imag() >= 0) ? w : -w);
43 result. real (z.imag() / (2.0 * result.imag()));
44 }
45 return result;
46 }
47
48 /* End of file complex.cpp */