ml-matrix 6.8.2 → 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 +68 -19
- package/matrix.d.ts +44 -37
- package/matrix.js +38 -53
- package/matrix.umd.js +1 -1
- package/package.json +13 -12
- package/src/correlation.js +3 -1
- package/src/covariance.js +3 -1
- package/src/dc/nipals.js +3 -1
- package/src/matrix.js +16 -13
- 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/README.md
CHANGED
|
@@ -12,9 +12,9 @@ Matrix manipulation and computation library.
|
|
|
12
12
|
Maintained by <a href="https://www.zakodium.com">Zakodium</a>
|
|
13
13
|
</p>
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
[![NPM version][npm-image]][npm-url]
|
|
16
|
+
[![build status][ci-image]][ci-url]
|
|
17
|
+
[![npm download][download-image]][download-url]
|
|
18
18
|
|
|
19
19
|
</h3>
|
|
20
20
|
|
|
@@ -40,7 +40,7 @@ const { Matrix } = require('ml-matrix');
|
|
|
40
40
|
const matrix = Matrix.ones(5, 5);
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
## [API Documentation](https://mljs.github.io/matrix/
|
|
43
|
+
## [API Documentation](https://mljs.github.io/matrix/)
|
|
44
44
|
|
|
45
45
|
## Examples
|
|
46
46
|
|
|
@@ -49,9 +49,18 @@ const matrix = Matrix.ones(5, 5);
|
|
|
49
49
|
```js
|
|
50
50
|
const { Matrix } = require('ml-matrix');
|
|
51
51
|
|
|
52
|
-
var A = new Matrix([
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
var A = new Matrix([
|
|
53
|
+
[1, 1],
|
|
54
|
+
[2, 2],
|
|
55
|
+
]);
|
|
56
|
+
var B = new Matrix([
|
|
57
|
+
[3, 3],
|
|
58
|
+
[1, 1],
|
|
59
|
+
]);
|
|
60
|
+
var C = new Matrix([
|
|
61
|
+
[3, 3],
|
|
62
|
+
[1, 1],
|
|
63
|
+
]);
|
|
55
64
|
|
|
56
65
|
// ============================
|
|
57
66
|
// Operations with the matrix :
|
|
@@ -75,7 +84,10 @@ C.div(10); // => C = Cinit
|
|
|
75
84
|
C.mod(2); // => C = Cinit % 2
|
|
76
85
|
|
|
77
86
|
// Standard Math operations : (abs, cos, round, etc.)
|
|
78
|
-
var A = new Matrix([
|
|
87
|
+
var A = new Matrix([
|
|
88
|
+
[1, 1],
|
|
89
|
+
[-1, -1],
|
|
90
|
+
]);
|
|
79
91
|
var exponential = Matrix.exp(A); // exponential = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].
|
|
80
92
|
var cosinus = Matrix.cos(A); // cosinus = Matrix [[Math.cos(1), Math.cos(1)], [Math.cos(-1), Math.cos(-1)], rows: 2, columns: 2].
|
|
81
93
|
var absolute = Matrix.abs(A); // expon = absolute [[1, 1], [1, 1], rows: 2, columns: 2].
|
|
@@ -122,24 +134,36 @@ const {
|
|
|
122
134
|
QrDecomposition,
|
|
123
135
|
LuDecomposition,
|
|
124
136
|
CholeskyDecomposition,
|
|
125
|
-
EigenvalueDecomposition
|
|
137
|
+
EigenvalueDecomposition,
|
|
126
138
|
} = require('ml-matrix');
|
|
127
139
|
|
|
128
140
|
//===========================
|
|
129
141
|
// inverse and pseudo-inverse
|
|
130
142
|
//===========================
|
|
131
143
|
|
|
132
|
-
var A = new Matrix([
|
|
144
|
+
var A = new Matrix([
|
|
145
|
+
[2, 3, 5],
|
|
146
|
+
[4, 1, 6],
|
|
147
|
+
[1, 3, 0],
|
|
148
|
+
]);
|
|
133
149
|
var inverseA = inverse(A);
|
|
134
150
|
var B = A.mmul(inverseA); // B = A * inverse(A), so B ~= Identity
|
|
135
151
|
|
|
136
152
|
// if A is singular, you can use SVD :
|
|
137
|
-
var A = new Matrix([
|
|
153
|
+
var A = new Matrix([
|
|
154
|
+
[1, 2, 3],
|
|
155
|
+
[4, 5, 6],
|
|
156
|
+
[7, 8, 9],
|
|
157
|
+
]); // A is singular, so the standard computation of inverse won't work (you can test if you don't trust me^^)
|
|
138
158
|
var inverseA = inverse(A, (useSVD = true)); // inverseA is only an approximation of the inverse, by using the Singular Values Decomposition
|
|
139
159
|
var B = A.mmul(inverseA); // B = A * inverse(A), but inverse(A) is only an approximation, so B doesn't really be identity.
|
|
140
160
|
|
|
141
161
|
// if you want the pseudo-inverse of a matrix :
|
|
142
|
-
var A = new Matrix([
|
|
162
|
+
var A = new Matrix([
|
|
163
|
+
[1, 2],
|
|
164
|
+
[3, 4],
|
|
165
|
+
[5, 6],
|
|
166
|
+
]);
|
|
143
167
|
var pseudoInverseA = A.pseudoInverse();
|
|
144
168
|
var B = A.mmul(pseudoInverseA).mmul(A); // with pseudo inverse, A*pseudo-inverse(A)*A ~= A. It's the case here
|
|
145
169
|
|
|
@@ -151,13 +175,22 @@ var B = A.mmul(pseudoInverseA).mmul(A); // with pseudo inverse, A*pseudo-inverse
|
|
|
151
175
|
// Below, how to solve least square with our function
|
|
152
176
|
|
|
153
177
|
// If A is non singular :
|
|
154
|
-
var A = new Matrix([
|
|
178
|
+
var A = new Matrix([
|
|
179
|
+
[3, 1],
|
|
180
|
+
[4.25, 1],
|
|
181
|
+
[5.5, 1],
|
|
182
|
+
[8, 1],
|
|
183
|
+
]);
|
|
155
184
|
var b = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);
|
|
156
185
|
var x = solve(A, b);
|
|
157
186
|
var error = Matrix.sub(b, A.mmul(x)); // The error enables to evaluate the solution x found.
|
|
158
187
|
|
|
159
188
|
// If A is non singular :
|
|
160
|
-
var A = new Matrix([
|
|
189
|
+
var A = new Matrix([
|
|
190
|
+
[1, 2, 3],
|
|
191
|
+
[4, 5, 6],
|
|
192
|
+
[7, 8, 9],
|
|
193
|
+
]);
|
|
161
194
|
var b = Matrix.columnVector([8, 20, 32]);
|
|
162
195
|
var x = solve(A, b, (useSVD = true)); // there are many solutions. x can be [1, 2, 1].transpose(), or [1.33, 1.33, 1.33].transpose(), etc.
|
|
163
196
|
var error = Matrix.sub(b, A.mmul(x)); // The error enables to evaluate the solution x found.
|
|
@@ -168,7 +201,11 @@ var error = Matrix.sub(b, A.mmul(x)); // The error enables to evaluate the solut
|
|
|
168
201
|
|
|
169
202
|
// QR Decomposition
|
|
170
203
|
|
|
171
|
-
var A = new Matrix([
|
|
204
|
+
var A = new Matrix([
|
|
205
|
+
[2, 3, 5],
|
|
206
|
+
[4, 1, 6],
|
|
207
|
+
[1, 3, 0],
|
|
208
|
+
]);
|
|
172
209
|
var QR = new QrDecomposition(A);
|
|
173
210
|
var Q = QR.orthogonalMatrix;
|
|
174
211
|
var R = QR.upperTriangularMatrix;
|
|
@@ -176,7 +213,11 @@ var R = QR.upperTriangularMatrix;
|
|
|
176
213
|
|
|
177
214
|
// LU Decomposition
|
|
178
215
|
|
|
179
|
-
var A = new Matrix([
|
|
216
|
+
var A = new Matrix([
|
|
217
|
+
[2, 3, 5],
|
|
218
|
+
[4, 1, 6],
|
|
219
|
+
[1, 3, 0],
|
|
220
|
+
]);
|
|
180
221
|
var LU = new LuDecomposition(A);
|
|
181
222
|
var L = LU.lowerTriangularMatrix;
|
|
182
223
|
var U = LU.upperTriangularMatrix;
|
|
@@ -185,13 +226,21 @@ var P = LU.pivotPermutationVector;
|
|
|
185
226
|
|
|
186
227
|
// Cholesky Decomposition
|
|
187
228
|
|
|
188
|
-
var A = new Matrix([
|
|
229
|
+
var A = new Matrix([
|
|
230
|
+
[2, 3, 5],
|
|
231
|
+
[4, 1, 6],
|
|
232
|
+
[1, 3, 0],
|
|
233
|
+
]);
|
|
189
234
|
var cholesky = new CholeskyDecomposition(A);
|
|
190
235
|
var L = cholesky.lowerTriangularMatrix;
|
|
191
236
|
|
|
192
237
|
// Eigenvalues & eigenvectors
|
|
193
238
|
|
|
194
|
-
var A = new Matrix([
|
|
239
|
+
var A = new Matrix([
|
|
240
|
+
[2, 3, 5],
|
|
241
|
+
[4, 1, 6],
|
|
242
|
+
[1, 3, 0],
|
|
243
|
+
]);
|
|
195
244
|
var e = new EigenvalueDecomposition(A);
|
|
196
245
|
var real = e.realEigenvalues;
|
|
197
246
|
var imaginary = e.imaginaryEigenvalues;
|
|
@@ -218,7 +267,7 @@ var dependencies = linearDependencies(A); // dependencies is a matrix with the d
|
|
|
218
267
|
|
|
219
268
|
[npm-image]: https://img.shields.io/npm/v/ml-matrix.svg
|
|
220
269
|
[npm-url]: https://npmjs.org/package/ml-matrix
|
|
221
|
-
[ci-image]: https://github.com/mljs/matrix/workflows/Node.js%20CI/badge.svg?branch=
|
|
270
|
+
[ci-image]: https://github.com/mljs/matrix/workflows/Node.js%20CI/badge.svg?branch=main
|
|
222
271
|
[ci-url]: https://github.com/mljs/matrix/actions?query=workflow%3A%22Node.js+CI%22
|
|
223
272
|
[download-image]: https://img.shields.io/npm/dm/ml-matrix.svg
|
|
224
273
|
[download-url]: https://npmjs.org/package/ml-matrix
|
package/matrix.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type MaybeMatrix = AbstractMatrix | number
|
|
1
|
+
type MaybeMatrix = AbstractMatrix | ArrayLike<ArrayLike<number>>;
|
|
2
2
|
type ScalarOrMatrix = number | MaybeMatrix;
|
|
3
3
|
type MatrixDimension = 'row' | 'column';
|
|
4
4
|
|
|
@@ -59,21 +59,21 @@ export interface IVarianceOptions {
|
|
|
59
59
|
}
|
|
60
60
|
export interface IVarianceByOptions {
|
|
61
61
|
unbiased?: boolean;
|
|
62
|
-
mean?: number
|
|
62
|
+
mean?: ArrayLike<number>;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export interface ICenterOptions {
|
|
66
66
|
center?: number;
|
|
67
67
|
}
|
|
68
68
|
export interface ICenterByOptions {
|
|
69
|
-
center?: number
|
|
69
|
+
center?: ArrayLike<number>;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export interface IScaleOptions {
|
|
73
73
|
scale?: number;
|
|
74
74
|
}
|
|
75
75
|
export interface IScaleByOptions {
|
|
76
|
-
scale?: number
|
|
76
|
+
scale?: ArrayLike<number>;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
export interface ICovarianceOptions {
|
|
@@ -138,7 +138,7 @@ export abstract class AbstractMatrix {
|
|
|
138
138
|
static from1DArray(
|
|
139
139
|
newRows: number,
|
|
140
140
|
newColumns: number,
|
|
141
|
-
newData: number
|
|
141
|
+
newData: ArrayLike<number>,
|
|
142
142
|
): Matrix;
|
|
143
143
|
|
|
144
144
|
/**
|
|
@@ -146,14 +146,14 @@ export abstract class AbstractMatrix {
|
|
|
146
146
|
* @param newData - A 1D array containing data for the vector.
|
|
147
147
|
* @returns The new matrix.
|
|
148
148
|
*/
|
|
149
|
-
static rowVector(newData: number
|
|
149
|
+
static rowVector(newData: ArrayLike<number>): Matrix;
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
152
|
* Creates a column vector, a matrix with only one column.
|
|
153
153
|
* @param newData - A 1D array containing data for the vector.
|
|
154
154
|
* @returns The new matrix.
|
|
155
155
|
*/
|
|
156
|
-
static columnVector(newData: number
|
|
156
|
+
static columnVector(newData: ArrayLike<number>): Matrix;
|
|
157
157
|
|
|
158
158
|
/**
|
|
159
159
|
* Creates a matrix with the given dimensions. Values will be set to zero.
|
|
@@ -219,12 +219,16 @@ export abstract class AbstractMatrix {
|
|
|
219
219
|
* @param columns - Number of columns. Default: `rows`.
|
|
220
220
|
* @returns - The new diagonal matrix.
|
|
221
221
|
*/
|
|
222
|
-
static diag(data: number
|
|
222
|
+
static diag(data: ArrayLike<number>, rows?: number, columns?: number): Matrix;
|
|
223
223
|
|
|
224
224
|
/**
|
|
225
225
|
* Alias for {@link AbstractMatrix.diag}.
|
|
226
226
|
*/
|
|
227
|
-
static diagonal(
|
|
227
|
+
static diagonal(
|
|
228
|
+
data: ArrayLike<number>,
|
|
229
|
+
rows?: number,
|
|
230
|
+
columns?: number,
|
|
231
|
+
): Matrix;
|
|
228
232
|
|
|
229
233
|
/**
|
|
230
234
|
* Returns a matrix whose elements are the minimum between `matrix1` and `matrix2`.
|
|
@@ -379,7 +383,7 @@ export abstract class AbstractMatrix {
|
|
|
379
383
|
* @param index - Row index.
|
|
380
384
|
* @param array - Array or vector to set.
|
|
381
385
|
*/
|
|
382
|
-
setRow(index: number, array: number
|
|
386
|
+
setRow(index: number, array: ArrayLike<number> | AbstractMatrix): this;
|
|
383
387
|
|
|
384
388
|
/**
|
|
385
389
|
* Swap two rows.
|
|
@@ -405,7 +409,7 @@ export abstract class AbstractMatrix {
|
|
|
405
409
|
* @param index - Column index.
|
|
406
410
|
* @param array - Array or vector to set.
|
|
407
411
|
*/
|
|
408
|
-
setColumn(index: number, array: number
|
|
412
|
+
setColumn(index: number, array: ArrayLike<number> | AbstractMatrix): this;
|
|
409
413
|
|
|
410
414
|
/**
|
|
411
415
|
* Swap two columns.
|
|
@@ -418,49 +422,49 @@ export abstract class AbstractMatrix {
|
|
|
418
422
|
* Adds the values of a vector to each row.
|
|
419
423
|
* @param vector - Array or vector.
|
|
420
424
|
*/
|
|
421
|
-
addRowVector(vector: number
|
|
425
|
+
addRowVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
422
426
|
|
|
423
427
|
/**
|
|
424
428
|
* Subtracts the values of a vector from each row.
|
|
425
429
|
* @param vector - Array or vector.
|
|
426
430
|
*/
|
|
427
|
-
subRowVector(vector: number
|
|
431
|
+
subRowVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
428
432
|
|
|
429
433
|
/**
|
|
430
434
|
* Multiplies the values of a vector with each row.
|
|
431
435
|
* @param vector - Array or vector.
|
|
432
436
|
*/
|
|
433
|
-
mulRowVector(vector: number
|
|
437
|
+
mulRowVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
434
438
|
|
|
435
439
|
/**
|
|
436
440
|
* Divides the values of each row by those of a vector.
|
|
437
441
|
* @param vector - Array or vector.
|
|
438
442
|
*/
|
|
439
|
-
divRowVector(vector: number
|
|
443
|
+
divRowVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
440
444
|
|
|
441
445
|
/**
|
|
442
446
|
* Adds the values of a vector to each column.
|
|
443
447
|
* @param vector - Array or vector.
|
|
444
448
|
*/
|
|
445
|
-
addColumnVector(vector: number
|
|
449
|
+
addColumnVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
446
450
|
|
|
447
451
|
/**
|
|
448
452
|
* Subtracts the values of a vector from each column.
|
|
449
453
|
* @param vector - Array or vector.
|
|
450
454
|
*/
|
|
451
|
-
subColumnVector(vector: number
|
|
455
|
+
subColumnVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
452
456
|
|
|
453
457
|
/**
|
|
454
458
|
* Multiplies the values of a vector with each column.
|
|
455
459
|
* @param vector - Array or vector.
|
|
456
460
|
*/
|
|
457
|
-
mulColumnVector(vector: number
|
|
461
|
+
mulColumnVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
458
462
|
|
|
459
463
|
/**
|
|
460
464
|
* Divides the values of each column by those of a vector.
|
|
461
465
|
* @param vector - Array or vector.
|
|
462
466
|
*/
|
|
463
|
-
divColumnVector(vector: number
|
|
467
|
+
divColumnVector(vector: ArrayLike<number> | AbstractMatrix): this;
|
|
464
468
|
|
|
465
469
|
/**
|
|
466
470
|
* Multiplies the values of a row with a scalar.
|
|
@@ -608,7 +612,7 @@ export abstract class AbstractMatrix {
|
|
|
608
612
|
* @param other - Other matrix.
|
|
609
613
|
*/
|
|
610
614
|
kroneckerProduct(other: MaybeMatrix): Matrix;
|
|
611
|
-
|
|
615
|
+
|
|
612
616
|
/**
|
|
613
617
|
* Returns the Kronecker sum between `this` and `other`.
|
|
614
618
|
* @link https://en.wikipedia.org/wiki/Kronecker_product#Kronecker_sum
|
|
@@ -659,7 +663,7 @@ export abstract class AbstractMatrix {
|
|
|
659
663
|
* @param endColumn - Last column index. Default: `this.columns - 1`.
|
|
660
664
|
*/
|
|
661
665
|
subMatrixRow(
|
|
662
|
-
indices: number
|
|
666
|
+
indices: ArrayLike<number>,
|
|
663
667
|
startColumn?: number,
|
|
664
668
|
endColumn?: number,
|
|
665
669
|
): Matrix;
|
|
@@ -671,7 +675,7 @@ export abstract class AbstractMatrix {
|
|
|
671
675
|
* @param endRow - Last row index. Default: `this.rows - 1`.
|
|
672
676
|
*/
|
|
673
677
|
subMatrixColumn(
|
|
674
|
-
indices: number
|
|
678
|
+
indices: ArrayLike<number>,
|
|
675
679
|
startRow?: number,
|
|
676
680
|
endRow?: number,
|
|
677
681
|
): Matrix;
|
|
@@ -683,7 +687,7 @@ export abstract class AbstractMatrix {
|
|
|
683
687
|
* @param startColumn - The index of the first column to set.
|
|
684
688
|
*/
|
|
685
689
|
setSubMatrix(
|
|
686
|
-
matrix: MaybeMatrix
|
|
690
|
+
matrix: MaybeMatrix,
|
|
687
691
|
startRow: number,
|
|
688
692
|
startColumn: number,
|
|
689
693
|
): this;
|
|
@@ -694,7 +698,10 @@ export abstract class AbstractMatrix {
|
|
|
694
698
|
* @param rowIndices - The row indices to select.
|
|
695
699
|
* @param columnIndices - The column indices to select.
|
|
696
700
|
*/
|
|
697
|
-
selection(
|
|
701
|
+
selection(
|
|
702
|
+
rowIndices: ArrayLike<number>,
|
|
703
|
+
columnIndices: ArrayLike<number>,
|
|
704
|
+
): Matrix;
|
|
698
705
|
|
|
699
706
|
/**
|
|
700
707
|
* Returns the trace of the matrix (sum of the diagonal elements).
|
|
@@ -912,7 +919,7 @@ export abstract class AbstractMatrix {
|
|
|
912
919
|
|
|
913
920
|
export class Matrix extends AbstractMatrix {
|
|
914
921
|
constructor(nRows: number, nColumns: number);
|
|
915
|
-
constructor(data: number
|
|
922
|
+
constructor(data: ArrayLike<ArrayLike<number>>);
|
|
916
923
|
constructor(otherMatrix: AbstractMatrix);
|
|
917
924
|
|
|
918
925
|
/**
|
|
@@ -932,14 +939,14 @@ export class Matrix extends AbstractMatrix {
|
|
|
932
939
|
* @param index - Column index. Default: `this.columns`.
|
|
933
940
|
* @param array - Column to add.
|
|
934
941
|
*/
|
|
935
|
-
addColumn(index: number, array: number
|
|
942
|
+
addColumn(index: number, array: ArrayLike<number> | AbstractMatrix): this;
|
|
936
943
|
|
|
937
944
|
/**
|
|
938
945
|
* Adds a new row to the matrix (in place).
|
|
939
946
|
* @param index - Row index. Default: `this.rows`.
|
|
940
947
|
* @param array - Row to add.
|
|
941
948
|
*/
|
|
942
|
-
addRow(index: number, array: number
|
|
949
|
+
addRow(index: number, array: ArrayLike<number> | AbstractMatrix): this;
|
|
943
950
|
}
|
|
944
951
|
|
|
945
952
|
export default Matrix;
|
|
@@ -949,7 +956,7 @@ export class MatrixColumnView extends AbstractMatrix {
|
|
|
949
956
|
}
|
|
950
957
|
|
|
951
958
|
export class MatrixColumnSelectionView extends AbstractMatrix {
|
|
952
|
-
constructor(matrix: AbstractMatrix, columnIndices: number
|
|
959
|
+
constructor(matrix: AbstractMatrix, columnIndices: ArrayLike<number>);
|
|
953
960
|
}
|
|
954
961
|
|
|
955
962
|
export class MatrixFlipColumnView extends AbstractMatrix {
|
|
@@ -965,14 +972,14 @@ export class MatrixRowView extends AbstractMatrix {
|
|
|
965
972
|
}
|
|
966
973
|
|
|
967
974
|
export class MatrixRowSelectionView extends AbstractMatrix {
|
|
968
|
-
constructor(matrix: AbstractMatrix, rowIndices: number
|
|
975
|
+
constructor(matrix: AbstractMatrix, rowIndices: ArrayLike<number>);
|
|
969
976
|
}
|
|
970
977
|
|
|
971
978
|
export class MatrixSelectionView extends AbstractMatrix {
|
|
972
979
|
constructor(
|
|
973
980
|
matrix: AbstractMatrix,
|
|
974
|
-
rowIndices: number
|
|
975
|
-
columnIndices: number
|
|
981
|
+
rowIndices: ArrayLike<number>,
|
|
982
|
+
columnIndices: ArrayLike<number>,
|
|
976
983
|
);
|
|
977
984
|
}
|
|
978
985
|
|
|
@@ -998,18 +1005,18 @@ export interface IWrap1DOptions {
|
|
|
998
1005
|
}
|
|
999
1006
|
|
|
1000
1007
|
export function wrap(
|
|
1001
|
-
array: number
|
|
1008
|
+
array: ArrayLike<number>,
|
|
1002
1009
|
options?: IWrap1DOptions,
|
|
1003
1010
|
): WrapperMatrix1D;
|
|
1004
1011
|
|
|
1005
|
-
export function wrap(twoDAray: number
|
|
1012
|
+
export function wrap(twoDAray: ArrayLike<ArrayLike<number>>): WrapperMatrix2D;
|
|
1006
1013
|
|
|
1007
1014
|
export class WrapperMatrix1D extends AbstractMatrix {
|
|
1008
|
-
constructor(data: number
|
|
1015
|
+
constructor(data: ArrayLike<number>, options?: IWrap1DOptions);
|
|
1009
1016
|
}
|
|
1010
1017
|
|
|
1011
1018
|
export class WrapperMatrix2D extends AbstractMatrix {
|
|
1012
|
-
constructor(data: number
|
|
1019
|
+
constructor(data: ArrayLike<ArrayLike<number>>);
|
|
1013
1020
|
}
|
|
1014
1021
|
|
|
1015
1022
|
/**
|
|
@@ -1133,7 +1140,7 @@ export class SingularValueDecomposition {
|
|
|
1133
1140
|
* @returns - The vector x.
|
|
1134
1141
|
*/
|
|
1135
1142
|
solve(value: Matrix): Matrix;
|
|
1136
|
-
solveForDiagonal(value: number
|
|
1143
|
+
solveForDiagonal(value: ArrayLike<number>): Matrix;
|
|
1137
1144
|
readonly norm2: number;
|
|
1138
1145
|
readonly threshold: number;
|
|
1139
1146
|
readonly leftSingularVectors: Matrix;
|
|
@@ -1223,7 +1230,7 @@ export interface INipalsOptions {
|
|
|
1223
1230
|
/**
|
|
1224
1231
|
* A column vector of length `X.rows` that contains known labels for supervised PLS.
|
|
1225
1232
|
*/
|
|
1226
|
-
Y?: MaybeMatrix | number
|
|
1233
|
+
Y?: MaybeMatrix | ArrayLike<number>;
|
|
1227
1234
|
/**
|
|
1228
1235
|
* The maximum number of allowed iterations before beraking the loop if convergence is not achieved.
|
|
1229
1236
|
* @default 1000
|
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) {
|
|
@@ -2454,12 +2437,13 @@ class AbstractMatrix {
|
|
|
2454
2437
|
}
|
|
2455
2438
|
|
|
2456
2439
|
selection(rowIndices, columnIndices) {
|
|
2457
|
-
|
|
2440
|
+
checkRowIndices(this, rowIndices);
|
|
2441
|
+
checkColumnIndices(this, columnIndices);
|
|
2458
2442
|
let newMatrix = new Matrix(rowIndices.length, columnIndices.length);
|
|
2459
|
-
for (let i = 0; i <
|
|
2460
|
-
let rowIndex =
|
|
2461
|
-
for (let j = 0; j <
|
|
2462
|
-
let columnIndex =
|
|
2443
|
+
for (let i = 0; i < rowIndices.length; i++) {
|
|
2444
|
+
let rowIndex = rowIndices[i];
|
|
2445
|
+
for (let j = 0; j < columnIndices.length; j++) {
|
|
2446
|
+
let columnIndex = columnIndices[j];
|
|
2463
2447
|
newMatrix.set(i, j, this.get(rowIndex, columnIndex));
|
|
2464
2448
|
}
|
|
2465
2449
|
}
|
|
@@ -2547,13 +2531,13 @@ class AbstractMatrix {
|
|
|
2547
2531
|
}
|
|
2548
2532
|
switch (by) {
|
|
2549
2533
|
case 'row': {
|
|
2550
|
-
if (!
|
|
2534
|
+
if (!isAnyArray.isAnyArray(mean)) {
|
|
2551
2535
|
throw new TypeError('mean must be an array');
|
|
2552
2536
|
}
|
|
2553
2537
|
return varianceByRow(this, unbiased, mean);
|
|
2554
2538
|
}
|
|
2555
2539
|
case 'column': {
|
|
2556
|
-
if (!
|
|
2540
|
+
if (!isAnyArray.isAnyArray(mean)) {
|
|
2557
2541
|
throw new TypeError('mean must be an array');
|
|
2558
2542
|
}
|
|
2559
2543
|
return varianceByColumn(this, unbiased, mean);
|
|
@@ -2596,14 +2580,14 @@ class AbstractMatrix {
|
|
|
2596
2580
|
const { center = this.mean(by) } = options;
|
|
2597
2581
|
switch (by) {
|
|
2598
2582
|
case 'row': {
|
|
2599
|
-
if (!
|
|
2583
|
+
if (!isAnyArray.isAnyArray(center)) {
|
|
2600
2584
|
throw new TypeError('center must be an array');
|
|
2601
2585
|
}
|
|
2602
2586
|
centerByRow(this, center);
|
|
2603
2587
|
return this;
|
|
2604
2588
|
}
|
|
2605
2589
|
case 'column': {
|
|
2606
|
-
if (!
|
|
2590
|
+
if (!isAnyArray.isAnyArray(center)) {
|
|
2607
2591
|
throw new TypeError('center must be an array');
|
|
2608
2592
|
}
|
|
2609
2593
|
centerByColumn(this, center);
|
|
@@ -2634,7 +2618,7 @@ class AbstractMatrix {
|
|
|
2634
2618
|
case 'row': {
|
|
2635
2619
|
if (scale === undefined) {
|
|
2636
2620
|
scale = getScaleByRow(this);
|
|
2637
|
-
} else if (!
|
|
2621
|
+
} else if (!isAnyArray.isAnyArray(scale)) {
|
|
2638
2622
|
throw new TypeError('scale must be an array');
|
|
2639
2623
|
}
|
|
2640
2624
|
scaleByRow(this, scale);
|
|
@@ -2643,7 +2627,7 @@ class AbstractMatrix {
|
|
|
2643
2627
|
case 'column': {
|
|
2644
2628
|
if (scale === undefined) {
|
|
2645
2629
|
scale = getScaleByColumn(this);
|
|
2646
|
-
} else if (!
|
|
2630
|
+
} else if (!isAnyArray.isAnyArray(scale)) {
|
|
2647
2631
|
throw new TypeError('scale must be an array');
|
|
2648
2632
|
}
|
|
2649
2633
|
scaleByColumn(this, scale);
|
|
@@ -2704,7 +2688,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2704
2688
|
} else {
|
|
2705
2689
|
throw new TypeError('nColumns must be a positive integer');
|
|
2706
2690
|
}
|
|
2707
|
-
} else if (
|
|
2691
|
+
} else if (isAnyArray.isAnyArray(nRows)) {
|
|
2708
2692
|
// Copy the values from the 2D array
|
|
2709
2693
|
const arrayData = nRows;
|
|
2710
2694
|
nRows = arrayData.length;
|
|
@@ -2828,7 +2812,7 @@ class MatrixColumnView extends BaseView {
|
|
|
2828
2812
|
|
|
2829
2813
|
class MatrixColumnSelectionView extends BaseView {
|
|
2830
2814
|
constructor(matrix, columnIndices) {
|
|
2831
|
-
|
|
2815
|
+
checkColumnIndices(matrix, columnIndices);
|
|
2832
2816
|
super(matrix, matrix.rows, columnIndices.length);
|
|
2833
2817
|
this.columnIndices = columnIndices;
|
|
2834
2818
|
}
|
|
@@ -2892,7 +2876,7 @@ class MatrixRowView extends BaseView {
|
|
|
2892
2876
|
|
|
2893
2877
|
class MatrixRowSelectionView extends BaseView {
|
|
2894
2878
|
constructor(matrix, rowIndices) {
|
|
2895
|
-
|
|
2879
|
+
checkRowIndices(matrix, rowIndices);
|
|
2896
2880
|
super(matrix, rowIndices.length, matrix.columns);
|
|
2897
2881
|
this.rowIndices = rowIndices;
|
|
2898
2882
|
}
|
|
@@ -2909,10 +2893,11 @@ class MatrixRowSelectionView extends BaseView {
|
|
|
2909
2893
|
|
|
2910
2894
|
class MatrixSelectionView extends BaseView {
|
|
2911
2895
|
constructor(matrix, rowIndices, columnIndices) {
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
this.
|
|
2896
|
+
checkRowIndices(matrix, rowIndices);
|
|
2897
|
+
checkColumnIndices(matrix, columnIndices);
|
|
2898
|
+
super(matrix, rowIndices.length, columnIndices.length);
|
|
2899
|
+
this.rowIndices = rowIndices;
|
|
2900
|
+
this.columnIndices = columnIndices;
|
|
2916
2901
|
}
|
|
2917
2902
|
|
|
2918
2903
|
set(rowIndex, columnIndex, value) {
|
|
@@ -3020,8 +3005,8 @@ class WrapperMatrix2D extends AbstractMatrix {
|
|
|
3020
3005
|
}
|
|
3021
3006
|
|
|
3022
3007
|
function wrap(array, options) {
|
|
3023
|
-
if (
|
|
3024
|
-
if (array[0] &&
|
|
3008
|
+
if (isAnyArray.isAnyArray(array)) {
|
|
3009
|
+
if (array[0] && isAnyArray.isAnyArray(array[0])) {
|
|
3025
3010
|
return new WrapperMatrix2D(array);
|
|
3026
3011
|
} else {
|
|
3027
3012
|
return new WrapperMatrix1D(array, options);
|
|
@@ -4021,7 +4006,7 @@ function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4021
4006
|
if (
|
|
4022
4007
|
typeof yMatrix === 'object' &&
|
|
4023
4008
|
!Matrix.isMatrix(yMatrix) &&
|
|
4024
|
-
!
|
|
4009
|
+
!isAnyArray.isAnyArray(yMatrix)
|
|
4025
4010
|
) {
|
|
4026
4011
|
options = yMatrix;
|
|
4027
4012
|
yMatrix = xMatrix;
|
|
@@ -4054,7 +4039,7 @@ function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4054
4039
|
if (
|
|
4055
4040
|
typeof yMatrix === 'object' &&
|
|
4056
4041
|
!Matrix.isMatrix(yMatrix) &&
|
|
4057
|
-
!
|
|
4042
|
+
!isAnyArray.isAnyArray(yMatrix)
|
|
4058
4043
|
) {
|
|
4059
4044
|
options = yMatrix;
|
|
4060
4045
|
yMatrix = xMatrix;
|
|
@@ -4990,7 +4975,7 @@ class nipals {
|
|
|
4990
4975
|
|
|
4991
4976
|
let u;
|
|
4992
4977
|
if (Y) {
|
|
4993
|
-
if (
|
|
4978
|
+
if (isAnyArray.isAnyArray(Y) && typeof Y[0] === 'number') {
|
|
4994
4979
|
Y = Matrix.columnVector(Y);
|
|
4995
4980
|
} else {
|
|
4996
4981
|
Y = WrapperMatrix2D.checkMatrix(Y);
|