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 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
- [![NPM version][npm-image]][npm-url]
16
- [![build status][ci-image]][ci-url]
17
- [![npm download][download-image]][download-url]
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/globals.html)
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([[1, 1], [2, 2]]);
53
- var B = new Matrix([[3, 3], [1, 1]]);
54
- var C = new Matrix([[3, 3], [1, 1]]);
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,8 +84,11 @@ 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([[1, 1], [-1, -1]]);
79
- var expon = Matrix.exp(A); // expon = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].
87
+ var A = new Matrix([
88
+ [1, 1],
89
+ [-1, -1],
90
+ ]);
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].
82
94
  // you can use 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'
@@ -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([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
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([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // A is singular, so the standard computation of inverse won't work (you can test if you don't trust me^^)
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([[1, 2], [3, 4], [5, 6]]);
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([[3, 1], [4.25, 1], [5.5, 1], [8, 1]]);
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([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
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([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
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([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
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([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
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([[2, 3, 5], [4, 1, 6], [1, 3, 0]]);
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=master
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[]): Matrix;
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[]): Matrix;
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[], rows?: number, columns?: number): Matrix;
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(data: number[], rows?: number, columns?: number): Matrix;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
467
+ divColumnVector(vector: ArrayLike<number> | AbstractMatrix): this;
464
468
 
465
469
  /**
466
470
  * Multiplies the values of a row with a scalar.
@@ -481,6 +485,12 @@ export abstract class AbstractMatrix {
481
485
  */
482
486
  max(): number;
483
487
 
488
+ /**
489
+ * Returns the maximum value by the given dimension.
490
+ * @param by - max by 'row' or 'column'.
491
+ */
492
+ max(by: MatrixDimension): number[];
493
+
484
494
  /**
485
495
  * Returns the index of the maximum value.
486
496
  */
@@ -491,6 +501,12 @@ export abstract class AbstractMatrix {
491
501
  */
492
502
  min(): number;
493
503
 
504
+ /**
505
+ * Returns the minimum value by the given dimension.
506
+ * @param by - min by 'row' or 'column'.
507
+ */
508
+ min(by: MatrixDimension): number[];
509
+
494
510
  /**
495
511
  * Returns the index of the minimum value.
496
512
  */
@@ -608,7 +624,7 @@ export abstract class AbstractMatrix {
608
624
  * @param other - Other matrix.
609
625
  */
610
626
  kroneckerProduct(other: MaybeMatrix): Matrix;
611
-
627
+
612
628
  /**
613
629
  * Returns the Kronecker sum between `this` and `other`.
614
630
  * @link https://en.wikipedia.org/wiki/Kronecker_product#Kronecker_sum
@@ -659,7 +675,7 @@ export abstract class AbstractMatrix {
659
675
  * @param endColumn - Last column index. Default: `this.columns - 1`.
660
676
  */
661
677
  subMatrixRow(
662
- indices: number[],
678
+ indices: ArrayLike<number>,
663
679
  startColumn?: number,
664
680
  endColumn?: number,
665
681
  ): Matrix;
@@ -671,7 +687,7 @@ export abstract class AbstractMatrix {
671
687
  * @param endRow - Last row index. Default: `this.rows - 1`.
672
688
  */
673
689
  subMatrixColumn(
674
- indices: number[],
690
+ indices: ArrayLike<number>,
675
691
  startRow?: number,
676
692
  endRow?: number,
677
693
  ): Matrix;
@@ -683,7 +699,7 @@ export abstract class AbstractMatrix {
683
699
  * @param startColumn - The index of the first column to set.
684
700
  */
685
701
  setSubMatrix(
686
- matrix: MaybeMatrix | number[],
702
+ matrix: MaybeMatrix,
687
703
  startRow: number,
688
704
  startColumn: number,
689
705
  ): this;
@@ -694,7 +710,10 @@ export abstract class AbstractMatrix {
694
710
  * @param rowIndices - The row indices to select.
695
711
  * @param columnIndices - The column indices to select.
696
712
  */
697
- selection(rowIndices: number[], columnIndices: number[]): Matrix;
713
+ selection(
714
+ rowIndices: ArrayLike<number>,
715
+ columnIndices: ArrayLike<number>,
716
+ ): Matrix;
698
717
 
699
718
  /**
700
719
  * Returns the trace of the matrix (sum of the diagonal elements).
@@ -912,7 +931,7 @@ export abstract class AbstractMatrix {
912
931
 
913
932
  export class Matrix extends AbstractMatrix {
914
933
  constructor(nRows: number, nColumns: number);
915
- constructor(data: number[][]);
934
+ constructor(data: ArrayLike<ArrayLike<number>>);
916
935
  constructor(otherMatrix: AbstractMatrix);
917
936
 
918
937
  /**
@@ -932,14 +951,14 @@ export class Matrix extends AbstractMatrix {
932
951
  * @param index - Column index. Default: `this.columns`.
933
952
  * @param array - Column to add.
934
953
  */
935
- addColumn(index: number, array: number[] | AbstractMatrix): this;
954
+ addColumn(index: number, array: ArrayLike<number> | AbstractMatrix): this;
936
955
 
937
956
  /**
938
957
  * Adds a new row to the matrix (in place).
939
958
  * @param index - Row index. Default: `this.rows`.
940
959
  * @param array - Row to add.
941
960
  */
942
- addRow(index: number, array: number[] | AbstractMatrix): this;
961
+ addRow(index: number, array: ArrayLike<number> | AbstractMatrix): this;
943
962
  }
944
963
 
945
964
  export default Matrix;
@@ -949,7 +968,7 @@ export class MatrixColumnView extends AbstractMatrix {
949
968
  }
950
969
 
951
970
  export class MatrixColumnSelectionView extends AbstractMatrix {
952
- constructor(matrix: AbstractMatrix, columnIndices: number[]);
971
+ constructor(matrix: AbstractMatrix, columnIndices: ArrayLike<number>);
953
972
  }
954
973
 
955
974
  export class MatrixFlipColumnView extends AbstractMatrix {
@@ -965,14 +984,14 @@ export class MatrixRowView extends AbstractMatrix {
965
984
  }
966
985
 
967
986
  export class MatrixRowSelectionView extends AbstractMatrix {
968
- constructor(matrix: AbstractMatrix, rowIndices: number[]);
987
+ constructor(matrix: AbstractMatrix, rowIndices: ArrayLike<number>);
969
988
  }
970
989
 
971
990
  export class MatrixSelectionView extends AbstractMatrix {
972
991
  constructor(
973
992
  matrix: AbstractMatrix,
974
- rowIndices: number[],
975
- columnIndices: number[],
993
+ rowIndices: ArrayLike<number>,
994
+ columnIndices: ArrayLike<number>,
976
995
  );
977
996
  }
978
997
 
@@ -998,18 +1017,18 @@ export interface IWrap1DOptions {
998
1017
  }
999
1018
 
1000
1019
  export function wrap(
1001
- array: number[],
1020
+ array: ArrayLike<number>,
1002
1021
  options?: IWrap1DOptions,
1003
1022
  ): WrapperMatrix1D;
1004
1023
 
1005
- export function wrap(twoDAray: number[][]): WrapperMatrix2D;
1024
+ export function wrap(twoDAray: ArrayLike<ArrayLike<number>>): WrapperMatrix2D;
1006
1025
 
1007
1026
  export class WrapperMatrix1D extends AbstractMatrix {
1008
- constructor(data: number[], options?: IWrap1DOptions);
1027
+ constructor(data: ArrayLike<number>, options?: IWrap1DOptions);
1009
1028
  }
1010
1029
 
1011
1030
  export class WrapperMatrix2D extends AbstractMatrix {
1012
- constructor(data: number[][]);
1031
+ constructor(data: ArrayLike<ArrayLike<number>>);
1013
1032
  }
1014
1033
 
1015
1034
  /**
@@ -1133,7 +1152,7 @@ export class SingularValueDecomposition {
1133
1152
  * @returns - The vector x.
1134
1153
  */
1135
1154
  solve(value: Matrix): Matrix;
1136
- solveForDiagonal(value: number[]): Matrix;
1155
+ solveForDiagonal(value: ArrayLike<number>): Matrix;
1137
1156
  readonly norm2: number;
1138
1157
  readonly threshold: number;
1139
1158
  readonly leftSingularVectors: Matrix;
@@ -1223,7 +1242,7 @@ export interface INipalsOptions {
1223
1242
  /**
1224
1243
  * A column vector of length `X.rows` that contains known labels for supervised PLS.
1225
1244
  */
1226
- Y?: MaybeMatrix | number[];
1245
+ Y?: MaybeMatrix | ArrayLike<number>;
1227
1246
  /**
1228
1247
  * The maximum number of allowed iterations before beraking the loop if convergence is not achieved.
1229
1248
  * @default 1000