ml-matrix 6.8.0 → 6.9.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 +48 -41
- package/matrix.js +44 -59
- package/matrix.umd.js +1 -1
- package/package.json +21 -19
- package/src/correlation.js +3 -1
- package/src/covariance.js +3 -1
- package/src/dc/nipals.js +3 -1
- package/src/matrix.js +19 -16
- 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/CHANGELOG.md +0 -539
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ml-matrix",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.9.0",
|
|
4
4
|
"description": "Matrix manipulation and computation library",
|
|
5
5
|
"main": "matrix.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
18
|
"compile": "rollup -c",
|
|
19
|
-
"eslint": "eslint
|
|
19
|
+
"eslint": "eslint src testUtils.js",
|
|
20
20
|
"eslint-fix": "npm run eslint -- --fix",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"test
|
|
21
|
+
"prepack": "npm run compile",
|
|
22
|
+
"prettier": "prettier --check src",
|
|
23
|
+
"prettier-write": "prettier --write src",
|
|
24
|
+
"test": "npm run test-only && npm run eslint && npm run prettier",
|
|
25
|
+
"test-only": "jest --coverage"
|
|
25
26
|
},
|
|
26
27
|
"jest": {
|
|
27
28
|
"testEnvironment": "node"
|
|
@@ -54,24 +55,25 @@
|
|
|
54
55
|
},
|
|
55
56
|
"homepage": "https://github.com/mljs/matrix",
|
|
56
57
|
"devDependencies": {
|
|
57
|
-
"@babel/plugin-transform-modules-commonjs": "^7.
|
|
58
|
-
"@rollup/plugin-commonjs": "^
|
|
59
|
-
"@rollup/plugin-node-resolve": "^
|
|
58
|
+
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
|
|
59
|
+
"@rollup/plugin-commonjs": "^21.0.2",
|
|
60
|
+
"@rollup/plugin-node-resolve": "^13.1.3",
|
|
60
61
|
"benchmark": "^2.1.4",
|
|
61
|
-
"csv-parse": "^
|
|
62
|
-
"eslint": "^
|
|
63
|
-
"eslint-config-cheminfo": "^
|
|
64
|
-
"jest": "^
|
|
65
|
-
"jest-matcher-deep-close-to": "^
|
|
66
|
-
"mathjs": "^
|
|
67
|
-
"ml-dataset-iris": "^1.
|
|
62
|
+
"csv-parse": "^5.0.4",
|
|
63
|
+
"eslint": "^8.10.0",
|
|
64
|
+
"eslint-config-cheminfo": "^7.2.2",
|
|
65
|
+
"jest": "^27.5.1",
|
|
66
|
+
"jest-matcher-deep-close-to": "^3.0.2",
|
|
67
|
+
"mathjs": "^10.1.1",
|
|
68
|
+
"ml-dataset-iris": "^1.2.1",
|
|
68
69
|
"numeric": "^1.2.6",
|
|
69
|
-
"prettier": "^2.
|
|
70
|
+
"prettier": "^2.5.1",
|
|
70
71
|
"pretty-hrtime": "^1.0.3",
|
|
71
|
-
"rollup": "^2.
|
|
72
|
+
"rollup": "^2.68.0",
|
|
72
73
|
"rollup-plugin-terser": "^7.0.2"
|
|
73
74
|
},
|
|
74
75
|
"dependencies": {
|
|
75
|
-
"
|
|
76
|
+
"is-any-array": "^2.0.0",
|
|
77
|
+
"ml-array-rescale": "^1.3.7"
|
|
76
78
|
}
|
|
77
79
|
}
|
package/src/correlation.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isAnyArray } from 'is-any-array';
|
|
2
|
+
|
|
1
3
|
import Matrix from './matrix';
|
|
2
4
|
|
|
3
5
|
export function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
@@ -6,7 +8,7 @@ export function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
6
8
|
if (
|
|
7
9
|
typeof yMatrix === 'object' &&
|
|
8
10
|
!Matrix.isMatrix(yMatrix) &&
|
|
9
|
-
!
|
|
11
|
+
!isAnyArray(yMatrix)
|
|
10
12
|
) {
|
|
11
13
|
options = yMatrix;
|
|
12
14
|
yMatrix = xMatrix;
|
package/src/covariance.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isAnyArray } from 'is-any-array';
|
|
2
|
+
|
|
1
3
|
import Matrix from './matrix';
|
|
2
4
|
|
|
3
5
|
export function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
@@ -6,7 +8,7 @@ export function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
6
8
|
if (
|
|
7
9
|
typeof yMatrix === 'object' &&
|
|
8
10
|
!Matrix.isMatrix(yMatrix) &&
|
|
9
|
-
!
|
|
11
|
+
!isAnyArray(yMatrix)
|
|
10
12
|
) {
|
|
11
13
|
options = yMatrix;
|
|
12
14
|
yMatrix = xMatrix;
|
package/src/dc/nipals.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isAnyArray } from 'is-any-array';
|
|
2
|
+
|
|
1
3
|
import Matrix from '../matrix';
|
|
2
4
|
import WrapperMatrix2D from '../wrap/WrapperMatrix2D';
|
|
3
5
|
|
|
@@ -13,7 +15,7 @@ export default class nipals {
|
|
|
13
15
|
|
|
14
16
|
let u;
|
|
15
17
|
if (Y) {
|
|
16
|
-
if (
|
|
18
|
+
if (isAnyArray(Y) && typeof Y[0] === 'number') {
|
|
17
19
|
Y = Matrix.columnVector(Y);
|
|
18
20
|
} else {
|
|
19
21
|
Y = WrapperMatrix2D.checkMatrix(Y);
|
package/src/matrix.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isAnyArray } from 'is-any-array';
|
|
1
2
|
import rescale from 'ml-array-rescale';
|
|
2
3
|
|
|
3
4
|
import { inspectMatrix, inspectMatrixWithOptions } from './inspect';
|
|
@@ -28,8 +29,9 @@ import {
|
|
|
28
29
|
checkColumnIndex,
|
|
29
30
|
checkColumnVector,
|
|
30
31
|
checkRange,
|
|
31
|
-
checkIndices,
|
|
32
32
|
checkNonEmpty,
|
|
33
|
+
checkRowIndices,
|
|
34
|
+
checkColumnIndices,
|
|
33
35
|
} from './util';
|
|
34
36
|
|
|
35
37
|
export class AbstractMatrix {
|
|
@@ -1027,6 +1029,7 @@ export class AbstractMatrix {
|
|
|
1027
1029
|
resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns);
|
|
1028
1030
|
return resultat.subMatrix(0, rows - 1, 0, cols - 1);
|
|
1029
1031
|
}
|
|
1032
|
+
|
|
1030
1033
|
return blockMult(x, y, r, c);
|
|
1031
1034
|
}
|
|
1032
1035
|
|
|
@@ -1236,12 +1239,13 @@ export class AbstractMatrix {
|
|
|
1236
1239
|
}
|
|
1237
1240
|
|
|
1238
1241
|
selection(rowIndices, columnIndices) {
|
|
1239
|
-
|
|
1242
|
+
checkRowIndices(this, rowIndices);
|
|
1243
|
+
checkColumnIndices(this, columnIndices);
|
|
1240
1244
|
let newMatrix = new Matrix(rowIndices.length, columnIndices.length);
|
|
1241
|
-
for (let i = 0; i <
|
|
1242
|
-
let rowIndex =
|
|
1243
|
-
for (let j = 0; j <
|
|
1244
|
-
let columnIndex =
|
|
1245
|
+
for (let i = 0; i < rowIndices.length; i++) {
|
|
1246
|
+
let rowIndex = rowIndices[i];
|
|
1247
|
+
for (let j = 0; j < columnIndices.length; j++) {
|
|
1248
|
+
let columnIndex = columnIndices[j];
|
|
1245
1249
|
newMatrix.set(i, j, this.get(rowIndex, columnIndex));
|
|
1246
1250
|
}
|
|
1247
1251
|
}
|
|
@@ -1329,13 +1333,13 @@ export class AbstractMatrix {
|
|
|
1329
1333
|
}
|
|
1330
1334
|
switch (by) {
|
|
1331
1335
|
case 'row': {
|
|
1332
|
-
if (!
|
|
1336
|
+
if (!isAnyArray(mean)) {
|
|
1333
1337
|
throw new TypeError('mean must be an array');
|
|
1334
1338
|
}
|
|
1335
1339
|
return varianceByRow(this, unbiased, mean);
|
|
1336
1340
|
}
|
|
1337
1341
|
case 'column': {
|
|
1338
|
-
if (!
|
|
1342
|
+
if (!isAnyArray(mean)) {
|
|
1339
1343
|
throw new TypeError('mean must be an array');
|
|
1340
1344
|
}
|
|
1341
1345
|
return varianceByColumn(this, unbiased, mean);
|
|
@@ -1378,14 +1382,14 @@ export class AbstractMatrix {
|
|
|
1378
1382
|
const { center = this.mean(by) } = options;
|
|
1379
1383
|
switch (by) {
|
|
1380
1384
|
case 'row': {
|
|
1381
|
-
if (!
|
|
1385
|
+
if (!isAnyArray(center)) {
|
|
1382
1386
|
throw new TypeError('center must be an array');
|
|
1383
1387
|
}
|
|
1384
1388
|
centerByRow(this, center);
|
|
1385
1389
|
return this;
|
|
1386
1390
|
}
|
|
1387
1391
|
case 'column': {
|
|
1388
|
-
if (!
|
|
1392
|
+
if (!isAnyArray(center)) {
|
|
1389
1393
|
throw new TypeError('center must be an array');
|
|
1390
1394
|
}
|
|
1391
1395
|
centerByColumn(this, center);
|
|
@@ -1416,7 +1420,7 @@ export class AbstractMatrix {
|
|
|
1416
1420
|
case 'row': {
|
|
1417
1421
|
if (scale === undefined) {
|
|
1418
1422
|
scale = getScaleByRow(this);
|
|
1419
|
-
} else if (!
|
|
1423
|
+
} else if (!isAnyArray(scale)) {
|
|
1420
1424
|
throw new TypeError('scale must be an array');
|
|
1421
1425
|
}
|
|
1422
1426
|
scaleByRow(this, scale);
|
|
@@ -1425,7 +1429,7 @@ export class AbstractMatrix {
|
|
|
1425
1429
|
case 'column': {
|
|
1426
1430
|
if (scale === undefined) {
|
|
1427
1431
|
scale = getScaleByColumn(this);
|
|
1428
|
-
} else if (!
|
|
1432
|
+
} else if (!isAnyArray(scale)) {
|
|
1429
1433
|
throw new TypeError('scale must be an array');
|
|
1430
1434
|
}
|
|
1431
1435
|
scaleByColumn(this, scale);
|
|
@@ -1452,9 +1456,8 @@ export class AbstractMatrix {
|
|
|
1452
1456
|
|
|
1453
1457
|
AbstractMatrix.prototype.klass = 'Matrix';
|
|
1454
1458
|
if (typeof Symbol !== 'undefined') {
|
|
1455
|
-
AbstractMatrix.prototype[
|
|
1456
|
-
|
|
1457
|
-
] = inspectMatrix;
|
|
1459
|
+
AbstractMatrix.prototype[Symbol.for('nodejs.util.inspect.custom')] =
|
|
1460
|
+
inspectMatrix;
|
|
1458
1461
|
}
|
|
1459
1462
|
|
|
1460
1463
|
function compareNumbers(a, b) {
|
|
@@ -1487,7 +1490,7 @@ export default class Matrix extends AbstractMatrix {
|
|
|
1487
1490
|
} else {
|
|
1488
1491
|
throw new TypeError('nColumns must be a positive integer');
|
|
1489
1492
|
}
|
|
1490
|
-
} else if (
|
|
1493
|
+
} else if (isAnyArray(nRows)) {
|
|
1491
1494
|
// Copy the values from the 2D array
|
|
1492
1495
|
const arrayData = nRows;
|
|
1493
1496
|
nRows = arrayData.length;
|
package/src/util.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isAnyArray } from 'is-any-array';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @private
|
|
3
5
|
* Check that a row index is not out of bounds
|
|
@@ -64,46 +66,28 @@ export function checkColumnVector(matrix, vector) {
|
|
|
64
66
|
return vector;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
export function checkIndices(matrix, rowIndices, columnIndices) {
|
|
68
|
-
return {
|
|
69
|
-
row: checkRowIndices(matrix, rowIndices),
|
|
70
|
-
column: checkColumnIndices(matrix, columnIndices),
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
69
|
export function checkRowIndices(matrix, rowIndices) {
|
|
75
|
-
if (
|
|
76
|
-
throw new TypeError('
|
|
70
|
+
if (!isAnyArray(rowIndices)) {
|
|
71
|
+
throw new TypeError('row indices must be an array');
|
|
77
72
|
}
|
|
78
73
|
|
|
79
|
-
let
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (rowOut) {
|
|
84
|
-
throw new RangeError('row indices are out of range');
|
|
74
|
+
for (let i = 0; i < rowIndices.length; i++) {
|
|
75
|
+
if (rowIndices[i] < 0 || rowIndices[i] >= matrix.rows) {
|
|
76
|
+
throw new RangeError('row indices are out of range');
|
|
77
|
+
}
|
|
85
78
|
}
|
|
86
|
-
|
|
87
|
-
if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices);
|
|
88
|
-
|
|
89
|
-
return rowIndices;
|
|
90
79
|
}
|
|
91
80
|
|
|
92
81
|
export function checkColumnIndices(matrix, columnIndices) {
|
|
93
|
-
if (
|
|
94
|
-
throw new TypeError('
|
|
82
|
+
if (!isAnyArray(columnIndices)) {
|
|
83
|
+
throw new TypeError('column indices must be an array');
|
|
95
84
|
}
|
|
96
85
|
|
|
97
|
-
let
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (columnOut) {
|
|
102
|
-
throw new RangeError('column indices are out of range');
|
|
86
|
+
for (let i = 0; i < columnIndices.length; i++) {
|
|
87
|
+
if (columnIndices[i] < 0 || columnIndices[i] >= matrix.columns) {
|
|
88
|
+
throw new RangeError('column indices are out of range');
|
|
89
|
+
}
|
|
103
90
|
}
|
|
104
|
-
if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices);
|
|
105
|
-
|
|
106
|
-
return columnIndices;
|
|
107
91
|
}
|
|
108
92
|
|
|
109
93
|
export function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
|
|
@@ -4,7 +4,7 @@ import BaseView from './base';
|
|
|
4
4
|
|
|
5
5
|
export default class MatrixColumnSelectionView extends BaseView {
|
|
6
6
|
constructor(matrix, columnIndices) {
|
|
7
|
-
|
|
7
|
+
checkColumnIndices(matrix, columnIndices);
|
|
8
8
|
super(matrix, matrix.rows, columnIndices.length);
|
|
9
9
|
this.columnIndices = columnIndices;
|
|
10
10
|
}
|
|
@@ -4,7 +4,7 @@ import BaseView from './base';
|
|
|
4
4
|
|
|
5
5
|
export default class MatrixRowSelectionView extends BaseView {
|
|
6
6
|
constructor(matrix, rowIndices) {
|
|
7
|
-
|
|
7
|
+
checkRowIndices(matrix, rowIndices);
|
|
8
8
|
super(matrix, rowIndices.length, matrix.columns);
|
|
9
9
|
this.rowIndices = rowIndices;
|
|
10
10
|
}
|
package/src/views/selection.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { checkRowIndices, checkColumnIndices } from '../util';
|
|
2
2
|
|
|
3
3
|
import BaseView from './base';
|
|
4
4
|
|
|
5
5
|
export default class MatrixSelectionView extends BaseView {
|
|
6
6
|
constructor(matrix, rowIndices, columnIndices) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
7
|
+
checkRowIndices(matrix, rowIndices);
|
|
8
|
+
checkColumnIndices(matrix, columnIndices);
|
|
9
|
+
super(matrix, rowIndices.length, columnIndices.length);
|
|
10
|
+
this.rowIndices = rowIndices;
|
|
11
|
+
this.columnIndices = columnIndices;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
set(rowIndex, columnIndex, value) {
|
package/src/wrap/wrap.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { isAnyArray } from 'is-any-array';
|
|
2
|
+
|
|
1
3
|
import WrapperMatrix1D from './WrapperMatrix1D';
|
|
2
4
|
import WrapperMatrix2D from './WrapperMatrix2D';
|
|
3
5
|
|
|
4
6
|
export function wrap(array, options) {
|
|
5
|
-
if (
|
|
6
|
-
if (array[0] &&
|
|
7
|
+
if (isAnyArray(array)) {
|
|
8
|
+
if (array[0] && isAnyArray(array[0])) {
|
|
7
9
|
return new WrapperMatrix2D(array);
|
|
8
10
|
} else {
|
|
9
11
|
return new WrapperMatrix1D(array, options);
|