1 | import { abs, atan2, cos, exp, halfPi, log, pow, sin, sqrt } from "./math.js";
|
2 |
|
3 | export function complexAtan(x, y) {
|
4 | var x2 = x * x,
|
5 | y_1 = y + 1,
|
6 | t = 1 - x2 - y * y;
|
7 | return [
|
8 | 0.5 * ((x >= 0 ? halfPi : -halfPi) - atan2(t, 2 * x)),
|
9 | -0.25 * log(t * t + 4 * x2) + 0.5 * log(y_1 * y_1 + x2)
|
10 | ];
|
11 | }
|
12 |
|
13 | export function complexDivide(a, b) {
|
14 | if (b[1]) (a = complexMul(a, [b[0], -b[1]])), (b = complexNorm2(b));
|
15 | else b = b[0];
|
16 | return [a[0] / b, a[1] / b];
|
17 | }
|
18 |
|
19 | export function complexMul(a, b) {
|
20 | return [a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]];
|
21 | }
|
22 |
|
23 | export function complexAdd(a, b) {
|
24 | return [a[0] + b[0], a[1] + b[1]];
|
25 | }
|
26 |
|
27 | export function complexSub(a, b) {
|
28 | return [a[0] - b[0], a[1] - b[1]];
|
29 | }
|
30 |
|
31 | export function complexNorm2(a) {
|
32 | return a[0] * a[0] + a[1] * a[1];
|
33 | }
|
34 |
|
35 | export function complexNorm(a) {
|
36 | return sqrt(complexNorm2(a));
|
37 | }
|
38 |
|
39 | export function complexLogHypot(a, b) {
|
40 | var _a = abs(a),
|
41 | _b = abs(b);
|
42 | if (a === 0) return log(_b);
|
43 | if (b === 0) return log(_a);
|
44 | if (_a < 3000 && _b < 3000) return log(a * a + b * b) * 0.5;
|
45 | return log(a / cos(atan2(b, a)));
|
46 | }
|
47 |
|
48 |
|
49 | export function complexPow(a, n) {
|
50 | var b = a[1],
|
51 | arg,
|
52 | loh;
|
53 | a = a[0];
|
54 | if (a === 0 && b === 0) return [0, 0];
|
55 |
|
56 | if (typeof n === "number") n = [n, 0];
|
57 |
|
58 | if (!n[1]) {
|
59 | if (b === 0 && a >= 0) {
|
60 | return [pow(a, n[0]), 0];
|
61 | } else if (a === 0) {
|
62 | switch ((n[1] % 4 + 4) % 4) {
|
63 | case 0:
|
64 | return [pow(b, n[0]), 0];
|
65 | case 1:
|
66 | return [0, pow(b, n[0])];
|
67 | case 2:
|
68 | return [-pow(b, n[0]), 0];
|
69 | case 3:
|
70 | return [0, -pow(b, n[0])];
|
71 | }
|
72 | }
|
73 | }
|
74 |
|
75 | arg = atan2(b, a);
|
76 | loh = complexLogHypot(a, b);
|
77 | a = exp(n[0] * loh - n[1] * arg);
|
78 | b = n[1] * loh + n[0] * arg;
|
79 | return [a * cos(b), a * sin(b)];
|
80 | }
|