@fluixi/utils 1.0.0-alpha.83 → 1.0.0-alpha.84
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/dist/index.cjs +2858 -1
- package/dist/index.mjs +2838 -1
- package/dist/lib/array/advanced.cjs +2227 -1
- package/dist/lib/array/advanced.mjs +2204 -1
- package/dist/lib/array/array.cjs +57 -1
- package/dist/lib/array/array.mjs +36 -1
- package/dist/lib/array/index.cjs +2344 -1
- package/dist/lib/array/index.mjs +2322 -1
- package/dist/lib/array/utils.cjs +103 -1
- package/dist/lib/array/utils.mjs +82 -1
- package/dist/lib/color/color.cjs +2190 -1
- package/dist/lib/color/color.mjs +2167 -1
- package/dist/lib/color/constants.cjs +380 -1
- package/dist/lib/color/constants.mjs +359 -1
- package/dist/lib/color/conversions.cjs +629 -1
- package/dist/lib/color/conversions.mjs +608 -1
- package/dist/lib/color/helpers.cjs +2534 -1
- package/dist/lib/color/helpers.mjs +2511 -1
- package/dist/lib/color/index.cjs +2641 -1
- package/dist/lib/color/index.mjs +2619 -1
- package/dist/lib/color/types.cjs +18 -1
- package/dist/lib/color/utils.cjs +962 -1
- package/dist/lib/color/utils.mjs +939 -1
- package/dist/lib/compare/comparators.cjs +171 -1
- package/dist/lib/compare/comparators.mjs +150 -1
- package/dist/lib/compare/index.cjs +172 -1
- package/dist/lib/compare/index.mjs +152 -1
- package/dist/lib/crypto/hash.cjs +83 -1
- package/dist/lib/crypto/hash.mjs +62 -1
- package/dist/lib/crypto/index.cjs +85 -1
- package/dist/lib/crypto/index.mjs +62 -1
- package/dist/lib/dom/events.cjs +40 -1
- package/dist/lib/dom/events.mjs +19 -1
- package/dist/lib/dom/index.cjs +78 -1
- package/dist/lib/dom/index.mjs +56 -1
- package/dist/lib/dom/utils.cjs +57 -1
- package/dist/lib/dom/utils.mjs +36 -1
- package/dist/lib/functions/functions.cjs +366 -1
- package/dist/lib/functions/functions.mjs +345 -1
- package/dist/lib/functions/index.cjs +369 -1
- package/dist/lib/functions/index.mjs +347 -1
- package/dist/lib/functions/types.cjs +18 -1
- package/dist/lib/helpers.cjs +78 -1
- package/dist/lib/helpers.mjs +59 -1
- package/dist/lib/index.cjs +2857 -1
- package/dist/lib/index.mjs +2836 -1
- package/dist/lib/maths/constants.cjs +70 -1
- package/dist/lib/maths/constants.mjs +50 -1
- package/dist/lib/maths/fn/advanced.cjs +231 -1
- package/dist/lib/maths/fn/advanced.mjs +208 -1
- package/dist/lib/maths/fn/basic.cjs +212 -1
- package/dist/lib/maths/fn/basic.mjs +191 -1
- package/dist/lib/maths/fn/complex.cjs +268 -1
- package/dist/lib/maths/fn/complex.mjs +247 -1
- package/dist/lib/maths/fn/index.cjs +877 -1
- package/dist/lib/maths/fn/index.mjs +855 -1
- package/dist/lib/maths/fn/trigo.cjs +179 -1
- package/dist/lib/maths/fn/trigo.mjs +156 -1
- package/dist/lib/maths/fn/vector.cjs +728 -1
- package/dist/lib/maths/fn/vector.mjs +707 -1
- package/dist/lib/maths/formatter.cjs +61 -1
- package/dist/lib/maths/formatter.mjs +40 -1
- package/dist/lib/maths/guards.cjs +164 -1
- package/dist/lib/maths/guards.mjs +144 -1
- package/dist/lib/maths/index.cjs +1152 -1
- package/dist/lib/maths/index.mjs +1130 -1
- package/dist/lib/maths/parser.cjs +75 -1
- package/dist/lib/maths/parser.mjs +54 -1
- package/dist/lib/maths/random.cjs +49 -1
- package/dist/lib/maths/random.mjs +28 -1
- package/dist/lib/maths/types/advanced.cjs +18 -1
- package/dist/lib/maths/types/basic.cjs +18 -1
- package/dist/lib/maths/types/index.cjs +19 -1
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -1
- package/dist/lib/maths/types/operations.cjs +18 -1
- package/dist/lib/object/clone.cjs +88 -1
- package/dist/lib/object/clone.mjs +65 -1
- package/dist/lib/object/diff.cjs +573 -1
- package/dist/lib/object/diff.mjs +550 -1
- package/dist/lib/object/getter-setter.cjs +2351 -1
- package/dist/lib/object/getter-setter.mjs +2330 -1
- package/dist/lib/object/index.cjs +3956 -6
- package/dist/lib/object/index.mjs +3936 -6
- package/dist/lib/object/merge.cjs +1029 -1
- package/dist/lib/object/merge.mjs +1006 -1
- package/dist/lib/object/object.cjs +280 -1
- package/dist/lib/object/object.mjs +257 -1
- package/dist/lib/object/omit.cjs +2022 -1
- package/dist/lib/object/omit.mjs +1999 -1
- package/dist/lib/object/reconcile.cjs +126 -1
- package/dist/lib/object/reconcile.mjs +105 -1
- package/dist/lib/object/selector.cjs +303 -1
- package/dist/lib/object/selector.mjs +282 -1
- package/dist/lib/object/sort.cjs +209 -1
- package/dist/lib/object/sort.mjs +186 -1
- package/dist/lib/object/transform.cjs +2211 -1
- package/dist/lib/object/transform.mjs +2190 -1
- package/dist/lib/object/types.cjs +18 -1
- package/dist/lib/object/utils.cjs +326 -6
- package/dist/lib/object/utils.mjs +305 -6
- package/dist/lib/primitive/boolean/boolean.cjs +407 -1
- package/dist/lib/primitive/boolean/boolean.mjs +386 -1
- package/dist/lib/primitive/boolean/index.cjs +382 -1
- package/dist/lib/primitive/boolean/index.mjs +359 -1
- package/dist/lib/primitive/boolean/types.cjs +18 -1
- package/dist/lib/primitive/date/date.cjs +1126 -1
- package/dist/lib/primitive/date/date.mjs +1105 -1
- package/dist/lib/primitive/date/index.cjs +1128 -1
- package/dist/lib/primitive/date/index.mjs +1105 -1
- package/dist/lib/primitive/date/types.cjs +18 -1
- package/dist/lib/primitive/index.cjs +2338 -5
- package/dist/lib/primitive/index.mjs +2316 -5
- package/dist/lib/primitive/string/index.cjs +875 -5
- package/dist/lib/primitive/string/index.mjs +852 -5
- package/dist/lib/primitive/string/parser-2.cjs +543 -8
- package/dist/lib/primitive/string/parser-2.mjs +522 -8
- package/dist/lib/primitive/string/parser.cjs +236 -3
- package/dist/lib/primitive/string/parser.mjs +215 -3
- package/dist/lib/primitive/string/string.cjs +926 -5
- package/dist/lib/primitive/string/string.mjs +903 -5
- package/dist/lib/primitive/string/types.cjs +18 -1
- package/dist/lib/primitive/types.cjs +18 -1
- package/dist/lib/url/index.cjs +327 -1
- package/dist/lib/url/index.mjs +304 -1
- package/dist/lib/url/parser.cjs +325 -1
- package/dist/lib/url/parser.mjs +304 -1
- package/dist/lib/url/types.cjs +18 -1
- package/package.json +5 -5
|
@@ -1 +1,247 @@
|
|
|
1
|
-
|
|
1
|
+
// src/lib/maths/fn/complex.ts
|
|
2
|
+
var EPSILON = 1e-10;
|
|
3
|
+
var TWO_PI = 2 * Math.PI;
|
|
4
|
+
function isApproximatelyZero(value) {
|
|
5
|
+
return Math.abs(value) < EPSILON;
|
|
6
|
+
}
|
|
7
|
+
function approximatelyEqual(a, b) {
|
|
8
|
+
return Math.abs(a - b) < EPSILON;
|
|
9
|
+
}
|
|
10
|
+
function normalizeComplex(c) {
|
|
11
|
+
return {
|
|
12
|
+
real: isApproximatelyZero(c.real) ? 0 : c.real,
|
|
13
|
+
imag: isApproximatelyZero(c.imag) ? 0 : c.imag
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function validateNumber(value, name) {
|
|
17
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
18
|
+
throw new Error(`${name} must be a finite number, got ${value}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function validateComplexNumber(c, name = "Complex number") {
|
|
22
|
+
if (!c || typeof c !== "object") {
|
|
23
|
+
throw new Error(`${name} must be an object`);
|
|
24
|
+
}
|
|
25
|
+
validateNumber(c.real, `${name}.real`);
|
|
26
|
+
validateNumber(c.imag, `${name}.imag`);
|
|
27
|
+
}
|
|
28
|
+
function createComplex(real, imag = 0) {
|
|
29
|
+
validateNumber(real, "real");
|
|
30
|
+
validateNumber(imag, "imag");
|
|
31
|
+
return normalizeComplex({ real, imag });
|
|
32
|
+
}
|
|
33
|
+
function complexEquals(a, b) {
|
|
34
|
+
validateComplexNumber(a, "First complex number");
|
|
35
|
+
validateComplexNumber(b, "Second complex number");
|
|
36
|
+
return approximatelyEqual(a.real, b.real) && approximatelyEqual(a.imag, b.imag);
|
|
37
|
+
}
|
|
38
|
+
function addComplex(a, b) {
|
|
39
|
+
validateComplexNumber(a, "First complex number");
|
|
40
|
+
validateComplexNumber(b, "Second complex number");
|
|
41
|
+
return normalizeComplex({
|
|
42
|
+
real: a.real + b.real,
|
|
43
|
+
imag: a.imag + b.imag
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function subtractComplex(a, b) {
|
|
47
|
+
validateComplexNumber(a, "First complex number");
|
|
48
|
+
validateComplexNumber(b, "Second complex number");
|
|
49
|
+
return normalizeComplex({
|
|
50
|
+
real: a.real - b.real,
|
|
51
|
+
imag: a.imag - b.imag
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function multiplyComplex(a, b) {
|
|
55
|
+
validateComplexNumber(a, "First complex number");
|
|
56
|
+
validateComplexNumber(b, "Second complex number");
|
|
57
|
+
return normalizeComplex({
|
|
58
|
+
real: a.real * b.real - a.imag * b.imag,
|
|
59
|
+
imag: a.real * b.imag + a.imag * b.real
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function divideComplex(a, b) {
|
|
63
|
+
validateComplexNumber(a, "Numerator complex number");
|
|
64
|
+
validateComplexNumber(b, "Denominator complex number");
|
|
65
|
+
const denominator = b.real * b.real + b.imag * b.imag;
|
|
66
|
+
if (isApproximatelyZero(denominator)) {
|
|
67
|
+
throw new Error("Cannot divide by zero or near-zero complex number");
|
|
68
|
+
}
|
|
69
|
+
return normalizeComplex({
|
|
70
|
+
real: (a.real * b.real + a.imag * b.imag) / denominator,
|
|
71
|
+
imag: (a.imag * b.real - a.real * b.imag) / denominator
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function magnitudeComplex(c) {
|
|
75
|
+
validateComplexNumber(c);
|
|
76
|
+
const magnitude = Math.sqrt(c.real * c.real + c.imag * c.imag);
|
|
77
|
+
return isApproximatelyZero(magnitude) ? 0 : magnitude;
|
|
78
|
+
}
|
|
79
|
+
function conjugateComplex(c) {
|
|
80
|
+
validateComplexNumber(c);
|
|
81
|
+
return normalizeComplex({
|
|
82
|
+
real: c.real,
|
|
83
|
+
imag: -c.imag
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function argumentComplex(c) {
|
|
87
|
+
validateComplexNumber(c);
|
|
88
|
+
if (isApproximatelyZero(c.real) && isApproximatelyZero(c.imag)) {
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
return Math.atan2(c.imag, c.real);
|
|
92
|
+
}
|
|
93
|
+
function toPolarComplex(c) {
|
|
94
|
+
validateComplexNumber(c);
|
|
95
|
+
return {
|
|
96
|
+
magnitude: magnitudeComplex(c),
|
|
97
|
+
angle: argumentComplex(c)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function fromPolarComplex(magnitude, angle) {
|
|
101
|
+
validateNumber(magnitude, "magnitude");
|
|
102
|
+
validateNumber(angle, "angle");
|
|
103
|
+
if (magnitude < -EPSILON) {
|
|
104
|
+
throw new Error("Magnitude must be non-negative");
|
|
105
|
+
}
|
|
106
|
+
let normalizedAngle = angle % TWO_PI;
|
|
107
|
+
if (normalizedAngle > Math.PI) normalizedAngle -= TWO_PI;
|
|
108
|
+
if (normalizedAngle <= -Math.PI) normalizedAngle += TWO_PI;
|
|
109
|
+
const mag = Math.max(0, magnitude);
|
|
110
|
+
return normalizeComplex({
|
|
111
|
+
real: mag * Math.cos(normalizedAngle),
|
|
112
|
+
imag: mag * Math.sin(normalizedAngle)
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function sqrtComplex(c) {
|
|
116
|
+
validateComplexNumber(c);
|
|
117
|
+
if (isApproximatelyZero(c.real) && isApproximatelyZero(c.imag)) {
|
|
118
|
+
const zero = createComplex(0, 0);
|
|
119
|
+
return [zero, zero];
|
|
120
|
+
}
|
|
121
|
+
const magnitude = magnitudeComplex(c);
|
|
122
|
+
const angle = argumentComplex(c);
|
|
123
|
+
const rootMagnitude = Math.sqrt(magnitude);
|
|
124
|
+
const rootAngle1 = angle / 2;
|
|
125
|
+
const rootAngle2 = rootAngle1 + Math.PI;
|
|
126
|
+
return [
|
|
127
|
+
fromPolarComplex(rootMagnitude, rootAngle1),
|
|
128
|
+
fromPolarComplex(rootMagnitude, rootAngle2)
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
function powerComplex(c, exponent) {
|
|
132
|
+
validateComplexNumber(c);
|
|
133
|
+
validateNumber(exponent, "exponent");
|
|
134
|
+
if (isApproximatelyZero(exponent)) {
|
|
135
|
+
return createComplex(1, 0);
|
|
136
|
+
}
|
|
137
|
+
if (approximatelyEqual(exponent, 1)) {
|
|
138
|
+
return createComplex(c.real, c.imag);
|
|
139
|
+
}
|
|
140
|
+
if (approximatelyEqual(exponent, 2)) {
|
|
141
|
+
return multiplyComplex(c, c);
|
|
142
|
+
}
|
|
143
|
+
if (isApproximatelyZero(c.real) && isApproximatelyZero(c.imag)) {
|
|
144
|
+
if (exponent > 0) {
|
|
145
|
+
return createComplex(0, 0);
|
|
146
|
+
} else if (exponent < 0) {
|
|
147
|
+
throw new Error("Cannot raise zero to a negative power");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const polar = toPolarComplex(c);
|
|
151
|
+
const magnitude = Math.pow(polar.magnitude, exponent);
|
|
152
|
+
const safeMagnitude = magnitude < -EPSILON ? Math.abs(magnitude) : Math.max(0, magnitude);
|
|
153
|
+
const angle = polar.angle * exponent;
|
|
154
|
+
return fromPolarComplex(safeMagnitude, angle);
|
|
155
|
+
}
|
|
156
|
+
function lnComplex(c) {
|
|
157
|
+
validateComplexNumber(c);
|
|
158
|
+
const magnitude = magnitudeComplex(c);
|
|
159
|
+
if (isApproximatelyZero(magnitude)) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
"Cannot take logarithm of zero or near-zero complex number"
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const angle = argumentComplex(c);
|
|
165
|
+
return normalizeComplex({
|
|
166
|
+
real: Math.log(magnitude),
|
|
167
|
+
imag: angle
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function expComplex(c) {
|
|
171
|
+
validateComplexNumber(c);
|
|
172
|
+
const expReal = Math.exp(c.real);
|
|
173
|
+
return normalizeComplex({
|
|
174
|
+
real: expReal * Math.cos(c.imag),
|
|
175
|
+
imag: expReal * Math.sin(c.imag)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function sinComplex(c) {
|
|
179
|
+
validateComplexNumber(c);
|
|
180
|
+
return normalizeComplex({
|
|
181
|
+
real: Math.sin(c.real) * Math.cosh(c.imag),
|
|
182
|
+
imag: Math.cos(c.real) * Math.sinh(c.imag)
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
function cosComplex(c) {
|
|
186
|
+
validateComplexNumber(c);
|
|
187
|
+
return normalizeComplex({
|
|
188
|
+
real: Math.cos(c.real) * Math.cosh(c.imag),
|
|
189
|
+
imag: -Math.sin(c.real) * Math.sinh(c.imag)
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function isZeroComplex(c) {
|
|
193
|
+
validateComplexNumber(c);
|
|
194
|
+
return isApproximatelyZero(c.real) && isApproximatelyZero(c.imag);
|
|
195
|
+
}
|
|
196
|
+
function getReal(c) {
|
|
197
|
+
validateComplexNumber(c);
|
|
198
|
+
return c.real;
|
|
199
|
+
}
|
|
200
|
+
function getImag(c) {
|
|
201
|
+
validateComplexNumber(c);
|
|
202
|
+
return c.imag;
|
|
203
|
+
}
|
|
204
|
+
function negateComplex(c) {
|
|
205
|
+
validateComplexNumber(c);
|
|
206
|
+
return normalizeComplex({
|
|
207
|
+
real: -c.real,
|
|
208
|
+
imag: -c.imag
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function reciprocalComplex(c) {
|
|
212
|
+
validateComplexNumber(c);
|
|
213
|
+
const denominator = c.real * c.real + c.imag * c.imag;
|
|
214
|
+
if (isApproximatelyZero(denominator)) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
"Cannot calculate reciprocal of zero or near-zero complex number"
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return normalizeComplex({
|
|
220
|
+
real: c.real / denominator,
|
|
221
|
+
imag: -c.imag / denominator
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
export {
|
|
225
|
+
addComplex,
|
|
226
|
+
argumentComplex,
|
|
227
|
+
complexEquals,
|
|
228
|
+
conjugateComplex,
|
|
229
|
+
cosComplex,
|
|
230
|
+
createComplex,
|
|
231
|
+
divideComplex,
|
|
232
|
+
expComplex,
|
|
233
|
+
fromPolarComplex,
|
|
234
|
+
getImag,
|
|
235
|
+
getReal,
|
|
236
|
+
isZeroComplex,
|
|
237
|
+
lnComplex,
|
|
238
|
+
magnitudeComplex,
|
|
239
|
+
multiplyComplex,
|
|
240
|
+
negateComplex,
|
|
241
|
+
powerComplex,
|
|
242
|
+
reciprocalComplex,
|
|
243
|
+
sinComplex,
|
|
244
|
+
sqrtComplex,
|
|
245
|
+
subtractComplex,
|
|
246
|
+
toPolarComplex
|
|
247
|
+
};
|