superjs-core 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/dist/async/index.js.map +1 -1
- package/dist/collection/index.js.map +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/crypto/index.js.map +1 -1
- package/dist/date/index.js.map +1 -1
- package/dist/dep-exray/analyzer/index.d.ts +7 -0
- package/dist/dep-exray/analyzer/index.js +68 -0
- package/dist/dep-exray/analyzer/index.js.map +1 -0
- package/dist/dep-exray/cli.d.ts +1 -0
- package/dist/dep-exray/cli.js +389 -0
- package/dist/dep-exray/cli.js.map +1 -0
- package/dist/dep-exray/index.d.ts +5 -0
- package/dist/dep-exray/index.js +440 -0
- package/dist/dep-exray/index.js.map +1 -0
- package/dist/dep-exray/known-mappings.d.ts +17 -0
- package/dist/dep-exray/known-mappings.js +122 -0
- package/dist/dep-exray/known-mappings.js.map +1 -0
- package/dist/dep-exray/reporter/index.d.ts +5 -0
- package/dist/dep-exray/reporter/index.js +75 -0
- package/dist/dep-exray/reporter/index.js.map +1 -0
- package/dist/dep-exray/scanner/index.d.ts +5 -0
- package/dist/dep-exray/scanner/index.js +299 -0
- package/dist/dep-exray/scanner/index.js.map +1 -0
- package/dist/dep-exray/types.d.ts +38 -0
- package/dist/dep-exray/types.js +1 -0
- package/dist/dep-exray/types.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +439 -814
- package/dist/index.js.map +1 -1
- package/dist/io/index.js.map +1 -1
- package/dist/math/index.d.ts +1 -664
- package/dist/math/index.js +1 -822
- package/dist/math/index.js.map +1 -1
- package/dist/path/index.js.map +1 -1
- package/dist/string/index.js.map +1 -1
- package/dist/type/index.js.map +1 -1
- package/package.json +44 -4
package/dist/math/index.js
CHANGED
|
@@ -85,841 +85,20 @@ function randomInt(min, max) {
|
|
|
85
85
|
function inRange(value, min, max) {
|
|
86
86
|
return value >= min && value <= max;
|
|
87
87
|
}
|
|
88
|
-
function toDegrees(radians) {
|
|
89
|
-
return radians * (180 / Math.PI);
|
|
90
|
-
}
|
|
91
|
-
function toRadians(degrees) {
|
|
92
|
-
return degrees * (Math.PI / 180);
|
|
93
|
-
}
|
|
94
|
-
function sin(angle, unit = "rad") {
|
|
95
|
-
const rad = unit === "deg" ? toRadians(angle) : angle;
|
|
96
|
-
return Math.sin(rad);
|
|
97
|
-
}
|
|
98
|
-
function cos(angle, unit = "rad") {
|
|
99
|
-
const rad = unit === "deg" ? toRadians(angle) : angle;
|
|
100
|
-
return Math.cos(rad);
|
|
101
|
-
}
|
|
102
|
-
function tan(angle, unit = "rad") {
|
|
103
|
-
const rad = unit === "deg" ? toRadians(angle) : angle;
|
|
104
|
-
return Math.tan(rad);
|
|
105
|
-
}
|
|
106
|
-
function asin(value) {
|
|
107
|
-
if (value < -1 || value > 1) {
|
|
108
|
-
throw new RangeError("Input to asin must be between -1 and 1");
|
|
109
|
-
}
|
|
110
|
-
return Math.asin(value);
|
|
111
|
-
}
|
|
112
|
-
function acos(value) {
|
|
113
|
-
if (value < -1 || value > 1) {
|
|
114
|
-
throw new RangeError("Input to acos must be between -1 and 1");
|
|
115
|
-
}
|
|
116
|
-
return Math.acos(value);
|
|
117
|
-
}
|
|
118
|
-
function atan(value) {
|
|
119
|
-
return Math.atan(value);
|
|
120
|
-
}
|
|
121
|
-
function atan2(y, x) {
|
|
122
|
-
return Math.atan2(y, x);
|
|
123
|
-
}
|
|
124
|
-
function hypot(...values) {
|
|
125
|
-
return Math.hypot(...values);
|
|
126
|
-
}
|
|
127
|
-
function median(values) {
|
|
128
|
-
if (values.length === 0) {
|
|
129
|
-
throw new RangeError("Cannot compute median of an empty array");
|
|
130
|
-
}
|
|
131
|
-
const sorted = [...values].sort((a, b) => a - b);
|
|
132
|
-
const mid = Math.floor(sorted.length / 2);
|
|
133
|
-
if (sorted.length % 2 === 0) {
|
|
134
|
-
return (sorted[mid - 1] + sorted[mid]) / 2;
|
|
135
|
-
}
|
|
136
|
-
return sorted[mid];
|
|
137
|
-
}
|
|
138
|
-
function mode(values) {
|
|
139
|
-
if (values.length === 0) {
|
|
140
|
-
throw new RangeError("Cannot compute mode of an empty array");
|
|
141
|
-
}
|
|
142
|
-
const freq = /* @__PURE__ */ new Map();
|
|
143
|
-
let maxFreq = 0;
|
|
144
|
-
for (const v of values) {
|
|
145
|
-
const f = (freq.get(v) ?? 0) + 1;
|
|
146
|
-
freq.set(v, f);
|
|
147
|
-
if (f > maxFreq) maxFreq = f;
|
|
148
|
-
}
|
|
149
|
-
if (maxFreq === 1) return [];
|
|
150
|
-
return Array.from(freq.entries()).filter(([, f]) => f === maxFreq).map(([v]) => v).sort((a, b) => a - b);
|
|
151
|
-
}
|
|
152
|
-
function percentile(values, p) {
|
|
153
|
-
if (values.length === 0) {
|
|
154
|
-
throw new RangeError("Cannot compute percentile of an empty array");
|
|
155
|
-
}
|
|
156
|
-
if (p < 0 || p > 100) {
|
|
157
|
-
throw new RangeError("Percentile must be between 0 and 100");
|
|
158
|
-
}
|
|
159
|
-
const sorted = [...values].sort((a, b) => a - b);
|
|
160
|
-
const index = p / 100 * (sorted.length - 1);
|
|
161
|
-
const lower = Math.floor(index);
|
|
162
|
-
const upper = Math.ceil(index);
|
|
163
|
-
if (lower === upper) return sorted[lower];
|
|
164
|
-
return sorted[lower] + (sorted[upper] - sorted[lower]) * (index - lower);
|
|
165
|
-
}
|
|
166
|
-
function quartiles(values) {
|
|
167
|
-
if (values.length === 0) {
|
|
168
|
-
throw new RangeError("Cannot compute quartiles of an empty array");
|
|
169
|
-
}
|
|
170
|
-
const sorted = [...values].sort((a, b) => a - b);
|
|
171
|
-
if (sorted.length === 1) {
|
|
172
|
-
return { q1: sorted[0], q2: sorted[0], q3: sorted[0] };
|
|
173
|
-
}
|
|
174
|
-
const q2 = median(sorted);
|
|
175
|
-
const mid = Math.floor(sorted.length / 2);
|
|
176
|
-
const lowerHalf = sorted.slice(0, mid);
|
|
177
|
-
const upperHalf = sorted.length % 2 === 0 ? sorted.slice(mid) : sorted.slice(mid + 1);
|
|
178
|
-
return {
|
|
179
|
-
q1: median(lowerHalf),
|
|
180
|
-
q2,
|
|
181
|
-
q3: median(upperHalf)
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
function variance(values, sample = false) {
|
|
185
|
-
if (values.length === 0) {
|
|
186
|
-
throw new RangeError("Cannot compute variance of an empty array");
|
|
187
|
-
}
|
|
188
|
-
if (sample && values.length < 2) {
|
|
189
|
-
throw new RangeError("Sample variance requires at least 2 values");
|
|
190
|
-
}
|
|
191
|
-
const mean = average(values);
|
|
192
|
-
const sqDiffs = values.map((v) => (v - mean) ** 2);
|
|
193
|
-
const divisor = sample ? values.length - 1 : values.length;
|
|
194
|
-
return sum(sqDiffs) / divisor;
|
|
195
|
-
}
|
|
196
|
-
function stddev(values, sample = false) {
|
|
197
|
-
return Math.sqrt(variance(values, sample));
|
|
198
|
-
}
|
|
199
|
-
function covariance(x, y) {
|
|
200
|
-
if (x.length !== y.length) {
|
|
201
|
-
throw new RangeError("Arrays must have the same length");
|
|
202
|
-
}
|
|
203
|
-
if (x.length < 2) {
|
|
204
|
-
throw new RangeError("Covariance requires at least 2 values");
|
|
205
|
-
}
|
|
206
|
-
const meanX = average(x);
|
|
207
|
-
const meanY = average(y);
|
|
208
|
-
let total = 0;
|
|
209
|
-
for (let i = 0; i < x.length; i++) {
|
|
210
|
-
total += (x[i] - meanX) * (y[i] - meanY);
|
|
211
|
-
}
|
|
212
|
-
return total / (x.length - 1);
|
|
213
|
-
}
|
|
214
|
-
function correlation(x, y) {
|
|
215
|
-
const cov = covariance(x, y);
|
|
216
|
-
const stdX = stddev(x, true);
|
|
217
|
-
const stdY = stddev(y, true);
|
|
218
|
-
if (stdX === 0 || stdY === 0) return 0;
|
|
219
|
-
return cov / (stdX * stdY);
|
|
220
|
-
}
|
|
221
|
-
function skewness(values) {
|
|
222
|
-
if (values.length < 3) {
|
|
223
|
-
throw new RangeError("Skewness requires at least 3 values");
|
|
224
|
-
}
|
|
225
|
-
const mean = average(values);
|
|
226
|
-
const n = values.length;
|
|
227
|
-
const m2 = sum(values.map((v) => (v - mean) ** 2)) / n;
|
|
228
|
-
const m3 = sum(values.map((v) => (v - mean) ** 3)) / n;
|
|
229
|
-
if (m2 === 0) return 0;
|
|
230
|
-
return m3 / Math.pow(m2, 1.5) * Math.sqrt(n * (n - 1)) / (n - 2);
|
|
231
|
-
}
|
|
232
|
-
function kurtosis(values) {
|
|
233
|
-
if (values.length < 4) {
|
|
234
|
-
throw new RangeError("Kurtosis requires at least 4 values");
|
|
235
|
-
}
|
|
236
|
-
const mean = average(values);
|
|
237
|
-
const n = values.length;
|
|
238
|
-
const m2 = sum(values.map((v) => (v - mean) ** 2)) / n;
|
|
239
|
-
const m4 = sum(values.map((v) => (v - mean) ** 4)) / n;
|
|
240
|
-
if (m2 === 0) return 0;
|
|
241
|
-
const kurt = m4 / (m2 * m2);
|
|
242
|
-
const adjustment = (n - 1) * (n + 1) / ((n - 2) * (n - 3));
|
|
243
|
-
return adjustment * (kurt - 3 * (n - 1) / (n + 1));
|
|
244
|
-
}
|
|
245
|
-
function weightedMean(values, weights) {
|
|
246
|
-
if (values.length !== weights.length) {
|
|
247
|
-
throw new RangeError("Values and weights must have the same length");
|
|
248
|
-
}
|
|
249
|
-
if (values.length === 0) {
|
|
250
|
-
throw new RangeError("Cannot compute weighted mean of empty arrays");
|
|
251
|
-
}
|
|
252
|
-
const totalWeight = sum(weights);
|
|
253
|
-
if (totalWeight === 0) {
|
|
254
|
-
throw new RangeError("Sum of weights cannot be zero");
|
|
255
|
-
}
|
|
256
|
-
let total = 0;
|
|
257
|
-
for (let i = 0; i < values.length; i++) {
|
|
258
|
-
total += values[i] * weights[i];
|
|
259
|
-
}
|
|
260
|
-
return total / totalWeight;
|
|
261
|
-
}
|
|
262
|
-
function geometricMean(values) {
|
|
263
|
-
if (values.length === 0) {
|
|
264
|
-
throw new RangeError("Cannot compute geometric mean of an empty array");
|
|
265
|
-
}
|
|
266
|
-
if (values.some((v) => v <= 0)) {
|
|
267
|
-
throw new RangeError("Geometric mean requires positive values only");
|
|
268
|
-
}
|
|
269
|
-
const logSum = sum(values.map((v) => Math.log(v)));
|
|
270
|
-
return Math.exp(logSum / values.length);
|
|
271
|
-
}
|
|
272
|
-
function harmonicMean(values) {
|
|
273
|
-
if (values.length === 0) {
|
|
274
|
-
throw new RangeError("Cannot compute harmonic mean of an empty array");
|
|
275
|
-
}
|
|
276
|
-
if (values.some((v) => v <= 0)) {
|
|
277
|
-
throw new RangeError("Harmonic mean requires positive values only");
|
|
278
|
-
}
|
|
279
|
-
const reciprocalSum = sum(values.map((v) => 1 / v));
|
|
280
|
-
return values.length / reciprocalSum;
|
|
281
|
-
}
|
|
282
|
-
function zScore(value, mean, stddev2) {
|
|
283
|
-
if (stddev2 === 0) return 0;
|
|
284
|
-
return (value - mean) / stddev2;
|
|
285
|
-
}
|
|
286
|
-
function normalize(values) {
|
|
287
|
-
if (values.length === 0) return [];
|
|
288
|
-
const min = Math.min(...values);
|
|
289
|
-
const max = Math.max(...values);
|
|
290
|
-
if (max === min) return values.map(() => 0);
|
|
291
|
-
return values.map((v) => (v - min) / (max - min));
|
|
292
|
-
}
|
|
293
|
-
function standardize(values) {
|
|
294
|
-
if (values.length === 0) return [];
|
|
295
|
-
const mean = average(values);
|
|
296
|
-
const sd = stddev(values, true);
|
|
297
|
-
if (sd === 0) return values.map(() => 0);
|
|
298
|
-
return values.map((v) => (v - mean) / sd);
|
|
299
|
-
}
|
|
300
|
-
function factorial(n) {
|
|
301
|
-
if (!Number.isInteger(n) || n < 0) {
|
|
302
|
-
throw new RangeError("Factorial requires a non-negative integer");
|
|
303
|
-
}
|
|
304
|
-
if (n === 0 || n === 1) return 1;
|
|
305
|
-
let result = 1;
|
|
306
|
-
for (let i = 2; i <= n; i++) {
|
|
307
|
-
result *= i;
|
|
308
|
-
}
|
|
309
|
-
return result;
|
|
310
|
-
}
|
|
311
|
-
function binomial(n, k) {
|
|
312
|
-
if (!Number.isInteger(n) || !Number.isInteger(k) || n < 0 || k < 0) {
|
|
313
|
-
throw new RangeError("Binomial requires non-negative integers");
|
|
314
|
-
}
|
|
315
|
-
if (k > n) {
|
|
316
|
-
throw new RangeError("k cannot be greater than n");
|
|
317
|
-
}
|
|
318
|
-
if (k === 0 || k === n) return 1;
|
|
319
|
-
if (k > n - k) return binomial(n, n - k);
|
|
320
|
-
let result = 1;
|
|
321
|
-
for (let i = 1; i <= k; i++) {
|
|
322
|
-
result = result * (n - k + i) / i;
|
|
323
|
-
}
|
|
324
|
-
return result;
|
|
325
|
-
}
|
|
326
|
-
function permutation(n, k) {
|
|
327
|
-
if (!Number.isInteger(n) || !Number.isInteger(k) || n < 0 || k < 0) {
|
|
328
|
-
throw new RangeError("Permutation requires non-negative integers");
|
|
329
|
-
}
|
|
330
|
-
if (k > n) {
|
|
331
|
-
throw new RangeError("k cannot be greater than n");
|
|
332
|
-
}
|
|
333
|
-
let result = 1;
|
|
334
|
-
for (let i = n; i > n - k; i--) {
|
|
335
|
-
result *= i;
|
|
336
|
-
}
|
|
337
|
-
return result;
|
|
338
|
-
}
|
|
339
|
-
function gcd(...values) {
|
|
340
|
-
if (values.length < 2) {
|
|
341
|
-
throw new RangeError("GCD requires at least 2 values");
|
|
342
|
-
}
|
|
343
|
-
const gcd2 = (a, b) => {
|
|
344
|
-
a = Math.abs(a);
|
|
345
|
-
b = Math.abs(b);
|
|
346
|
-
while (b !== 0) {
|
|
347
|
-
const t = b;
|
|
348
|
-
b = a % b;
|
|
349
|
-
a = t;
|
|
350
|
-
}
|
|
351
|
-
return a;
|
|
352
|
-
};
|
|
353
|
-
return values.reduce((a, b) => gcd2(a, b));
|
|
354
|
-
}
|
|
355
|
-
function lcm(...values) {
|
|
356
|
-
if (values.length < 2) {
|
|
357
|
-
throw new RangeError("LCM requires at least 2 values");
|
|
358
|
-
}
|
|
359
|
-
const lcm2 = (a, b) => {
|
|
360
|
-
if (a === 0 || b === 0) return 0;
|
|
361
|
-
return Math.abs(a * b) / gcd(a, b);
|
|
362
|
-
};
|
|
363
|
-
return values.reduce((a, b) => lcm2(a, b));
|
|
364
|
-
}
|
|
365
|
-
function isPrime(n) {
|
|
366
|
-
if (!Number.isInteger(n) || n < 2) return false;
|
|
367
|
-
if (n === 2) return true;
|
|
368
|
-
if (n % 2 === 0) return false;
|
|
369
|
-
const sqrt = Math.sqrt(n);
|
|
370
|
-
for (let i = 3; i <= sqrt; i += 2) {
|
|
371
|
-
if (n % i === 0) return false;
|
|
372
|
-
}
|
|
373
|
-
return true;
|
|
374
|
-
}
|
|
375
|
-
function primeFactors(n) {
|
|
376
|
-
if (!Number.isInteger(n) || n < 2) {
|
|
377
|
-
throw new RangeError("Prime factors require an integer greater than 1");
|
|
378
|
-
}
|
|
379
|
-
const factors = [];
|
|
380
|
-
let num = n;
|
|
381
|
-
while (num % 2 === 0) {
|
|
382
|
-
factors.push(2);
|
|
383
|
-
num /= 2;
|
|
384
|
-
}
|
|
385
|
-
for (let i = 3; i <= Math.sqrt(num); i += 2) {
|
|
386
|
-
while (num % i === 0) {
|
|
387
|
-
factors.push(i);
|
|
388
|
-
num /= i;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if (num > 1) factors.push(num);
|
|
392
|
-
return factors;
|
|
393
|
-
}
|
|
394
|
-
function fibonacci(n) {
|
|
395
|
-
if (!Number.isInteger(n) || n < 0) {
|
|
396
|
-
throw new RangeError("Fibonacci requires a non-negative integer");
|
|
397
|
-
}
|
|
398
|
-
if (n === 0) return 0;
|
|
399
|
-
if (n === 1) return 1;
|
|
400
|
-
let a = 0;
|
|
401
|
-
let b = 1;
|
|
402
|
-
for (let i = 2; i <= n; i++) {
|
|
403
|
-
const temp = a + b;
|
|
404
|
-
a = b;
|
|
405
|
-
b = temp;
|
|
406
|
-
}
|
|
407
|
-
return b;
|
|
408
|
-
}
|
|
409
|
-
function isEven(n) {
|
|
410
|
-
return n % 2 === 0;
|
|
411
|
-
}
|
|
412
|
-
function isOdd(n) {
|
|
413
|
-
return n % 2 !== 0;
|
|
414
|
-
}
|
|
415
|
-
function sign(n) {
|
|
416
|
-
if (n > 0) return 1;
|
|
417
|
-
if (n < 0) return -1;
|
|
418
|
-
return 0;
|
|
419
|
-
}
|
|
420
|
-
function validateMatrix(a) {
|
|
421
|
-
if (a.length === 0) throw new RangeError("Matrix cannot be empty");
|
|
422
|
-
const cols = a[0].length;
|
|
423
|
-
if (cols === 0) throw new RangeError("Matrix rows cannot be empty");
|
|
424
|
-
if (a.some((row) => row.length !== cols)) {
|
|
425
|
-
throw new RangeError("All rows must have the same length");
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
function matrixAdd(a, b) {
|
|
429
|
-
if (a.length !== b.length || a[0].length !== b[0].length) {
|
|
430
|
-
throw new RangeError("Matrices must have same dimensions for addition");
|
|
431
|
-
}
|
|
432
|
-
validateMatrix(a);
|
|
433
|
-
return a.map((row, i) => row.map((val, j) => val + b[i][j]));
|
|
434
|
-
}
|
|
435
|
-
function matrixSub(a, b) {
|
|
436
|
-
if (a.length !== b.length || a[0].length !== b[0].length) {
|
|
437
|
-
throw new RangeError("Matrices must have same dimensions for subtraction");
|
|
438
|
-
}
|
|
439
|
-
validateMatrix(a);
|
|
440
|
-
return a.map((row, i) => row.map((val, j) => val - b[i][j]));
|
|
441
|
-
}
|
|
442
|
-
function matrixMul(a, b) {
|
|
443
|
-
if (a.length !== b.length || a[0].length !== b[0].length) {
|
|
444
|
-
throw new RangeError("Matrices must have same dimensions for element-wise multiplication");
|
|
445
|
-
}
|
|
446
|
-
validateMatrix(a);
|
|
447
|
-
return a.map((row, i) => row.map((val, j) => val * b[i][j]));
|
|
448
|
-
}
|
|
449
|
-
function matrixScale(a, scalar) {
|
|
450
|
-
validateMatrix(a);
|
|
451
|
-
return a.map((row) => row.map((val) => val * scalar));
|
|
452
|
-
}
|
|
453
|
-
function matrixTranspose(a) {
|
|
454
|
-
if (a.length === 0) return [];
|
|
455
|
-
validateMatrix(a);
|
|
456
|
-
return a[0].map((_, j) => a.map((row) => row[j]));
|
|
457
|
-
}
|
|
458
|
-
function matrixDeterminant(a) {
|
|
459
|
-
validateMatrix(a);
|
|
460
|
-
const n = a.length;
|
|
461
|
-
if (a.some((row) => row.length !== n)) {
|
|
462
|
-
throw new RangeError("Matrix must be square for determinant");
|
|
463
|
-
}
|
|
464
|
-
if (n > 3) {
|
|
465
|
-
throw new RangeError("Determinant only supported for max 3x3 matrices");
|
|
466
|
-
}
|
|
467
|
-
if (n === 1) return a[0][0];
|
|
468
|
-
if (n === 2) return a[0][0] * a[1][1] - a[0][1] * a[1][0];
|
|
469
|
-
const [a00, a01, a02] = a[0];
|
|
470
|
-
const [a10, a11, a12] = a[1];
|
|
471
|
-
const [a20, a21, a22] = a[2];
|
|
472
|
-
return a00 * (a11 * a22 - a12 * a21) - a01 * (a10 * a22 - a12 * a20) + a02 * (a10 * a21 - a11 * a20);
|
|
473
|
-
}
|
|
474
|
-
function matrixInverse(a) {
|
|
475
|
-
const det = matrixDeterminant(a);
|
|
476
|
-
if (det === 0) throw new RangeError("Matrix is singular and cannot be inverted");
|
|
477
|
-
const n = a.length;
|
|
478
|
-
if (n === 1) return [[1 / a[0][0]]];
|
|
479
|
-
if (n === 2) {
|
|
480
|
-
return [
|
|
481
|
-
[a[1][1] / det, -a[0][1] / det],
|
|
482
|
-
[-a[1][0] / det, a[0][0] / det]
|
|
483
|
-
];
|
|
484
|
-
}
|
|
485
|
-
const [a00, a01, a02] = a[0];
|
|
486
|
-
const [a10, a11, a12] = a[1];
|
|
487
|
-
const [a20, a21, a22] = a[2];
|
|
488
|
-
return [
|
|
489
|
-
[(a11 * a22 - a12 * a21) / det, -(a01 * a22 - a02 * a21) / det, (a01 * a12 - a02 * a11) / det],
|
|
490
|
-
[-(a10 * a22 - a12 * a20) / det, (a00 * a22 - a02 * a20) / det, -(a00 * a12 - a02 * a10) / det],
|
|
491
|
-
[(a10 * a21 - a11 * a20) / det, -(a00 * a21 - a01 * a20) / det, (a00 * a11 - a01 * a10) / det]
|
|
492
|
-
];
|
|
493
|
-
}
|
|
494
|
-
function matrixIdentity(n) {
|
|
495
|
-
if (!Number.isInteger(n) || n < 1) {
|
|
496
|
-
throw new RangeError("Matrix size must be a positive integer");
|
|
497
|
-
}
|
|
498
|
-
return Array.from(
|
|
499
|
-
{ length: n },
|
|
500
|
-
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
501
|
-
);
|
|
502
|
-
}
|
|
503
|
-
function matrixMultiply(a, b) {
|
|
504
|
-
validateMatrix(a);
|
|
505
|
-
validateMatrix(b);
|
|
506
|
-
if (a[0].length !== b.length) {
|
|
507
|
-
throw new RangeError("Matrix dimensions incompatible for multiplication");
|
|
508
|
-
}
|
|
509
|
-
const result = Array.from(
|
|
510
|
-
{ length: a.length },
|
|
511
|
-
() => Array(b[0].length).fill(0)
|
|
512
|
-
);
|
|
513
|
-
for (let i = 0; i < a.length; i++) {
|
|
514
|
-
for (let j = 0; j < b[0].length; j++) {
|
|
515
|
-
for (let k = 0; k < b.length; k++) {
|
|
516
|
-
result[i][j] = (result[i][j] ?? 0) + (a[i][k] ?? 0) * (b[k][j] ?? 0);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
return result;
|
|
521
|
-
}
|
|
522
|
-
function matrixTrace(a) {
|
|
523
|
-
validateMatrix(a);
|
|
524
|
-
const n = a.length;
|
|
525
|
-
if (a.some((row) => row.length !== n)) {
|
|
526
|
-
throw new RangeError("Matrix must be square for trace");
|
|
527
|
-
}
|
|
528
|
-
let trace = 0;
|
|
529
|
-
for (let i = 0; i < n; i++) {
|
|
530
|
-
trace += a[i][i];
|
|
531
|
-
}
|
|
532
|
-
return trace;
|
|
533
|
-
}
|
|
534
|
-
var Complex = class _Complex {
|
|
535
|
-
constructor(re, im = 0) {
|
|
536
|
-
this.re = re;
|
|
537
|
-
this.im = im;
|
|
538
|
-
}
|
|
539
|
-
re;
|
|
540
|
-
im;
|
|
541
|
-
/**
|
|
542
|
-
* Adds another complex number.
|
|
543
|
-
*
|
|
544
|
-
* @param other - The complex number to add.
|
|
545
|
-
* @returns A new Complex instance.
|
|
546
|
-
*/
|
|
547
|
-
add(other) {
|
|
548
|
-
return new _Complex(this.re + other.re, this.im + other.im);
|
|
549
|
-
}
|
|
550
|
-
/**
|
|
551
|
-
* Subtracts another complex number.
|
|
552
|
-
*
|
|
553
|
-
* @param other - The complex number to subtract.
|
|
554
|
-
* @returns A new Complex instance.
|
|
555
|
-
*/
|
|
556
|
-
sub(other) {
|
|
557
|
-
return new _Complex(this.re - other.re, this.im - other.im);
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Multiplies by another complex number.
|
|
561
|
-
*
|
|
562
|
-
* @param other - The complex number to multiply by.
|
|
563
|
-
* @returns A new Complex instance.
|
|
564
|
-
*/
|
|
565
|
-
mul(other) {
|
|
566
|
-
return new _Complex(
|
|
567
|
-
this.re * other.re - this.im * other.im,
|
|
568
|
-
this.re * other.im + this.im * other.re
|
|
569
|
-
);
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* Divides by another complex number.
|
|
573
|
-
*
|
|
574
|
-
* @param other - The complex number to divide by.
|
|
575
|
-
* @returns A new Complex instance.
|
|
576
|
-
* @throws {DivisionByZeroError} If the divisor is zero.
|
|
577
|
-
*/
|
|
578
|
-
div(other) {
|
|
579
|
-
const denom = other.re * other.re + other.im * other.im;
|
|
580
|
-
if (denom === 0) throw new DivisionByZeroError();
|
|
581
|
-
return new _Complex(
|
|
582
|
-
(this.re * other.re + this.im * other.im) / denom,
|
|
583
|
-
(this.im * other.re - this.re * other.im) / denom
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* Computes the magnitude (absolute value) of the complex number.
|
|
588
|
-
*
|
|
589
|
-
* @returns The magnitude.
|
|
590
|
-
*/
|
|
591
|
-
abs() {
|
|
592
|
-
return Math.sqrt(this.re * this.re + this.im * this.im);
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* Computes the complex conjugate.
|
|
596
|
-
*
|
|
597
|
-
* @returns A new Complex instance with negated imaginary part.
|
|
598
|
-
*/
|
|
599
|
-
conj() {
|
|
600
|
-
return new _Complex(this.re, this.im === 0 ? 0 : -this.im);
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* Computes the argument (phase angle) in radians.
|
|
604
|
-
*
|
|
605
|
-
* @returns The phase angle.
|
|
606
|
-
*/
|
|
607
|
-
arg() {
|
|
608
|
-
return Math.atan2(this.im, this.re);
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Raises the complex number to a real power.
|
|
612
|
-
*
|
|
613
|
-
* @param n - The exponent.
|
|
614
|
-
* @returns A new Complex instance.
|
|
615
|
-
*/
|
|
616
|
-
pow(n) {
|
|
617
|
-
const r = this.abs();
|
|
618
|
-
const theta = this.arg();
|
|
619
|
-
return _Complex.fromPolar(Math.pow(r, n), theta * n);
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Computes the square root of the complex number.
|
|
623
|
-
*
|
|
624
|
-
* @returns A new Complex instance.
|
|
625
|
-
*/
|
|
626
|
-
sqrt() {
|
|
627
|
-
const r = this.abs();
|
|
628
|
-
const theta = this.arg();
|
|
629
|
-
return _Complex.fromPolar(Math.sqrt(r), theta / 2);
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
|
-
* Computes e raised to this complex number.
|
|
633
|
-
*
|
|
634
|
-
* @returns A new Complex instance.
|
|
635
|
-
*/
|
|
636
|
-
exp() {
|
|
637
|
-
return new _Complex(
|
|
638
|
-
Math.exp(this.re) * Math.cos(this.im),
|
|
639
|
-
Math.exp(this.re) * Math.sin(this.im)
|
|
640
|
-
);
|
|
641
|
-
}
|
|
642
|
-
/**
|
|
643
|
-
* Computes the natural logarithm of the complex number.
|
|
644
|
-
*
|
|
645
|
-
* @returns A new Complex instance.
|
|
646
|
-
*/
|
|
647
|
-
log() {
|
|
648
|
-
return new _Complex(Math.log(this.abs()), this.arg());
|
|
649
|
-
}
|
|
650
|
-
/**
|
|
651
|
-
* Computes the sine of the complex number.
|
|
652
|
-
*
|
|
653
|
-
* @returns A new Complex instance.
|
|
654
|
-
*/
|
|
655
|
-
sin() {
|
|
656
|
-
return new _Complex(
|
|
657
|
-
Math.sin(this.re) * Math.cosh(this.im),
|
|
658
|
-
Math.cos(this.re) * Math.sinh(this.im)
|
|
659
|
-
);
|
|
660
|
-
}
|
|
661
|
-
/**
|
|
662
|
-
* Computes the cosine of the complex number.
|
|
663
|
-
*
|
|
664
|
-
* @returns A new Complex instance.
|
|
665
|
-
*/
|
|
666
|
-
cos() {
|
|
667
|
-
return new _Complex(
|
|
668
|
-
Math.cos(this.re) * Math.cosh(this.im),
|
|
669
|
-
-Math.sin(this.re) * Math.sinh(this.im)
|
|
670
|
-
);
|
|
671
|
-
}
|
|
672
|
-
/**
|
|
673
|
-
* Returns a string representation of the complex number.
|
|
674
|
-
*
|
|
675
|
-
* @returns String like "a + bi".
|
|
676
|
-
*/
|
|
677
|
-
toString() {
|
|
678
|
-
if (this.im === 0) return `${this.re}`;
|
|
679
|
-
if (this.re === 0) return `${this.im}i`;
|
|
680
|
-
if (this.im < 0) return `${this.re} - ${Math.abs(this.im)}i`;
|
|
681
|
-
return `${this.re} + ${this.im}i`;
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* Creates a complex number from polar coordinates.
|
|
685
|
-
*
|
|
686
|
-
* @param r - The magnitude.
|
|
687
|
-
* @param theta - The angle in radians.
|
|
688
|
-
* @returns A new Complex instance.
|
|
689
|
-
*/
|
|
690
|
-
static fromPolar(r, theta) {
|
|
691
|
-
return new _Complex(r * Math.cos(theta), r * Math.sin(theta));
|
|
692
|
-
}
|
|
693
|
-
/** The real part. */
|
|
694
|
-
get real() {
|
|
695
|
-
return this.re;
|
|
696
|
-
}
|
|
697
|
-
/** The imaginary part. */
|
|
698
|
-
get imag() {
|
|
699
|
-
return this.im;
|
|
700
|
-
}
|
|
701
|
-
};
|
|
702
|
-
function compoundInterest(principal, rate, time, n = 12) {
|
|
703
|
-
return principal * Math.pow(1 + rate / n, n * time);
|
|
704
|
-
}
|
|
705
|
-
function futureValue(pv, rate, nper) {
|
|
706
|
-
return pv * Math.pow(1 + rate, nper);
|
|
707
|
-
}
|
|
708
|
-
function presentValue(fv, rate, nper) {
|
|
709
|
-
return fv / Math.pow(1 + rate, nper);
|
|
710
|
-
}
|
|
711
|
-
function pmt(rate, nper, pv, fv = 0) {
|
|
712
|
-
if (rate === 0) return -(pv + fv) / nper;
|
|
713
|
-
const pvif = Math.pow(1 + rate, nper);
|
|
714
|
-
return -(pv * pvif + fv) / ((pvif - 1) / rate);
|
|
715
|
-
}
|
|
716
|
-
function npv(rate, cashflows) {
|
|
717
|
-
if (cashflows.length === 0) return 0;
|
|
718
|
-
let result = 0;
|
|
719
|
-
for (let i = 0; i < cashflows.length; i++) {
|
|
720
|
-
result += cashflows[i] / Math.pow(1 + rate, i);
|
|
721
|
-
}
|
|
722
|
-
return result;
|
|
723
|
-
}
|
|
724
|
-
function irr(cashflows) {
|
|
725
|
-
if (cashflows.length < 2) {
|
|
726
|
-
throw new RangeError("IRR requires at least 2 cashflows");
|
|
727
|
-
}
|
|
728
|
-
let guess = 0.1;
|
|
729
|
-
const tolerance = 1e-7;
|
|
730
|
-
const maxIter = 1e3;
|
|
731
|
-
for (let i = 0; i < maxIter; i++) {
|
|
732
|
-
let npvVal = 0;
|
|
733
|
-
let dnpv = 0;
|
|
734
|
-
for (let j = 0; j < cashflows.length; j++) {
|
|
735
|
-
npvVal += cashflows[j] / Math.pow(1 + guess, j);
|
|
736
|
-
dnpv -= j * cashflows[j] / Math.pow(1 + guess, j + 1);
|
|
737
|
-
}
|
|
738
|
-
if (Math.abs(npvVal) < tolerance) return guess;
|
|
739
|
-
if (dnpv === 0) return NaN;
|
|
740
|
-
guess -= npvVal / dnpv;
|
|
741
|
-
}
|
|
742
|
-
return NaN;
|
|
743
|
-
}
|
|
744
|
-
function roi(gain, cost) {
|
|
745
|
-
if (cost === 0) throw new RangeError("Cost cannot be zero");
|
|
746
|
-
return (gain - cost) / cost;
|
|
747
|
-
}
|
|
748
|
-
function amortizationSchedule(principal, rate, nper) {
|
|
749
|
-
const payment = -pmt(rate, nper, principal);
|
|
750
|
-
let balance = principal;
|
|
751
|
-
const schedule = [];
|
|
752
|
-
for (let period = 1; period <= nper; period++) {
|
|
753
|
-
const interest = balance * rate;
|
|
754
|
-
const principalPart = payment - interest;
|
|
755
|
-
balance -= principalPart;
|
|
756
|
-
schedule.push({
|
|
757
|
-
period,
|
|
758
|
-
payment: Math.round(payment * 100) / 100,
|
|
759
|
-
interest: Math.round(interest * 100) / 100,
|
|
760
|
-
principal: Math.round(principalPart * 100) / 100,
|
|
761
|
-
balance: Math.round(Math.max(balance, 0) * 100) / 100
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
return schedule;
|
|
765
|
-
}
|
|
766
|
-
function randomNormal(mean = 0, stddev2 = 1) {
|
|
767
|
-
let u = 0;
|
|
768
|
-
let v = 0;
|
|
769
|
-
while (u === 0) u = Math.random();
|
|
770
|
-
while (v === 0) v = Math.random();
|
|
771
|
-
const z = Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
|
|
772
|
-
return mean + z * stddev2;
|
|
773
|
-
}
|
|
774
|
-
function randomPoisson(lambda) {
|
|
775
|
-
if (lambda <= 0) throw new RangeError("Lambda must be positive");
|
|
776
|
-
const L = Math.exp(-lambda);
|
|
777
|
-
let k = 0;
|
|
778
|
-
let p = 1;
|
|
779
|
-
do {
|
|
780
|
-
k++;
|
|
781
|
-
p *= Math.random();
|
|
782
|
-
} while (p > L);
|
|
783
|
-
return k - 1;
|
|
784
|
-
}
|
|
785
|
-
function randomBinomial(n, p) {
|
|
786
|
-
if (!Number.isInteger(n) || n < 0) throw new RangeError("n must be a non-negative integer");
|
|
787
|
-
if (p < 0 || p > 1) throw new RangeError("p must be between 0 and 1");
|
|
788
|
-
let successes = 0;
|
|
789
|
-
for (let i = 0; i < n; i++) {
|
|
790
|
-
if (Math.random() < p) successes++;
|
|
791
|
-
}
|
|
792
|
-
return successes;
|
|
793
|
-
}
|
|
794
|
-
function randomExponential(lambda) {
|
|
795
|
-
if (lambda <= 0) throw new RangeError("Lambda must be positive");
|
|
796
|
-
return -Math.log(1 - Math.random()) / lambda;
|
|
797
|
-
}
|
|
798
|
-
function randomUniform(min = 0, max = 1) {
|
|
799
|
-
return Math.random() * (max - min) + min;
|
|
800
|
-
}
|
|
801
|
-
function derivative(f, x, h = 1e-10) {
|
|
802
|
-
return (f(x + h) - f(x - h)) / (2 * h);
|
|
803
|
-
}
|
|
804
|
-
function integral(f, a, b, n = 100) {
|
|
805
|
-
if (n % 2 !== 0) {
|
|
806
|
-
throw new RangeError("Number of subintervals must be even for Simpson's rule");
|
|
807
|
-
}
|
|
808
|
-
const h = (b - a) / n;
|
|
809
|
-
let sum2 = f(a) + f(b);
|
|
810
|
-
for (let i = 1; i < n; i++) {
|
|
811
|
-
const x = a + i * h;
|
|
812
|
-
sum2 += i % 2 === 0 ? 2 * f(x) : 4 * f(x);
|
|
813
|
-
}
|
|
814
|
-
return h / 3 * sum2;
|
|
815
|
-
}
|
|
816
|
-
function trapezoidal(f, a, b, n = 100) {
|
|
817
|
-
const h = (b - a) / n;
|
|
818
|
-
let sum2 = (f(a) + f(b)) / 2;
|
|
819
|
-
for (let i = 1; i < n; i++) {
|
|
820
|
-
sum2 += f(a + i * h);
|
|
821
|
-
}
|
|
822
|
-
return h * sum2;
|
|
823
|
-
}
|
|
824
|
-
function newtonRoot(f, guess, tolerance = 1e-7, maxIter = 100) {
|
|
825
|
-
let x = guess;
|
|
826
|
-
for (let i = 0; i < maxIter; i++) {
|
|
827
|
-
const fx = f(x);
|
|
828
|
-
if (Math.abs(fx) < tolerance) return x;
|
|
829
|
-
const df = derivative(f, x);
|
|
830
|
-
if (df === 0) return NaN;
|
|
831
|
-
x -= fx / df;
|
|
832
|
-
}
|
|
833
|
-
return NaN;
|
|
834
|
-
}
|
|
835
|
-
function lerp(a, b, t) {
|
|
836
|
-
return a + (b - a) * t;
|
|
837
|
-
}
|
|
838
|
-
function mapRange(value, inMin, inMax, outMin, outMax) {
|
|
839
|
-
if (inMax === inMin) throw new RangeError("Input range cannot be zero");
|
|
840
|
-
return (value - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
|
|
841
|
-
}
|
|
842
88
|
export {
|
|
843
|
-
Complex,
|
|
844
89
|
DivisionByZeroError,
|
|
845
|
-
acos,
|
|
846
90
|
add,
|
|
847
|
-
amortizationSchedule,
|
|
848
91
|
approxEqual,
|
|
849
|
-
asin,
|
|
850
|
-
atan,
|
|
851
|
-
atan2,
|
|
852
92
|
average,
|
|
853
|
-
binomial,
|
|
854
93
|
ceil,
|
|
855
94
|
clamp,
|
|
856
|
-
compoundInterest,
|
|
857
|
-
correlation,
|
|
858
|
-
cos,
|
|
859
|
-
covariance,
|
|
860
|
-
derivative,
|
|
861
95
|
div,
|
|
862
|
-
factorial,
|
|
863
|
-
fibonacci,
|
|
864
96
|
floor,
|
|
865
|
-
futureValue,
|
|
866
|
-
gcd,
|
|
867
|
-
geometricMean,
|
|
868
|
-
harmonicMean,
|
|
869
|
-
hypot,
|
|
870
97
|
inRange,
|
|
871
|
-
integral,
|
|
872
|
-
irr,
|
|
873
|
-
isEven,
|
|
874
|
-
isOdd,
|
|
875
|
-
isPrime,
|
|
876
|
-
kurtosis,
|
|
877
|
-
lcm,
|
|
878
|
-
lerp,
|
|
879
|
-
mapRange,
|
|
880
|
-
matrixAdd,
|
|
881
|
-
matrixDeterminant,
|
|
882
|
-
matrixIdentity,
|
|
883
|
-
matrixInverse,
|
|
884
|
-
matrixMul,
|
|
885
|
-
matrixMultiply,
|
|
886
|
-
matrixScale,
|
|
887
|
-
matrixSub,
|
|
888
|
-
matrixTrace,
|
|
889
|
-
matrixTranspose,
|
|
890
|
-
median,
|
|
891
|
-
mode,
|
|
892
98
|
mul,
|
|
893
|
-
newtonRoot,
|
|
894
|
-
normalize,
|
|
895
|
-
npv,
|
|
896
|
-
percentile,
|
|
897
|
-
permutation,
|
|
898
|
-
pmt,
|
|
899
|
-
presentValue,
|
|
900
|
-
primeFactors,
|
|
901
|
-
quartiles,
|
|
902
|
-
randomBinomial,
|
|
903
|
-
randomExponential,
|
|
904
99
|
randomInt,
|
|
905
|
-
randomNormal,
|
|
906
|
-
randomPoisson,
|
|
907
|
-
randomUniform,
|
|
908
|
-
roi,
|
|
909
100
|
round,
|
|
910
|
-
sign,
|
|
911
|
-
sin,
|
|
912
|
-
skewness,
|
|
913
|
-
standardize,
|
|
914
|
-
stddev,
|
|
915
101
|
sub,
|
|
916
|
-
sum
|
|
917
|
-
tan,
|
|
918
|
-
toDegrees,
|
|
919
|
-
toRadians,
|
|
920
|
-
trapezoidal,
|
|
921
|
-
variance,
|
|
922
|
-
weightedMean,
|
|
923
|
-
zScore
|
|
102
|
+
sum
|
|
924
103
|
};
|
|
925
104
|
//# sourceMappingURL=index.js.map
|