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/matrix.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
checkColumnIndex,
|
|
7
7
|
checkColumnVector,
|
|
8
8
|
checkRange,
|
|
9
|
-
checkIndices
|
|
9
|
+
checkIndices,
|
|
10
10
|
} from './util';
|
|
11
11
|
import {
|
|
12
12
|
sumByRow,
|
|
@@ -26,20 +26,20 @@ import {
|
|
|
26
26
|
scaleAll,
|
|
27
27
|
getScaleByRow,
|
|
28
28
|
getScaleByColumn,
|
|
29
|
-
getScaleAll
|
|
29
|
+
getScaleAll,
|
|
30
30
|
} from './stat';
|
|
31
31
|
import { inspectMatrix } from './inspect';
|
|
32
32
|
import { installMathOperations } from './mathOperations';
|
|
33
33
|
|
|
34
34
|
export class AbstractMatrix {
|
|
35
35
|
static from1DArray(newRows, newColumns, newData) {
|
|
36
|
-
|
|
36
|
+
let length = newRows * newColumns;
|
|
37
37
|
if (length !== newData.length) {
|
|
38
38
|
throw new RangeError('data length does not match given dimensions');
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
for (
|
|
42
|
-
for (
|
|
40
|
+
let newMatrix = new Matrix(newRows, newColumns);
|
|
41
|
+
for (let row = 0; row < newRows; row++) {
|
|
42
|
+
for (let column = 0; column < newColumns; column++) {
|
|
43
43
|
newMatrix.set(row, column, newData[row * newColumns + column]);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -47,16 +47,16 @@ export class AbstractMatrix {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
static rowVector(newData) {
|
|
50
|
-
|
|
51
|
-
for (
|
|
50
|
+
let vector = new Matrix(1, newData.length);
|
|
51
|
+
for (let i = 0; i < newData.length; i++) {
|
|
52
52
|
vector.set(0, i, newData[i]);
|
|
53
53
|
}
|
|
54
54
|
return vector;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
static columnVector(newData) {
|
|
58
|
-
|
|
59
|
-
for (
|
|
58
|
+
let vector = new Matrix(newData.length, 1);
|
|
59
|
+
for (let i = 0; i < newData.length; i++) {
|
|
60
60
|
vector.set(i, 0, newData[i]);
|
|
61
61
|
}
|
|
62
62
|
return vector;
|
|
@@ -75,9 +75,9 @@ export class AbstractMatrix {
|
|
|
75
75
|
throw new TypeError('options must be an object');
|
|
76
76
|
}
|
|
77
77
|
const { random = Math.random } = options;
|
|
78
|
-
|
|
79
|
-
for (
|
|
80
|
-
for (
|
|
78
|
+
let matrix = new Matrix(rows, columns);
|
|
79
|
+
for (let i = 0; i < rows; i++) {
|
|
80
|
+
for (let j = 0; j < columns; j++) {
|
|
81
81
|
matrix.set(i, j, random());
|
|
82
82
|
}
|
|
83
83
|
}
|
|
@@ -92,11 +92,11 @@ export class AbstractMatrix {
|
|
|
92
92
|
if (!Number.isInteger(min)) throw new TypeError('min must be an integer');
|
|
93
93
|
if (!Number.isInteger(max)) throw new TypeError('max must be an integer');
|
|
94
94
|
if (min >= max) throw new RangeError('min must be smaller than max');
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
for (
|
|
98
|
-
for (
|
|
99
|
-
|
|
95
|
+
let interval = max - min;
|
|
96
|
+
let matrix = new Matrix(rows, columns);
|
|
97
|
+
for (let i = 0; i < rows; i++) {
|
|
98
|
+
for (let j = 0; j < columns; j++) {
|
|
99
|
+
let value = min + Math.round(random() * interval);
|
|
100
100
|
matrix.set(i, j, value);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -106,21 +106,21 @@ export class AbstractMatrix {
|
|
|
106
106
|
static eye(rows, columns, value) {
|
|
107
107
|
if (columns === undefined) columns = rows;
|
|
108
108
|
if (value === undefined) value = 1;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
for (
|
|
109
|
+
let min = Math.min(rows, columns);
|
|
110
|
+
let matrix = this.zeros(rows, columns);
|
|
111
|
+
for (let i = 0; i < min; i++) {
|
|
112
112
|
matrix.set(i, i, value);
|
|
113
113
|
}
|
|
114
114
|
return matrix;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
static diag(data, rows, columns) {
|
|
118
|
-
|
|
118
|
+
let l = data.length;
|
|
119
119
|
if (rows === undefined) rows = l;
|
|
120
120
|
if (columns === undefined) columns = rows;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
for (
|
|
121
|
+
let min = Math.min(l, rows, columns);
|
|
122
|
+
let matrix = this.zeros(rows, columns);
|
|
123
|
+
for (let i = 0; i < min; i++) {
|
|
124
124
|
matrix.set(i, i, data[i]);
|
|
125
125
|
}
|
|
126
126
|
return matrix;
|
|
@@ -129,11 +129,11 @@ export class AbstractMatrix {
|
|
|
129
129
|
static min(matrix1, matrix2) {
|
|
130
130
|
matrix1 = this.checkMatrix(matrix1);
|
|
131
131
|
matrix2 = this.checkMatrix(matrix2);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
for (
|
|
136
|
-
for (
|
|
132
|
+
let rows = matrix1.rows;
|
|
133
|
+
let columns = matrix1.columns;
|
|
134
|
+
let result = new Matrix(rows, columns);
|
|
135
|
+
for (let i = 0; i < rows; i++) {
|
|
136
|
+
for (let j = 0; j < columns; j++) {
|
|
137
137
|
result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j)));
|
|
138
138
|
}
|
|
139
139
|
}
|
|
@@ -143,11 +143,11 @@ export class AbstractMatrix {
|
|
|
143
143
|
static max(matrix1, matrix2) {
|
|
144
144
|
matrix1 = this.checkMatrix(matrix1);
|
|
145
145
|
matrix2 = this.checkMatrix(matrix2);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
for (
|
|
150
|
-
for (
|
|
146
|
+
let rows = matrix1.rows;
|
|
147
|
+
let columns = matrix1.columns;
|
|
148
|
+
let result = new this(rows, columns);
|
|
149
|
+
for (let i = 0; i < rows; i++) {
|
|
150
|
+
for (let j = 0; j < columns; j++) {
|
|
151
151
|
result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j)));
|
|
152
152
|
}
|
|
153
153
|
}
|
|
@@ -170,8 +170,8 @@ export class AbstractMatrix {
|
|
|
170
170
|
if (typeof callback !== 'function') {
|
|
171
171
|
throw new TypeError('callback must be a function');
|
|
172
172
|
}
|
|
173
|
-
for (
|
|
174
|
-
for (
|
|
173
|
+
for (let i = 0; i < this.rows; i++) {
|
|
174
|
+
for (let j = 0; j < this.columns; j++) {
|
|
175
175
|
callback.call(this, i, j);
|
|
176
176
|
}
|
|
177
177
|
}
|
|
@@ -179,9 +179,9 @@ export class AbstractMatrix {
|
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
to1DArray() {
|
|
182
|
-
|
|
183
|
-
for (
|
|
184
|
-
for (
|
|
182
|
+
let array = [];
|
|
183
|
+
for (let i = 0; i < this.rows; i++) {
|
|
184
|
+
for (let j = 0; j < this.columns; j++) {
|
|
185
185
|
array.push(this.get(i, j));
|
|
186
186
|
}
|
|
187
187
|
}
|
|
@@ -189,10 +189,10 @@ export class AbstractMatrix {
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
to2DArray() {
|
|
192
|
-
|
|
193
|
-
for (
|
|
192
|
+
let copy = [];
|
|
193
|
+
for (let i = 0; i < this.rows; i++) {
|
|
194
194
|
copy.push([]);
|
|
195
|
-
for (
|
|
195
|
+
for (let j = 0; j < this.columns; j++) {
|
|
196
196
|
copy[i].push(this.get(i, j));
|
|
197
197
|
}
|
|
198
198
|
}
|
|
@@ -221,8 +221,8 @@ export class AbstractMatrix {
|
|
|
221
221
|
|
|
222
222
|
isSymmetric() {
|
|
223
223
|
if (this.isSquare()) {
|
|
224
|
-
for (
|
|
225
|
-
for (
|
|
224
|
+
for (let i = 0; i < this.rows; i++) {
|
|
225
|
+
for (let j = 0; j <= i; j++) {
|
|
226
226
|
if (this.get(i, j) !== this.get(j, i)) {
|
|
227
227
|
return false;
|
|
228
228
|
}
|
|
@@ -292,7 +292,7 @@ export class AbstractMatrix {
|
|
|
292
292
|
let result = this.clone();
|
|
293
293
|
let h = 0;
|
|
294
294
|
let k = 0;
|
|
295
|
-
while (
|
|
295
|
+
while (h < result.rows && k < result.columns) {
|
|
296
296
|
let iMax = h;
|
|
297
297
|
for (let i = h; i < result.rows; i++) {
|
|
298
298
|
if (result.get(i, k) > result.get(iMax, k)) {
|
|
@@ -332,7 +332,7 @@ export class AbstractMatrix {
|
|
|
332
332
|
} else {
|
|
333
333
|
let p = 0;
|
|
334
334
|
let pivot = false;
|
|
335
|
-
while (
|
|
335
|
+
while (p < n && pivot === false) {
|
|
336
336
|
if (result.get(h, p) === 1) {
|
|
337
337
|
pivot = true;
|
|
338
338
|
} else {
|
|
@@ -371,9 +371,9 @@ export class AbstractMatrix {
|
|
|
371
371
|
if (!Number.isInteger(columns) || columns <= 0) {
|
|
372
372
|
throw new TypeError('columns must be a positive integer');
|
|
373
373
|
}
|
|
374
|
-
|
|
375
|
-
for (
|
|
376
|
-
for (
|
|
374
|
+
let matrix = new Matrix(this.rows * rows, this.columns * columns);
|
|
375
|
+
for (let i = 0; i < rows; i++) {
|
|
376
|
+
for (let j = 0; j < columns; j++) {
|
|
377
377
|
matrix.setSubMatrix(this, this.rows * i, this.columns * j);
|
|
378
378
|
}
|
|
379
379
|
}
|
|
@@ -381,8 +381,8 @@ export class AbstractMatrix {
|
|
|
381
381
|
}
|
|
382
382
|
|
|
383
383
|
fill(value) {
|
|
384
|
-
for (
|
|
385
|
-
for (
|
|
384
|
+
for (let i = 0; i < this.rows; i++) {
|
|
385
|
+
for (let j = 0; j < this.columns; j++) {
|
|
386
386
|
this.set(i, j, value);
|
|
387
387
|
}
|
|
388
388
|
}
|
|
@@ -395,8 +395,8 @@ export class AbstractMatrix {
|
|
|
395
395
|
|
|
396
396
|
getRow(index) {
|
|
397
397
|
checkRowIndex(this, index);
|
|
398
|
-
|
|
399
|
-
for (
|
|
398
|
+
let row = [];
|
|
399
|
+
for (let i = 0; i < this.columns; i++) {
|
|
400
400
|
row.push(this.get(index, i));
|
|
401
401
|
}
|
|
402
402
|
return row;
|
|
@@ -409,7 +409,7 @@ export class AbstractMatrix {
|
|
|
409
409
|
setRow(index, array) {
|
|
410
410
|
checkRowIndex(this, index);
|
|
411
411
|
array = checkRowVector(this, array);
|
|
412
|
-
for (
|
|
412
|
+
for (let i = 0; i < this.columns; i++) {
|
|
413
413
|
this.set(index, i, array[i]);
|
|
414
414
|
}
|
|
415
415
|
return this;
|
|
@@ -418,8 +418,8 @@ export class AbstractMatrix {
|
|
|
418
418
|
swapRows(row1, row2) {
|
|
419
419
|
checkRowIndex(this, row1);
|
|
420
420
|
checkRowIndex(this, row2);
|
|
421
|
-
for (
|
|
422
|
-
|
|
421
|
+
for (let i = 0; i < this.columns; i++) {
|
|
422
|
+
let temp = this.get(row1, i);
|
|
423
423
|
this.set(row1, i, this.get(row2, i));
|
|
424
424
|
this.set(row2, i, temp);
|
|
425
425
|
}
|
|
@@ -428,8 +428,8 @@ export class AbstractMatrix {
|
|
|
428
428
|
|
|
429
429
|
getColumn(index) {
|
|
430
430
|
checkColumnIndex(this, index);
|
|
431
|
-
|
|
432
|
-
for (
|
|
431
|
+
let column = [];
|
|
432
|
+
for (let i = 0; i < this.rows; i++) {
|
|
433
433
|
column.push(this.get(i, index));
|
|
434
434
|
}
|
|
435
435
|
return column;
|
|
@@ -442,7 +442,7 @@ export class AbstractMatrix {
|
|
|
442
442
|
setColumn(index, array) {
|
|
443
443
|
checkColumnIndex(this, index);
|
|
444
444
|
array = checkColumnVector(this, array);
|
|
445
|
-
for (
|
|
445
|
+
for (let i = 0; i < this.rows; i++) {
|
|
446
446
|
this.set(i, index, array[i]);
|
|
447
447
|
}
|
|
448
448
|
return this;
|
|
@@ -451,8 +451,8 @@ export class AbstractMatrix {
|
|
|
451
451
|
swapColumns(column1, column2) {
|
|
452
452
|
checkColumnIndex(this, column1);
|
|
453
453
|
checkColumnIndex(this, column2);
|
|
454
|
-
for (
|
|
455
|
-
|
|
454
|
+
for (let i = 0; i < this.rows; i++) {
|
|
455
|
+
let temp = this.get(i, column1);
|
|
456
456
|
this.set(i, column1, this.get(i, column2));
|
|
457
457
|
this.set(i, column2, temp);
|
|
458
458
|
}
|
|
@@ -461,8 +461,8 @@ export class AbstractMatrix {
|
|
|
461
461
|
|
|
462
462
|
addRowVector(vector) {
|
|
463
463
|
vector = checkRowVector(this, vector);
|
|
464
|
-
for (
|
|
465
|
-
for (
|
|
464
|
+
for (let i = 0; i < this.rows; i++) {
|
|
465
|
+
for (let j = 0; j < this.columns; j++) {
|
|
466
466
|
this.set(i, j, this.get(i, j) + vector[j]);
|
|
467
467
|
}
|
|
468
468
|
}
|
|
@@ -471,8 +471,8 @@ export class AbstractMatrix {
|
|
|
471
471
|
|
|
472
472
|
subRowVector(vector) {
|
|
473
473
|
vector = checkRowVector(this, vector);
|
|
474
|
-
for (
|
|
475
|
-
for (
|
|
474
|
+
for (let i = 0; i < this.rows; i++) {
|
|
475
|
+
for (let j = 0; j < this.columns; j++) {
|
|
476
476
|
this.set(i, j, this.get(i, j) - vector[j]);
|
|
477
477
|
}
|
|
478
478
|
}
|
|
@@ -481,8 +481,8 @@ export class AbstractMatrix {
|
|
|
481
481
|
|
|
482
482
|
mulRowVector(vector) {
|
|
483
483
|
vector = checkRowVector(this, vector);
|
|
484
|
-
for (
|
|
485
|
-
for (
|
|
484
|
+
for (let i = 0; i < this.rows; i++) {
|
|
485
|
+
for (let j = 0; j < this.columns; j++) {
|
|
486
486
|
this.set(i, j, this.get(i, j) * vector[j]);
|
|
487
487
|
}
|
|
488
488
|
}
|
|
@@ -491,8 +491,8 @@ export class AbstractMatrix {
|
|
|
491
491
|
|
|
492
492
|
divRowVector(vector) {
|
|
493
493
|
vector = checkRowVector(this, vector);
|
|
494
|
-
for (
|
|
495
|
-
for (
|
|
494
|
+
for (let i = 0; i < this.rows; i++) {
|
|
495
|
+
for (let j = 0; j < this.columns; j++) {
|
|
496
496
|
this.set(i, j, this.get(i, j) / vector[j]);
|
|
497
497
|
}
|
|
498
498
|
}
|
|
@@ -501,8 +501,8 @@ export class AbstractMatrix {
|
|
|
501
501
|
|
|
502
502
|
addColumnVector(vector) {
|
|
503
503
|
vector = checkColumnVector(this, vector);
|
|
504
|
-
for (
|
|
505
|
-
for (
|
|
504
|
+
for (let i = 0; i < this.rows; i++) {
|
|
505
|
+
for (let j = 0; j < this.columns; j++) {
|
|
506
506
|
this.set(i, j, this.get(i, j) + vector[i]);
|
|
507
507
|
}
|
|
508
508
|
}
|
|
@@ -511,8 +511,8 @@ export class AbstractMatrix {
|
|
|
511
511
|
|
|
512
512
|
subColumnVector(vector) {
|
|
513
513
|
vector = checkColumnVector(this, vector);
|
|
514
|
-
for (
|
|
515
|
-
for (
|
|
514
|
+
for (let i = 0; i < this.rows; i++) {
|
|
515
|
+
for (let j = 0; j < this.columns; j++) {
|
|
516
516
|
this.set(i, j, this.get(i, j) - vector[i]);
|
|
517
517
|
}
|
|
518
518
|
}
|
|
@@ -521,8 +521,8 @@ export class AbstractMatrix {
|
|
|
521
521
|
|
|
522
522
|
mulColumnVector(vector) {
|
|
523
523
|
vector = checkColumnVector(this, vector);
|
|
524
|
-
for (
|
|
525
|
-
for (
|
|
524
|
+
for (let i = 0; i < this.rows; i++) {
|
|
525
|
+
for (let j = 0; j < this.columns; j++) {
|
|
526
526
|
this.set(i, j, this.get(i, j) * vector[i]);
|
|
527
527
|
}
|
|
528
528
|
}
|
|
@@ -531,8 +531,8 @@ export class AbstractMatrix {
|
|
|
531
531
|
|
|
532
532
|
divColumnVector(vector) {
|
|
533
533
|
vector = checkColumnVector(this, vector);
|
|
534
|
-
for (
|
|
535
|
-
for (
|
|
534
|
+
for (let i = 0; i < this.rows; i++) {
|
|
535
|
+
for (let j = 0; j < this.columns; j++) {
|
|
536
536
|
this.set(i, j, this.get(i, j) / vector[i]);
|
|
537
537
|
}
|
|
538
538
|
}
|
|
@@ -541,7 +541,7 @@ export class AbstractMatrix {
|
|
|
541
541
|
|
|
542
542
|
mulRow(index, value) {
|
|
543
543
|
checkRowIndex(this, index);
|
|
544
|
-
for (
|
|
544
|
+
for (let i = 0; i < this.columns; i++) {
|
|
545
545
|
this.set(index, i, this.get(index, i) * value);
|
|
546
546
|
}
|
|
547
547
|
return this;
|
|
@@ -549,16 +549,16 @@ export class AbstractMatrix {
|
|
|
549
549
|
|
|
550
550
|
mulColumn(index, value) {
|
|
551
551
|
checkColumnIndex(this, index);
|
|
552
|
-
for (
|
|
552
|
+
for (let i = 0; i < this.rows; i++) {
|
|
553
553
|
this.set(i, index, this.get(i, index) * value);
|
|
554
554
|
}
|
|
555
555
|
return this;
|
|
556
556
|
}
|
|
557
557
|
|
|
558
558
|
max() {
|
|
559
|
-
|
|
560
|
-
for (
|
|
561
|
-
for (
|
|
559
|
+
let v = this.get(0, 0);
|
|
560
|
+
for (let i = 0; i < this.rows; i++) {
|
|
561
|
+
for (let j = 0; j < this.columns; j++) {
|
|
562
562
|
if (this.get(i, j) > v) {
|
|
563
563
|
v = this.get(i, j);
|
|
564
564
|
}
|
|
@@ -568,10 +568,10 @@ export class AbstractMatrix {
|
|
|
568
568
|
}
|
|
569
569
|
|
|
570
570
|
maxIndex() {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
for (
|
|
574
|
-
for (
|
|
571
|
+
let v = this.get(0, 0);
|
|
572
|
+
let idx = [0, 0];
|
|
573
|
+
for (let i = 0; i < this.rows; i++) {
|
|
574
|
+
for (let j = 0; j < this.columns; j++) {
|
|
575
575
|
if (this.get(i, j) > v) {
|
|
576
576
|
v = this.get(i, j);
|
|
577
577
|
idx[0] = i;
|
|
@@ -583,9 +583,9 @@ export class AbstractMatrix {
|
|
|
583
583
|
}
|
|
584
584
|
|
|
585
585
|
min() {
|
|
586
|
-
|
|
587
|
-
for (
|
|
588
|
-
for (
|
|
586
|
+
let v = this.get(0, 0);
|
|
587
|
+
for (let i = 0; i < this.rows; i++) {
|
|
588
|
+
for (let j = 0; j < this.columns; j++) {
|
|
589
589
|
if (this.get(i, j) < v) {
|
|
590
590
|
v = this.get(i, j);
|
|
591
591
|
}
|
|
@@ -595,10 +595,10 @@ export class AbstractMatrix {
|
|
|
595
595
|
}
|
|
596
596
|
|
|
597
597
|
minIndex() {
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
for (
|
|
601
|
-
for (
|
|
598
|
+
let v = this.get(0, 0);
|
|
599
|
+
let idx = [0, 0];
|
|
600
|
+
for (let i = 0; i < this.rows; i++) {
|
|
601
|
+
for (let j = 0; j < this.columns; j++) {
|
|
602
602
|
if (this.get(i, j) < v) {
|
|
603
603
|
v = this.get(i, j);
|
|
604
604
|
idx[0] = i;
|
|
@@ -611,8 +611,8 @@ export class AbstractMatrix {
|
|
|
611
611
|
|
|
612
612
|
maxRow(row) {
|
|
613
613
|
checkRowIndex(this, row);
|
|
614
|
-
|
|
615
|
-
for (
|
|
614
|
+
let v = this.get(row, 0);
|
|
615
|
+
for (let i = 1; i < this.columns; i++) {
|
|
616
616
|
if (this.get(row, i) > v) {
|
|
617
617
|
v = this.get(row, i);
|
|
618
618
|
}
|
|
@@ -622,9 +622,9 @@ export class AbstractMatrix {
|
|
|
622
622
|
|
|
623
623
|
maxRowIndex(row) {
|
|
624
624
|
checkRowIndex(this, row);
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
for (
|
|
625
|
+
let v = this.get(row, 0);
|
|
626
|
+
let idx = [row, 0];
|
|
627
|
+
for (let i = 1; i < this.columns; i++) {
|
|
628
628
|
if (this.get(row, i) > v) {
|
|
629
629
|
v = this.get(row, i);
|
|
630
630
|
idx[1] = i;
|
|
@@ -635,8 +635,8 @@ export class AbstractMatrix {
|
|
|
635
635
|
|
|
636
636
|
minRow(row) {
|
|
637
637
|
checkRowIndex(this, row);
|
|
638
|
-
|
|
639
|
-
for (
|
|
638
|
+
let v = this.get(row, 0);
|
|
639
|
+
for (let i = 1; i < this.columns; i++) {
|
|
640
640
|
if (this.get(row, i) < v) {
|
|
641
641
|
v = this.get(row, i);
|
|
642
642
|
}
|
|
@@ -646,9 +646,9 @@ export class AbstractMatrix {
|
|
|
646
646
|
|
|
647
647
|
minRowIndex(row) {
|
|
648
648
|
checkRowIndex(this, row);
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
for (
|
|
649
|
+
let v = this.get(row, 0);
|
|
650
|
+
let idx = [row, 0];
|
|
651
|
+
for (let i = 1; i < this.columns; i++) {
|
|
652
652
|
if (this.get(row, i) < v) {
|
|
653
653
|
v = this.get(row, i);
|
|
654
654
|
idx[1] = i;
|
|
@@ -659,8 +659,8 @@ export class AbstractMatrix {
|
|
|
659
659
|
|
|
660
660
|
maxColumn(column) {
|
|
661
661
|
checkColumnIndex(this, column);
|
|
662
|
-
|
|
663
|
-
for (
|
|
662
|
+
let v = this.get(0, column);
|
|
663
|
+
for (let i = 1; i < this.rows; i++) {
|
|
664
664
|
if (this.get(i, column) > v) {
|
|
665
665
|
v = this.get(i, column);
|
|
666
666
|
}
|
|
@@ -670,9 +670,9 @@ export class AbstractMatrix {
|
|
|
670
670
|
|
|
671
671
|
maxColumnIndex(column) {
|
|
672
672
|
checkColumnIndex(this, column);
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
for (
|
|
673
|
+
let v = this.get(0, column);
|
|
674
|
+
let idx = [0, column];
|
|
675
|
+
for (let i = 1; i < this.rows; i++) {
|
|
676
676
|
if (this.get(i, column) > v) {
|
|
677
677
|
v = this.get(i, column);
|
|
678
678
|
idx[0] = i;
|
|
@@ -683,8 +683,8 @@ export class AbstractMatrix {
|
|
|
683
683
|
|
|
684
684
|
minColumn(column) {
|
|
685
685
|
checkColumnIndex(this, column);
|
|
686
|
-
|
|
687
|
-
for (
|
|
686
|
+
let v = this.get(0, column);
|
|
687
|
+
for (let i = 1; i < this.rows; i++) {
|
|
688
688
|
if (this.get(i, column) < v) {
|
|
689
689
|
v = this.get(i, column);
|
|
690
690
|
}
|
|
@@ -694,9 +694,9 @@ export class AbstractMatrix {
|
|
|
694
694
|
|
|
695
695
|
minColumnIndex(column) {
|
|
696
696
|
checkColumnIndex(this, column);
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
for (
|
|
697
|
+
let v = this.get(0, column);
|
|
698
|
+
let idx = [0, column];
|
|
699
|
+
for (let i = 1; i < this.rows; i++) {
|
|
700
700
|
if (this.get(i, column) < v) {
|
|
701
701
|
v = this.get(i, column);
|
|
702
702
|
idx[0] = i;
|
|
@@ -706,21 +706,21 @@ export class AbstractMatrix {
|
|
|
706
706
|
}
|
|
707
707
|
|
|
708
708
|
diag() {
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
for (
|
|
709
|
+
let min = Math.min(this.rows, this.columns);
|
|
710
|
+
let diag = [];
|
|
711
|
+
for (let i = 0; i < min; i++) {
|
|
712
712
|
diag.push(this.get(i, i));
|
|
713
713
|
}
|
|
714
714
|
return diag;
|
|
715
715
|
}
|
|
716
716
|
|
|
717
717
|
norm(type = 'frobenius') {
|
|
718
|
-
|
|
718
|
+
let result = 0;
|
|
719
719
|
if (type === 'max') {
|
|
720
720
|
return this.max();
|
|
721
721
|
} else if (type === 'frobenius') {
|
|
722
|
-
for (
|
|
723
|
-
for (
|
|
722
|
+
for (let i = 0; i < this.rows; i++) {
|
|
723
|
+
for (let j = 0; j < this.columns; j++) {
|
|
724
724
|
result = result + this.get(i, j) * this.get(i, j);
|
|
725
725
|
}
|
|
726
726
|
}
|
|
@@ -731,9 +731,9 @@ export class AbstractMatrix {
|
|
|
731
731
|
}
|
|
732
732
|
|
|
733
733
|
cumulativeSum() {
|
|
734
|
-
|
|
735
|
-
for (
|
|
736
|
-
for (
|
|
734
|
+
let sum = 0;
|
|
735
|
+
for (let i = 0; i < this.rows; i++) {
|
|
736
|
+
for (let j = 0; j < this.columns; j++) {
|
|
737
737
|
sum += this.get(i, j);
|
|
738
738
|
this.set(i, j, sum);
|
|
739
739
|
}
|
|
@@ -743,12 +743,12 @@ export class AbstractMatrix {
|
|
|
743
743
|
|
|
744
744
|
dot(vector2) {
|
|
745
745
|
if (AbstractMatrix.isMatrix(vector2)) vector2 = vector2.to1DArray();
|
|
746
|
-
|
|
746
|
+
let vector1 = this.to1DArray();
|
|
747
747
|
if (vector1.length !== vector2.length) {
|
|
748
748
|
throw new RangeError('vectors do not have the same size');
|
|
749
749
|
}
|
|
750
|
-
|
|
751
|
-
for (
|
|
750
|
+
let dot = 0;
|
|
751
|
+
for (let i = 0; i < vector1.length; i++) {
|
|
752
752
|
dot += vector1[i] * vector2[i];
|
|
753
753
|
}
|
|
754
754
|
return dot;
|
|
@@ -757,21 +757,21 @@ export class AbstractMatrix {
|
|
|
757
757
|
mmul(other) {
|
|
758
758
|
other = Matrix.checkMatrix(other);
|
|
759
759
|
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
760
|
+
let m = this.rows;
|
|
761
|
+
let n = this.columns;
|
|
762
|
+
let p = other.columns;
|
|
763
763
|
|
|
764
|
-
|
|
764
|
+
let result = new Matrix(m, p);
|
|
765
765
|
|
|
766
|
-
|
|
767
|
-
for (
|
|
768
|
-
for (
|
|
766
|
+
let Bcolj = new Float64Array(n);
|
|
767
|
+
for (let j = 0; j < p; j++) {
|
|
768
|
+
for (let k = 0; k < n; k++) {
|
|
769
769
|
Bcolj[k] = other.get(k, j);
|
|
770
770
|
}
|
|
771
771
|
|
|
772
|
-
for (
|
|
773
|
-
|
|
774
|
-
for (k = 0; k < n; k++) {
|
|
772
|
+
for (let i = 0; i < m; i++) {
|
|
773
|
+
let s = 0;
|
|
774
|
+
for (let k = 0; k < n; k++) {
|
|
775
775
|
s += this.get(i, k) * Bcolj[k];
|
|
776
776
|
}
|
|
777
777
|
|
|
@@ -783,7 +783,7 @@ export class AbstractMatrix {
|
|
|
783
783
|
|
|
784
784
|
strassen2x2(other) {
|
|
785
785
|
other = Matrix.checkMatrix(other);
|
|
786
|
-
|
|
786
|
+
let result = new Matrix(2, 2);
|
|
787
787
|
const a11 = this.get(0, 0);
|
|
788
788
|
const b11 = other.get(0, 0);
|
|
789
789
|
const a12 = this.get(0, 1);
|
|
@@ -817,7 +817,7 @@ export class AbstractMatrix {
|
|
|
817
817
|
|
|
818
818
|
strassen3x3(other) {
|
|
819
819
|
other = Matrix.checkMatrix(other);
|
|
820
|
-
|
|
820
|
+
let result = new Matrix(3, 3);
|
|
821
821
|
|
|
822
822
|
const a00 = this.get(0, 0);
|
|
823
823
|
const a01 = this.get(0, 1);
|
|
@@ -887,27 +887,27 @@ export class AbstractMatrix {
|
|
|
887
887
|
|
|
888
888
|
mmulStrassen(y) {
|
|
889
889
|
y = Matrix.checkMatrix(y);
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
890
|
+
let x = this.clone();
|
|
891
|
+
let r1 = x.rows;
|
|
892
|
+
let c1 = x.columns;
|
|
893
|
+
let r2 = y.rows;
|
|
894
|
+
let c2 = y.columns;
|
|
895
895
|
if (c1 !== r2) {
|
|
896
896
|
// eslint-disable-next-line no-console
|
|
897
897
|
console.warn(
|
|
898
|
-
`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match
|
|
898
|
+
`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`,
|
|
899
899
|
);
|
|
900
900
|
}
|
|
901
901
|
|
|
902
902
|
// Put a matrix into the top left of a matrix of zeros.
|
|
903
903
|
// `rows` and `cols` are the dimensions of the output matrix.
|
|
904
904
|
function embed(mat, rows, cols) {
|
|
905
|
-
|
|
906
|
-
|
|
905
|
+
let r = mat.rows;
|
|
906
|
+
let c = mat.columns;
|
|
907
907
|
if (r === rows && c === cols) {
|
|
908
908
|
return mat;
|
|
909
909
|
} else {
|
|
910
|
-
|
|
910
|
+
let resultat = AbstractMatrix.zeros(rows, cols);
|
|
911
911
|
resultat = resultat.setSubMatrix(mat, 0, 0);
|
|
912
912
|
return resultat;
|
|
913
913
|
}
|
|
@@ -917,8 +917,8 @@ export class AbstractMatrix {
|
|
|
917
917
|
// This is exclusively for simplicity:
|
|
918
918
|
// this algorithm can be implemented with matrices of different sizes.
|
|
919
919
|
|
|
920
|
-
|
|
921
|
-
|
|
920
|
+
let r = Math.max(r1, r2);
|
|
921
|
+
let c = Math.max(c1, c2);
|
|
922
922
|
x = embed(x, r, c);
|
|
923
923
|
y = embed(y, r, c);
|
|
924
924
|
|
|
@@ -941,57 +941,57 @@ export class AbstractMatrix {
|
|
|
941
941
|
b = embed(b, rows, cols + 1);
|
|
942
942
|
}
|
|
943
943
|
|
|
944
|
-
|
|
945
|
-
|
|
944
|
+
let halfRows = parseInt(a.rows / 2, 10);
|
|
945
|
+
let halfCols = parseInt(a.columns / 2, 10);
|
|
946
946
|
// Subdivide input matrices.
|
|
947
|
-
|
|
948
|
-
|
|
947
|
+
let a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1);
|
|
948
|
+
let b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1);
|
|
949
949
|
|
|
950
|
-
|
|
951
|
-
|
|
950
|
+
let a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1);
|
|
951
|
+
let b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1);
|
|
952
952
|
|
|
953
|
-
|
|
954
|
-
|
|
953
|
+
let a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1);
|
|
954
|
+
let b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1);
|
|
955
955
|
|
|
956
|
-
|
|
957
|
-
|
|
956
|
+
let a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1);
|
|
957
|
+
let b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1);
|
|
958
958
|
|
|
959
959
|
// Compute intermediate values.
|
|
960
|
-
|
|
960
|
+
let m1 = blockMult(
|
|
961
961
|
AbstractMatrix.add(a11, a22),
|
|
962
962
|
AbstractMatrix.add(b11, b22),
|
|
963
963
|
halfRows,
|
|
964
|
-
halfCols
|
|
964
|
+
halfCols,
|
|
965
965
|
);
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
966
|
+
let m2 = blockMult(AbstractMatrix.add(a21, a22), b11, halfRows, halfCols);
|
|
967
|
+
let m3 = blockMult(a11, AbstractMatrix.sub(b12, b22), halfRows, halfCols);
|
|
968
|
+
let m4 = blockMult(a22, AbstractMatrix.sub(b21, b11), halfRows, halfCols);
|
|
969
|
+
let m5 = blockMult(AbstractMatrix.add(a11, a12), b22, halfRows, halfCols);
|
|
970
|
+
let m6 = blockMult(
|
|
971
971
|
AbstractMatrix.sub(a21, a11),
|
|
972
972
|
AbstractMatrix.add(b11, b12),
|
|
973
973
|
halfRows,
|
|
974
|
-
halfCols
|
|
974
|
+
halfCols,
|
|
975
975
|
);
|
|
976
|
-
|
|
976
|
+
let m7 = blockMult(
|
|
977
977
|
AbstractMatrix.sub(a12, a22),
|
|
978
978
|
AbstractMatrix.add(b21, b22),
|
|
979
979
|
halfRows,
|
|
980
|
-
halfCols
|
|
980
|
+
halfCols,
|
|
981
981
|
);
|
|
982
982
|
|
|
983
983
|
// Combine intermediate values into the output.
|
|
984
|
-
|
|
984
|
+
let c11 = AbstractMatrix.add(m1, m4);
|
|
985
985
|
c11.sub(m5);
|
|
986
986
|
c11.add(m7);
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
987
|
+
let c12 = AbstractMatrix.add(m3, m5);
|
|
988
|
+
let c21 = AbstractMatrix.add(m2, m4);
|
|
989
|
+
let c22 = AbstractMatrix.sub(m1, m2);
|
|
990
990
|
c22.add(m3);
|
|
991
991
|
c22.add(m6);
|
|
992
992
|
|
|
993
993
|
// Crop output to the desired size (undo dynamic padding).
|
|
994
|
-
|
|
994
|
+
let resultat = AbstractMatrix.zeros(2 * c11.rows, 2 * c11.columns);
|
|
995
995
|
resultat = resultat.setSubMatrix(c11, 0, 0);
|
|
996
996
|
resultat = resultat.setSubMatrix(c12, c11.rows, 0);
|
|
997
997
|
resultat = resultat.setSubMatrix(c21, 0, c11.columns);
|
|
@@ -1009,8 +1009,8 @@ export class AbstractMatrix {
|
|
|
1009
1009
|
if (!Number.isFinite(min)) throw new TypeError('min must be a number');
|
|
1010
1010
|
if (!Number.isFinite(max)) throw new TypeError('max must be a number');
|
|
1011
1011
|
if (min >= max) throw new RangeError('min must be smaller than max');
|
|
1012
|
-
|
|
1013
|
-
for (
|
|
1012
|
+
let newMatrix = new Matrix(this.rows, this.columns);
|
|
1013
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1014
1014
|
const row = this.getRow(i);
|
|
1015
1015
|
rescale(row, { min, max, output: row });
|
|
1016
1016
|
newMatrix.setRow(i, row);
|
|
@@ -1026,13 +1026,13 @@ export class AbstractMatrix {
|
|
|
1026
1026
|
if (!Number.isFinite(min)) throw new TypeError('min must be a number');
|
|
1027
1027
|
if (!Number.isFinite(max)) throw new TypeError('max must be a number');
|
|
1028
1028
|
if (min >= max) throw new RangeError('min must be smaller than max');
|
|
1029
|
-
|
|
1030
|
-
for (
|
|
1029
|
+
let newMatrix = new Matrix(this.rows, this.columns);
|
|
1030
|
+
for (let i = 0; i < this.columns; i++) {
|
|
1031
1031
|
const column = this.getColumn(i);
|
|
1032
1032
|
rescale(column, {
|
|
1033
1033
|
min: min,
|
|
1034
1034
|
max: max,
|
|
1035
|
-
output: column
|
|
1035
|
+
output: column,
|
|
1036
1036
|
});
|
|
1037
1037
|
newMatrix.setColumn(i, column);
|
|
1038
1038
|
}
|
|
@@ -1041,10 +1041,10 @@ export class AbstractMatrix {
|
|
|
1041
1041
|
|
|
1042
1042
|
flipRows() {
|
|
1043
1043
|
const middle = Math.ceil(this.columns / 2);
|
|
1044
|
-
for (
|
|
1045
|
-
for (
|
|
1046
|
-
|
|
1047
|
-
|
|
1044
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1045
|
+
for (let j = 0; j < middle; j++) {
|
|
1046
|
+
let first = this.get(i, j);
|
|
1047
|
+
let last = this.get(i, this.columns - 1 - j);
|
|
1048
1048
|
this.set(i, j, last);
|
|
1049
1049
|
this.set(i, this.columns - 1 - j, first);
|
|
1050
1050
|
}
|
|
@@ -1054,10 +1054,10 @@ export class AbstractMatrix {
|
|
|
1054
1054
|
|
|
1055
1055
|
flipColumns() {
|
|
1056
1056
|
const middle = Math.ceil(this.rows / 2);
|
|
1057
|
-
for (
|
|
1058
|
-
for (
|
|
1059
|
-
|
|
1060
|
-
|
|
1057
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1058
|
+
for (let i = 0; i < middle; i++) {
|
|
1059
|
+
let first = this.get(i, j);
|
|
1060
|
+
let last = this.get(this.rows - 1 - i, j);
|
|
1061
1061
|
this.set(i, j, last);
|
|
1062
1062
|
this.set(this.rows - 1 - i, j, first);
|
|
1063
1063
|
}
|
|
@@ -1068,16 +1068,16 @@ export class AbstractMatrix {
|
|
|
1068
1068
|
kroneckerProduct(other) {
|
|
1069
1069
|
other = Matrix.checkMatrix(other);
|
|
1070
1070
|
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1071
|
+
let m = this.rows;
|
|
1072
|
+
let n = this.columns;
|
|
1073
|
+
let p = other.rows;
|
|
1074
|
+
let q = other.columns;
|
|
1075
1075
|
|
|
1076
|
-
|
|
1077
|
-
for (
|
|
1078
|
-
for (
|
|
1079
|
-
for (
|
|
1080
|
-
for (
|
|
1076
|
+
let result = new Matrix(m * p, n * q);
|
|
1077
|
+
for (let i = 0; i < m; i++) {
|
|
1078
|
+
for (let j = 0; j < n; j++) {
|
|
1079
|
+
for (let k = 0; k < p; k++) {
|
|
1080
|
+
for (let l = 0; l < q; l++) {
|
|
1081
1081
|
result.set(p * i + k, q * j + l, this.get(i, j) * other.get(k, l));
|
|
1082
1082
|
}
|
|
1083
1083
|
}
|
|
@@ -1087,9 +1087,9 @@ export class AbstractMatrix {
|
|
|
1087
1087
|
}
|
|
1088
1088
|
|
|
1089
1089
|
transpose() {
|
|
1090
|
-
|
|
1091
|
-
for (
|
|
1092
|
-
for (
|
|
1090
|
+
let result = new Matrix(this.columns, this.rows);
|
|
1091
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1092
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1093
1093
|
result.set(j, i, this.get(i, j));
|
|
1094
1094
|
}
|
|
1095
1095
|
}
|
|
@@ -1097,14 +1097,14 @@ export class AbstractMatrix {
|
|
|
1097
1097
|
}
|
|
1098
1098
|
|
|
1099
1099
|
sortRows(compareFunction = compareNumbers) {
|
|
1100
|
-
for (
|
|
1100
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1101
1101
|
this.setRow(i, this.getRow(i).sort(compareFunction));
|
|
1102
1102
|
}
|
|
1103
1103
|
return this;
|
|
1104
1104
|
}
|
|
1105
1105
|
|
|
1106
1106
|
sortColumns(compareFunction = compareNumbers) {
|
|
1107
|
-
for (
|
|
1107
|
+
for (let i = 0; i < this.columns; i++) {
|
|
1108
1108
|
this.setColumn(i, this.getColumn(i).sort(compareFunction));
|
|
1109
1109
|
}
|
|
1110
1110
|
return this;
|
|
@@ -1112,12 +1112,12 @@ export class AbstractMatrix {
|
|
|
1112
1112
|
|
|
1113
1113
|
subMatrix(startRow, endRow, startColumn, endColumn) {
|
|
1114
1114
|
checkRange(this, startRow, endRow, startColumn, endColumn);
|
|
1115
|
-
|
|
1115
|
+
let newMatrix = new Matrix(
|
|
1116
1116
|
endRow - startRow + 1,
|
|
1117
|
-
endColumn - startColumn + 1
|
|
1117
|
+
endColumn - startColumn + 1,
|
|
1118
1118
|
);
|
|
1119
|
-
for (
|
|
1120
|
-
for (
|
|
1119
|
+
for (let i = startRow; i <= endRow; i++) {
|
|
1120
|
+
for (let j = startColumn; j <= endColumn; j++) {
|
|
1121
1121
|
newMatrix.set(i - startRow, j - startColumn, this.get(i, j));
|
|
1122
1122
|
}
|
|
1123
1123
|
}
|
|
@@ -1137,9 +1137,9 @@ export class AbstractMatrix {
|
|
|
1137
1137
|
throw new RangeError('Argument out of range');
|
|
1138
1138
|
}
|
|
1139
1139
|
|
|
1140
|
-
|
|
1141
|
-
for (
|
|
1142
|
-
for (
|
|
1140
|
+
let newMatrix = new Matrix(indices.length, endColumn - startColumn + 1);
|
|
1141
|
+
for (let i = 0; i < indices.length; i++) {
|
|
1142
|
+
for (let j = startColumn; j <= endColumn; j++) {
|
|
1143
1143
|
if (indices[i] < 0 || indices[i] >= this.rows) {
|
|
1144
1144
|
throw new RangeError(`Row index out of range: ${indices[i]}`);
|
|
1145
1145
|
}
|
|
@@ -1162,9 +1162,9 @@ export class AbstractMatrix {
|
|
|
1162
1162
|
throw new RangeError('Argument out of range');
|
|
1163
1163
|
}
|
|
1164
1164
|
|
|
1165
|
-
|
|
1166
|
-
for (
|
|
1167
|
-
for (
|
|
1165
|
+
let newMatrix = new Matrix(endRow - startRow + 1, indices.length);
|
|
1166
|
+
for (let i = 0; i < indices.length; i++) {
|
|
1167
|
+
for (let j = startRow; j <= endRow; j++) {
|
|
1168
1168
|
if (indices[i] < 0 || indices[i] >= this.columns) {
|
|
1169
1169
|
throw new RangeError(`Column index out of range: ${indices[i]}`);
|
|
1170
1170
|
}
|
|
@@ -1176,11 +1176,11 @@ export class AbstractMatrix {
|
|
|
1176
1176
|
|
|
1177
1177
|
setSubMatrix(matrix, startRow, startColumn) {
|
|
1178
1178
|
matrix = Matrix.checkMatrix(matrix);
|
|
1179
|
-
|
|
1180
|
-
|
|
1179
|
+
let endRow = startRow + matrix.rows - 1;
|
|
1180
|
+
let endColumn = startColumn + matrix.columns - 1;
|
|
1181
1181
|
checkRange(this, startRow, endRow, startColumn, endColumn);
|
|
1182
|
-
for (
|
|
1183
|
-
for (
|
|
1182
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
1183
|
+
for (let j = 0; j < matrix.columns; j++) {
|
|
1184
1184
|
this.set(startRow + i, startColumn + j, matrix.get(i, j));
|
|
1185
1185
|
}
|
|
1186
1186
|
}
|
|
@@ -1188,12 +1188,12 @@ export class AbstractMatrix {
|
|
|
1188
1188
|
}
|
|
1189
1189
|
|
|
1190
1190
|
selection(rowIndices, columnIndices) {
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
for (
|
|
1194
|
-
|
|
1195
|
-
for (
|
|
1196
|
-
|
|
1191
|
+
let indices = checkIndices(this, rowIndices, columnIndices);
|
|
1192
|
+
let newMatrix = new Matrix(rowIndices.length, columnIndices.length);
|
|
1193
|
+
for (let i = 0; i < indices.row.length; i++) {
|
|
1194
|
+
let rowIndex = indices.row[i];
|
|
1195
|
+
for (let j = 0; j < indices.column.length; j++) {
|
|
1196
|
+
let columnIndex = indices.column[j];
|
|
1197
1197
|
newMatrix.set(i, j, this.get(rowIndex, columnIndex));
|
|
1198
1198
|
}
|
|
1199
1199
|
}
|
|
@@ -1201,18 +1201,18 @@ export class AbstractMatrix {
|
|
|
1201
1201
|
}
|
|
1202
1202
|
|
|
1203
1203
|
trace() {
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
for (
|
|
1204
|
+
let min = Math.min(this.rows, this.columns);
|
|
1205
|
+
let trace = 0;
|
|
1206
|
+
for (let i = 0; i < min; i++) {
|
|
1207
1207
|
trace += this.get(i, i);
|
|
1208
1208
|
}
|
|
1209
1209
|
return trace;
|
|
1210
1210
|
}
|
|
1211
1211
|
|
|
1212
1212
|
clone() {
|
|
1213
|
-
|
|
1214
|
-
for (
|
|
1215
|
-
for (
|
|
1213
|
+
let newMatrix = new Matrix(this.rows, this.columns);
|
|
1214
|
+
for (let row = 0; row < this.rows; row++) {
|
|
1215
|
+
for (let column = 0; column < this.columns; column++) {
|
|
1216
1216
|
newMatrix.set(row, column, this.get(row, column));
|
|
1217
1217
|
}
|
|
1218
1218
|
}
|
|
@@ -1312,7 +1312,7 @@ export class AbstractMatrix {
|
|
|
1312
1312
|
if (by === undefined) {
|
|
1313
1313
|
return Math.sqrt(variance);
|
|
1314
1314
|
} else {
|
|
1315
|
-
for (
|
|
1315
|
+
for (let i = 0; i < variance.length; i++) {
|
|
1316
1316
|
variance[i] = Math.sqrt(variance[i]);
|
|
1317
1317
|
}
|
|
1318
1318
|
return variance;
|
|
@@ -1342,7 +1342,8 @@ export class AbstractMatrix {
|
|
|
1342
1342
|
}
|
|
1343
1343
|
centerByColumn(this, center);
|
|
1344
1344
|
return this;
|
|
1345
|
-
}
|
|
1345
|
+
}
|
|
1346
|
+
case undefined: {
|
|
1346
1347
|
if (typeof center !== 'number') {
|
|
1347
1348
|
throw new TypeError('center must be a number');
|
|
1348
1349
|
}
|
|
@@ -1440,7 +1441,7 @@ export default class Matrix extends AbstractMatrix {
|
|
|
1440
1441
|
nColumns = arrayData[0].length;
|
|
1441
1442
|
if (typeof nColumns !== 'number' || nColumns === 0) {
|
|
1442
1443
|
throw new TypeError(
|
|
1443
|
-
'Data must be a 2D array with at least one element'
|
|
1444
|
+
'Data must be a 2D array with at least one element',
|
|
1444
1445
|
);
|
|
1445
1446
|
}
|
|
1446
1447
|
this.data = [];
|
|
@@ -1452,7 +1453,7 @@ export default class Matrix extends AbstractMatrix {
|
|
|
1452
1453
|
}
|
|
1453
1454
|
} else {
|
|
1454
1455
|
throw new TypeError(
|
|
1455
|
-
'First argument must be a positive number or an array'
|
|
1456
|
+
'First argument must be a positive number or an array',
|
|
1456
1457
|
);
|
|
1457
1458
|
}
|
|
1458
1459
|
this.rows = nRows;
|
|
@@ -1496,7 +1497,7 @@ export default class Matrix extends AbstractMatrix {
|
|
|
1496
1497
|
if (this.columns === 1) {
|
|
1497
1498
|
throw new RangeError('A matrix cannot have less than one column');
|
|
1498
1499
|
}
|
|
1499
|
-
for (
|
|
1500
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1500
1501
|
const newRow = new Float64Array(this.columns - 1);
|
|
1501
1502
|
for (let j = 0; j < index; j++) {
|
|
1502
1503
|
newRow[j] = this.data[i][j];
|
|
@@ -1517,7 +1518,7 @@ export default class Matrix extends AbstractMatrix {
|
|
|
1517
1518
|
}
|
|
1518
1519
|
checkColumnIndex(this, index, true);
|
|
1519
1520
|
array = checkColumnVector(this, array);
|
|
1520
|
-
for (
|
|
1521
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1521
1522
|
const newRow = new Float64Array(this.columns + 1);
|
|
1522
1523
|
let j = 0;
|
|
1523
1524
|
for (; j < index; j++) {
|