@ac-kit/math-complex 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -0
- package/README.md +78 -20
- package/dist/complex/complex-field.d.ts +28 -0
- package/dist/complex/complex-field.d.ts.map +1 -0
- package/dist/complex/complex-field.js +41 -0
- package/dist/complex/complex-field.js.map +1 -0
- package/dist/complex/complex-num.d.ts +29 -11
- package/dist/complex/complex-num.d.ts.map +1 -1
- package/dist/complex/complex-num.js +65 -28
- package/dist/complex/complex-num.js.map +1 -1
- package/dist/complex/complex.d.ts +227 -42
- package/dist/complex/complex.d.ts.map +1 -1
- package/dist/complex/complex.js +548 -74
- package/dist/complex/complex.js.map +1 -1
- package/dist/dual/dual.d.ts +117 -0
- package/dist/dual/dual.d.ts.map +1 -0
- package/dist/dual/dual.js +250 -0
- package/dist/dual/dual.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/quaternion/quaternion-num.d.ts +1 -1
- package/dist/quaternion/quaternion-num.d.ts.map +1 -1
- package/dist/quaternion/quaternion-num.js +13 -13
- package/dist/quaternion/quaternion-num.js.map +1 -1
- package/package.json +8 -7
package/dist/complex/complex.js
CHANGED
|
@@ -1,88 +1,562 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
];
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
1
|
+
import { numberOrNaN } from "@ac-kit/core";
|
|
2
|
+
import { diffOfProductsInPlace, hypot2, hypot2InPlace, } from "@ac-kit/math-scalar";
|
|
3
|
+
/**
|
|
4
|
+
* Every creator copies this array, which holds non-integer doubles and is never
|
|
5
|
+
* exposed: nothing a caller does can change the element kind it copies.
|
|
6
|
+
*/
|
|
7
|
+
const TEMPLATE_ = [0.5, 0.5];
|
|
8
|
+
/**
|
|
9
|
+
* The four factors handed to `diffOfProductsInPlace`, so no double crosses a
|
|
10
|
+
* call that V8 might not inline.
|
|
11
|
+
*/
|
|
12
|
+
const PRODUCT_TERMS_ = new Float64Array(4);
|
|
13
|
+
/** The two parts handed to `hypot2InPlace`, for the same reason. */
|
|
14
|
+
const MODULUS_TERMS_ = new Float64Array(2);
|
|
15
|
+
/** `1 + 0i`, the dividend of an inverse. Never exposed, so never written. */
|
|
16
|
+
const UNIT_ = complexOf(1, 0);
|
|
17
|
+
/** The running square and the running result of {@link complexPowInt}. */
|
|
18
|
+
const POWER_SQUARE_ = complexCreate();
|
|
19
|
+
const POWER_RESULT_ = complexCreate();
|
|
20
|
+
/**
|
|
21
|
+
* `source[index]` if the index is inside `source` and holds a number; else
|
|
22
|
+
* `NaN`.
|
|
23
|
+
*/
|
|
24
|
+
function numberAt_(source, index) {
|
|
25
|
+
return index >= 0 && index < source.length
|
|
26
|
+
? numberOrNaN(source[index])
|
|
27
|
+
: Number.NaN;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Writes `a · b` into `out`, each part within 1.5 units in the last place of
|
|
31
|
+
* the exact value (Kahan's compensated `a·b − c·d`). Both inputs are read
|
|
32
|
+
* before the first write, and no double is passed or returned, so it allocates
|
|
33
|
+
* nothing whether the engine inlines it or not.
|
|
34
|
+
*/
|
|
35
|
+
function multiplyPrecise_(out, a, b) {
|
|
36
|
+
const ar = a[0];
|
|
37
|
+
const ai = a[1];
|
|
38
|
+
const br = b[0];
|
|
39
|
+
const bi = b[1];
|
|
40
|
+
PRODUCT_TERMS_[0] = ar;
|
|
41
|
+
PRODUCT_TERMS_[1] = br;
|
|
42
|
+
PRODUCT_TERMS_[2] = ai;
|
|
43
|
+
PRODUCT_TERMS_[3] = bi;
|
|
44
|
+
diffOfProductsInPlace(PRODUCT_TERMS_);
|
|
45
|
+
const re = PRODUCT_TERMS_[0];
|
|
46
|
+
PRODUCT_TERMS_[0] = ar;
|
|
47
|
+
PRODUCT_TERMS_[1] = bi;
|
|
48
|
+
PRODUCT_TERMS_[2] = -ai;
|
|
49
|
+
PRODUCT_TERMS_[3] = br;
|
|
50
|
+
diffOfProductsInPlace(PRODUCT_TERMS_);
|
|
51
|
+
out[0] = re;
|
|
52
|
+
out[1] = PRODUCT_TERMS_[0];
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* `±1` for an infinite `value`, else a zero of its sign: Annex G's `copysign(
|
|
57
|
+
* isinf(value) ? 1 : 0, value)`, which keeps only where an operand is
|
|
58
|
+
* infinite.
|
|
59
|
+
*/
|
|
60
|
+
function infinityBox_(value) {
|
|
61
|
+
if (value === Infinity) {
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
if (value === -Infinity) {
|
|
65
|
+
return -1;
|
|
66
|
+
}
|
|
67
|
+
return value < 0 || 1 / value < 0 ? -0 : 0;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Annex G's `0 · value`: a zero of `value`'s sign, `NaN` for an infinite
|
|
71
|
+
* `value` (a sum of finite terms that overflowed) as for a `NaN` one.
|
|
72
|
+
*/
|
|
73
|
+
function zeroOfSign_(value) {
|
|
74
|
+
if (value - value !== 0) {
|
|
75
|
+
return Number.NaN;
|
|
76
|
+
}
|
|
77
|
+
return value < 0 || 1 / value < 0 ? -0 : 0;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Writes `a / b` into `out` by Smith's algorithm, with C99 Annex G's handling
|
|
81
|
+
* of infinities (`_Cdivd`); alias-safe, as above. An infinite divisor is taken
|
|
82
|
+
* first, before Smith's ratios turn into `∞/∞`: a finite dividend gives a zero
|
|
83
|
+
* of the signs Annex G gives (and its `NaN` where Annex G's `a·c′ + b·d′`
|
|
84
|
+
* overflows to `0 · ∞`), anything else `NaN`. An infinite dividend over a
|
|
85
|
+
* finite nonzero divisor that Smith's arithmetic leaves `NaN` in both parts is
|
|
86
|
+
* recovered as an infinity. A zero divisor gives `NaN` parts.
|
|
50
87
|
*/
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
88
|
+
function divide_(out, a, b) {
|
|
89
|
+
const ar = a[0];
|
|
90
|
+
const ai = a[1];
|
|
91
|
+
const br = b[0];
|
|
92
|
+
const bi = b[1];
|
|
93
|
+
if (br === Infinity ||
|
|
94
|
+
br === -Infinity ||
|
|
95
|
+
bi === Infinity ||
|
|
96
|
+
bi === -Infinity) {
|
|
97
|
+
if (ar - ar === 0 && ai - ai === 0) {
|
|
98
|
+
const cr = infinityBox_(br);
|
|
99
|
+
const ci = infinityBox_(bi);
|
|
100
|
+
out[0] = zeroOfSign_(ar * cr + ai * ci);
|
|
101
|
+
out[1] = zeroOfSign_(ai * cr - ar * ci);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
out[0] = Number.NaN;
|
|
105
|
+
out[1] = Number.NaN;
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
55
108
|
}
|
|
56
|
-
|
|
57
|
-
|
|
109
|
+
let re;
|
|
110
|
+
let im;
|
|
111
|
+
if (Math.abs(br) >= Math.abs(bi)) {
|
|
112
|
+
const ratio = bi / br;
|
|
113
|
+
const denominator = br + bi * ratio;
|
|
114
|
+
re = (ar + ai * ratio) / denominator;
|
|
115
|
+
im = (ai - ar * ratio) / denominator;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const ratio = br / bi;
|
|
119
|
+
const denominator = br * ratio + bi;
|
|
120
|
+
re = (ar * ratio + ai) / denominator;
|
|
121
|
+
im = (ai * ratio - ar) / denominator;
|
|
122
|
+
}
|
|
123
|
+
if (re !== re &&
|
|
124
|
+
im !== im &&
|
|
125
|
+
(ar === Infinity ||
|
|
126
|
+
ar === -Infinity ||
|
|
127
|
+
ai === Infinity ||
|
|
128
|
+
ai === -Infinity) &&
|
|
129
|
+
br - br === 0 &&
|
|
130
|
+
bi - bi === 0 &&
|
|
131
|
+
(br !== 0 || bi !== 0)) {
|
|
132
|
+
const xr = infinityBox_(ar);
|
|
133
|
+
const xi = infinityBox_(ai);
|
|
134
|
+
re = Infinity * (xr * br + xi * bi);
|
|
135
|
+
im = Infinity * (xi * br - xr * bi);
|
|
136
|
+
}
|
|
137
|
+
out[0] = re;
|
|
138
|
+
out[1] = im;
|
|
139
|
+
return out;
|
|
58
140
|
}
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Creates the complex number `0 + 0i`.
|
|
143
|
+
*
|
|
144
|
+
* Allocates; keep it out of hot loops.
|
|
145
|
+
*/
|
|
146
|
+
export function complexCreate() {
|
|
147
|
+
const out = TEMPLATE_.slice();
|
|
148
|
+
out[0] = 0;
|
|
149
|
+
out[1] = 0;
|
|
150
|
+
return out;
|
|
62
151
|
}
|
|
63
|
-
/**
|
|
64
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Creates the complex number `re + im·i`. A real number is `complexOf(x, 0)`,
|
|
154
|
+
* the imaginary unit `complexOf(0, 1)`.
|
|
155
|
+
*
|
|
156
|
+
* Allocates; keep it out of hot loops.
|
|
157
|
+
*/
|
|
158
|
+
export function complexOf(re, im) {
|
|
159
|
+
const out = TEMPLATE_.slice();
|
|
160
|
+
out[0] = re;
|
|
161
|
+
out[1] = im;
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Creates a copy of `a`. Like every creator it starts from a clean array, so a
|
|
166
|
+
* clone of an array of small integers is an array of doubles.
|
|
167
|
+
*
|
|
168
|
+
* Allocates; keep it out of hot loops.
|
|
169
|
+
*/
|
|
170
|
+
export function complexClone(a) {
|
|
171
|
+
return complexOf(a[0], a[1]);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The checked way in from untyped data (JSON, messages, arrays of unknown
|
|
175
|
+
* origin, interleaved `re, im` buffers): `source[offset … offset + 2)` copied
|
|
176
|
+
* into `out`, each element that is not a number written as `NaN`, an index past
|
|
177
|
+
* the end of `source` too, so `out` never holds a non-number. Test the result
|
|
178
|
+
* with {@link complexIsFinite} when `NaN` must not get through. O(1) time, O(1)
|
|
179
|
+
* memory.
|
|
180
|
+
*
|
|
181
|
+
* `source` may be `out` itself: both elements are read before the first write.
|
|
182
|
+
*
|
|
183
|
+
* @param out The complex number to fill.
|
|
184
|
+
* @param source The array to read, of any length and any content.
|
|
185
|
+
* @param offset The index of the real part in `source`.
|
|
186
|
+
* @returns `out`.
|
|
187
|
+
*/
|
|
188
|
+
export function complexFromArray(out, source, offset = 0) {
|
|
189
|
+
const re = numberAt_(source, offset);
|
|
190
|
+
const im = numberAt_(source, offset + 1);
|
|
191
|
+
out[0] = re;
|
|
192
|
+
out[1] = im;
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
/** Writes `re + im·i` into `out`. */
|
|
196
|
+
export function complexSet(out, re, im) {
|
|
197
|
+
out[0] = re;
|
|
198
|
+
out[1] = im;
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
/** Writes a copy of `a` into `out`. */
|
|
202
|
+
export function complexCopy(out, a) {
|
|
203
|
+
const re = a[0];
|
|
204
|
+
const im = a[1];
|
|
205
|
+
out[0] = re;
|
|
206
|
+
out[1] = im;
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
/** Writes the polar form `modulus · e^(i·argument)` into `out`. */
|
|
210
|
+
export function complexFromPolar(out, modulus, argument) {
|
|
211
|
+
out[0] = modulus * Math.cos(argument);
|
|
212
|
+
out[1] = modulus * Math.sin(argument);
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Writes Euler's `cis θ = cos θ + i·sin θ`, the unit vector at angle `θ`, into
|
|
217
|
+
* `out`.
|
|
218
|
+
*/
|
|
219
|
+
export function complexCis(out, argument) {
|
|
220
|
+
out[0] = Math.cos(argument);
|
|
221
|
+
out[1] = Math.sin(argument);
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
/** Writes `a + b` into `out`. `out` may be `a` or `b`. */
|
|
225
|
+
export function complexAdd(out, a, b) {
|
|
226
|
+
out[0] = a[0] + b[0];
|
|
227
|
+
out[1] = a[1] + b[1];
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
/** Writes `a − b` into `out`. `out` may be `a` or `b`. */
|
|
231
|
+
export function complexSubtract(out, a, b) {
|
|
232
|
+
out[0] = a[0] - b[0];
|
|
233
|
+
out[1] = a[1] - b[1];
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
/** Writes `−a` into `out`. `out` may be `a`. */
|
|
237
|
+
export function complexNegate(out, a) {
|
|
238
|
+
out[0] = -a[0];
|
|
239
|
+
out[1] = -a[1];
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
/** Writes `a · scalar` into `out`, a real factor. `out` may be `a`. */
|
|
243
|
+
export function complexMultiplyScalar(out, a, scalar) {
|
|
244
|
+
out[0] = a[0] * scalar;
|
|
245
|
+
out[1] = a[1] * scalar;
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Writes `a · b = (ac − bd) + (ad + bc)i` into `out`. `out` may be `a` or `b`.
|
|
250
|
+
*
|
|
251
|
+
* Plain arithmetic, as numpy and C99 without Annex G round it: the result is
|
|
252
|
+
* within a few roundings of the exact product relative to `|a|·|b|`, so a part
|
|
253
|
+
* that is small next to the modulus can lose its digits where `ac` and `bd` (or
|
|
254
|
+
* `ad` and `bc`) cancel. {@link complexMultiplyPrecise} keeps them.
|
|
255
|
+
*/
|
|
256
|
+
export function complexMultiply(out, a, b) {
|
|
257
|
+
const ar = a[0];
|
|
258
|
+
const ai = a[1];
|
|
259
|
+
const br = b[0];
|
|
260
|
+
const bi = b[1];
|
|
261
|
+
out[0] = ar * br - ai * bi;
|
|
262
|
+
out[1] = ar * bi + ai * br;
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Writes `a · b` into `out` with each part within 1.5 units in the last place
|
|
267
|
+
* of its exact value, however badly its two products cancel: Kahan's
|
|
268
|
+
* compensated `ac − bd` and `ad + bc` (`@ac-kit/math-scalar`'s
|
|
269
|
+
* `diffOfProductsInPlace`). `[1 + 2⁻²⁷, 1]·[1 − 2⁻²⁷, 1]` has the real part
|
|
270
|
+
* `−2⁻⁵⁴`, where {@link complexMultiply} gives `0`. `out` may be `a` or `b`.
|
|
271
|
+
*
|
|
272
|
+
* About five times the cost of the plain product. Allocates nothing while every
|
|
273
|
+
* product is from `2⁻⁹⁶⁹` to `2¹⁰²²` in magnitude, or zero; beyond, the
|
|
274
|
+
* compensation is settled by the boxed `diffOfProducts`.
|
|
275
|
+
*/
|
|
276
|
+
export function complexMultiplyPrecise(out, a, b) {
|
|
277
|
+
return multiplyPrecise_(out, a, b);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Writes `a / b` into `out` by Smith's algorithm, which divides by the larger
|
|
281
|
+
* part of `b` first, so no intermediate overflows or underflows where the
|
|
282
|
+
* textbook `(ac + bd) / (c² + d²)` squares `b`'s parts and fails for `|b|`
|
|
283
|
+
* beyond about `1e±154` while the quotient is representable. Infinities follow
|
|
284
|
+
* C99 Annex G: a finite number over an infinite one is a signed zero (`NaN` in
|
|
285
|
+
* a part whose Annex G sum `a·c′ ± b·d′` overflows, as `_Cdivd` gives), an
|
|
286
|
+
* infinite one over a finite nonzero one infinite, `∞/∞` is `NaN`. Dividing by
|
|
287
|
+
* zero writes `NaN` parts, where Annex G gives an infinity: no throw, and no
|
|
288
|
+
* direction to pick. `out` may be `a` or `b`.
|
|
289
|
+
*/
|
|
290
|
+
export function complexDivide(out, a, b) {
|
|
291
|
+
return divide_(out, a, b);
|
|
292
|
+
}
|
|
293
|
+
/** Writes the conjugate `re − im·i` into `out`. `out` may be `a`. */
|
|
294
|
+
export function complexConjugate(out, a) {
|
|
295
|
+
out[0] = a[0];
|
|
296
|
+
out[1] = -a[1];
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Writes the inverse `1 / a` into `out`, by Smith's algorithm as
|
|
301
|
+
* {@link complexDivide}, so it holds at any magnitude whose inverse is
|
|
302
|
+
* representable; the inverse of an infinite number is a signed zero (Annex G).
|
|
303
|
+
* Zero has no inverse: the parts written are `NaN`. `out` may be `a`.
|
|
304
|
+
*/
|
|
305
|
+
export function complexInvert(out, a) {
|
|
306
|
+
return divide_(out, UNIT_, a);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The modulus `|z| = √(re² + im²)`, the length of `z` in the plane, by
|
|
310
|
+
* `@ac-kit/math-scalar`'s `hypot2`: right at every magnitude, no overflow or
|
|
311
|
+
* underflow on the way, `+∞` when a part is infinite even beside `NaN` (C99
|
|
312
|
+
* `cabs`).
|
|
313
|
+
*/
|
|
314
|
+
export function complexLength(a) {
|
|
315
|
+
return hypot2(a[0], a[1]);
|
|
316
|
+
}
|
|
317
|
+
/** The squared modulus `re² + im²`, without the square root. */
|
|
318
|
+
export function complexLengthSquared(a) {
|
|
65
319
|
return a[0] * a[0] + a[1] * a[1];
|
|
66
320
|
}
|
|
67
|
-
/** The principal argument `arg(z) ∈
|
|
321
|
+
/** The principal argument `arg(z) = atan2(im, re) ∈ [−π, π]`. */
|
|
68
322
|
export function complexArgument(a) {
|
|
69
323
|
return Math.atan2(a[1], a[0]);
|
|
70
324
|
}
|
|
71
|
-
/**
|
|
72
|
-
export function
|
|
73
|
-
|
|
325
|
+
/** Writes `e^a = e^re · (cos im + i·sin im)` into `out`. `out` may be `a`. */
|
|
326
|
+
export function complexExp(out, a) {
|
|
327
|
+
const magnitude = Math.exp(a[0]);
|
|
328
|
+
const im = a[1];
|
|
329
|
+
out[0] = magnitude * Math.cos(im);
|
|
330
|
+
out[1] = magnitude * Math.sin(im);
|
|
331
|
+
return out;
|
|
74
332
|
}
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
|
|
333
|
+
/**
|
|
334
|
+
* Writes the principal logarithm `ln|a| + i·arg(a)` into `out`: the imaginary
|
|
335
|
+
* part in `[−π, π]`, the branch cut along the negative real axis, its side
|
|
336
|
+
* taken from the sign of a zero imaginary part. `ln 0` is `−∞ + 0i`. `out` may
|
|
337
|
+
* be `a`.
|
|
338
|
+
*/
|
|
339
|
+
export function complexLog(out, a) {
|
|
340
|
+
const re = a[0];
|
|
341
|
+
const im = a[1];
|
|
342
|
+
MODULUS_TERMS_[0] = re;
|
|
343
|
+
MODULUS_TERMS_[1] = im;
|
|
344
|
+
hypot2InPlace(MODULUS_TERMS_);
|
|
345
|
+
out[0] = Math.log(MODULUS_TERMS_[0]);
|
|
346
|
+
out[1] = Math.atan2(im, re);
|
|
347
|
+
return out;
|
|
78
348
|
}
|
|
79
|
-
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
349
|
+
/** Components at or above this are scaled by `1/4` before the square root. */
|
|
350
|
+
const SQRT_LARGE_ = Number.MAX_VALUE / 4;
|
|
351
|
+
/** Components below this (both) are scaled up by `2¹⁰⁶` before it. */
|
|
352
|
+
const SQRT_SMALL_ = 2 ** -1020;
|
|
353
|
+
const SQRT_UP_ = 2 ** 106;
|
|
354
|
+
const SQRT_UP_ROOT_INVERSE_ = 2 ** -53;
|
|
355
|
+
/**
|
|
356
|
+
* Writes the principal square root `√a` into `out`: the root with a
|
|
357
|
+
* non-negative real part, and on the negative real axis the one whose imaginary
|
|
358
|
+
* part has the sign of `a`'s (so `√(−4 + 0i) = 2i` and `√(−4 − 0i) = −2i`, the
|
|
359
|
+
* branch cut taken from the sign of zero as C99 Annex G's `csqrt` does). `out`
|
|
360
|
+
* may be `a`.
|
|
361
|
+
*
|
|
362
|
+
* Kahan's formulation: `t = √((|x| + |z|) / 2)` is computed from two
|
|
363
|
+
* non-negative terms, so it never cancels, and the other part comes from `y /
|
|
364
|
+
* 2t`. The naive `√((|z| + x) / 2)`, `√((|z| − x) / 2)` pair loses every digit
|
|
365
|
+
* of the smaller part when `|y| ≪ |x|`. Components near the top of the range
|
|
366
|
+
* are scaled down by 4 and tiny ones up by `2¹⁰⁶` (exact powers of two), so
|
|
367
|
+
* nothing overflows or flushes to zero on the way. Special values follow Annex
|
|
368
|
+
* G: an infinite imaginary part gives `+∞ ± ∞i` even with a `NaN` real part,
|
|
369
|
+
* `−∞ + yi` gives `0 ± ∞i`, `+∞ + yi` gives `+∞ ± 0i`, and `NaN` propagates.
|
|
370
|
+
* O(1) time, no allocation.
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* ```ts
|
|
374
|
+
* const root = complexCreate();
|
|
375
|
+
* complexSqrt(root, complexOf(-4, 0)); // [0, 2]
|
|
376
|
+
* complexSqrt(root, complexOf(3, 4)); // [2, 1]
|
|
377
|
+
* complexSqrt(root, complexOf(1, 1e-20)); // [1, 5e-21] — the small part is kept
|
|
378
|
+
* ```;
|
|
379
|
+
*/
|
|
380
|
+
export function complexSqrt(out, a) {
|
|
381
|
+
let x = a[0];
|
|
382
|
+
let y = a[1];
|
|
383
|
+
// Not finite (`x − x` is `NaN` for ±∞ and `NaN`), or zero.
|
|
384
|
+
if (x - x !== 0 || y - y !== 0 || (x === 0 && y === 0)) {
|
|
385
|
+
return sqrtSpecial_(out, a);
|
|
386
|
+
}
|
|
387
|
+
let rescale = 1;
|
|
388
|
+
const largest = Math.max(Math.abs(x), Math.abs(y));
|
|
389
|
+
if (largest >= SQRT_LARGE_) {
|
|
390
|
+
x /= 4;
|
|
391
|
+
y /= 4;
|
|
392
|
+
rescale = 2;
|
|
393
|
+
}
|
|
394
|
+
else if (largest < SQRT_SMALL_) {
|
|
395
|
+
x *= SQRT_UP_;
|
|
396
|
+
y *= SQRT_UP_;
|
|
397
|
+
rescale = SQRT_UP_ROOT_INVERSE_;
|
|
398
|
+
}
|
|
399
|
+
MODULUS_TERMS_[0] = x;
|
|
400
|
+
MODULUS_TERMS_[1] = y;
|
|
401
|
+
hypot2InPlace(MODULUS_TERMS_);
|
|
402
|
+
const t = Math.sqrt((Math.abs(x) + MODULUS_TERMS_[0]) / 2);
|
|
403
|
+
if (x >= 0) {
|
|
404
|
+
out[0] = t * rescale;
|
|
405
|
+
out[1] = (y / (2 * t)) * rescale;
|
|
406
|
+
return out;
|
|
407
|
+
}
|
|
408
|
+
// `y < 0 || 1 / y < 0` is the sign bit of `y`, `−0` included, with no call.
|
|
409
|
+
const imaginary = y < 0 || 1 / y < 0 ? -t : t;
|
|
410
|
+
out[0] = (Math.abs(y) / (2 * t)) * rescale;
|
|
411
|
+
out[1] = imaginary * rescale;
|
|
412
|
+
return out;
|
|
413
|
+
}
|
|
414
|
+
/** {@link complexSqrt} of a zero, an infinite or a `NaN` argument. */
|
|
415
|
+
function sqrtSpecial_(out, a) {
|
|
416
|
+
const x = a[0];
|
|
417
|
+
const y = a[1];
|
|
418
|
+
const negativeY = y < 0 || 1 / y < 0;
|
|
419
|
+
if (y === Infinity || y === -Infinity) {
|
|
420
|
+
out[0] = Infinity;
|
|
421
|
+
out[1] = y;
|
|
422
|
+
}
|
|
423
|
+
else if (x !== x) {
|
|
424
|
+
out[0] = Number.NaN;
|
|
425
|
+
out[1] = Number.NaN;
|
|
426
|
+
}
|
|
427
|
+
else if (x === -Infinity) {
|
|
428
|
+
out[0] = y === y ? 0 : Number.NaN;
|
|
429
|
+
out[1] = negativeY ? -Infinity : Infinity;
|
|
430
|
+
}
|
|
431
|
+
else if (x === Infinity) {
|
|
432
|
+
out[0] = Infinity;
|
|
433
|
+
out[1] = y === y ? (negativeY ? -0 : 0) : Number.NaN;
|
|
434
|
+
}
|
|
435
|
+
else if (y !== y) {
|
|
436
|
+
out[0] = Number.NaN;
|
|
437
|
+
out[1] = Number.NaN;
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
out[0] = 0;
|
|
441
|
+
out[1] = y;
|
|
442
|
+
}
|
|
443
|
+
return out;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Writes `aⁿ` for an integer exponent into `out`, by binary exponentiation:
|
|
447
|
+
* about `2·log₂|n|` multiplications, each the compensated
|
|
448
|
+
* {@link complexMultiplyPrecise}, instead of the `exp(n · log a)` detour whose
|
|
449
|
+
* `log` and `cis` round `arg a` before it is multiplied by `n`. Exact whenever
|
|
450
|
+
* every intermediate power is representable — Gaussian integers stay integers:
|
|
451
|
+
* `[1, 1]⁸` is `[16, 0]`, not `[16, −3.9e-15]`. `out` may be `a`.
|
|
452
|
+
*
|
|
453
|
+
* `a⁰` is `1` for every `a`, zero and `NaN` included, as `Math.pow` has it. A
|
|
454
|
+
* negative exponent is the reciprocal of the positive power, by Smith's
|
|
455
|
+
* division as {@link complexDivide}, as CPython's complex power does; so `0⁻ⁿ`
|
|
456
|
+
* gives `NaN` parts. An exponent that is not a safe integer gives `NaN` parts
|
|
457
|
+
* too (use `exp(w · log a)` for a real or complex power). O(log |n|) time, no
|
|
458
|
+
* allocation for coordinate-sized powers.
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```ts
|
|
462
|
+
* const power = complexCreate();
|
|
463
|
+
* complexPowInt(power, complexOf(0, 1), 2); // [-1, 0]
|
|
464
|
+
* complexPowInt(power, complexOf(1, 1), 8); // [16, 0]
|
|
465
|
+
* complexPowInt(power, complexOf(0, 2), -1); // [0, -0.5]
|
|
466
|
+
* ```;
|
|
467
|
+
*/
|
|
468
|
+
export function complexPowInt(out, a, n) {
|
|
469
|
+
if (!Number.isSafeInteger(n)) {
|
|
470
|
+
out[0] = Number.NaN;
|
|
471
|
+
out[1] = Number.NaN;
|
|
472
|
+
return out;
|
|
473
|
+
}
|
|
474
|
+
// The running square and the result live in private scratch numbers, and
|
|
475
|
+
// `a` is read once before `out` is written, so `out` may be `a`.
|
|
476
|
+
const square = POWER_SQUARE_;
|
|
477
|
+
const result = POWER_RESULT_;
|
|
478
|
+
square[0] = a[0];
|
|
479
|
+
square[1] = a[1];
|
|
480
|
+
result[0] = 1;
|
|
481
|
+
result[1] = 0;
|
|
482
|
+
let remaining = Math.abs(n);
|
|
483
|
+
while (remaining > 0) {
|
|
484
|
+
if (remaining % 2 === 1) {
|
|
485
|
+
multiplyPrecise_(result, result, square);
|
|
486
|
+
}
|
|
487
|
+
remaining = Math.floor(remaining / 2);
|
|
488
|
+
if (remaining > 0) {
|
|
489
|
+
multiplyPrecise_(square, square, square);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (n < 0) {
|
|
493
|
+
return divide_(out, UNIT_, result);
|
|
494
|
+
}
|
|
495
|
+
out[0] = result[0];
|
|
496
|
+
out[1] = result[1];
|
|
497
|
+
return out;
|
|
83
498
|
}
|
|
84
|
-
/**
|
|
85
|
-
|
|
86
|
-
|
|
499
|
+
/**
|
|
500
|
+
* Exact equality of both parts. `0` and `−0` are equal, `NaN` is equal to
|
|
501
|
+
* nothing. See {@link complexIsClose} for a tolerant test.
|
|
502
|
+
*/
|
|
503
|
+
export function complexEquals(a, b) {
|
|
504
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Whether two complex numbers are close in the complex plane: `|a − b| ≤
|
|
508
|
+
* max(relTol · max(|a|, |b|), absTol)`, Python's `cmath.isclose`.
|
|
509
|
+
*
|
|
510
|
+
* The distance is the modulus of the difference, not a test per component, so
|
|
511
|
+
* the answer does not change when both numbers are rotated together, and a
|
|
512
|
+
* component that is tiny next to the other cannot fail on its own: `[1, 1e-17]`
|
|
513
|
+
* is close to `[1, 0]` at `relTol: 1e-15`, where a component-wise relative test
|
|
514
|
+
* refuses it. Identical values, infinite ones included, are close; otherwise an
|
|
515
|
+
* infinite or `NaN` component makes them not close. Symmetric. No defaults:
|
|
516
|
+
* omitting both bounds asks for equality. Not transitive, so never a basis for
|
|
517
|
+
* a `Set`, a `Map` or an ordering. O(1) time, O(1) memory.
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```ts
|
|
521
|
+
* complexIsClose(complexOf(1, 1e-17), complexOf(1, 0), { relTol: 1e-15 }); // true
|
|
522
|
+
* complexIsClose(complexOf(0, 1e-10), complexOf(0, 0), { absTol: 1e-9 }); // true
|
|
523
|
+
* ```;
|
|
524
|
+
*
|
|
525
|
+
* @param a First value.
|
|
526
|
+
* @param b Second value.
|
|
527
|
+
* @param tolerance The relative and absolute bounds, as `@ac-kit/core`'s
|
|
528
|
+
* `isCloseTo` takes them.
|
|
529
|
+
*/
|
|
530
|
+
export function complexIsClose(a, b, tolerance) {
|
|
531
|
+
if (a[0] === b[0] && a[1] === b[1]) {
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
MODULUS_TERMS_[0] = a[0] - b[0];
|
|
535
|
+
MODULUS_TERMS_[1] = a[1] - b[1];
|
|
536
|
+
hypot2InPlace(MODULUS_TERMS_);
|
|
537
|
+
const distance = MODULUS_TERMS_[0];
|
|
538
|
+
if (distance - distance !== 0) {
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
const relTol = tolerance?.relTol ?? 0;
|
|
542
|
+
const absTol = tolerance?.absTol ?? 0;
|
|
543
|
+
if (distance <= absTol) {
|
|
544
|
+
return true;
|
|
545
|
+
}
|
|
546
|
+
MODULUS_TERMS_[0] = a[0];
|
|
547
|
+
MODULUS_TERMS_[1] = a[1];
|
|
548
|
+
hypot2InPlace(MODULUS_TERMS_);
|
|
549
|
+
const lengthA = MODULUS_TERMS_[0];
|
|
550
|
+
MODULUS_TERMS_[0] = b[0];
|
|
551
|
+
MODULUS_TERMS_[1] = b[1];
|
|
552
|
+
hypot2InPlace(MODULUS_TERMS_);
|
|
553
|
+
return distance <= relTol * Math.max(lengthA, MODULUS_TERMS_[0]);
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Whether both parts are finite numbers: the check to run after parsing
|
|
557
|
+
* untrusted data, as operations never check.
|
|
558
|
+
*/
|
|
559
|
+
export function complexIsFinite(a) {
|
|
560
|
+
return Number.isFinite(a[0]) && Number.isFinite(a[1]);
|
|
87
561
|
}
|
|
88
562
|
//# sourceMappingURL=complex.js.map
|