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 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.
@@ -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 | number[],
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(rowIndices: number[], columnIndices: number[]): Matrix;
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[] | AbstractMatrix): this;
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[] | AbstractMatrix): this;
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[][]): WrapperMatrix2D;
1012
+ export function wrap(twoDAray: ArrayLike<ArrayLike<number>>): WrapperMatrix2D;
1006
1013
 
1007
1014
  export class WrapperMatrix1D extends AbstractMatrix {
1008
- constructor(data: number[], options?: IWrap1DOptions);
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[]): Matrix;
1143
+ solveForDiagonal(value: ArrayLike<number>): Matrix;
1137
1144
  readonly norm2: number;
1138
1145
  readonly threshold: number;
1139
1146
  readonly leftSingularVectors: Matrix;
@@ -1223,12 +1230,12 @@ 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
1230
1237
  */
1231
- maxIterations?: boolean;
1238
+ maxIterations?: number;
1232
1239
  /**
1233
1240
  * Termination criteria
1234
1241
  * @default 1e-10
@@ -1249,13 +1256,13 @@ export class Nipals {
1249
1256
  constructor(X: MaybeMatrix, options?: INipalsOptions);
1250
1257
  w: Matrix;
1251
1258
  s: Matrix;
1252
- t: number;
1259
+ t: Matrix;
1253
1260
  xResidual: Matrix;
1254
1261
  p: Matrix;
1255
1262
  q: Matrix;
1256
- u: number;
1263
+ u: Matrix;
1257
1264
  yResidual: Matrix;
1258
- betas: number;
1265
+ betas: Matrix;
1259
1266
  }
1260
1267
 
1261
1268
  export { Nipals as NIPALS };