| 265 | const d = z.re * z.re + z.im * z.im; |
| 266 | const nr = a.re * z.re + a.im * z.im; |
| 267 | const ni = a.im * z.re - a.re * z.im; |
| 268 | if (textbookQuotientIsAccurate(a.re, a.im, z.re, z.im, d, nr, ni)) |
| 269 | return a.div(z); |
| 270 | const q = scaledComplexDivide(a.re, a.im, z.re, z.im); |
| 271 | return new Complex(q.re, q.im); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * `1 / z` for a `complex.js` value, a drop-in replacement for |
| 276 | * `z.inverse()`, computed with `complexQuotient()`. It does not overflow or |
| 277 | * underflow when the result is in the range of a double. (The unpatched |
| 278 | * `z.inverse()` of `complex.js` computes `conj(z) / |z|²`, whose `|z|²` |
| 279 | * overflows for `z = 1e308 + 1e308·i` and underflows for |
| 280 | * `z = 1e-200 + 1e-200·i`. `installComplexScaling()` in |
| 281 | * `numerics/complex-scaling.ts` corrects `z.inverse()` for these values |
| 282 | * too.) A zero, infinite or NaN value is left to `z.inverse()`, so its |
| 283 | * result does not change. |
| 284 | */ |
| 285 | export function complexInverse(z: Complex): Complex { |
| 286 | if (z.isZero() || !Number.isFinite(z.re) || !Number.isFinite(z.im)) |
| 287 | return z.inverse(); |
| 288 | const q = complexQuotient(1, 0, z.re, z.im); |
| 289 | return new Complex(q.re, q.im); |
| 290 | } |