ml-matrix 6.12.1 → 6.12.2
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/matrix.js +123 -16
- package/matrix.js.map +1 -0
- package/matrix.umd.js +2 -1
- package/matrix.umd.js.map +1 -0
- package/package.json +16 -14
package/matrix.js
CHANGED
|
@@ -2,8 +2,114 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
6
|
+
const toString = Object.prototype.toString;
|
|
7
|
+
/**
|
|
8
|
+
* Checks if an object is an instance of an Array (array or typed array, except those that contain bigint values).
|
|
9
|
+
* @param value - Object to check.
|
|
10
|
+
* @returns True if the object is an array or a typed array.
|
|
11
|
+
*/
|
|
12
|
+
function isAnyArray(value) {
|
|
13
|
+
const tag = toString.call(value);
|
|
14
|
+
return tag.endsWith('Array]') && !tag.includes('Big');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Computes the maximum of the given values.
|
|
19
|
+
*
|
|
20
|
+
* @param input
|
|
21
|
+
* @param options
|
|
22
|
+
*/
|
|
23
|
+
function max(input, options = {}) {
|
|
24
|
+
if (!isAnyArray(input)) {
|
|
25
|
+
throw new TypeError('input must be an array');
|
|
26
|
+
}
|
|
27
|
+
if (input.length === 0) {
|
|
28
|
+
throw new TypeError('input must not be empty');
|
|
29
|
+
}
|
|
30
|
+
const { fromIndex = 0, toIndex = input.length } = options;
|
|
31
|
+
if (fromIndex < 0 ||
|
|
32
|
+
fromIndex >= input.length ||
|
|
33
|
+
!Number.isInteger(fromIndex)) {
|
|
34
|
+
throw new Error('fromIndex must be a positive integer smaller than length');
|
|
35
|
+
}
|
|
36
|
+
if (toIndex <= fromIndex ||
|
|
37
|
+
toIndex > input.length ||
|
|
38
|
+
!Number.isInteger(toIndex)) {
|
|
39
|
+
throw new Error('toIndex must be an integer greater than fromIndex and at most equal to length');
|
|
40
|
+
}
|
|
41
|
+
let maxValue = input[fromIndex];
|
|
42
|
+
for (let i = fromIndex + 1; i < toIndex; i++) {
|
|
43
|
+
if (input[i] > maxValue)
|
|
44
|
+
maxValue = input[i];
|
|
45
|
+
}
|
|
46
|
+
return maxValue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Computes the minimum of the given values.
|
|
51
|
+
*/
|
|
52
|
+
function min(input, options = {}) {
|
|
53
|
+
if (!isAnyArray(input)) {
|
|
54
|
+
throw new TypeError('input must be an array');
|
|
55
|
+
}
|
|
56
|
+
if (input.length === 0) {
|
|
57
|
+
throw new TypeError('input must not be empty');
|
|
58
|
+
}
|
|
59
|
+
const { fromIndex = 0, toIndex = input.length } = options;
|
|
60
|
+
if (fromIndex < 0 ||
|
|
61
|
+
fromIndex >= input.length ||
|
|
62
|
+
!Number.isInteger(fromIndex)) {
|
|
63
|
+
throw new Error('fromIndex must be a positive integer smaller than length');
|
|
64
|
+
}
|
|
65
|
+
if (toIndex <= fromIndex ||
|
|
66
|
+
toIndex > input.length ||
|
|
67
|
+
!Number.isInteger(toIndex)) {
|
|
68
|
+
throw new Error('toIndex must be an integer greater than fromIndex and at most equal to length');
|
|
69
|
+
}
|
|
70
|
+
let minValue = input[fromIndex];
|
|
71
|
+
for (let i = fromIndex + 1; i < toIndex; i++) {
|
|
72
|
+
if (input[i] < minValue)
|
|
73
|
+
minValue = input[i];
|
|
74
|
+
}
|
|
75
|
+
return minValue;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Rescale an array into a range.
|
|
80
|
+
*/
|
|
81
|
+
function rescale(input, options = {}) {
|
|
82
|
+
if (!isAnyArray(input)) {
|
|
83
|
+
throw new TypeError('input must be an array');
|
|
84
|
+
}
|
|
85
|
+
else if (input.length === 0) {
|
|
86
|
+
throw new TypeError('input must not be empty');
|
|
87
|
+
}
|
|
88
|
+
let output;
|
|
89
|
+
if (options.output !== undefined) {
|
|
90
|
+
if (!isAnyArray(options.output)) {
|
|
91
|
+
throw new TypeError('output option must be an array if specified');
|
|
92
|
+
}
|
|
93
|
+
output = options.output;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
output = new Array(input.length);
|
|
97
|
+
}
|
|
98
|
+
const currentMin = min(input);
|
|
99
|
+
const currentMax = max(input);
|
|
100
|
+
if (currentMin === currentMax) {
|
|
101
|
+
throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array');
|
|
102
|
+
}
|
|
103
|
+
const { min: minValue = options.autoMinMax ? currentMin : 0, max: maxValue = options.autoMinMax ? currentMax : 1, } = options;
|
|
104
|
+
if (minValue >= maxValue) {
|
|
105
|
+
throw new RangeError('min option must be smaller than max option');
|
|
106
|
+
}
|
|
107
|
+
const factor = (maxValue - minValue) / (currentMax - currentMin);
|
|
108
|
+
for (let i = 0; i < input.length; i++) {
|
|
109
|
+
output[i] = (input[i] - currentMin) * factor + minValue;
|
|
110
|
+
}
|
|
111
|
+
return output;
|
|
112
|
+
}
|
|
7
113
|
|
|
8
114
|
const indent = ' '.repeat(2);
|
|
9
115
|
const indentData = ' '.repeat(4);
|
|
@@ -992,7 +1098,7 @@ function checkColumnVector(matrix, vector) {
|
|
|
992
1098
|
}
|
|
993
1099
|
|
|
994
1100
|
function checkRowIndices(matrix, rowIndices) {
|
|
995
|
-
if (!isAnyArray
|
|
1101
|
+
if (!isAnyArray(rowIndices)) {
|
|
996
1102
|
throw new TypeError('row indices must be an array');
|
|
997
1103
|
}
|
|
998
1104
|
|
|
@@ -1004,7 +1110,7 @@ function checkRowIndices(matrix, rowIndices) {
|
|
|
1004
1110
|
}
|
|
1005
1111
|
|
|
1006
1112
|
function checkColumnIndices(matrix, columnIndices) {
|
|
1007
|
-
if (!isAnyArray
|
|
1113
|
+
if (!isAnyArray(columnIndices)) {
|
|
1008
1114
|
throw new TypeError('column indices must be an array');
|
|
1009
1115
|
}
|
|
1010
1116
|
|
|
@@ -2659,13 +2765,13 @@ class AbstractMatrix {
|
|
|
2659
2765
|
}
|
|
2660
2766
|
switch (by) {
|
|
2661
2767
|
case 'row': {
|
|
2662
|
-
if (!isAnyArray
|
|
2768
|
+
if (!isAnyArray(mean)) {
|
|
2663
2769
|
throw new TypeError('mean must be an array');
|
|
2664
2770
|
}
|
|
2665
2771
|
return varianceByRow(this, unbiased, mean);
|
|
2666
2772
|
}
|
|
2667
2773
|
case 'column': {
|
|
2668
|
-
if (!isAnyArray
|
|
2774
|
+
if (!isAnyArray(mean)) {
|
|
2669
2775
|
throw new TypeError('mean must be an array');
|
|
2670
2776
|
}
|
|
2671
2777
|
return varianceByColumn(this, unbiased, mean);
|
|
@@ -2708,14 +2814,14 @@ class AbstractMatrix {
|
|
|
2708
2814
|
const { center = this.mean(by) } = options;
|
|
2709
2815
|
switch (by) {
|
|
2710
2816
|
case 'row': {
|
|
2711
|
-
if (!isAnyArray
|
|
2817
|
+
if (!isAnyArray(center)) {
|
|
2712
2818
|
throw new TypeError('center must be an array');
|
|
2713
2819
|
}
|
|
2714
2820
|
centerByRow(this, center);
|
|
2715
2821
|
return this;
|
|
2716
2822
|
}
|
|
2717
2823
|
case 'column': {
|
|
2718
|
-
if (!isAnyArray
|
|
2824
|
+
if (!isAnyArray(center)) {
|
|
2719
2825
|
throw new TypeError('center must be an array');
|
|
2720
2826
|
}
|
|
2721
2827
|
centerByColumn(this, center);
|
|
@@ -2746,7 +2852,7 @@ class AbstractMatrix {
|
|
|
2746
2852
|
case 'row': {
|
|
2747
2853
|
if (scale === undefined) {
|
|
2748
2854
|
scale = getScaleByRow(this);
|
|
2749
|
-
} else if (!isAnyArray
|
|
2855
|
+
} else if (!isAnyArray(scale)) {
|
|
2750
2856
|
throw new TypeError('scale must be an array');
|
|
2751
2857
|
}
|
|
2752
2858
|
scaleByRow(this, scale);
|
|
@@ -2755,7 +2861,7 @@ class AbstractMatrix {
|
|
|
2755
2861
|
case 'column': {
|
|
2756
2862
|
if (scale === undefined) {
|
|
2757
2863
|
scale = getScaleByColumn(this);
|
|
2758
|
-
} else if (!isAnyArray
|
|
2864
|
+
} else if (!isAnyArray(scale)) {
|
|
2759
2865
|
throw new TypeError('scale must be an array');
|
|
2760
2866
|
}
|
|
2761
2867
|
scaleByColumn(this, scale);
|
|
@@ -2869,7 +2975,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2869
2975
|
Matrix.copy(nRows, this);
|
|
2870
2976
|
} else if (Number.isInteger(nRows) && nRows >= 0) {
|
|
2871
2977
|
this.#initData(nRows, nColumns);
|
|
2872
|
-
} else if (isAnyArray
|
|
2978
|
+
} else if (isAnyArray(nRows)) {
|
|
2873
2979
|
// Copy the values from the 2D array
|
|
2874
2980
|
const arrayData = nRows;
|
|
2875
2981
|
nRows = arrayData.length;
|
|
@@ -3565,8 +3671,8 @@ class WrapperMatrix2D extends AbstractMatrix {
|
|
|
3565
3671
|
}
|
|
3566
3672
|
|
|
3567
3673
|
function wrap(array, options) {
|
|
3568
|
-
if (isAnyArray
|
|
3569
|
-
if (array[0] && isAnyArray
|
|
3674
|
+
if (isAnyArray(array)) {
|
|
3675
|
+
if (array[0] && isAnyArray(array[0])) {
|
|
3570
3676
|
return new WrapperMatrix2D(array);
|
|
3571
3677
|
} else {
|
|
3572
3678
|
return new WrapperMatrix1D(array, options);
|
|
@@ -4566,7 +4672,7 @@ function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4566
4672
|
if (
|
|
4567
4673
|
typeof yMatrix === 'object' &&
|
|
4568
4674
|
!Matrix.isMatrix(yMatrix) &&
|
|
4569
|
-
!isAnyArray
|
|
4675
|
+
!isAnyArray(yMatrix)
|
|
4570
4676
|
) {
|
|
4571
4677
|
options = yMatrix;
|
|
4572
4678
|
yMatrix = xMatrix;
|
|
@@ -4599,7 +4705,7 @@ function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4599
4705
|
if (
|
|
4600
4706
|
typeof yMatrix === 'object' &&
|
|
4601
4707
|
!Matrix.isMatrix(yMatrix) &&
|
|
4602
|
-
!isAnyArray
|
|
4708
|
+
!isAnyArray(yMatrix)
|
|
4603
4709
|
) {
|
|
4604
4710
|
options = yMatrix;
|
|
4605
4711
|
yMatrix = xMatrix;
|
|
@@ -5537,7 +5643,7 @@ class nipals {
|
|
|
5537
5643
|
|
|
5538
5644
|
let u;
|
|
5539
5645
|
if (Y) {
|
|
5540
|
-
if (isAnyArray
|
|
5646
|
+
if (isAnyArray(Y) && typeof Y[0] === 'number') {
|
|
5541
5647
|
Y = Matrix.columnVector(Y);
|
|
5542
5648
|
} else {
|
|
5543
5649
|
Y = WrapperMatrix2D.checkMatrix(Y);
|
|
@@ -5645,3 +5751,4 @@ exports.linearDependencies = linearDependencies;
|
|
|
5645
5751
|
exports.pseudoInverse = pseudoInverse;
|
|
5646
5752
|
exports.solve = solve;
|
|
5647
5753
|
exports.wrap = wrap;
|
|
5754
|
+
//# sourceMappingURL=matrix.js.map
|