ml-matrix 6.8.1 → 6.10.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/README.md +69 -20
- package/matrix.d.ts +56 -37
- package/matrix.js +110 -67
- package/matrix.umd.js +1 -1
- package/package.json +13 -15
- package/src/correlation.js +3 -1
- package/src/covariance.js +3 -1
- package/src/dc/nipals.js +3 -1
- package/src/matrix.js +88 -27
- package/src/util.js +14 -30
- package/src/views/columnSelection.js +1 -1
- package/src/views/rowSelection.js +1 -1
- package/src/views/selection.js +6 -5
- package/src/wrap/wrap.js +4 -2
package/matrix.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var isAnyArray = require('is-any-array');
|
|
5
6
|
var rescale = require('ml-array-rescale');
|
|
6
7
|
|
|
7
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
@@ -952,46 +953,28 @@ function checkColumnVector(matrix, vector) {
|
|
|
952
953
|
return vector;
|
|
953
954
|
}
|
|
954
955
|
|
|
955
|
-
function checkIndices(matrix, rowIndices, columnIndices) {
|
|
956
|
-
return {
|
|
957
|
-
row: checkRowIndices(matrix, rowIndices),
|
|
958
|
-
column: checkColumnIndices(matrix, columnIndices),
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
|
|
962
956
|
function checkRowIndices(matrix, rowIndices) {
|
|
963
|
-
if (
|
|
964
|
-
throw new TypeError('
|
|
957
|
+
if (!isAnyArray.isAnyArray(rowIndices)) {
|
|
958
|
+
throw new TypeError('row indices must be an array');
|
|
965
959
|
}
|
|
966
960
|
|
|
967
|
-
let
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
if (rowOut) {
|
|
972
|
-
throw new RangeError('row indices are out of range');
|
|
961
|
+
for (let i = 0; i < rowIndices.length; i++) {
|
|
962
|
+
if (rowIndices[i] < 0 || rowIndices[i] >= matrix.rows) {
|
|
963
|
+
throw new RangeError('row indices are out of range');
|
|
964
|
+
}
|
|
973
965
|
}
|
|
974
|
-
|
|
975
|
-
if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices);
|
|
976
|
-
|
|
977
|
-
return rowIndices;
|
|
978
966
|
}
|
|
979
967
|
|
|
980
968
|
function checkColumnIndices(matrix, columnIndices) {
|
|
981
|
-
if (
|
|
982
|
-
throw new TypeError('
|
|
969
|
+
if (!isAnyArray.isAnyArray(columnIndices)) {
|
|
970
|
+
throw new TypeError('column indices must be an array');
|
|
983
971
|
}
|
|
984
972
|
|
|
985
|
-
let
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
if (columnOut) {
|
|
990
|
-
throw new RangeError('column indices are out of range');
|
|
973
|
+
for (let i = 0; i < columnIndices.length; i++) {
|
|
974
|
+
if (columnIndices[i] < 0 || columnIndices[i] >= matrix.columns) {
|
|
975
|
+
throw new RangeError('column indices are out of range');
|
|
976
|
+
}
|
|
991
977
|
}
|
|
992
|
-
if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices);
|
|
993
|
-
|
|
994
|
-
return columnIndices;
|
|
995
978
|
}
|
|
996
979
|
|
|
997
980
|
function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
|
|
@@ -1777,19 +1760,47 @@ class AbstractMatrix {
|
|
|
1777
1760
|
return this;
|
|
1778
1761
|
}
|
|
1779
1762
|
|
|
1780
|
-
max() {
|
|
1763
|
+
max(by) {
|
|
1781
1764
|
if (this.isEmpty()) {
|
|
1782
1765
|
return NaN;
|
|
1783
1766
|
}
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1767
|
+
switch (by) {
|
|
1768
|
+
case 'row': {
|
|
1769
|
+
const max = new Array(this.rows).fill(Number.NEGATIVE_INFINITY);
|
|
1770
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1771
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1772
|
+
if (this.get(row, column) > max[row]) {
|
|
1773
|
+
max[row] = this.get(row, column);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
return max;
|
|
1778
|
+
}
|
|
1779
|
+
case 'column': {
|
|
1780
|
+
const max = new Array(this.columns).fill(Number.NEGATIVE_INFINITY);
|
|
1781
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1782
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1783
|
+
if (this.get(row, column) > max[column]) {
|
|
1784
|
+
max[column] = this.get(row, column);
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
return max;
|
|
1789
|
+
}
|
|
1790
|
+
case undefined: {
|
|
1791
|
+
let max = this.get(0, 0);
|
|
1792
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1793
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1794
|
+
if (this.get(row, column) > max) {
|
|
1795
|
+
max = this.get(row, column);
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1789
1798
|
}
|
|
1799
|
+
return max;
|
|
1790
1800
|
}
|
|
1801
|
+
default:
|
|
1802
|
+
throw new Error(`invalid option: ${by}`);
|
|
1791
1803
|
}
|
|
1792
|
-
return v;
|
|
1793
1804
|
}
|
|
1794
1805
|
|
|
1795
1806
|
maxIndex() {
|
|
@@ -1808,19 +1819,48 @@ class AbstractMatrix {
|
|
|
1808
1819
|
return idx;
|
|
1809
1820
|
}
|
|
1810
1821
|
|
|
1811
|
-
min() {
|
|
1822
|
+
min(by) {
|
|
1812
1823
|
if (this.isEmpty()) {
|
|
1813
1824
|
return NaN;
|
|
1814
1825
|
}
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1826
|
+
|
|
1827
|
+
switch (by) {
|
|
1828
|
+
case 'row': {
|
|
1829
|
+
const min = new Array(this.rows).fill(Number.POSITIVE_INFINITY);
|
|
1830
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1831
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1832
|
+
if (this.get(row, column) < min[row]) {
|
|
1833
|
+
min[row] = this.get(row, column);
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1820
1836
|
}
|
|
1837
|
+
return min;
|
|
1821
1838
|
}
|
|
1839
|
+
case 'column': {
|
|
1840
|
+
const min = new Array(this.columns).fill(Number.POSITIVE_INFINITY);
|
|
1841
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1842
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1843
|
+
if (this.get(row, column) < min[column]) {
|
|
1844
|
+
min[column] = this.get(row, column);
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
return min;
|
|
1849
|
+
}
|
|
1850
|
+
case undefined: {
|
|
1851
|
+
let min = this.get(0, 0);
|
|
1852
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1853
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1854
|
+
if (this.get(row, column) < min) {
|
|
1855
|
+
min = this.get(row, column);
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
return min;
|
|
1860
|
+
}
|
|
1861
|
+
default:
|
|
1862
|
+
throw new Error(`invalid option: ${by}`);
|
|
1822
1863
|
}
|
|
1823
|
-
return v;
|
|
1824
1864
|
}
|
|
1825
1865
|
|
|
1826
1866
|
minIndex() {
|
|
@@ -2244,6 +2284,7 @@ class AbstractMatrix {
|
|
|
2244
2284
|
resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns);
|
|
2245
2285
|
return resultat.subMatrix(0, rows - 1, 0, cols - 1);
|
|
2246
2286
|
}
|
|
2287
|
+
|
|
2247
2288
|
return blockMult(x, y, r, c);
|
|
2248
2289
|
}
|
|
2249
2290
|
|
|
@@ -2453,12 +2494,13 @@ class AbstractMatrix {
|
|
|
2453
2494
|
}
|
|
2454
2495
|
|
|
2455
2496
|
selection(rowIndices, columnIndices) {
|
|
2456
|
-
|
|
2497
|
+
checkRowIndices(this, rowIndices);
|
|
2498
|
+
checkColumnIndices(this, columnIndices);
|
|
2457
2499
|
let newMatrix = new Matrix(rowIndices.length, columnIndices.length);
|
|
2458
|
-
for (let i = 0; i <
|
|
2459
|
-
let rowIndex =
|
|
2460
|
-
for (let j = 0; j <
|
|
2461
|
-
let columnIndex =
|
|
2500
|
+
for (let i = 0; i < rowIndices.length; i++) {
|
|
2501
|
+
let rowIndex = rowIndices[i];
|
|
2502
|
+
for (let j = 0; j < columnIndices.length; j++) {
|
|
2503
|
+
let columnIndex = columnIndices[j];
|
|
2462
2504
|
newMatrix.set(i, j, this.get(rowIndex, columnIndex));
|
|
2463
2505
|
}
|
|
2464
2506
|
}
|
|
@@ -2546,13 +2588,13 @@ class AbstractMatrix {
|
|
|
2546
2588
|
}
|
|
2547
2589
|
switch (by) {
|
|
2548
2590
|
case 'row': {
|
|
2549
|
-
if (!
|
|
2591
|
+
if (!isAnyArray.isAnyArray(mean)) {
|
|
2550
2592
|
throw new TypeError('mean must be an array');
|
|
2551
2593
|
}
|
|
2552
2594
|
return varianceByRow(this, unbiased, mean);
|
|
2553
2595
|
}
|
|
2554
2596
|
case 'column': {
|
|
2555
|
-
if (!
|
|
2597
|
+
if (!isAnyArray.isAnyArray(mean)) {
|
|
2556
2598
|
throw new TypeError('mean must be an array');
|
|
2557
2599
|
}
|
|
2558
2600
|
return varianceByColumn(this, unbiased, mean);
|
|
@@ -2595,14 +2637,14 @@ class AbstractMatrix {
|
|
|
2595
2637
|
const { center = this.mean(by) } = options;
|
|
2596
2638
|
switch (by) {
|
|
2597
2639
|
case 'row': {
|
|
2598
|
-
if (!
|
|
2640
|
+
if (!isAnyArray.isAnyArray(center)) {
|
|
2599
2641
|
throw new TypeError('center must be an array');
|
|
2600
2642
|
}
|
|
2601
2643
|
centerByRow(this, center);
|
|
2602
2644
|
return this;
|
|
2603
2645
|
}
|
|
2604
2646
|
case 'column': {
|
|
2605
|
-
if (!
|
|
2647
|
+
if (!isAnyArray.isAnyArray(center)) {
|
|
2606
2648
|
throw new TypeError('center must be an array');
|
|
2607
2649
|
}
|
|
2608
2650
|
centerByColumn(this, center);
|
|
@@ -2633,7 +2675,7 @@ class AbstractMatrix {
|
|
|
2633
2675
|
case 'row': {
|
|
2634
2676
|
if (scale === undefined) {
|
|
2635
2677
|
scale = getScaleByRow(this);
|
|
2636
|
-
} else if (!
|
|
2678
|
+
} else if (!isAnyArray.isAnyArray(scale)) {
|
|
2637
2679
|
throw new TypeError('scale must be an array');
|
|
2638
2680
|
}
|
|
2639
2681
|
scaleByRow(this, scale);
|
|
@@ -2642,7 +2684,7 @@ class AbstractMatrix {
|
|
|
2642
2684
|
case 'column': {
|
|
2643
2685
|
if (scale === undefined) {
|
|
2644
2686
|
scale = getScaleByColumn(this);
|
|
2645
|
-
} else if (!
|
|
2687
|
+
} else if (!isAnyArray.isAnyArray(scale)) {
|
|
2646
2688
|
throw new TypeError('scale must be an array');
|
|
2647
2689
|
}
|
|
2648
2690
|
scaleByColumn(this, scale);
|
|
@@ -2703,7 +2745,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2703
2745
|
} else {
|
|
2704
2746
|
throw new TypeError('nColumns must be a positive integer');
|
|
2705
2747
|
}
|
|
2706
|
-
} else if (
|
|
2748
|
+
} else if (isAnyArray.isAnyArray(nRows)) {
|
|
2707
2749
|
// Copy the values from the 2D array
|
|
2708
2750
|
const arrayData = nRows;
|
|
2709
2751
|
nRows = arrayData.length;
|
|
@@ -2827,7 +2869,7 @@ class MatrixColumnView extends BaseView {
|
|
|
2827
2869
|
|
|
2828
2870
|
class MatrixColumnSelectionView extends BaseView {
|
|
2829
2871
|
constructor(matrix, columnIndices) {
|
|
2830
|
-
|
|
2872
|
+
checkColumnIndices(matrix, columnIndices);
|
|
2831
2873
|
super(matrix, matrix.rows, columnIndices.length);
|
|
2832
2874
|
this.columnIndices = columnIndices;
|
|
2833
2875
|
}
|
|
@@ -2891,7 +2933,7 @@ class MatrixRowView extends BaseView {
|
|
|
2891
2933
|
|
|
2892
2934
|
class MatrixRowSelectionView extends BaseView {
|
|
2893
2935
|
constructor(matrix, rowIndices) {
|
|
2894
|
-
|
|
2936
|
+
checkRowIndices(matrix, rowIndices);
|
|
2895
2937
|
super(matrix, rowIndices.length, matrix.columns);
|
|
2896
2938
|
this.rowIndices = rowIndices;
|
|
2897
2939
|
}
|
|
@@ -2908,10 +2950,11 @@ class MatrixRowSelectionView extends BaseView {
|
|
|
2908
2950
|
|
|
2909
2951
|
class MatrixSelectionView extends BaseView {
|
|
2910
2952
|
constructor(matrix, rowIndices, columnIndices) {
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
this.
|
|
2953
|
+
checkRowIndices(matrix, rowIndices);
|
|
2954
|
+
checkColumnIndices(matrix, columnIndices);
|
|
2955
|
+
super(matrix, rowIndices.length, columnIndices.length);
|
|
2956
|
+
this.rowIndices = rowIndices;
|
|
2957
|
+
this.columnIndices = columnIndices;
|
|
2915
2958
|
}
|
|
2916
2959
|
|
|
2917
2960
|
set(rowIndex, columnIndex, value) {
|
|
@@ -3019,8 +3062,8 @@ class WrapperMatrix2D extends AbstractMatrix {
|
|
|
3019
3062
|
}
|
|
3020
3063
|
|
|
3021
3064
|
function wrap(array, options) {
|
|
3022
|
-
if (
|
|
3023
|
-
if (array[0] &&
|
|
3065
|
+
if (isAnyArray.isAnyArray(array)) {
|
|
3066
|
+
if (array[0] && isAnyArray.isAnyArray(array[0])) {
|
|
3024
3067
|
return new WrapperMatrix2D(array);
|
|
3025
3068
|
} else {
|
|
3026
3069
|
return new WrapperMatrix1D(array, options);
|
|
@@ -4020,7 +4063,7 @@ function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4020
4063
|
if (
|
|
4021
4064
|
typeof yMatrix === 'object' &&
|
|
4022
4065
|
!Matrix.isMatrix(yMatrix) &&
|
|
4023
|
-
!
|
|
4066
|
+
!isAnyArray.isAnyArray(yMatrix)
|
|
4024
4067
|
) {
|
|
4025
4068
|
options = yMatrix;
|
|
4026
4069
|
yMatrix = xMatrix;
|
|
@@ -4053,7 +4096,7 @@ function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4053
4096
|
if (
|
|
4054
4097
|
typeof yMatrix === 'object' &&
|
|
4055
4098
|
!Matrix.isMatrix(yMatrix) &&
|
|
4056
|
-
!
|
|
4099
|
+
!isAnyArray.isAnyArray(yMatrix)
|
|
4057
4100
|
) {
|
|
4058
4101
|
options = yMatrix;
|
|
4059
4102
|
yMatrix = xMatrix;
|
|
@@ -4989,7 +5032,7 @@ class nipals {
|
|
|
4989
5032
|
|
|
4990
5033
|
let u;
|
|
4991
5034
|
if (Y) {
|
|
4992
|
-
if (
|
|
5035
|
+
if (isAnyArray.isAnyArray(Y) && typeof Y[0] === 'number') {
|
|
4993
5036
|
Y = Matrix.columnVector(Y);
|
|
4994
5037
|
} else {
|
|
4995
5038
|
Y = WrapperMatrix2D.checkMatrix(Y);
|