superjs-core 0.1.0 → 0.2.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/index.d.ts +1 -1
- package/dist/index.js +814 -1
- package/dist/index.js.map +1 -1
- package/dist/math/index.d.ts +664 -1
- package/dist/math/index.js +822 -1
- package/dist/math/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -346,6 +346,753 @@ function randomInt(min, max) {
|
|
|
346
346
|
function inRange(value, min, max) {
|
|
347
347
|
return value >= min && value <= max;
|
|
348
348
|
}
|
|
349
|
+
function toDegrees(radians) {
|
|
350
|
+
return radians * (180 / Math.PI);
|
|
351
|
+
}
|
|
352
|
+
function toRadians(degrees) {
|
|
353
|
+
return degrees * (Math.PI / 180);
|
|
354
|
+
}
|
|
355
|
+
function sin(angle, unit = "rad") {
|
|
356
|
+
const rad = unit === "deg" ? toRadians(angle) : angle;
|
|
357
|
+
return Math.sin(rad);
|
|
358
|
+
}
|
|
359
|
+
function cos(angle, unit = "rad") {
|
|
360
|
+
const rad = unit === "deg" ? toRadians(angle) : angle;
|
|
361
|
+
return Math.cos(rad);
|
|
362
|
+
}
|
|
363
|
+
function tan(angle, unit = "rad") {
|
|
364
|
+
const rad = unit === "deg" ? toRadians(angle) : angle;
|
|
365
|
+
return Math.tan(rad);
|
|
366
|
+
}
|
|
367
|
+
function asin(value) {
|
|
368
|
+
if (value < -1 || value > 1) {
|
|
369
|
+
throw new RangeError("Input to asin must be between -1 and 1");
|
|
370
|
+
}
|
|
371
|
+
return Math.asin(value);
|
|
372
|
+
}
|
|
373
|
+
function acos(value) {
|
|
374
|
+
if (value < -1 || value > 1) {
|
|
375
|
+
throw new RangeError("Input to acos must be between -1 and 1");
|
|
376
|
+
}
|
|
377
|
+
return Math.acos(value);
|
|
378
|
+
}
|
|
379
|
+
function atan(value) {
|
|
380
|
+
return Math.atan(value);
|
|
381
|
+
}
|
|
382
|
+
function atan2(y, x) {
|
|
383
|
+
return Math.atan2(y, x);
|
|
384
|
+
}
|
|
385
|
+
function hypot(...values) {
|
|
386
|
+
return Math.hypot(...values);
|
|
387
|
+
}
|
|
388
|
+
function median(values) {
|
|
389
|
+
if (values.length === 0) {
|
|
390
|
+
throw new RangeError("Cannot compute median of an empty array");
|
|
391
|
+
}
|
|
392
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
393
|
+
const mid = Math.floor(sorted.length / 2);
|
|
394
|
+
if (sorted.length % 2 === 0) {
|
|
395
|
+
return (sorted[mid - 1] + sorted[mid]) / 2;
|
|
396
|
+
}
|
|
397
|
+
return sorted[mid];
|
|
398
|
+
}
|
|
399
|
+
function mode(values) {
|
|
400
|
+
if (values.length === 0) {
|
|
401
|
+
throw new RangeError("Cannot compute mode of an empty array");
|
|
402
|
+
}
|
|
403
|
+
const freq = /* @__PURE__ */ new Map();
|
|
404
|
+
let maxFreq = 0;
|
|
405
|
+
for (const v of values) {
|
|
406
|
+
const f = (freq.get(v) ?? 0) + 1;
|
|
407
|
+
freq.set(v, f);
|
|
408
|
+
if (f > maxFreq) maxFreq = f;
|
|
409
|
+
}
|
|
410
|
+
if (maxFreq === 1) return [];
|
|
411
|
+
return Array.from(freq.entries()).filter(([, f]) => f === maxFreq).map(([v]) => v).sort((a, b) => a - b);
|
|
412
|
+
}
|
|
413
|
+
function percentile(values, p) {
|
|
414
|
+
if (values.length === 0) {
|
|
415
|
+
throw new RangeError("Cannot compute percentile of an empty array");
|
|
416
|
+
}
|
|
417
|
+
if (p < 0 || p > 100) {
|
|
418
|
+
throw new RangeError("Percentile must be between 0 and 100");
|
|
419
|
+
}
|
|
420
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
421
|
+
const index = p / 100 * (sorted.length - 1);
|
|
422
|
+
const lower = Math.floor(index);
|
|
423
|
+
const upper = Math.ceil(index);
|
|
424
|
+
if (lower === upper) return sorted[lower];
|
|
425
|
+
return sorted[lower] + (sorted[upper] - sorted[lower]) * (index - lower);
|
|
426
|
+
}
|
|
427
|
+
function quartiles(values) {
|
|
428
|
+
if (values.length === 0) {
|
|
429
|
+
throw new RangeError("Cannot compute quartiles of an empty array");
|
|
430
|
+
}
|
|
431
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
432
|
+
if (sorted.length === 1) {
|
|
433
|
+
return { q1: sorted[0], q2: sorted[0], q3: sorted[0] };
|
|
434
|
+
}
|
|
435
|
+
const q2 = median(sorted);
|
|
436
|
+
const mid = Math.floor(sorted.length / 2);
|
|
437
|
+
const lowerHalf = sorted.slice(0, mid);
|
|
438
|
+
const upperHalf = sorted.length % 2 === 0 ? sorted.slice(mid) : sorted.slice(mid + 1);
|
|
439
|
+
return {
|
|
440
|
+
q1: median(lowerHalf),
|
|
441
|
+
q2,
|
|
442
|
+
q3: median(upperHalf)
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function variance(values, sample2 = false) {
|
|
446
|
+
if (values.length === 0) {
|
|
447
|
+
throw new RangeError("Cannot compute variance of an empty array");
|
|
448
|
+
}
|
|
449
|
+
if (sample2 && values.length < 2) {
|
|
450
|
+
throw new RangeError("Sample variance requires at least 2 values");
|
|
451
|
+
}
|
|
452
|
+
const mean = average(values);
|
|
453
|
+
const sqDiffs = values.map((v) => (v - mean) ** 2);
|
|
454
|
+
const divisor = sample2 ? values.length - 1 : values.length;
|
|
455
|
+
return sum(sqDiffs) / divisor;
|
|
456
|
+
}
|
|
457
|
+
function stddev(values, sample2 = false) {
|
|
458
|
+
return Math.sqrt(variance(values, sample2));
|
|
459
|
+
}
|
|
460
|
+
function covariance(x, y) {
|
|
461
|
+
if (x.length !== y.length) {
|
|
462
|
+
throw new RangeError("Arrays must have the same length");
|
|
463
|
+
}
|
|
464
|
+
if (x.length < 2) {
|
|
465
|
+
throw new RangeError("Covariance requires at least 2 values");
|
|
466
|
+
}
|
|
467
|
+
const meanX = average(x);
|
|
468
|
+
const meanY = average(y);
|
|
469
|
+
let total = 0;
|
|
470
|
+
for (let i = 0; i < x.length; i++) {
|
|
471
|
+
total += (x[i] - meanX) * (y[i] - meanY);
|
|
472
|
+
}
|
|
473
|
+
return total / (x.length - 1);
|
|
474
|
+
}
|
|
475
|
+
function correlation(x, y) {
|
|
476
|
+
const cov = covariance(x, y);
|
|
477
|
+
const stdX = stddev(x, true);
|
|
478
|
+
const stdY = stddev(y, true);
|
|
479
|
+
if (stdX === 0 || stdY === 0) return 0;
|
|
480
|
+
return cov / (stdX * stdY);
|
|
481
|
+
}
|
|
482
|
+
function skewness(values) {
|
|
483
|
+
if (values.length < 3) {
|
|
484
|
+
throw new RangeError("Skewness requires at least 3 values");
|
|
485
|
+
}
|
|
486
|
+
const mean = average(values);
|
|
487
|
+
const n = values.length;
|
|
488
|
+
const m2 = sum(values.map((v) => (v - mean) ** 2)) / n;
|
|
489
|
+
const m3 = sum(values.map((v) => (v - mean) ** 3)) / n;
|
|
490
|
+
if (m2 === 0) return 0;
|
|
491
|
+
return m3 / Math.pow(m2, 1.5) * Math.sqrt(n * (n - 1)) / (n - 2);
|
|
492
|
+
}
|
|
493
|
+
function kurtosis(values) {
|
|
494
|
+
if (values.length < 4) {
|
|
495
|
+
throw new RangeError("Kurtosis requires at least 4 values");
|
|
496
|
+
}
|
|
497
|
+
const mean = average(values);
|
|
498
|
+
const n = values.length;
|
|
499
|
+
const m2 = sum(values.map((v) => (v - mean) ** 2)) / n;
|
|
500
|
+
const m4 = sum(values.map((v) => (v - mean) ** 4)) / n;
|
|
501
|
+
if (m2 === 0) return 0;
|
|
502
|
+
const kurt = m4 / (m2 * m2);
|
|
503
|
+
const adjustment = (n - 1) * (n + 1) / ((n - 2) * (n - 3));
|
|
504
|
+
return adjustment * (kurt - 3 * (n - 1) / (n + 1));
|
|
505
|
+
}
|
|
506
|
+
function weightedMean(values, weights) {
|
|
507
|
+
if (values.length !== weights.length) {
|
|
508
|
+
throw new RangeError("Values and weights must have the same length");
|
|
509
|
+
}
|
|
510
|
+
if (values.length === 0) {
|
|
511
|
+
throw new RangeError("Cannot compute weighted mean of empty arrays");
|
|
512
|
+
}
|
|
513
|
+
const totalWeight = sum(weights);
|
|
514
|
+
if (totalWeight === 0) {
|
|
515
|
+
throw new RangeError("Sum of weights cannot be zero");
|
|
516
|
+
}
|
|
517
|
+
let total = 0;
|
|
518
|
+
for (let i = 0; i < values.length; i++) {
|
|
519
|
+
total += values[i] * weights[i];
|
|
520
|
+
}
|
|
521
|
+
return total / totalWeight;
|
|
522
|
+
}
|
|
523
|
+
function geometricMean(values) {
|
|
524
|
+
if (values.length === 0) {
|
|
525
|
+
throw new RangeError("Cannot compute geometric mean of an empty array");
|
|
526
|
+
}
|
|
527
|
+
if (values.some((v) => v <= 0)) {
|
|
528
|
+
throw new RangeError("Geometric mean requires positive values only");
|
|
529
|
+
}
|
|
530
|
+
const logSum = sum(values.map((v) => Math.log(v)));
|
|
531
|
+
return Math.exp(logSum / values.length);
|
|
532
|
+
}
|
|
533
|
+
function harmonicMean(values) {
|
|
534
|
+
if (values.length === 0) {
|
|
535
|
+
throw new RangeError("Cannot compute harmonic mean of an empty array");
|
|
536
|
+
}
|
|
537
|
+
if (values.some((v) => v <= 0)) {
|
|
538
|
+
throw new RangeError("Harmonic mean requires positive values only");
|
|
539
|
+
}
|
|
540
|
+
const reciprocalSum = sum(values.map((v) => 1 / v));
|
|
541
|
+
return values.length / reciprocalSum;
|
|
542
|
+
}
|
|
543
|
+
function zScore(value, mean, stddev2) {
|
|
544
|
+
if (stddev2 === 0) return 0;
|
|
545
|
+
return (value - mean) / stddev2;
|
|
546
|
+
}
|
|
547
|
+
function standardize(values) {
|
|
548
|
+
if (values.length === 0) return [];
|
|
549
|
+
const mean = average(values);
|
|
550
|
+
const sd = stddev(values, true);
|
|
551
|
+
if (sd === 0) return values.map(() => 0);
|
|
552
|
+
return values.map((v) => (v - mean) / sd);
|
|
553
|
+
}
|
|
554
|
+
function factorial(n) {
|
|
555
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
556
|
+
throw new RangeError("Factorial requires a non-negative integer");
|
|
557
|
+
}
|
|
558
|
+
if (n === 0 || n === 1) return 1;
|
|
559
|
+
let result = 1;
|
|
560
|
+
for (let i = 2; i <= n; i++) {
|
|
561
|
+
result *= i;
|
|
562
|
+
}
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
565
|
+
function binomial(n, k) {
|
|
566
|
+
if (!Number.isInteger(n) || !Number.isInteger(k) || n < 0 || k < 0) {
|
|
567
|
+
throw new RangeError("Binomial requires non-negative integers");
|
|
568
|
+
}
|
|
569
|
+
if (k > n) {
|
|
570
|
+
throw new RangeError("k cannot be greater than n");
|
|
571
|
+
}
|
|
572
|
+
if (k === 0 || k === n) return 1;
|
|
573
|
+
if (k > n - k) return binomial(n, n - k);
|
|
574
|
+
let result = 1;
|
|
575
|
+
for (let i = 1; i <= k; i++) {
|
|
576
|
+
result = result * (n - k + i) / i;
|
|
577
|
+
}
|
|
578
|
+
return result;
|
|
579
|
+
}
|
|
580
|
+
function permutation(n, k) {
|
|
581
|
+
if (!Number.isInteger(n) || !Number.isInteger(k) || n < 0 || k < 0) {
|
|
582
|
+
throw new RangeError("Permutation requires non-negative integers");
|
|
583
|
+
}
|
|
584
|
+
if (k > n) {
|
|
585
|
+
throw new RangeError("k cannot be greater than n");
|
|
586
|
+
}
|
|
587
|
+
let result = 1;
|
|
588
|
+
for (let i = n; i > n - k; i--) {
|
|
589
|
+
result *= i;
|
|
590
|
+
}
|
|
591
|
+
return result;
|
|
592
|
+
}
|
|
593
|
+
function gcd(...values) {
|
|
594
|
+
if (values.length < 2) {
|
|
595
|
+
throw new RangeError("GCD requires at least 2 values");
|
|
596
|
+
}
|
|
597
|
+
const gcd2 = (a, b) => {
|
|
598
|
+
a = Math.abs(a);
|
|
599
|
+
b = Math.abs(b);
|
|
600
|
+
while (b !== 0) {
|
|
601
|
+
const t = b;
|
|
602
|
+
b = a % b;
|
|
603
|
+
a = t;
|
|
604
|
+
}
|
|
605
|
+
return a;
|
|
606
|
+
};
|
|
607
|
+
return values.reduce((a, b) => gcd2(a, b));
|
|
608
|
+
}
|
|
609
|
+
function lcm(...values) {
|
|
610
|
+
if (values.length < 2) {
|
|
611
|
+
throw new RangeError("LCM requires at least 2 values");
|
|
612
|
+
}
|
|
613
|
+
const lcm2 = (a, b) => {
|
|
614
|
+
if (a === 0 || b === 0) return 0;
|
|
615
|
+
return Math.abs(a * b) / gcd(a, b);
|
|
616
|
+
};
|
|
617
|
+
return values.reduce((a, b) => lcm2(a, b));
|
|
618
|
+
}
|
|
619
|
+
function isPrime(n) {
|
|
620
|
+
if (!Number.isInteger(n) || n < 2) return false;
|
|
621
|
+
if (n === 2) return true;
|
|
622
|
+
if (n % 2 === 0) return false;
|
|
623
|
+
const sqrt = Math.sqrt(n);
|
|
624
|
+
for (let i = 3; i <= sqrt; i += 2) {
|
|
625
|
+
if (n % i === 0) return false;
|
|
626
|
+
}
|
|
627
|
+
return true;
|
|
628
|
+
}
|
|
629
|
+
function primeFactors(n) {
|
|
630
|
+
if (!Number.isInteger(n) || n < 2) {
|
|
631
|
+
throw new RangeError("Prime factors require an integer greater than 1");
|
|
632
|
+
}
|
|
633
|
+
const factors = [];
|
|
634
|
+
let num = n;
|
|
635
|
+
while (num % 2 === 0) {
|
|
636
|
+
factors.push(2);
|
|
637
|
+
num /= 2;
|
|
638
|
+
}
|
|
639
|
+
for (let i = 3; i <= Math.sqrt(num); i += 2) {
|
|
640
|
+
while (num % i === 0) {
|
|
641
|
+
factors.push(i);
|
|
642
|
+
num /= i;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (num > 1) factors.push(num);
|
|
646
|
+
return factors;
|
|
647
|
+
}
|
|
648
|
+
function fibonacci(n) {
|
|
649
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
650
|
+
throw new RangeError("Fibonacci requires a non-negative integer");
|
|
651
|
+
}
|
|
652
|
+
if (n === 0) return 0;
|
|
653
|
+
if (n === 1) return 1;
|
|
654
|
+
let a = 0;
|
|
655
|
+
let b = 1;
|
|
656
|
+
for (let i = 2; i <= n; i++) {
|
|
657
|
+
const temp = a + b;
|
|
658
|
+
a = b;
|
|
659
|
+
b = temp;
|
|
660
|
+
}
|
|
661
|
+
return b;
|
|
662
|
+
}
|
|
663
|
+
function isEven(n) {
|
|
664
|
+
return n % 2 === 0;
|
|
665
|
+
}
|
|
666
|
+
function isOdd(n) {
|
|
667
|
+
return n % 2 !== 0;
|
|
668
|
+
}
|
|
669
|
+
function sign(n) {
|
|
670
|
+
if (n > 0) return 1;
|
|
671
|
+
if (n < 0) return -1;
|
|
672
|
+
return 0;
|
|
673
|
+
}
|
|
674
|
+
function validateMatrix(a) {
|
|
675
|
+
if (a.length === 0) throw new RangeError("Matrix cannot be empty");
|
|
676
|
+
const cols = a[0].length;
|
|
677
|
+
if (cols === 0) throw new RangeError("Matrix rows cannot be empty");
|
|
678
|
+
if (a.some((row) => row.length !== cols)) {
|
|
679
|
+
throw new RangeError("All rows must have the same length");
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function matrixAdd(a, b) {
|
|
683
|
+
if (a.length !== b.length || a[0].length !== b[0].length) {
|
|
684
|
+
throw new RangeError("Matrices must have same dimensions for addition");
|
|
685
|
+
}
|
|
686
|
+
validateMatrix(a);
|
|
687
|
+
return a.map((row, i) => row.map((val, j) => val + b[i][j]));
|
|
688
|
+
}
|
|
689
|
+
function matrixSub(a, b) {
|
|
690
|
+
if (a.length !== b.length || a[0].length !== b[0].length) {
|
|
691
|
+
throw new RangeError("Matrices must have same dimensions for subtraction");
|
|
692
|
+
}
|
|
693
|
+
validateMatrix(a);
|
|
694
|
+
return a.map((row, i) => row.map((val, j) => val - b[i][j]));
|
|
695
|
+
}
|
|
696
|
+
function matrixMul(a, b) {
|
|
697
|
+
if (a.length !== b.length || a[0].length !== b[0].length) {
|
|
698
|
+
throw new RangeError("Matrices must have same dimensions for element-wise multiplication");
|
|
699
|
+
}
|
|
700
|
+
validateMatrix(a);
|
|
701
|
+
return a.map((row, i) => row.map((val, j) => val * b[i][j]));
|
|
702
|
+
}
|
|
703
|
+
function matrixScale(a, scalar) {
|
|
704
|
+
validateMatrix(a);
|
|
705
|
+
return a.map((row) => row.map((val) => val * scalar));
|
|
706
|
+
}
|
|
707
|
+
function matrixTranspose(a) {
|
|
708
|
+
if (a.length === 0) return [];
|
|
709
|
+
validateMatrix(a);
|
|
710
|
+
return a[0].map((_, j) => a.map((row) => row[j]));
|
|
711
|
+
}
|
|
712
|
+
function matrixDeterminant(a) {
|
|
713
|
+
validateMatrix(a);
|
|
714
|
+
const n = a.length;
|
|
715
|
+
if (a.some((row) => row.length !== n)) {
|
|
716
|
+
throw new RangeError("Matrix must be square for determinant");
|
|
717
|
+
}
|
|
718
|
+
if (n > 3) {
|
|
719
|
+
throw new RangeError("Determinant only supported for max 3x3 matrices");
|
|
720
|
+
}
|
|
721
|
+
if (n === 1) return a[0][0];
|
|
722
|
+
if (n === 2) return a[0][0] * a[1][1] - a[0][1] * a[1][0];
|
|
723
|
+
const [a00, a01, a02] = a[0];
|
|
724
|
+
const [a10, a11, a12] = a[1];
|
|
725
|
+
const [a20, a21, a22] = a[2];
|
|
726
|
+
return a00 * (a11 * a22 - a12 * a21) - a01 * (a10 * a22 - a12 * a20) + a02 * (a10 * a21 - a11 * a20);
|
|
727
|
+
}
|
|
728
|
+
function matrixInverse(a) {
|
|
729
|
+
const det = matrixDeterminant(a);
|
|
730
|
+
if (det === 0) throw new RangeError("Matrix is singular and cannot be inverted");
|
|
731
|
+
const n = a.length;
|
|
732
|
+
if (n === 1) return [[1 / a[0][0]]];
|
|
733
|
+
if (n === 2) {
|
|
734
|
+
return [
|
|
735
|
+
[a[1][1] / det, -a[0][1] / det],
|
|
736
|
+
[-a[1][0] / det, a[0][0] / det]
|
|
737
|
+
];
|
|
738
|
+
}
|
|
739
|
+
const [a00, a01, a02] = a[0];
|
|
740
|
+
const [a10, a11, a12] = a[1];
|
|
741
|
+
const [a20, a21, a22] = a[2];
|
|
742
|
+
return [
|
|
743
|
+
[(a11 * a22 - a12 * a21) / det, -(a01 * a22 - a02 * a21) / det, (a01 * a12 - a02 * a11) / det],
|
|
744
|
+
[-(a10 * a22 - a12 * a20) / det, (a00 * a22 - a02 * a20) / det, -(a00 * a12 - a02 * a10) / det],
|
|
745
|
+
[(a10 * a21 - a11 * a20) / det, -(a00 * a21 - a01 * a20) / det, (a00 * a11 - a01 * a10) / det]
|
|
746
|
+
];
|
|
747
|
+
}
|
|
748
|
+
function matrixIdentity(n) {
|
|
749
|
+
if (!Number.isInteger(n) || n < 1) {
|
|
750
|
+
throw new RangeError("Matrix size must be a positive integer");
|
|
751
|
+
}
|
|
752
|
+
return Array.from(
|
|
753
|
+
{ length: n },
|
|
754
|
+
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
function matrixMultiply(a, b) {
|
|
758
|
+
validateMatrix(a);
|
|
759
|
+
validateMatrix(b);
|
|
760
|
+
if (a[0].length !== b.length) {
|
|
761
|
+
throw new RangeError("Matrix dimensions incompatible for multiplication");
|
|
762
|
+
}
|
|
763
|
+
const result = Array.from(
|
|
764
|
+
{ length: a.length },
|
|
765
|
+
() => Array(b[0].length).fill(0)
|
|
766
|
+
);
|
|
767
|
+
for (let i = 0; i < a.length; i++) {
|
|
768
|
+
for (let j = 0; j < b[0].length; j++) {
|
|
769
|
+
for (let k = 0; k < b.length; k++) {
|
|
770
|
+
result[i][j] = (result[i][j] ?? 0) + (a[i][k] ?? 0) * (b[k][j] ?? 0);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return result;
|
|
775
|
+
}
|
|
776
|
+
function matrixTrace(a) {
|
|
777
|
+
validateMatrix(a);
|
|
778
|
+
const n = a.length;
|
|
779
|
+
if (a.some((row) => row.length !== n)) {
|
|
780
|
+
throw new RangeError("Matrix must be square for trace");
|
|
781
|
+
}
|
|
782
|
+
let trace = 0;
|
|
783
|
+
for (let i = 0; i < n; i++) {
|
|
784
|
+
trace += a[i][i];
|
|
785
|
+
}
|
|
786
|
+
return trace;
|
|
787
|
+
}
|
|
788
|
+
var Complex = class _Complex {
|
|
789
|
+
constructor(re, im = 0) {
|
|
790
|
+
this.re = re;
|
|
791
|
+
this.im = im;
|
|
792
|
+
}
|
|
793
|
+
re;
|
|
794
|
+
im;
|
|
795
|
+
/**
|
|
796
|
+
* Adds another complex number.
|
|
797
|
+
*
|
|
798
|
+
* @param other - The complex number to add.
|
|
799
|
+
* @returns A new Complex instance.
|
|
800
|
+
*/
|
|
801
|
+
add(other) {
|
|
802
|
+
return new _Complex(this.re + other.re, this.im + other.im);
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Subtracts another complex number.
|
|
806
|
+
*
|
|
807
|
+
* @param other - The complex number to subtract.
|
|
808
|
+
* @returns A new Complex instance.
|
|
809
|
+
*/
|
|
810
|
+
sub(other) {
|
|
811
|
+
return new _Complex(this.re - other.re, this.im - other.im);
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Multiplies by another complex number.
|
|
815
|
+
*
|
|
816
|
+
* @param other - The complex number to multiply by.
|
|
817
|
+
* @returns A new Complex instance.
|
|
818
|
+
*/
|
|
819
|
+
mul(other) {
|
|
820
|
+
return new _Complex(
|
|
821
|
+
this.re * other.re - this.im * other.im,
|
|
822
|
+
this.re * other.im + this.im * other.re
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Divides by another complex number.
|
|
827
|
+
*
|
|
828
|
+
* @param other - The complex number to divide by.
|
|
829
|
+
* @returns A new Complex instance.
|
|
830
|
+
* @throws {DivisionByZeroError} If the divisor is zero.
|
|
831
|
+
*/
|
|
832
|
+
div(other) {
|
|
833
|
+
const denom = other.re * other.re + other.im * other.im;
|
|
834
|
+
if (denom === 0) throw new DivisionByZeroError();
|
|
835
|
+
return new _Complex(
|
|
836
|
+
(this.re * other.re + this.im * other.im) / denom,
|
|
837
|
+
(this.im * other.re - this.re * other.im) / denom
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Computes the magnitude (absolute value) of the complex number.
|
|
842
|
+
*
|
|
843
|
+
* @returns The magnitude.
|
|
844
|
+
*/
|
|
845
|
+
abs() {
|
|
846
|
+
return Math.sqrt(this.re * this.re + this.im * this.im);
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Computes the complex conjugate.
|
|
850
|
+
*
|
|
851
|
+
* @returns A new Complex instance with negated imaginary part.
|
|
852
|
+
*/
|
|
853
|
+
conj() {
|
|
854
|
+
return new _Complex(this.re, this.im === 0 ? 0 : -this.im);
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Computes the argument (phase angle) in radians.
|
|
858
|
+
*
|
|
859
|
+
* @returns The phase angle.
|
|
860
|
+
*/
|
|
861
|
+
arg() {
|
|
862
|
+
return Math.atan2(this.im, this.re);
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Raises the complex number to a real power.
|
|
866
|
+
*
|
|
867
|
+
* @param n - The exponent.
|
|
868
|
+
* @returns A new Complex instance.
|
|
869
|
+
*/
|
|
870
|
+
pow(n) {
|
|
871
|
+
const r = this.abs();
|
|
872
|
+
const theta = this.arg();
|
|
873
|
+
return _Complex.fromPolar(Math.pow(r, n), theta * n);
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Computes the square root of the complex number.
|
|
877
|
+
*
|
|
878
|
+
* @returns A new Complex instance.
|
|
879
|
+
*/
|
|
880
|
+
sqrt() {
|
|
881
|
+
const r = this.abs();
|
|
882
|
+
const theta = this.arg();
|
|
883
|
+
return _Complex.fromPolar(Math.sqrt(r), theta / 2);
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Computes e raised to this complex number.
|
|
887
|
+
*
|
|
888
|
+
* @returns A new Complex instance.
|
|
889
|
+
*/
|
|
890
|
+
exp() {
|
|
891
|
+
return new _Complex(
|
|
892
|
+
Math.exp(this.re) * Math.cos(this.im),
|
|
893
|
+
Math.exp(this.re) * Math.sin(this.im)
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Computes the natural logarithm of the complex number.
|
|
898
|
+
*
|
|
899
|
+
* @returns A new Complex instance.
|
|
900
|
+
*/
|
|
901
|
+
log() {
|
|
902
|
+
return new _Complex(Math.log(this.abs()), this.arg());
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Computes the sine of the complex number.
|
|
906
|
+
*
|
|
907
|
+
* @returns A new Complex instance.
|
|
908
|
+
*/
|
|
909
|
+
sin() {
|
|
910
|
+
return new _Complex(
|
|
911
|
+
Math.sin(this.re) * Math.cosh(this.im),
|
|
912
|
+
Math.cos(this.re) * Math.sinh(this.im)
|
|
913
|
+
);
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Computes the cosine of the complex number.
|
|
917
|
+
*
|
|
918
|
+
* @returns A new Complex instance.
|
|
919
|
+
*/
|
|
920
|
+
cos() {
|
|
921
|
+
return new _Complex(
|
|
922
|
+
Math.cos(this.re) * Math.cosh(this.im),
|
|
923
|
+
-Math.sin(this.re) * Math.sinh(this.im)
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Returns a string representation of the complex number.
|
|
928
|
+
*
|
|
929
|
+
* @returns String like "a + bi".
|
|
930
|
+
*/
|
|
931
|
+
toString() {
|
|
932
|
+
if (this.im === 0) return `${this.re}`;
|
|
933
|
+
if (this.re === 0) return `${this.im}i`;
|
|
934
|
+
if (this.im < 0) return `${this.re} - ${Math.abs(this.im)}i`;
|
|
935
|
+
return `${this.re} + ${this.im}i`;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Creates a complex number from polar coordinates.
|
|
939
|
+
*
|
|
940
|
+
* @param r - The magnitude.
|
|
941
|
+
* @param theta - The angle in radians.
|
|
942
|
+
* @returns A new Complex instance.
|
|
943
|
+
*/
|
|
944
|
+
static fromPolar(r, theta) {
|
|
945
|
+
return new _Complex(r * Math.cos(theta), r * Math.sin(theta));
|
|
946
|
+
}
|
|
947
|
+
/** The real part. */
|
|
948
|
+
get real() {
|
|
949
|
+
return this.re;
|
|
950
|
+
}
|
|
951
|
+
/** The imaginary part. */
|
|
952
|
+
get imag() {
|
|
953
|
+
return this.im;
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
function compoundInterest(principal, rate, time, n = 12) {
|
|
957
|
+
return principal * Math.pow(1 + rate / n, n * time);
|
|
958
|
+
}
|
|
959
|
+
function futureValue(pv, rate, nper) {
|
|
960
|
+
return pv * Math.pow(1 + rate, nper);
|
|
961
|
+
}
|
|
962
|
+
function presentValue(fv, rate, nper) {
|
|
963
|
+
return fv / Math.pow(1 + rate, nper);
|
|
964
|
+
}
|
|
965
|
+
function pmt(rate, nper, pv, fv = 0) {
|
|
966
|
+
if (rate === 0) return -(pv + fv) / nper;
|
|
967
|
+
const pvif = Math.pow(1 + rate, nper);
|
|
968
|
+
return -(pv * pvif + fv) / ((pvif - 1) / rate);
|
|
969
|
+
}
|
|
970
|
+
function npv(rate, cashflows) {
|
|
971
|
+
if (cashflows.length === 0) return 0;
|
|
972
|
+
let result = 0;
|
|
973
|
+
for (let i = 0; i < cashflows.length; i++) {
|
|
974
|
+
result += cashflows[i] / Math.pow(1 + rate, i);
|
|
975
|
+
}
|
|
976
|
+
return result;
|
|
977
|
+
}
|
|
978
|
+
function irr(cashflows) {
|
|
979
|
+
if (cashflows.length < 2) {
|
|
980
|
+
throw new RangeError("IRR requires at least 2 cashflows");
|
|
981
|
+
}
|
|
982
|
+
let guess = 0.1;
|
|
983
|
+
const tolerance = 1e-7;
|
|
984
|
+
const maxIter = 1e3;
|
|
985
|
+
for (let i = 0; i < maxIter; i++) {
|
|
986
|
+
let npvVal = 0;
|
|
987
|
+
let dnpv = 0;
|
|
988
|
+
for (let j = 0; j < cashflows.length; j++) {
|
|
989
|
+
npvVal += cashflows[j] / Math.pow(1 + guess, j);
|
|
990
|
+
dnpv -= j * cashflows[j] / Math.pow(1 + guess, j + 1);
|
|
991
|
+
}
|
|
992
|
+
if (Math.abs(npvVal) < tolerance) return guess;
|
|
993
|
+
if (dnpv === 0) return NaN;
|
|
994
|
+
guess -= npvVal / dnpv;
|
|
995
|
+
}
|
|
996
|
+
return NaN;
|
|
997
|
+
}
|
|
998
|
+
function roi(gain, cost) {
|
|
999
|
+
if (cost === 0) throw new RangeError("Cost cannot be zero");
|
|
1000
|
+
return (gain - cost) / cost;
|
|
1001
|
+
}
|
|
1002
|
+
function amortizationSchedule(principal, rate, nper) {
|
|
1003
|
+
const payment = -pmt(rate, nper, principal);
|
|
1004
|
+
let balance = principal;
|
|
1005
|
+
const schedule = [];
|
|
1006
|
+
for (let period = 1; period <= nper; period++) {
|
|
1007
|
+
const interest = balance * rate;
|
|
1008
|
+
const principalPart = payment - interest;
|
|
1009
|
+
balance -= principalPart;
|
|
1010
|
+
schedule.push({
|
|
1011
|
+
period,
|
|
1012
|
+
payment: Math.round(payment * 100) / 100,
|
|
1013
|
+
interest: Math.round(interest * 100) / 100,
|
|
1014
|
+
principal: Math.round(principalPart * 100) / 100,
|
|
1015
|
+
balance: Math.round(Math.max(balance, 0) * 100) / 100
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
return schedule;
|
|
1019
|
+
}
|
|
1020
|
+
function randomNormal(mean = 0, stddev2 = 1) {
|
|
1021
|
+
let u = 0;
|
|
1022
|
+
let v = 0;
|
|
1023
|
+
while (u === 0) u = Math.random();
|
|
1024
|
+
while (v === 0) v = Math.random();
|
|
1025
|
+
const z = Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
|
|
1026
|
+
return mean + z * stddev2;
|
|
1027
|
+
}
|
|
1028
|
+
function randomPoisson(lambda) {
|
|
1029
|
+
if (lambda <= 0) throw new RangeError("Lambda must be positive");
|
|
1030
|
+
const L = Math.exp(-lambda);
|
|
1031
|
+
let k = 0;
|
|
1032
|
+
let p = 1;
|
|
1033
|
+
do {
|
|
1034
|
+
k++;
|
|
1035
|
+
p *= Math.random();
|
|
1036
|
+
} while (p > L);
|
|
1037
|
+
return k - 1;
|
|
1038
|
+
}
|
|
1039
|
+
function randomBinomial(n, p) {
|
|
1040
|
+
if (!Number.isInteger(n) || n < 0) throw new RangeError("n must be a non-negative integer");
|
|
1041
|
+
if (p < 0 || p > 1) throw new RangeError("p must be between 0 and 1");
|
|
1042
|
+
let successes = 0;
|
|
1043
|
+
for (let i = 0; i < n; i++) {
|
|
1044
|
+
if (Math.random() < p) successes++;
|
|
1045
|
+
}
|
|
1046
|
+
return successes;
|
|
1047
|
+
}
|
|
1048
|
+
function randomExponential(lambda) {
|
|
1049
|
+
if (lambda <= 0) throw new RangeError("Lambda must be positive");
|
|
1050
|
+
return -Math.log(1 - Math.random()) / lambda;
|
|
1051
|
+
}
|
|
1052
|
+
function randomUniform(min = 0, max = 1) {
|
|
1053
|
+
return Math.random() * (max - min) + min;
|
|
1054
|
+
}
|
|
1055
|
+
function derivative(f, x, h = 1e-10) {
|
|
1056
|
+
return (f(x + h) - f(x - h)) / (2 * h);
|
|
1057
|
+
}
|
|
1058
|
+
function integral(f, a, b, n = 100) {
|
|
1059
|
+
if (n % 2 !== 0) {
|
|
1060
|
+
throw new RangeError("Number of subintervals must be even for Simpson's rule");
|
|
1061
|
+
}
|
|
1062
|
+
const h = (b - a) / n;
|
|
1063
|
+
let sum2 = f(a) + f(b);
|
|
1064
|
+
for (let i = 1; i < n; i++) {
|
|
1065
|
+
const x = a + i * h;
|
|
1066
|
+
sum2 += i % 2 === 0 ? 2 * f(x) : 4 * f(x);
|
|
1067
|
+
}
|
|
1068
|
+
return h / 3 * sum2;
|
|
1069
|
+
}
|
|
1070
|
+
function trapezoidal(f, a, b, n = 100) {
|
|
1071
|
+
const h = (b - a) / n;
|
|
1072
|
+
let sum2 = (f(a) + f(b)) / 2;
|
|
1073
|
+
for (let i = 1; i < n; i++) {
|
|
1074
|
+
sum2 += f(a + i * h);
|
|
1075
|
+
}
|
|
1076
|
+
return h * sum2;
|
|
1077
|
+
}
|
|
1078
|
+
function newtonRoot(f, guess, tolerance = 1e-7, maxIter = 100) {
|
|
1079
|
+
let x = guess;
|
|
1080
|
+
for (let i = 0; i < maxIter; i++) {
|
|
1081
|
+
const fx = f(x);
|
|
1082
|
+
if (Math.abs(fx) < tolerance) return x;
|
|
1083
|
+
const df = derivative(f, x);
|
|
1084
|
+
if (df === 0) return NaN;
|
|
1085
|
+
x -= fx / df;
|
|
1086
|
+
}
|
|
1087
|
+
return NaN;
|
|
1088
|
+
}
|
|
1089
|
+
function lerp(a, b, t) {
|
|
1090
|
+
return a + (b - a) * t;
|
|
1091
|
+
}
|
|
1092
|
+
function mapRange(value, inMin, inMax, outMin, outMax) {
|
|
1093
|
+
if (inMax === inMin) throw new RangeError("Input range cannot be zero");
|
|
1094
|
+
return (value - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
|
|
1095
|
+
}
|
|
349
1096
|
|
|
350
1097
|
// src/date/index.ts
|
|
351
1098
|
var InvalidDateError = class extends Error {
|
|
@@ -1366,21 +2113,28 @@ function getType2(value) {
|
|
|
1366
2113
|
return "object";
|
|
1367
2114
|
}
|
|
1368
2115
|
export {
|
|
2116
|
+
Complex,
|
|
1369
2117
|
DivisionByZeroError,
|
|
1370
2118
|
InvalidDateError,
|
|
2119
|
+
acos,
|
|
1371
2120
|
add,
|
|
1372
2121
|
addBusinessDays,
|
|
1373
2122
|
addDays,
|
|
1374
2123
|
addMonths,
|
|
1375
2124
|
addYears,
|
|
1376
2125
|
allSettledMap,
|
|
2126
|
+
amortizationSchedule,
|
|
1377
2127
|
approxEqual,
|
|
2128
|
+
asin,
|
|
1378
2129
|
assertDefined,
|
|
1379
2130
|
assertType,
|
|
2131
|
+
atan,
|
|
2132
|
+
atan2,
|
|
1380
2133
|
average,
|
|
1381
2134
|
base64Decode,
|
|
1382
2135
|
base64Encode,
|
|
1383
2136
|
basename,
|
|
2137
|
+
binomial,
|
|
1384
2138
|
calculateAge,
|
|
1385
2139
|
camelCase,
|
|
1386
2140
|
capitalize,
|
|
@@ -1389,13 +2143,18 @@ export {
|
|
|
1389
2143
|
checksum,
|
|
1390
2144
|
chunk,
|
|
1391
2145
|
clamp,
|
|
2146
|
+
compoundInterest,
|
|
1392
2147
|
constantTimeEqual,
|
|
2148
|
+
correlation,
|
|
2149
|
+
cos,
|
|
1393
2150
|
countOccurrences,
|
|
2151
|
+
covariance,
|
|
1394
2152
|
dateDiff,
|
|
1395
2153
|
debounce,
|
|
1396
2154
|
deepClone,
|
|
1397
2155
|
deepMerge,
|
|
1398
2156
|
deferred,
|
|
2157
|
+
derivative,
|
|
1399
2158
|
dirname,
|
|
1400
2159
|
div,
|
|
1401
2160
|
endOfDay,
|
|
@@ -1407,18 +2166,27 @@ export {
|
|
|
1407
2166
|
envInt,
|
|
1408
2167
|
escapeHtml,
|
|
1409
2168
|
extname,
|
|
2169
|
+
factorial,
|
|
2170
|
+
fibonacci,
|
|
1410
2171
|
first,
|
|
1411
2172
|
flatten,
|
|
1412
2173
|
floor,
|
|
1413
2174
|
format,
|
|
1414
2175
|
formatDate,
|
|
2176
|
+
futureValue,
|
|
2177
|
+
gcd,
|
|
1415
2178
|
generateOTP,
|
|
1416
2179
|
generateToken,
|
|
2180
|
+
geometricMean,
|
|
1417
2181
|
getType2 as getType,
|
|
1418
2182
|
groupBy,
|
|
2183
|
+
harmonicMean,
|
|
1419
2184
|
hash,
|
|
2185
|
+
hypot,
|
|
1420
2186
|
identity,
|
|
1421
2187
|
inRange,
|
|
2188
|
+
integral,
|
|
2189
|
+
irr,
|
|
1422
2190
|
isAbsolute,
|
|
1423
2191
|
isAfter,
|
|
1424
2192
|
isArray,
|
|
@@ -1428,6 +2196,7 @@ export {
|
|
|
1428
2196
|
isBusinessDay,
|
|
1429
2197
|
isDate,
|
|
1430
2198
|
isEmpty,
|
|
2199
|
+
isEven,
|
|
1431
2200
|
isFunction,
|
|
1432
2201
|
isLeapYear,
|
|
1433
2202
|
isMap,
|
|
@@ -1435,6 +2204,8 @@ export {
|
|
|
1435
2204
|
isNull,
|
|
1436
2205
|
isNumber,
|
|
1437
2206
|
isObject,
|
|
2207
|
+
isOdd,
|
|
2208
|
+
isPrime,
|
|
1438
2209
|
isPromise,
|
|
1439
2210
|
isRegExp,
|
|
1440
2211
|
isSet,
|
|
@@ -1444,12 +2215,30 @@ export {
|
|
|
1444
2215
|
join,
|
|
1445
2216
|
kebabCase,
|
|
1446
2217
|
keyBy,
|
|
2218
|
+
kurtosis,
|
|
1447
2219
|
last,
|
|
2220
|
+
lcm,
|
|
2221
|
+
lerp,
|
|
2222
|
+
mapRange,
|
|
2223
|
+
matrixAdd,
|
|
2224
|
+
matrixDeterminant,
|
|
2225
|
+
matrixIdentity,
|
|
2226
|
+
matrixInverse,
|
|
2227
|
+
matrixMul,
|
|
2228
|
+
matrixMultiply,
|
|
2229
|
+
matrixScale,
|
|
2230
|
+
matrixSub,
|
|
2231
|
+
matrixTrace,
|
|
2232
|
+
matrixTranspose,
|
|
2233
|
+
median,
|
|
1448
2234
|
memoize,
|
|
2235
|
+
mode,
|
|
1449
2236
|
mul,
|
|
1450
2237
|
nanoid,
|
|
2238
|
+
newtonRoot,
|
|
1451
2239
|
noop,
|
|
1452
2240
|
normalize,
|
|
2241
|
+
npv,
|
|
1453
2242
|
omit,
|
|
1454
2243
|
once,
|
|
1455
2244
|
orderBy,
|
|
@@ -1461,36 +2250,57 @@ export {
|
|
|
1461
2250
|
parseCsv,
|
|
1462
2251
|
parseDate,
|
|
1463
2252
|
pascalCase,
|
|
2253
|
+
percentile,
|
|
2254
|
+
permutation,
|
|
1464
2255
|
pick,
|
|
1465
2256
|
pipeline,
|
|
1466
2257
|
pluck,
|
|
2258
|
+
pmt,
|
|
2259
|
+
presentValue,
|
|
2260
|
+
primeFactors,
|
|
2261
|
+
quartiles,
|
|
1467
2262
|
raceWithTimeout,
|
|
2263
|
+
randomBinomial,
|
|
2264
|
+
randomExponential,
|
|
1468
2265
|
randomHex,
|
|
1469
2266
|
randomInt,
|
|
2267
|
+
randomNormal,
|
|
2268
|
+
randomPoisson,
|
|
2269
|
+
randomUniform,
|
|
1470
2270
|
relative,
|
|
1471
2271
|
resolve,
|
|
1472
2272
|
retry,
|
|
1473
2273
|
retryAsync,
|
|
1474
2274
|
reverse,
|
|
2275
|
+
roi,
|
|
1475
2276
|
round,
|
|
1476
2277
|
safeJsonParse,
|
|
1477
2278
|
sample,
|
|
1478
2279
|
sampleSize,
|
|
1479
2280
|
shuffle,
|
|
2281
|
+
sign,
|
|
1480
2282
|
simpleHash,
|
|
2283
|
+
sin,
|
|
2284
|
+
skewness,
|
|
1481
2285
|
sleep,
|
|
1482
2286
|
slugify,
|
|
1483
2287
|
snakeCase,
|
|
1484
2288
|
sortBy,
|
|
2289
|
+
standardize,
|
|
1485
2290
|
startOfDay,
|
|
1486
2291
|
startOfMonth,
|
|
1487
2292
|
startOfYear,
|
|
2293
|
+
stddev,
|
|
1488
2294
|
stringifyCsv,
|
|
1489
2295
|
sub,
|
|
1490
2296
|
sum,
|
|
2297
|
+
tan,
|
|
1491
2298
|
template,
|
|
1492
2299
|
throttle,
|
|
1493
2300
|
timeout,
|
|
2301
|
+
toDegrees,
|
|
2302
|
+
toRadians,
|
|
2303
|
+
trapezoidal,
|
|
1494
2304
|
trim,
|
|
1495
2305
|
trimEnd,
|
|
1496
2306
|
trimStart,
|
|
@@ -1499,7 +2309,10 @@ export {
|
|
|
1499
2309
|
uniq,
|
|
1500
2310
|
uniqueBy,
|
|
1501
2311
|
uuid,
|
|
2312
|
+
variance,
|
|
2313
|
+
weightedMean,
|
|
1502
2314
|
words,
|
|
1503
|
-
xorCipher
|
|
2315
|
+
xorCipher,
|
|
2316
|
+
zScore
|
|
1504
2317
|
};
|
|
1505
2318
|
//# sourceMappingURL=index.js.map
|