Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af5ff55

Browse files
committed
ENH: npymath: Use a better complex division algorithm.
Using the same technique used for divide complex arrays, copied from umath/loops.c.src.
1 parent d62c590 commit af5ff55

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

numpy/core/src/npymath/npy_math_complex.c.src

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,30 @@ static NPY_INLINE @ctype@ cmul@c@(@ctype@ a, @ctype@ b)
7878

7979
static NPY_INLINE @ctype@ cdiv@c@(@ctype@ a, @ctype@ b)
8080
{
81-
@type@ ar, ai, br, bi, d;
81+
@type@ ar, ai, br, bi, abs_br, abs_bi;
8282
ar = npy_creal@c@(a);
8383
ai = npy_cimag@c@(a);
8484
br = npy_creal@c@(b);
8585
bi = npy_cimag@c@(b);
86-
d = br*br + bi*bi;
87-
return npy_cpack@c@((ar*br +ai*bi)/d, (ai*br-ar*bi)/d);
86+
abs_br = npy_fabs@c@(br);
87+
abs_bi = npy_fabs@c@(bi);
88+
89+
if (abs_br >= abs_bi) {
90+
if (abs_br == 0 && abs_bi == 0) {
91+
/* divide by zeros should yield a complex inf or nan */
92+
return npy_cpack@c@(ar/abs_br, ai/abs_bi);
93+
}
94+
else {
95+
@type@ rat = bi/br;
96+
@type@ scl = 1.0@C@/(br+bi*rat);
97+
return npy_cpack@c@((ar + ai*rat)*scl, (ai - ar*rat)*scl);
98+
}
99+
}
100+
else {
101+
@type@ rat = br/bi;
102+
@type@ scl = 1.0@C@/(bi + br*rat);
103+
return npy_cpack@c@((ar*rat + ai)*scl, (ai*rat - ar)*scl);
104+
}
88105
}
89106

90107
static NPY_INLINE @ctype@ cneg@c@(@ctype@ a)

0 commit comments

Comments
 (0)