ml-matrix 6.1.2 → 6.4.1
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/History.md +36 -0
- package/README.md +6 -6
- package/matrix.d.ts +106 -70
- package/matrix.js +822 -668
- package/matrix.umd.js +1 -0
- package/package.json +21 -12
- package/src/correlation.js +17 -7
- package/src/covariance.js +10 -6
- package/src/dc/cholesky.js +20 -16
- package/src/dc/evd.js +45 -45
- package/src/dc/lu.js +32 -32
- package/src/dc/nipals.js +135 -0
- package/src/dc/qr.js +24 -24
- package/src/dc/svd.js +48 -48
- package/src/dc/util.js +1 -1
- package/src/determinant.js +2 -2
- package/src/index.js +4 -3
- package/src/inspect.js +2 -2
- package/src/linearDependencies.js +14 -14
- package/src/mathOperations.js +147 -147
- package/src/matrix.js +234 -233
- package/src/pseudoInverse.js +5 -5
- package/src/stat.js +33 -33
- package/src/util.js +8 -8
- package/src/views/selection.js +28 -28
- package/src/views/sub.js +28 -28
- package/src/wrap/WrapperMatrix1D.js +2 -2
package/src/pseudoInverse.js
CHANGED
|
@@ -3,13 +3,13 @@ import Matrix from './matrix';
|
|
|
3
3
|
|
|
4
4
|
export function pseudoInverse(matrix, threshold = Number.EPSILON) {
|
|
5
5
|
matrix = Matrix.checkMatrix(matrix);
|
|
6
|
-
|
|
6
|
+
let svdSolution = new SVD(matrix, { autoTranspose: true });
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
let U = svdSolution.leftSingularVectors;
|
|
9
|
+
let V = svdSolution.rightSingularVectors;
|
|
10
|
+
let s = svdSolution.diagonal;
|
|
11
11
|
|
|
12
|
-
for (
|
|
12
|
+
for (let i = 0; i < s.length; i++) {
|
|
13
13
|
if (Math.abs(s[i]) > threshold) {
|
|
14
14
|
s[i] = 1.0 / s[i];
|
|
15
15
|
} else {
|
package/src/stat.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { newArray } from './util';
|
|
2
2
|
|
|
3
3
|
export function sumByRow(matrix) {
|
|
4
|
-
|
|
5
|
-
for (
|
|
6
|
-
for (
|
|
4
|
+
let sum = newArray(matrix.rows);
|
|
5
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
6
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
7
7
|
sum[i] += matrix.get(i, j);
|
|
8
8
|
}
|
|
9
9
|
}
|
|
@@ -11,9 +11,9 @@ export function sumByRow(matrix) {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function sumByColumn(matrix) {
|
|
14
|
-
|
|
15
|
-
for (
|
|
16
|
-
for (
|
|
14
|
+
let sum = newArray(matrix.columns);
|
|
15
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
16
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
17
17
|
sum[j] += matrix.get(i, j);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -21,9 +21,9 @@ export function sumByColumn(matrix) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export function sumAll(matrix) {
|
|
24
|
-
|
|
25
|
-
for (
|
|
26
|
-
for (
|
|
24
|
+
let v = 0;
|
|
25
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
26
|
+
for (let j = 0; j < matrix.columns; j++) {
|
|
27
27
|
v += matrix.get(i, j);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
@@ -31,9 +31,9 @@ export function sumAll(matrix) {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export function productByRow(matrix) {
|
|
34
|
-
|
|
35
|
-
for (
|
|
36
|
-
for (
|
|
34
|
+
let sum = newArray(matrix.rows, 1);
|
|
35
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
36
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
37
37
|
sum[i] *= matrix.get(i, j);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -41,9 +41,9 @@ export function productByRow(matrix) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export function productByColumn(matrix) {
|
|
44
|
-
|
|
45
|
-
for (
|
|
46
|
-
for (
|
|
44
|
+
let sum = newArray(matrix.columns, 1);
|
|
45
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
46
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
47
47
|
sum[j] *= matrix.get(i, j);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -51,9 +51,9 @@ export function productByColumn(matrix) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export function productAll(matrix) {
|
|
54
|
-
|
|
55
|
-
for (
|
|
56
|
-
for (
|
|
54
|
+
let v = 1;
|
|
55
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
56
|
+
for (let j = 0; j < matrix.columns; j++) {
|
|
57
57
|
v *= matrix.get(i, j);
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -65,11 +65,11 @@ export function varianceByRow(matrix, unbiased, mean) {
|
|
|
65
65
|
const cols = matrix.columns;
|
|
66
66
|
const variance = [];
|
|
67
67
|
|
|
68
|
-
for (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
for (
|
|
68
|
+
for (let i = 0; i < rows; i++) {
|
|
69
|
+
let sum1 = 0;
|
|
70
|
+
let sum2 = 0;
|
|
71
|
+
let x = 0;
|
|
72
|
+
for (let j = 0; j < cols; j++) {
|
|
73
73
|
x = matrix.get(i, j) - mean[i];
|
|
74
74
|
sum1 += x;
|
|
75
75
|
sum2 += x * x;
|
|
@@ -88,11 +88,11 @@ export function varianceByColumn(matrix, unbiased, mean) {
|
|
|
88
88
|
const cols = matrix.columns;
|
|
89
89
|
const variance = [];
|
|
90
90
|
|
|
91
|
-
for (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (
|
|
91
|
+
for (let j = 0; j < cols; j++) {
|
|
92
|
+
let sum1 = 0;
|
|
93
|
+
let sum2 = 0;
|
|
94
|
+
let x = 0;
|
|
95
|
+
for (let i = 0; i < rows; i++) {
|
|
96
96
|
x = matrix.get(i, j) - mean[j];
|
|
97
97
|
sum1 += x;
|
|
98
98
|
sum2 += x * x;
|
|
@@ -111,11 +111,11 @@ export function varianceAll(matrix, unbiased, mean) {
|
|
|
111
111
|
const cols = matrix.columns;
|
|
112
112
|
const size = rows * cols;
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
for (
|
|
118
|
-
for (
|
|
114
|
+
let sum1 = 0;
|
|
115
|
+
let sum2 = 0;
|
|
116
|
+
let x = 0;
|
|
117
|
+
for (let i = 0; i < rows; i++) {
|
|
118
|
+
for (let j = 0; j < cols; j++) {
|
|
119
119
|
x = matrix.get(i, j) - mean;
|
|
120
120
|
sum1 += x;
|
|
121
121
|
sum2 += x * x;
|
package/src/util.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @param {boolean} [outer]
|
|
7
7
|
*/
|
|
8
8
|
export function checkRowIndex(matrix, index, outer) {
|
|
9
|
-
|
|
9
|
+
let max = outer ? matrix.rows : matrix.rows - 1;
|
|
10
10
|
if (index < 0 || index > max) {
|
|
11
11
|
throw new RangeError('Row index out of range');
|
|
12
12
|
}
|
|
@@ -20,7 +20,7 @@ export function checkRowIndex(matrix, index, outer) {
|
|
|
20
20
|
* @param {boolean} [outer]
|
|
21
21
|
*/
|
|
22
22
|
export function checkColumnIndex(matrix, index, outer) {
|
|
23
|
-
|
|
23
|
+
let max = outer ? matrix.columns : matrix.columns - 1;
|
|
24
24
|
if (index < 0 || index > max) {
|
|
25
25
|
throw new RangeError('Column index out of range');
|
|
26
26
|
}
|
|
@@ -40,7 +40,7 @@ export function checkRowVector(matrix, vector) {
|
|
|
40
40
|
}
|
|
41
41
|
if (vector.length !== matrix.columns) {
|
|
42
42
|
throw new RangeError(
|
|
43
|
-
'vector size must be the same as the number of columns'
|
|
43
|
+
'vector size must be the same as the number of columns',
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
return vector;
|
|
@@ -67,7 +67,7 @@ export function checkColumnVector(matrix, vector) {
|
|
|
67
67
|
export function checkIndices(matrix, rowIndices, columnIndices) {
|
|
68
68
|
return {
|
|
69
69
|
row: checkRowIndices(matrix, rowIndices),
|
|
70
|
-
column: checkColumnIndices(matrix, columnIndices)
|
|
70
|
+
column: checkColumnIndices(matrix, columnIndices),
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
|
|
@@ -76,7 +76,7 @@ export function checkRowIndices(matrix, rowIndices) {
|
|
|
76
76
|
throw new TypeError('unexpected type for row indices');
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
let rowOut = rowIndices.some((r) => {
|
|
80
80
|
return r < 0 || r >= matrix.rows;
|
|
81
81
|
});
|
|
82
82
|
|
|
@@ -94,7 +94,7 @@ export function checkColumnIndices(matrix, columnIndices) {
|
|
|
94
94
|
throw new TypeError('unexpected type for column indices');
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
let columnOut = columnIndices.some((c) => {
|
|
98
98
|
return c < 0 || c >= matrix.columns;
|
|
99
99
|
});
|
|
100
100
|
|
|
@@ -131,8 +131,8 @@ export function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
export function newArray(length, value = 0) {
|
|
134
|
-
|
|
135
|
-
for (
|
|
134
|
+
let array = [];
|
|
135
|
+
for (let i = 0; i < length; i++) {
|
|
136
136
|
array.push(value);
|
|
137
137
|
}
|
|
138
138
|
return array;
|
package/src/views/selection.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { checkIndices } from '../util';
|
|
2
|
-
|
|
3
|
-
import BaseView from './base';
|
|
4
|
-
|
|
5
|
-
export default class MatrixSelectionView extends BaseView {
|
|
6
|
-
constructor(matrix, rowIndices, columnIndices) {
|
|
7
|
-
|
|
8
|
-
super(matrix, indices.row.length, indices.column.length);
|
|
9
|
-
this.rowIndices = indices.row;
|
|
10
|
-
this.columnIndices = indices.column;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
set(rowIndex, columnIndex, value) {
|
|
14
|
-
this.matrix.set(
|
|
15
|
-
this.rowIndices[rowIndex],
|
|
16
|
-
this.columnIndices[columnIndex],
|
|
17
|
-
value
|
|
18
|
-
);
|
|
19
|
-
return this;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get(rowIndex, columnIndex) {
|
|
23
|
-
return this.matrix.get(
|
|
24
|
-
this.rowIndices[rowIndex],
|
|
25
|
-
this.columnIndices[columnIndex]
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
import { checkIndices } from '../util';
|
|
2
|
+
|
|
3
|
+
import BaseView from './base';
|
|
4
|
+
|
|
5
|
+
export default class MatrixSelectionView extends BaseView {
|
|
6
|
+
constructor(matrix, rowIndices, columnIndices) {
|
|
7
|
+
let indices = checkIndices(matrix, rowIndices, columnIndices);
|
|
8
|
+
super(matrix, indices.row.length, indices.column.length);
|
|
9
|
+
this.rowIndices = indices.row;
|
|
10
|
+
this.columnIndices = indices.column;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
set(rowIndex, columnIndex, value) {
|
|
14
|
+
this.matrix.set(
|
|
15
|
+
this.rowIndices[rowIndex],
|
|
16
|
+
this.columnIndices[columnIndex],
|
|
17
|
+
value,
|
|
18
|
+
);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get(rowIndex, columnIndex) {
|
|
23
|
+
return this.matrix.get(
|
|
24
|
+
this.rowIndices[rowIndex],
|
|
25
|
+
this.columnIndices[columnIndex],
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/views/sub.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { checkRange } from '../util';
|
|
2
|
-
|
|
3
|
-
import BaseView from './base';
|
|
4
|
-
|
|
5
|
-
export default class MatrixSubView extends BaseView {
|
|
6
|
-
constructor(matrix, startRow, endRow, startColumn, endColumn) {
|
|
7
|
-
checkRange(matrix, startRow, endRow, startColumn, endColumn);
|
|
8
|
-
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);
|
|
9
|
-
this.startRow = startRow;
|
|
10
|
-
this.startColumn = startColumn;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
set(rowIndex, columnIndex, value) {
|
|
14
|
-
this.matrix.set(
|
|
15
|
-
this.startRow + rowIndex,
|
|
16
|
-
this.startColumn + columnIndex,
|
|
17
|
-
value
|
|
18
|
-
);
|
|
19
|
-
return this;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get(rowIndex, columnIndex) {
|
|
23
|
-
return this.matrix.get(
|
|
24
|
-
this.startRow + rowIndex,
|
|
25
|
-
this.startColumn + columnIndex
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
import { checkRange } from '../util';
|
|
2
|
+
|
|
3
|
+
import BaseView from './base';
|
|
4
|
+
|
|
5
|
+
export default class MatrixSubView extends BaseView {
|
|
6
|
+
constructor(matrix, startRow, endRow, startColumn, endColumn) {
|
|
7
|
+
checkRange(matrix, startRow, endRow, startColumn, endColumn);
|
|
8
|
+
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);
|
|
9
|
+
this.startRow = startRow;
|
|
10
|
+
this.startColumn = startColumn;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
set(rowIndex, columnIndex, value) {
|
|
14
|
+
this.matrix.set(
|
|
15
|
+
this.startRow + rowIndex,
|
|
16
|
+
this.startColumn + columnIndex,
|
|
17
|
+
value,
|
|
18
|
+
);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get(rowIndex, columnIndex) {
|
|
23
|
+
return this.matrix.get(
|
|
24
|
+
this.startRow + rowIndex,
|
|
25
|
+
this.startColumn + columnIndex,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -14,13 +14,13 @@ export default class WrapperMatrix1D extends AbstractMatrix {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
set(rowIndex, columnIndex, value) {
|
|
17
|
-
|
|
17
|
+
let index = this._calculateIndex(rowIndex, columnIndex);
|
|
18
18
|
this.data[index] = value;
|
|
19
19
|
return this;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
get(rowIndex, columnIndex) {
|
|
23
|
-
|
|
23
|
+
let index = this._calculateIndex(rowIndex, columnIndex);
|
|
24
24
|
return this.data[index];
|
|
25
25
|
}
|
|
26
26
|
|