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/matrix.js
CHANGED
|
@@ -14,7 +14,7 @@ var rescale = _interopDefault(require('ml-array-rescale'));
|
|
|
14
14
|
* @param {boolean} [outer]
|
|
15
15
|
*/
|
|
16
16
|
function checkRowIndex(matrix, index, outer) {
|
|
17
|
-
|
|
17
|
+
let max = outer ? matrix.rows : matrix.rows - 1;
|
|
18
18
|
if (index < 0 || index > max) {
|
|
19
19
|
throw new RangeError('Row index out of range');
|
|
20
20
|
}
|
|
@@ -28,7 +28,7 @@ function checkRowIndex(matrix, index, outer) {
|
|
|
28
28
|
* @param {boolean} [outer]
|
|
29
29
|
*/
|
|
30
30
|
function checkColumnIndex(matrix, index, outer) {
|
|
31
|
-
|
|
31
|
+
let max = outer ? matrix.columns : matrix.columns - 1;
|
|
32
32
|
if (index < 0 || index > max) {
|
|
33
33
|
throw new RangeError('Column index out of range');
|
|
34
34
|
}
|
|
@@ -48,7 +48,7 @@ function checkRowVector(matrix, vector) {
|
|
|
48
48
|
}
|
|
49
49
|
if (vector.length !== matrix.columns) {
|
|
50
50
|
throw new RangeError(
|
|
51
|
-
'vector size must be the same as the number of columns'
|
|
51
|
+
'vector size must be the same as the number of columns',
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
54
|
return vector;
|
|
@@ -75,7 +75,7 @@ function checkColumnVector(matrix, vector) {
|
|
|
75
75
|
function checkIndices(matrix, rowIndices, columnIndices) {
|
|
76
76
|
return {
|
|
77
77
|
row: checkRowIndices(matrix, rowIndices),
|
|
78
|
-
column: checkColumnIndices(matrix, columnIndices)
|
|
78
|
+
column: checkColumnIndices(matrix, columnIndices),
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
|
|
@@ -84,7 +84,7 @@ function checkRowIndices(matrix, rowIndices) {
|
|
|
84
84
|
throw new TypeError('unexpected type for row indices');
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
let rowOut = rowIndices.some((r) => {
|
|
88
88
|
return r < 0 || r >= matrix.rows;
|
|
89
89
|
});
|
|
90
90
|
|
|
@@ -102,7 +102,7 @@ function checkColumnIndices(matrix, columnIndices) {
|
|
|
102
102
|
throw new TypeError('unexpected type for column indices');
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
let columnOut = columnIndices.some((c) => {
|
|
106
106
|
return c < 0 || c >= matrix.columns;
|
|
107
107
|
});
|
|
108
108
|
|
|
@@ -139,8 +139,8 @@ function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
function newArray(length, value = 0) {
|
|
142
|
-
|
|
143
|
-
for (
|
|
142
|
+
let array = [];
|
|
143
|
+
for (let i = 0; i < length; i++) {
|
|
144
144
|
array.push(value);
|
|
145
145
|
}
|
|
146
146
|
return array;
|
|
@@ -153,9 +153,9 @@ function checkNumber(name, value) {
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
function sumByRow(matrix) {
|
|
156
|
-
|
|
157
|
-
for (
|
|
158
|
-
for (
|
|
156
|
+
let sum = newArray(matrix.rows);
|
|
157
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
158
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
159
159
|
sum[i] += matrix.get(i, j);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
@@ -163,9 +163,9 @@ function sumByRow(matrix) {
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
function sumByColumn(matrix) {
|
|
166
|
-
|
|
167
|
-
for (
|
|
168
|
-
for (
|
|
166
|
+
let sum = newArray(matrix.columns);
|
|
167
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
168
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
169
169
|
sum[j] += matrix.get(i, j);
|
|
170
170
|
}
|
|
171
171
|
}
|
|
@@ -173,9 +173,9 @@ function sumByColumn(matrix) {
|
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
function sumAll(matrix) {
|
|
176
|
-
|
|
177
|
-
for (
|
|
178
|
-
for (
|
|
176
|
+
let v = 0;
|
|
177
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
178
|
+
for (let j = 0; j < matrix.columns; j++) {
|
|
179
179
|
v += matrix.get(i, j);
|
|
180
180
|
}
|
|
181
181
|
}
|
|
@@ -183,9 +183,9 @@ function sumAll(matrix) {
|
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
function productByRow(matrix) {
|
|
186
|
-
|
|
187
|
-
for (
|
|
188
|
-
for (
|
|
186
|
+
let sum = newArray(matrix.rows, 1);
|
|
187
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
188
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
189
189
|
sum[i] *= matrix.get(i, j);
|
|
190
190
|
}
|
|
191
191
|
}
|
|
@@ -193,9 +193,9 @@ function productByRow(matrix) {
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
function productByColumn(matrix) {
|
|
196
|
-
|
|
197
|
-
for (
|
|
198
|
-
for (
|
|
196
|
+
let sum = newArray(matrix.columns, 1);
|
|
197
|
+
for (let i = 0; i < matrix.rows; ++i) {
|
|
198
|
+
for (let j = 0; j < matrix.columns; ++j) {
|
|
199
199
|
sum[j] *= matrix.get(i, j);
|
|
200
200
|
}
|
|
201
201
|
}
|
|
@@ -203,9 +203,9 @@ function productByColumn(matrix) {
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
function productAll(matrix) {
|
|
206
|
-
|
|
207
|
-
for (
|
|
208
|
-
for (
|
|
206
|
+
let v = 1;
|
|
207
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
208
|
+
for (let j = 0; j < matrix.columns; j++) {
|
|
209
209
|
v *= matrix.get(i, j);
|
|
210
210
|
}
|
|
211
211
|
}
|
|
@@ -217,11 +217,11 @@ function varianceByRow(matrix, unbiased, mean) {
|
|
|
217
217
|
const cols = matrix.columns;
|
|
218
218
|
const variance = [];
|
|
219
219
|
|
|
220
|
-
for (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
for (
|
|
220
|
+
for (let i = 0; i < rows; i++) {
|
|
221
|
+
let sum1 = 0;
|
|
222
|
+
let sum2 = 0;
|
|
223
|
+
let x = 0;
|
|
224
|
+
for (let j = 0; j < cols; j++) {
|
|
225
225
|
x = matrix.get(i, j) - mean[i];
|
|
226
226
|
sum1 += x;
|
|
227
227
|
sum2 += x * x;
|
|
@@ -240,11 +240,11 @@ function varianceByColumn(matrix, unbiased, mean) {
|
|
|
240
240
|
const cols = matrix.columns;
|
|
241
241
|
const variance = [];
|
|
242
242
|
|
|
243
|
-
for (
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
for (
|
|
243
|
+
for (let j = 0; j < cols; j++) {
|
|
244
|
+
let sum1 = 0;
|
|
245
|
+
let sum2 = 0;
|
|
246
|
+
let x = 0;
|
|
247
|
+
for (let i = 0; i < rows; i++) {
|
|
248
248
|
x = matrix.get(i, j) - mean[j];
|
|
249
249
|
sum1 += x;
|
|
250
250
|
sum2 += x * x;
|
|
@@ -263,11 +263,11 @@ function varianceAll(matrix, unbiased, mean) {
|
|
|
263
263
|
const cols = matrix.columns;
|
|
264
264
|
const size = rows * cols;
|
|
265
265
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
for (
|
|
270
|
-
for (
|
|
266
|
+
let sum1 = 0;
|
|
267
|
+
let sum2 = 0;
|
|
268
|
+
let x = 0;
|
|
269
|
+
for (let i = 0; i < rows; i++) {
|
|
270
|
+
for (let j = 0; j < cols; j++) {
|
|
271
271
|
x = matrix.get(i, j) - mean;
|
|
272
272
|
sum1 += x;
|
|
273
273
|
sum2 += x * x;
|
|
@@ -384,9 +384,9 @@ function inspectData(matrix, indent) {
|
|
|
384
384
|
const maxI = Math.min(rows, maxRows);
|
|
385
385
|
const maxJ = Math.min(columns, maxColumns);
|
|
386
386
|
const result = [];
|
|
387
|
-
for (
|
|
387
|
+
for (let i = 0; i < maxI; i++) {
|
|
388
388
|
let line = [];
|
|
389
|
-
for (
|
|
389
|
+
for (let j = 0; j < maxJ; j++) {
|
|
390
390
|
line.push(formatNumber(matrix.get(i, j)));
|
|
391
391
|
}
|
|
392
392
|
result.push(`${line.join(' ')}`);
|
|
@@ -422,8 +422,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
422
422
|
};
|
|
423
423
|
|
|
424
424
|
AbstractMatrix.prototype.addS = function addS(value) {
|
|
425
|
-
for (
|
|
426
|
-
for (
|
|
425
|
+
for (let i = 0; i < this.rows; i++) {
|
|
426
|
+
for (let j = 0; j < this.columns; j++) {
|
|
427
427
|
this.set(i, j, this.get(i, j) + value);
|
|
428
428
|
}
|
|
429
429
|
}
|
|
@@ -436,8 +436,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
436
436
|
this.columns !== matrix.columns) {
|
|
437
437
|
throw new RangeError('Matrices dimensions must be equal');
|
|
438
438
|
}
|
|
439
|
-
for (
|
|
440
|
-
for (
|
|
439
|
+
for (let i = 0; i < this.rows; i++) {
|
|
440
|
+
for (let j = 0; j < this.columns; j++) {
|
|
441
441
|
this.set(i, j, this.get(i, j) + matrix.get(i, j));
|
|
442
442
|
}
|
|
443
443
|
}
|
|
@@ -445,7 +445,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
445
445
|
};
|
|
446
446
|
|
|
447
447
|
AbstractMatrix.add = function add(matrix, value) {
|
|
448
|
-
|
|
448
|
+
const newMatrix = new Matrix(matrix);
|
|
449
449
|
return newMatrix.add(value);
|
|
450
450
|
};
|
|
451
451
|
|
|
@@ -455,8 +455,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
455
455
|
};
|
|
456
456
|
|
|
457
457
|
AbstractMatrix.prototype.subS = function subS(value) {
|
|
458
|
-
for (
|
|
459
|
-
for (
|
|
458
|
+
for (let i = 0; i < this.rows; i++) {
|
|
459
|
+
for (let j = 0; j < this.columns; j++) {
|
|
460
460
|
this.set(i, j, this.get(i, j) - value);
|
|
461
461
|
}
|
|
462
462
|
}
|
|
@@ -469,8 +469,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
469
469
|
this.columns !== matrix.columns) {
|
|
470
470
|
throw new RangeError('Matrices dimensions must be equal');
|
|
471
471
|
}
|
|
472
|
-
for (
|
|
473
|
-
for (
|
|
472
|
+
for (let i = 0; i < this.rows; i++) {
|
|
473
|
+
for (let j = 0; j < this.columns; j++) {
|
|
474
474
|
this.set(i, j, this.get(i, j) - matrix.get(i, j));
|
|
475
475
|
}
|
|
476
476
|
}
|
|
@@ -478,7 +478,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
478
478
|
};
|
|
479
479
|
|
|
480
480
|
AbstractMatrix.sub = function sub(matrix, value) {
|
|
481
|
-
|
|
481
|
+
const newMatrix = new Matrix(matrix);
|
|
482
482
|
return newMatrix.sub(value);
|
|
483
483
|
};
|
|
484
484
|
AbstractMatrix.prototype.subtract = AbstractMatrix.prototype.sub;
|
|
@@ -492,8 +492,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
492
492
|
};
|
|
493
493
|
|
|
494
494
|
AbstractMatrix.prototype.mulS = function mulS(value) {
|
|
495
|
-
for (
|
|
496
|
-
for (
|
|
495
|
+
for (let i = 0; i < this.rows; i++) {
|
|
496
|
+
for (let j = 0; j < this.columns; j++) {
|
|
497
497
|
this.set(i, j, this.get(i, j) * value);
|
|
498
498
|
}
|
|
499
499
|
}
|
|
@@ -506,8 +506,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
506
506
|
this.columns !== matrix.columns) {
|
|
507
507
|
throw new RangeError('Matrices dimensions must be equal');
|
|
508
508
|
}
|
|
509
|
-
for (
|
|
510
|
-
for (
|
|
509
|
+
for (let i = 0; i < this.rows; i++) {
|
|
510
|
+
for (let j = 0; j < this.columns; j++) {
|
|
511
511
|
this.set(i, j, this.get(i, j) * matrix.get(i, j));
|
|
512
512
|
}
|
|
513
513
|
}
|
|
@@ -515,7 +515,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
515
515
|
};
|
|
516
516
|
|
|
517
517
|
AbstractMatrix.mul = function mul(matrix, value) {
|
|
518
|
-
|
|
518
|
+
const newMatrix = new Matrix(matrix);
|
|
519
519
|
return newMatrix.mul(value);
|
|
520
520
|
};
|
|
521
521
|
AbstractMatrix.prototype.multiply = AbstractMatrix.prototype.mul;
|
|
@@ -529,8 +529,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
529
529
|
};
|
|
530
530
|
|
|
531
531
|
AbstractMatrix.prototype.divS = function divS(value) {
|
|
532
|
-
for (
|
|
533
|
-
for (
|
|
532
|
+
for (let i = 0; i < this.rows; i++) {
|
|
533
|
+
for (let j = 0; j < this.columns; j++) {
|
|
534
534
|
this.set(i, j, this.get(i, j) / value);
|
|
535
535
|
}
|
|
536
536
|
}
|
|
@@ -543,8 +543,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
543
543
|
this.columns !== matrix.columns) {
|
|
544
544
|
throw new RangeError('Matrices dimensions must be equal');
|
|
545
545
|
}
|
|
546
|
-
for (
|
|
547
|
-
for (
|
|
546
|
+
for (let i = 0; i < this.rows; i++) {
|
|
547
|
+
for (let j = 0; j < this.columns; j++) {
|
|
548
548
|
this.set(i, j, this.get(i, j) / matrix.get(i, j));
|
|
549
549
|
}
|
|
550
550
|
}
|
|
@@ -552,7 +552,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
552
552
|
};
|
|
553
553
|
|
|
554
554
|
AbstractMatrix.div = function div(matrix, value) {
|
|
555
|
-
|
|
555
|
+
const newMatrix = new Matrix(matrix);
|
|
556
556
|
return newMatrix.div(value);
|
|
557
557
|
};
|
|
558
558
|
AbstractMatrix.prototype.divide = AbstractMatrix.prototype.div;
|
|
@@ -566,8 +566,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
566
566
|
};
|
|
567
567
|
|
|
568
568
|
AbstractMatrix.prototype.modS = function modS(value) {
|
|
569
|
-
for (
|
|
570
|
-
for (
|
|
569
|
+
for (let i = 0; i < this.rows; i++) {
|
|
570
|
+
for (let j = 0; j < this.columns; j++) {
|
|
571
571
|
this.set(i, j, this.get(i, j) % value);
|
|
572
572
|
}
|
|
573
573
|
}
|
|
@@ -580,8 +580,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
580
580
|
this.columns !== matrix.columns) {
|
|
581
581
|
throw new RangeError('Matrices dimensions must be equal');
|
|
582
582
|
}
|
|
583
|
-
for (
|
|
584
|
-
for (
|
|
583
|
+
for (let i = 0; i < this.rows; i++) {
|
|
584
|
+
for (let j = 0; j < this.columns; j++) {
|
|
585
585
|
this.set(i, j, this.get(i, j) % matrix.get(i, j));
|
|
586
586
|
}
|
|
587
587
|
}
|
|
@@ -589,7 +589,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
589
589
|
};
|
|
590
590
|
|
|
591
591
|
AbstractMatrix.mod = function mod(matrix, value) {
|
|
592
|
-
|
|
592
|
+
const newMatrix = new Matrix(matrix);
|
|
593
593
|
return newMatrix.mod(value);
|
|
594
594
|
};
|
|
595
595
|
AbstractMatrix.prototype.modulus = AbstractMatrix.prototype.mod;
|
|
@@ -603,8 +603,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
603
603
|
};
|
|
604
604
|
|
|
605
605
|
AbstractMatrix.prototype.andS = function andS(value) {
|
|
606
|
-
for (
|
|
607
|
-
for (
|
|
606
|
+
for (let i = 0; i < this.rows; i++) {
|
|
607
|
+
for (let j = 0; j < this.columns; j++) {
|
|
608
608
|
this.set(i, j, this.get(i, j) & value);
|
|
609
609
|
}
|
|
610
610
|
}
|
|
@@ -617,8 +617,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
617
617
|
this.columns !== matrix.columns) {
|
|
618
618
|
throw new RangeError('Matrices dimensions must be equal');
|
|
619
619
|
}
|
|
620
|
-
for (
|
|
621
|
-
for (
|
|
620
|
+
for (let i = 0; i < this.rows; i++) {
|
|
621
|
+
for (let j = 0; j < this.columns; j++) {
|
|
622
622
|
this.set(i, j, this.get(i, j) & matrix.get(i, j));
|
|
623
623
|
}
|
|
624
624
|
}
|
|
@@ -626,7 +626,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
626
626
|
};
|
|
627
627
|
|
|
628
628
|
AbstractMatrix.and = function and(matrix, value) {
|
|
629
|
-
|
|
629
|
+
const newMatrix = new Matrix(matrix);
|
|
630
630
|
return newMatrix.and(value);
|
|
631
631
|
};
|
|
632
632
|
|
|
@@ -636,8 +636,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
636
636
|
};
|
|
637
637
|
|
|
638
638
|
AbstractMatrix.prototype.orS = function orS(value) {
|
|
639
|
-
for (
|
|
640
|
-
for (
|
|
639
|
+
for (let i = 0; i < this.rows; i++) {
|
|
640
|
+
for (let j = 0; j < this.columns; j++) {
|
|
641
641
|
this.set(i, j, this.get(i, j) | value);
|
|
642
642
|
}
|
|
643
643
|
}
|
|
@@ -650,8 +650,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
650
650
|
this.columns !== matrix.columns) {
|
|
651
651
|
throw new RangeError('Matrices dimensions must be equal');
|
|
652
652
|
}
|
|
653
|
-
for (
|
|
654
|
-
for (
|
|
653
|
+
for (let i = 0; i < this.rows; i++) {
|
|
654
|
+
for (let j = 0; j < this.columns; j++) {
|
|
655
655
|
this.set(i, j, this.get(i, j) | matrix.get(i, j));
|
|
656
656
|
}
|
|
657
657
|
}
|
|
@@ -659,7 +659,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
659
659
|
};
|
|
660
660
|
|
|
661
661
|
AbstractMatrix.or = function or(matrix, value) {
|
|
662
|
-
|
|
662
|
+
const newMatrix = new Matrix(matrix);
|
|
663
663
|
return newMatrix.or(value);
|
|
664
664
|
};
|
|
665
665
|
|
|
@@ -669,8 +669,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
669
669
|
};
|
|
670
670
|
|
|
671
671
|
AbstractMatrix.prototype.xorS = function xorS(value) {
|
|
672
|
-
for (
|
|
673
|
-
for (
|
|
672
|
+
for (let i = 0; i < this.rows; i++) {
|
|
673
|
+
for (let j = 0; j < this.columns; j++) {
|
|
674
674
|
this.set(i, j, this.get(i, j) ^ value);
|
|
675
675
|
}
|
|
676
676
|
}
|
|
@@ -683,8 +683,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
683
683
|
this.columns !== matrix.columns) {
|
|
684
684
|
throw new RangeError('Matrices dimensions must be equal');
|
|
685
685
|
}
|
|
686
|
-
for (
|
|
687
|
-
for (
|
|
686
|
+
for (let i = 0; i < this.rows; i++) {
|
|
687
|
+
for (let j = 0; j < this.columns; j++) {
|
|
688
688
|
this.set(i, j, this.get(i, j) ^ matrix.get(i, j));
|
|
689
689
|
}
|
|
690
690
|
}
|
|
@@ -692,7 +692,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
692
692
|
};
|
|
693
693
|
|
|
694
694
|
AbstractMatrix.xor = function xor(matrix, value) {
|
|
695
|
-
|
|
695
|
+
const newMatrix = new Matrix(matrix);
|
|
696
696
|
return newMatrix.xor(value);
|
|
697
697
|
};
|
|
698
698
|
|
|
@@ -702,8 +702,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
702
702
|
};
|
|
703
703
|
|
|
704
704
|
AbstractMatrix.prototype.leftShiftS = function leftShiftS(value) {
|
|
705
|
-
for (
|
|
706
|
-
for (
|
|
705
|
+
for (let i = 0; i < this.rows; i++) {
|
|
706
|
+
for (let j = 0; j < this.columns; j++) {
|
|
707
707
|
this.set(i, j, this.get(i, j) << value);
|
|
708
708
|
}
|
|
709
709
|
}
|
|
@@ -716,8 +716,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
716
716
|
this.columns !== matrix.columns) {
|
|
717
717
|
throw new RangeError('Matrices dimensions must be equal');
|
|
718
718
|
}
|
|
719
|
-
for (
|
|
720
|
-
for (
|
|
719
|
+
for (let i = 0; i < this.rows; i++) {
|
|
720
|
+
for (let j = 0; j < this.columns; j++) {
|
|
721
721
|
this.set(i, j, this.get(i, j) << matrix.get(i, j));
|
|
722
722
|
}
|
|
723
723
|
}
|
|
@@ -725,7 +725,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
725
725
|
};
|
|
726
726
|
|
|
727
727
|
AbstractMatrix.leftShift = function leftShift(matrix, value) {
|
|
728
|
-
|
|
728
|
+
const newMatrix = new Matrix(matrix);
|
|
729
729
|
return newMatrix.leftShift(value);
|
|
730
730
|
};
|
|
731
731
|
|
|
@@ -735,8 +735,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
735
735
|
};
|
|
736
736
|
|
|
737
737
|
AbstractMatrix.prototype.signPropagatingRightShiftS = function signPropagatingRightShiftS(value) {
|
|
738
|
-
for (
|
|
739
|
-
for (
|
|
738
|
+
for (let i = 0; i < this.rows; i++) {
|
|
739
|
+
for (let j = 0; j < this.columns; j++) {
|
|
740
740
|
this.set(i, j, this.get(i, j) >> value);
|
|
741
741
|
}
|
|
742
742
|
}
|
|
@@ -749,8 +749,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
749
749
|
this.columns !== matrix.columns) {
|
|
750
750
|
throw new RangeError('Matrices dimensions must be equal');
|
|
751
751
|
}
|
|
752
|
-
for (
|
|
753
|
-
for (
|
|
752
|
+
for (let i = 0; i < this.rows; i++) {
|
|
753
|
+
for (let j = 0; j < this.columns; j++) {
|
|
754
754
|
this.set(i, j, this.get(i, j) >> matrix.get(i, j));
|
|
755
755
|
}
|
|
756
756
|
}
|
|
@@ -758,7 +758,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
758
758
|
};
|
|
759
759
|
|
|
760
760
|
AbstractMatrix.signPropagatingRightShift = function signPropagatingRightShift(matrix, value) {
|
|
761
|
-
|
|
761
|
+
const newMatrix = new Matrix(matrix);
|
|
762
762
|
return newMatrix.signPropagatingRightShift(value);
|
|
763
763
|
};
|
|
764
764
|
|
|
@@ -768,8 +768,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
768
768
|
};
|
|
769
769
|
|
|
770
770
|
AbstractMatrix.prototype.rightShiftS = function rightShiftS(value) {
|
|
771
|
-
for (
|
|
772
|
-
for (
|
|
771
|
+
for (let i = 0; i < this.rows; i++) {
|
|
772
|
+
for (let j = 0; j < this.columns; j++) {
|
|
773
773
|
this.set(i, j, this.get(i, j) >>> value);
|
|
774
774
|
}
|
|
775
775
|
}
|
|
@@ -782,8 +782,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
782
782
|
this.columns !== matrix.columns) {
|
|
783
783
|
throw new RangeError('Matrices dimensions must be equal');
|
|
784
784
|
}
|
|
785
|
-
for (
|
|
786
|
-
for (
|
|
785
|
+
for (let i = 0; i < this.rows; i++) {
|
|
786
|
+
for (let j = 0; j < this.columns; j++) {
|
|
787
787
|
this.set(i, j, this.get(i, j) >>> matrix.get(i, j));
|
|
788
788
|
}
|
|
789
789
|
}
|
|
@@ -791,7 +791,7 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
791
791
|
};
|
|
792
792
|
|
|
793
793
|
AbstractMatrix.rightShift = function rightShift(matrix, value) {
|
|
794
|
-
|
|
794
|
+
const newMatrix = new Matrix(matrix);
|
|
795
795
|
return newMatrix.rightShift(value);
|
|
796
796
|
};
|
|
797
797
|
AbstractMatrix.prototype.zeroFillRightShift = AbstractMatrix.prototype.rightShift;
|
|
@@ -800,8 +800,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
800
800
|
AbstractMatrix.zeroFillRightShift = AbstractMatrix.rightShift;
|
|
801
801
|
|
|
802
802
|
AbstractMatrix.prototype.not = function not() {
|
|
803
|
-
for (
|
|
804
|
-
for (
|
|
803
|
+
for (let i = 0; i < this.rows; i++) {
|
|
804
|
+
for (let j = 0; j < this.columns; j++) {
|
|
805
805
|
this.set(i, j, ~(this.get(i, j)));
|
|
806
806
|
}
|
|
807
807
|
}
|
|
@@ -809,13 +809,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
809
809
|
};
|
|
810
810
|
|
|
811
811
|
AbstractMatrix.not = function not(matrix) {
|
|
812
|
-
|
|
812
|
+
const newMatrix = new Matrix(matrix);
|
|
813
813
|
return newMatrix.not();
|
|
814
814
|
};
|
|
815
815
|
|
|
816
816
|
AbstractMatrix.prototype.abs = function abs() {
|
|
817
|
-
for (
|
|
818
|
-
for (
|
|
817
|
+
for (let i = 0; i < this.rows; i++) {
|
|
818
|
+
for (let j = 0; j < this.columns; j++) {
|
|
819
819
|
this.set(i, j, Math.abs(this.get(i, j)));
|
|
820
820
|
}
|
|
821
821
|
}
|
|
@@ -823,13 +823,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
823
823
|
};
|
|
824
824
|
|
|
825
825
|
AbstractMatrix.abs = function abs(matrix) {
|
|
826
|
-
|
|
826
|
+
const newMatrix = new Matrix(matrix);
|
|
827
827
|
return newMatrix.abs();
|
|
828
828
|
};
|
|
829
829
|
|
|
830
830
|
AbstractMatrix.prototype.acos = function acos() {
|
|
831
|
-
for (
|
|
832
|
-
for (
|
|
831
|
+
for (let i = 0; i < this.rows; i++) {
|
|
832
|
+
for (let j = 0; j < this.columns; j++) {
|
|
833
833
|
this.set(i, j, Math.acos(this.get(i, j)));
|
|
834
834
|
}
|
|
835
835
|
}
|
|
@@ -837,13 +837,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
837
837
|
};
|
|
838
838
|
|
|
839
839
|
AbstractMatrix.acos = function acos(matrix) {
|
|
840
|
-
|
|
840
|
+
const newMatrix = new Matrix(matrix);
|
|
841
841
|
return newMatrix.acos();
|
|
842
842
|
};
|
|
843
843
|
|
|
844
844
|
AbstractMatrix.prototype.acosh = function acosh() {
|
|
845
|
-
for (
|
|
846
|
-
for (
|
|
845
|
+
for (let i = 0; i < this.rows; i++) {
|
|
846
|
+
for (let j = 0; j < this.columns; j++) {
|
|
847
847
|
this.set(i, j, Math.acosh(this.get(i, j)));
|
|
848
848
|
}
|
|
849
849
|
}
|
|
@@ -851,13 +851,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
851
851
|
};
|
|
852
852
|
|
|
853
853
|
AbstractMatrix.acosh = function acosh(matrix) {
|
|
854
|
-
|
|
854
|
+
const newMatrix = new Matrix(matrix);
|
|
855
855
|
return newMatrix.acosh();
|
|
856
856
|
};
|
|
857
857
|
|
|
858
858
|
AbstractMatrix.prototype.asin = function asin() {
|
|
859
|
-
for (
|
|
860
|
-
for (
|
|
859
|
+
for (let i = 0; i < this.rows; i++) {
|
|
860
|
+
for (let j = 0; j < this.columns; j++) {
|
|
861
861
|
this.set(i, j, Math.asin(this.get(i, j)));
|
|
862
862
|
}
|
|
863
863
|
}
|
|
@@ -865,13 +865,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
865
865
|
};
|
|
866
866
|
|
|
867
867
|
AbstractMatrix.asin = function asin(matrix) {
|
|
868
|
-
|
|
868
|
+
const newMatrix = new Matrix(matrix);
|
|
869
869
|
return newMatrix.asin();
|
|
870
870
|
};
|
|
871
871
|
|
|
872
872
|
AbstractMatrix.prototype.asinh = function asinh() {
|
|
873
|
-
for (
|
|
874
|
-
for (
|
|
873
|
+
for (let i = 0; i < this.rows; i++) {
|
|
874
|
+
for (let j = 0; j < this.columns; j++) {
|
|
875
875
|
this.set(i, j, Math.asinh(this.get(i, j)));
|
|
876
876
|
}
|
|
877
877
|
}
|
|
@@ -879,13 +879,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
879
879
|
};
|
|
880
880
|
|
|
881
881
|
AbstractMatrix.asinh = function asinh(matrix) {
|
|
882
|
-
|
|
882
|
+
const newMatrix = new Matrix(matrix);
|
|
883
883
|
return newMatrix.asinh();
|
|
884
884
|
};
|
|
885
885
|
|
|
886
886
|
AbstractMatrix.prototype.atan = function atan() {
|
|
887
|
-
for (
|
|
888
|
-
for (
|
|
887
|
+
for (let i = 0; i < this.rows; i++) {
|
|
888
|
+
for (let j = 0; j < this.columns; j++) {
|
|
889
889
|
this.set(i, j, Math.atan(this.get(i, j)));
|
|
890
890
|
}
|
|
891
891
|
}
|
|
@@ -893,13 +893,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
893
893
|
};
|
|
894
894
|
|
|
895
895
|
AbstractMatrix.atan = function atan(matrix) {
|
|
896
|
-
|
|
896
|
+
const newMatrix = new Matrix(matrix);
|
|
897
897
|
return newMatrix.atan();
|
|
898
898
|
};
|
|
899
899
|
|
|
900
900
|
AbstractMatrix.prototype.atanh = function atanh() {
|
|
901
|
-
for (
|
|
902
|
-
for (
|
|
901
|
+
for (let i = 0; i < this.rows; i++) {
|
|
902
|
+
for (let j = 0; j < this.columns; j++) {
|
|
903
903
|
this.set(i, j, Math.atanh(this.get(i, j)));
|
|
904
904
|
}
|
|
905
905
|
}
|
|
@@ -907,13 +907,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
907
907
|
};
|
|
908
908
|
|
|
909
909
|
AbstractMatrix.atanh = function atanh(matrix) {
|
|
910
|
-
|
|
910
|
+
const newMatrix = new Matrix(matrix);
|
|
911
911
|
return newMatrix.atanh();
|
|
912
912
|
};
|
|
913
913
|
|
|
914
914
|
AbstractMatrix.prototype.cbrt = function cbrt() {
|
|
915
|
-
for (
|
|
916
|
-
for (
|
|
915
|
+
for (let i = 0; i < this.rows; i++) {
|
|
916
|
+
for (let j = 0; j < this.columns; j++) {
|
|
917
917
|
this.set(i, j, Math.cbrt(this.get(i, j)));
|
|
918
918
|
}
|
|
919
919
|
}
|
|
@@ -921,13 +921,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
921
921
|
};
|
|
922
922
|
|
|
923
923
|
AbstractMatrix.cbrt = function cbrt(matrix) {
|
|
924
|
-
|
|
924
|
+
const newMatrix = new Matrix(matrix);
|
|
925
925
|
return newMatrix.cbrt();
|
|
926
926
|
};
|
|
927
927
|
|
|
928
928
|
AbstractMatrix.prototype.ceil = function ceil() {
|
|
929
|
-
for (
|
|
930
|
-
for (
|
|
929
|
+
for (let i = 0; i < this.rows; i++) {
|
|
930
|
+
for (let j = 0; j < this.columns; j++) {
|
|
931
931
|
this.set(i, j, Math.ceil(this.get(i, j)));
|
|
932
932
|
}
|
|
933
933
|
}
|
|
@@ -935,13 +935,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
935
935
|
};
|
|
936
936
|
|
|
937
937
|
AbstractMatrix.ceil = function ceil(matrix) {
|
|
938
|
-
|
|
938
|
+
const newMatrix = new Matrix(matrix);
|
|
939
939
|
return newMatrix.ceil();
|
|
940
940
|
};
|
|
941
941
|
|
|
942
942
|
AbstractMatrix.prototype.clz32 = function clz32() {
|
|
943
|
-
for (
|
|
944
|
-
for (
|
|
943
|
+
for (let i = 0; i < this.rows; i++) {
|
|
944
|
+
for (let j = 0; j < this.columns; j++) {
|
|
945
945
|
this.set(i, j, Math.clz32(this.get(i, j)));
|
|
946
946
|
}
|
|
947
947
|
}
|
|
@@ -949,13 +949,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
949
949
|
};
|
|
950
950
|
|
|
951
951
|
AbstractMatrix.clz32 = function clz32(matrix) {
|
|
952
|
-
|
|
952
|
+
const newMatrix = new Matrix(matrix);
|
|
953
953
|
return newMatrix.clz32();
|
|
954
954
|
};
|
|
955
955
|
|
|
956
956
|
AbstractMatrix.prototype.cos = function cos() {
|
|
957
|
-
for (
|
|
958
|
-
for (
|
|
957
|
+
for (let i = 0; i < this.rows; i++) {
|
|
958
|
+
for (let j = 0; j < this.columns; j++) {
|
|
959
959
|
this.set(i, j, Math.cos(this.get(i, j)));
|
|
960
960
|
}
|
|
961
961
|
}
|
|
@@ -963,13 +963,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
963
963
|
};
|
|
964
964
|
|
|
965
965
|
AbstractMatrix.cos = function cos(matrix) {
|
|
966
|
-
|
|
966
|
+
const newMatrix = new Matrix(matrix);
|
|
967
967
|
return newMatrix.cos();
|
|
968
968
|
};
|
|
969
969
|
|
|
970
970
|
AbstractMatrix.prototype.cosh = function cosh() {
|
|
971
|
-
for (
|
|
972
|
-
for (
|
|
971
|
+
for (let i = 0; i < this.rows; i++) {
|
|
972
|
+
for (let j = 0; j < this.columns; j++) {
|
|
973
973
|
this.set(i, j, Math.cosh(this.get(i, j)));
|
|
974
974
|
}
|
|
975
975
|
}
|
|
@@ -977,13 +977,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
977
977
|
};
|
|
978
978
|
|
|
979
979
|
AbstractMatrix.cosh = function cosh(matrix) {
|
|
980
|
-
|
|
980
|
+
const newMatrix = new Matrix(matrix);
|
|
981
981
|
return newMatrix.cosh();
|
|
982
982
|
};
|
|
983
983
|
|
|
984
984
|
AbstractMatrix.prototype.exp = function exp() {
|
|
985
|
-
for (
|
|
986
|
-
for (
|
|
985
|
+
for (let i = 0; i < this.rows; i++) {
|
|
986
|
+
for (let j = 0; j < this.columns; j++) {
|
|
987
987
|
this.set(i, j, Math.exp(this.get(i, j)));
|
|
988
988
|
}
|
|
989
989
|
}
|
|
@@ -991,13 +991,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
991
991
|
};
|
|
992
992
|
|
|
993
993
|
AbstractMatrix.exp = function exp(matrix) {
|
|
994
|
-
|
|
994
|
+
const newMatrix = new Matrix(matrix);
|
|
995
995
|
return newMatrix.exp();
|
|
996
996
|
};
|
|
997
997
|
|
|
998
998
|
AbstractMatrix.prototype.expm1 = function expm1() {
|
|
999
|
-
for (
|
|
1000
|
-
for (
|
|
999
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1000
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1001
1001
|
this.set(i, j, Math.expm1(this.get(i, j)));
|
|
1002
1002
|
}
|
|
1003
1003
|
}
|
|
@@ -1005,13 +1005,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1005
1005
|
};
|
|
1006
1006
|
|
|
1007
1007
|
AbstractMatrix.expm1 = function expm1(matrix) {
|
|
1008
|
-
|
|
1008
|
+
const newMatrix = new Matrix(matrix);
|
|
1009
1009
|
return newMatrix.expm1();
|
|
1010
1010
|
};
|
|
1011
1011
|
|
|
1012
1012
|
AbstractMatrix.prototype.floor = function floor() {
|
|
1013
|
-
for (
|
|
1014
|
-
for (
|
|
1013
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1014
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1015
1015
|
this.set(i, j, Math.floor(this.get(i, j)));
|
|
1016
1016
|
}
|
|
1017
1017
|
}
|
|
@@ -1019,13 +1019,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1019
1019
|
};
|
|
1020
1020
|
|
|
1021
1021
|
AbstractMatrix.floor = function floor(matrix) {
|
|
1022
|
-
|
|
1022
|
+
const newMatrix = new Matrix(matrix);
|
|
1023
1023
|
return newMatrix.floor();
|
|
1024
1024
|
};
|
|
1025
1025
|
|
|
1026
1026
|
AbstractMatrix.prototype.fround = function fround() {
|
|
1027
|
-
for (
|
|
1028
|
-
for (
|
|
1027
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1028
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1029
1029
|
this.set(i, j, Math.fround(this.get(i, j)));
|
|
1030
1030
|
}
|
|
1031
1031
|
}
|
|
@@ -1033,13 +1033,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1033
1033
|
};
|
|
1034
1034
|
|
|
1035
1035
|
AbstractMatrix.fround = function fround(matrix) {
|
|
1036
|
-
|
|
1036
|
+
const newMatrix = new Matrix(matrix);
|
|
1037
1037
|
return newMatrix.fround();
|
|
1038
1038
|
};
|
|
1039
1039
|
|
|
1040
1040
|
AbstractMatrix.prototype.log = function log() {
|
|
1041
|
-
for (
|
|
1042
|
-
for (
|
|
1041
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1042
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1043
1043
|
this.set(i, j, Math.log(this.get(i, j)));
|
|
1044
1044
|
}
|
|
1045
1045
|
}
|
|
@@ -1047,13 +1047,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1047
1047
|
};
|
|
1048
1048
|
|
|
1049
1049
|
AbstractMatrix.log = function log(matrix) {
|
|
1050
|
-
|
|
1050
|
+
const newMatrix = new Matrix(matrix);
|
|
1051
1051
|
return newMatrix.log();
|
|
1052
1052
|
};
|
|
1053
1053
|
|
|
1054
1054
|
AbstractMatrix.prototype.log1p = function log1p() {
|
|
1055
|
-
for (
|
|
1056
|
-
for (
|
|
1055
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1056
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1057
1057
|
this.set(i, j, Math.log1p(this.get(i, j)));
|
|
1058
1058
|
}
|
|
1059
1059
|
}
|
|
@@ -1061,13 +1061,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1061
1061
|
};
|
|
1062
1062
|
|
|
1063
1063
|
AbstractMatrix.log1p = function log1p(matrix) {
|
|
1064
|
-
|
|
1064
|
+
const newMatrix = new Matrix(matrix);
|
|
1065
1065
|
return newMatrix.log1p();
|
|
1066
1066
|
};
|
|
1067
1067
|
|
|
1068
1068
|
AbstractMatrix.prototype.log10 = function log10() {
|
|
1069
|
-
for (
|
|
1070
|
-
for (
|
|
1069
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1070
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1071
1071
|
this.set(i, j, Math.log10(this.get(i, j)));
|
|
1072
1072
|
}
|
|
1073
1073
|
}
|
|
@@ -1075,13 +1075,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1075
1075
|
};
|
|
1076
1076
|
|
|
1077
1077
|
AbstractMatrix.log10 = function log10(matrix) {
|
|
1078
|
-
|
|
1078
|
+
const newMatrix = new Matrix(matrix);
|
|
1079
1079
|
return newMatrix.log10();
|
|
1080
1080
|
};
|
|
1081
1081
|
|
|
1082
1082
|
AbstractMatrix.prototype.log2 = function log2() {
|
|
1083
|
-
for (
|
|
1084
|
-
for (
|
|
1083
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1084
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1085
1085
|
this.set(i, j, Math.log2(this.get(i, j)));
|
|
1086
1086
|
}
|
|
1087
1087
|
}
|
|
@@ -1089,13 +1089,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1089
1089
|
};
|
|
1090
1090
|
|
|
1091
1091
|
AbstractMatrix.log2 = function log2(matrix) {
|
|
1092
|
-
|
|
1092
|
+
const newMatrix = new Matrix(matrix);
|
|
1093
1093
|
return newMatrix.log2();
|
|
1094
1094
|
};
|
|
1095
1095
|
|
|
1096
1096
|
AbstractMatrix.prototype.round = function round() {
|
|
1097
|
-
for (
|
|
1098
|
-
for (
|
|
1097
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1098
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1099
1099
|
this.set(i, j, Math.round(this.get(i, j)));
|
|
1100
1100
|
}
|
|
1101
1101
|
}
|
|
@@ -1103,13 +1103,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1103
1103
|
};
|
|
1104
1104
|
|
|
1105
1105
|
AbstractMatrix.round = function round(matrix) {
|
|
1106
|
-
|
|
1106
|
+
const newMatrix = new Matrix(matrix);
|
|
1107
1107
|
return newMatrix.round();
|
|
1108
1108
|
};
|
|
1109
1109
|
|
|
1110
1110
|
AbstractMatrix.prototype.sign = function sign() {
|
|
1111
|
-
for (
|
|
1112
|
-
for (
|
|
1111
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1112
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1113
1113
|
this.set(i, j, Math.sign(this.get(i, j)));
|
|
1114
1114
|
}
|
|
1115
1115
|
}
|
|
@@ -1117,13 +1117,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1117
1117
|
};
|
|
1118
1118
|
|
|
1119
1119
|
AbstractMatrix.sign = function sign(matrix) {
|
|
1120
|
-
|
|
1120
|
+
const newMatrix = new Matrix(matrix);
|
|
1121
1121
|
return newMatrix.sign();
|
|
1122
1122
|
};
|
|
1123
1123
|
|
|
1124
1124
|
AbstractMatrix.prototype.sin = function sin() {
|
|
1125
|
-
for (
|
|
1126
|
-
for (
|
|
1125
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1126
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1127
1127
|
this.set(i, j, Math.sin(this.get(i, j)));
|
|
1128
1128
|
}
|
|
1129
1129
|
}
|
|
@@ -1131,13 +1131,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1131
1131
|
};
|
|
1132
1132
|
|
|
1133
1133
|
AbstractMatrix.sin = function sin(matrix) {
|
|
1134
|
-
|
|
1134
|
+
const newMatrix = new Matrix(matrix);
|
|
1135
1135
|
return newMatrix.sin();
|
|
1136
1136
|
};
|
|
1137
1137
|
|
|
1138
1138
|
AbstractMatrix.prototype.sinh = function sinh() {
|
|
1139
|
-
for (
|
|
1140
|
-
for (
|
|
1139
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1140
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1141
1141
|
this.set(i, j, Math.sinh(this.get(i, j)));
|
|
1142
1142
|
}
|
|
1143
1143
|
}
|
|
@@ -1145,13 +1145,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1145
1145
|
};
|
|
1146
1146
|
|
|
1147
1147
|
AbstractMatrix.sinh = function sinh(matrix) {
|
|
1148
|
-
|
|
1148
|
+
const newMatrix = new Matrix(matrix);
|
|
1149
1149
|
return newMatrix.sinh();
|
|
1150
1150
|
};
|
|
1151
1151
|
|
|
1152
1152
|
AbstractMatrix.prototype.sqrt = function sqrt() {
|
|
1153
|
-
for (
|
|
1154
|
-
for (
|
|
1153
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1154
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1155
1155
|
this.set(i, j, Math.sqrt(this.get(i, j)));
|
|
1156
1156
|
}
|
|
1157
1157
|
}
|
|
@@ -1159,13 +1159,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1159
1159
|
};
|
|
1160
1160
|
|
|
1161
1161
|
AbstractMatrix.sqrt = function sqrt(matrix) {
|
|
1162
|
-
|
|
1162
|
+
const newMatrix = new Matrix(matrix);
|
|
1163
1163
|
return newMatrix.sqrt();
|
|
1164
1164
|
};
|
|
1165
1165
|
|
|
1166
1166
|
AbstractMatrix.prototype.tan = function tan() {
|
|
1167
|
-
for (
|
|
1168
|
-
for (
|
|
1167
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1168
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1169
1169
|
this.set(i, j, Math.tan(this.get(i, j)));
|
|
1170
1170
|
}
|
|
1171
1171
|
}
|
|
@@ -1173,13 +1173,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1173
1173
|
};
|
|
1174
1174
|
|
|
1175
1175
|
AbstractMatrix.tan = function tan(matrix) {
|
|
1176
|
-
|
|
1176
|
+
const newMatrix = new Matrix(matrix);
|
|
1177
1177
|
return newMatrix.tan();
|
|
1178
1178
|
};
|
|
1179
1179
|
|
|
1180
1180
|
AbstractMatrix.prototype.tanh = function tanh() {
|
|
1181
|
-
for (
|
|
1182
|
-
for (
|
|
1181
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1182
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1183
1183
|
this.set(i, j, Math.tanh(this.get(i, j)));
|
|
1184
1184
|
}
|
|
1185
1185
|
}
|
|
@@ -1187,13 +1187,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1187
1187
|
};
|
|
1188
1188
|
|
|
1189
1189
|
AbstractMatrix.tanh = function tanh(matrix) {
|
|
1190
|
-
|
|
1190
|
+
const newMatrix = new Matrix(matrix);
|
|
1191
1191
|
return newMatrix.tanh();
|
|
1192
1192
|
};
|
|
1193
1193
|
|
|
1194
1194
|
AbstractMatrix.prototype.trunc = function trunc() {
|
|
1195
|
-
for (
|
|
1196
|
-
for (
|
|
1195
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1196
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1197
1197
|
this.set(i, j, Math.trunc(this.get(i, j)));
|
|
1198
1198
|
}
|
|
1199
1199
|
}
|
|
@@ -1201,12 +1201,12 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1201
1201
|
};
|
|
1202
1202
|
|
|
1203
1203
|
AbstractMatrix.trunc = function trunc(matrix) {
|
|
1204
|
-
|
|
1204
|
+
const newMatrix = new Matrix(matrix);
|
|
1205
1205
|
return newMatrix.trunc();
|
|
1206
1206
|
};
|
|
1207
1207
|
|
|
1208
1208
|
AbstractMatrix.pow = function pow(matrix, arg0) {
|
|
1209
|
-
|
|
1209
|
+
const newMatrix = new Matrix(matrix);
|
|
1210
1210
|
return newMatrix.pow(arg0);
|
|
1211
1211
|
};
|
|
1212
1212
|
|
|
@@ -1216,8 +1216,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1216
1216
|
};
|
|
1217
1217
|
|
|
1218
1218
|
AbstractMatrix.prototype.powS = function powS(value) {
|
|
1219
|
-
for (
|
|
1220
|
-
for (
|
|
1219
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1220
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1221
1221
|
this.set(i, j, Math.pow(this.get(i, j), value));
|
|
1222
1222
|
}
|
|
1223
1223
|
}
|
|
@@ -1230,8 +1230,8 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1230
1230
|
this.columns !== matrix.columns) {
|
|
1231
1231
|
throw new RangeError('Matrices dimensions must be equal');
|
|
1232
1232
|
}
|
|
1233
|
-
for (
|
|
1234
|
-
for (
|
|
1233
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1234
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1235
1235
|
this.set(i, j, Math.pow(this.get(i, j), matrix.get(i, j)));
|
|
1236
1236
|
}
|
|
1237
1237
|
}
|
|
@@ -1241,13 +1241,13 @@ function installMathOperations(AbstractMatrix, Matrix) {
|
|
|
1241
1241
|
|
|
1242
1242
|
class AbstractMatrix {
|
|
1243
1243
|
static from1DArray(newRows, newColumns, newData) {
|
|
1244
|
-
|
|
1244
|
+
let length = newRows * newColumns;
|
|
1245
1245
|
if (length !== newData.length) {
|
|
1246
1246
|
throw new RangeError('data length does not match given dimensions');
|
|
1247
1247
|
}
|
|
1248
|
-
|
|
1249
|
-
for (
|
|
1250
|
-
for (
|
|
1248
|
+
let newMatrix = new Matrix(newRows, newColumns);
|
|
1249
|
+
for (let row = 0; row < newRows; row++) {
|
|
1250
|
+
for (let column = 0; column < newColumns; column++) {
|
|
1251
1251
|
newMatrix.set(row, column, newData[row * newColumns + column]);
|
|
1252
1252
|
}
|
|
1253
1253
|
}
|
|
@@ -1255,16 +1255,16 @@ class AbstractMatrix {
|
|
|
1255
1255
|
}
|
|
1256
1256
|
|
|
1257
1257
|
static rowVector(newData) {
|
|
1258
|
-
|
|
1259
|
-
for (
|
|
1258
|
+
let vector = new Matrix(1, newData.length);
|
|
1259
|
+
for (let i = 0; i < newData.length; i++) {
|
|
1260
1260
|
vector.set(0, i, newData[i]);
|
|
1261
1261
|
}
|
|
1262
1262
|
return vector;
|
|
1263
1263
|
}
|
|
1264
1264
|
|
|
1265
1265
|
static columnVector(newData) {
|
|
1266
|
-
|
|
1267
|
-
for (
|
|
1266
|
+
let vector = new Matrix(newData.length, 1);
|
|
1267
|
+
for (let i = 0; i < newData.length; i++) {
|
|
1268
1268
|
vector.set(i, 0, newData[i]);
|
|
1269
1269
|
}
|
|
1270
1270
|
return vector;
|
|
@@ -1283,9 +1283,9 @@ class AbstractMatrix {
|
|
|
1283
1283
|
throw new TypeError('options must be an object');
|
|
1284
1284
|
}
|
|
1285
1285
|
const { random = Math.random } = options;
|
|
1286
|
-
|
|
1287
|
-
for (
|
|
1288
|
-
for (
|
|
1286
|
+
let matrix = new Matrix(rows, columns);
|
|
1287
|
+
for (let i = 0; i < rows; i++) {
|
|
1288
|
+
for (let j = 0; j < columns; j++) {
|
|
1289
1289
|
matrix.set(i, j, random());
|
|
1290
1290
|
}
|
|
1291
1291
|
}
|
|
@@ -1300,11 +1300,11 @@ class AbstractMatrix {
|
|
|
1300
1300
|
if (!Number.isInteger(min)) throw new TypeError('min must be an integer');
|
|
1301
1301
|
if (!Number.isInteger(max)) throw new TypeError('max must be an integer');
|
|
1302
1302
|
if (min >= max) throw new RangeError('min must be smaller than max');
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
for (
|
|
1306
|
-
for (
|
|
1307
|
-
|
|
1303
|
+
let interval = max - min;
|
|
1304
|
+
let matrix = new Matrix(rows, columns);
|
|
1305
|
+
for (let i = 0; i < rows; i++) {
|
|
1306
|
+
for (let j = 0; j < columns; j++) {
|
|
1307
|
+
let value = min + Math.round(random() * interval);
|
|
1308
1308
|
matrix.set(i, j, value);
|
|
1309
1309
|
}
|
|
1310
1310
|
}
|
|
@@ -1314,21 +1314,21 @@ class AbstractMatrix {
|
|
|
1314
1314
|
static eye(rows, columns, value) {
|
|
1315
1315
|
if (columns === undefined) columns = rows;
|
|
1316
1316
|
if (value === undefined) value = 1;
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
for (
|
|
1317
|
+
let min = Math.min(rows, columns);
|
|
1318
|
+
let matrix = this.zeros(rows, columns);
|
|
1319
|
+
for (let i = 0; i < min; i++) {
|
|
1320
1320
|
matrix.set(i, i, value);
|
|
1321
1321
|
}
|
|
1322
1322
|
return matrix;
|
|
1323
1323
|
}
|
|
1324
1324
|
|
|
1325
1325
|
static diag(data, rows, columns) {
|
|
1326
|
-
|
|
1326
|
+
let l = data.length;
|
|
1327
1327
|
if (rows === undefined) rows = l;
|
|
1328
1328
|
if (columns === undefined) columns = rows;
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
for (
|
|
1329
|
+
let min = Math.min(l, rows, columns);
|
|
1330
|
+
let matrix = this.zeros(rows, columns);
|
|
1331
|
+
for (let i = 0; i < min; i++) {
|
|
1332
1332
|
matrix.set(i, i, data[i]);
|
|
1333
1333
|
}
|
|
1334
1334
|
return matrix;
|
|
@@ -1337,11 +1337,11 @@ class AbstractMatrix {
|
|
|
1337
1337
|
static min(matrix1, matrix2) {
|
|
1338
1338
|
matrix1 = this.checkMatrix(matrix1);
|
|
1339
1339
|
matrix2 = this.checkMatrix(matrix2);
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
for (
|
|
1344
|
-
for (
|
|
1340
|
+
let rows = matrix1.rows;
|
|
1341
|
+
let columns = matrix1.columns;
|
|
1342
|
+
let result = new Matrix(rows, columns);
|
|
1343
|
+
for (let i = 0; i < rows; i++) {
|
|
1344
|
+
for (let j = 0; j < columns; j++) {
|
|
1345
1345
|
result.set(i, j, Math.min(matrix1.get(i, j), matrix2.get(i, j)));
|
|
1346
1346
|
}
|
|
1347
1347
|
}
|
|
@@ -1351,11 +1351,11 @@ class AbstractMatrix {
|
|
|
1351
1351
|
static max(matrix1, matrix2) {
|
|
1352
1352
|
matrix1 = this.checkMatrix(matrix1);
|
|
1353
1353
|
matrix2 = this.checkMatrix(matrix2);
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
for (
|
|
1358
|
-
for (
|
|
1354
|
+
let rows = matrix1.rows;
|
|
1355
|
+
let columns = matrix1.columns;
|
|
1356
|
+
let result = new this(rows, columns);
|
|
1357
|
+
for (let i = 0; i < rows; i++) {
|
|
1358
|
+
for (let j = 0; j < columns; j++) {
|
|
1359
1359
|
result.set(i, j, Math.max(matrix1.get(i, j), matrix2.get(i, j)));
|
|
1360
1360
|
}
|
|
1361
1361
|
}
|
|
@@ -1378,8 +1378,8 @@ class AbstractMatrix {
|
|
|
1378
1378
|
if (typeof callback !== 'function') {
|
|
1379
1379
|
throw new TypeError('callback must be a function');
|
|
1380
1380
|
}
|
|
1381
|
-
for (
|
|
1382
|
-
for (
|
|
1381
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1382
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1383
1383
|
callback.call(this, i, j);
|
|
1384
1384
|
}
|
|
1385
1385
|
}
|
|
@@ -1387,9 +1387,9 @@ class AbstractMatrix {
|
|
|
1387
1387
|
}
|
|
1388
1388
|
|
|
1389
1389
|
to1DArray() {
|
|
1390
|
-
|
|
1391
|
-
for (
|
|
1392
|
-
for (
|
|
1390
|
+
let array = [];
|
|
1391
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1392
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1393
1393
|
array.push(this.get(i, j));
|
|
1394
1394
|
}
|
|
1395
1395
|
}
|
|
@@ -1397,10 +1397,10 @@ class AbstractMatrix {
|
|
|
1397
1397
|
}
|
|
1398
1398
|
|
|
1399
1399
|
to2DArray() {
|
|
1400
|
-
|
|
1401
|
-
for (
|
|
1400
|
+
let copy = [];
|
|
1401
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1402
1402
|
copy.push([]);
|
|
1403
|
-
for (
|
|
1403
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1404
1404
|
copy[i].push(this.get(i, j));
|
|
1405
1405
|
}
|
|
1406
1406
|
}
|
|
@@ -1429,8 +1429,8 @@ class AbstractMatrix {
|
|
|
1429
1429
|
|
|
1430
1430
|
isSymmetric() {
|
|
1431
1431
|
if (this.isSquare()) {
|
|
1432
|
-
for (
|
|
1433
|
-
for (
|
|
1432
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1433
|
+
for (let j = 0; j <= i; j++) {
|
|
1434
1434
|
if (this.get(i, j) !== this.get(j, i)) {
|
|
1435
1435
|
return false;
|
|
1436
1436
|
}
|
|
@@ -1500,7 +1500,7 @@ class AbstractMatrix {
|
|
|
1500
1500
|
let result = this.clone();
|
|
1501
1501
|
let h = 0;
|
|
1502
1502
|
let k = 0;
|
|
1503
|
-
while (
|
|
1503
|
+
while (h < result.rows && k < result.columns) {
|
|
1504
1504
|
let iMax = h;
|
|
1505
1505
|
for (let i = h; i < result.rows; i++) {
|
|
1506
1506
|
if (result.get(i, k) > result.get(iMax, k)) {
|
|
@@ -1540,7 +1540,7 @@ class AbstractMatrix {
|
|
|
1540
1540
|
} else {
|
|
1541
1541
|
let p = 0;
|
|
1542
1542
|
let pivot = false;
|
|
1543
|
-
while (
|
|
1543
|
+
while (p < n && pivot === false) {
|
|
1544
1544
|
if (result.get(h, p) === 1) {
|
|
1545
1545
|
pivot = true;
|
|
1546
1546
|
} else {
|
|
@@ -1579,9 +1579,9 @@ class AbstractMatrix {
|
|
|
1579
1579
|
if (!Number.isInteger(columns) || columns <= 0) {
|
|
1580
1580
|
throw new TypeError('columns must be a positive integer');
|
|
1581
1581
|
}
|
|
1582
|
-
|
|
1583
|
-
for (
|
|
1584
|
-
for (
|
|
1582
|
+
let matrix = new Matrix(this.rows * rows, this.columns * columns);
|
|
1583
|
+
for (let i = 0; i < rows; i++) {
|
|
1584
|
+
for (let j = 0; j < columns; j++) {
|
|
1585
1585
|
matrix.setSubMatrix(this, this.rows * i, this.columns * j);
|
|
1586
1586
|
}
|
|
1587
1587
|
}
|
|
@@ -1589,8 +1589,8 @@ class AbstractMatrix {
|
|
|
1589
1589
|
}
|
|
1590
1590
|
|
|
1591
1591
|
fill(value) {
|
|
1592
|
-
for (
|
|
1593
|
-
for (
|
|
1592
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1593
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1594
1594
|
this.set(i, j, value);
|
|
1595
1595
|
}
|
|
1596
1596
|
}
|
|
@@ -1603,8 +1603,8 @@ class AbstractMatrix {
|
|
|
1603
1603
|
|
|
1604
1604
|
getRow(index) {
|
|
1605
1605
|
checkRowIndex(this, index);
|
|
1606
|
-
|
|
1607
|
-
for (
|
|
1606
|
+
let row = [];
|
|
1607
|
+
for (let i = 0; i < this.columns; i++) {
|
|
1608
1608
|
row.push(this.get(index, i));
|
|
1609
1609
|
}
|
|
1610
1610
|
return row;
|
|
@@ -1617,7 +1617,7 @@ class AbstractMatrix {
|
|
|
1617
1617
|
setRow(index, array) {
|
|
1618
1618
|
checkRowIndex(this, index);
|
|
1619
1619
|
array = checkRowVector(this, array);
|
|
1620
|
-
for (
|
|
1620
|
+
for (let i = 0; i < this.columns; i++) {
|
|
1621
1621
|
this.set(index, i, array[i]);
|
|
1622
1622
|
}
|
|
1623
1623
|
return this;
|
|
@@ -1626,8 +1626,8 @@ class AbstractMatrix {
|
|
|
1626
1626
|
swapRows(row1, row2) {
|
|
1627
1627
|
checkRowIndex(this, row1);
|
|
1628
1628
|
checkRowIndex(this, row2);
|
|
1629
|
-
for (
|
|
1630
|
-
|
|
1629
|
+
for (let i = 0; i < this.columns; i++) {
|
|
1630
|
+
let temp = this.get(row1, i);
|
|
1631
1631
|
this.set(row1, i, this.get(row2, i));
|
|
1632
1632
|
this.set(row2, i, temp);
|
|
1633
1633
|
}
|
|
@@ -1636,8 +1636,8 @@ class AbstractMatrix {
|
|
|
1636
1636
|
|
|
1637
1637
|
getColumn(index) {
|
|
1638
1638
|
checkColumnIndex(this, index);
|
|
1639
|
-
|
|
1640
|
-
for (
|
|
1639
|
+
let column = [];
|
|
1640
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1641
1641
|
column.push(this.get(i, index));
|
|
1642
1642
|
}
|
|
1643
1643
|
return column;
|
|
@@ -1650,7 +1650,7 @@ class AbstractMatrix {
|
|
|
1650
1650
|
setColumn(index, array) {
|
|
1651
1651
|
checkColumnIndex(this, index);
|
|
1652
1652
|
array = checkColumnVector(this, array);
|
|
1653
|
-
for (
|
|
1653
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1654
1654
|
this.set(i, index, array[i]);
|
|
1655
1655
|
}
|
|
1656
1656
|
return this;
|
|
@@ -1659,8 +1659,8 @@ class AbstractMatrix {
|
|
|
1659
1659
|
swapColumns(column1, column2) {
|
|
1660
1660
|
checkColumnIndex(this, column1);
|
|
1661
1661
|
checkColumnIndex(this, column2);
|
|
1662
|
-
for (
|
|
1663
|
-
|
|
1662
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1663
|
+
let temp = this.get(i, column1);
|
|
1664
1664
|
this.set(i, column1, this.get(i, column2));
|
|
1665
1665
|
this.set(i, column2, temp);
|
|
1666
1666
|
}
|
|
@@ -1669,8 +1669,8 @@ class AbstractMatrix {
|
|
|
1669
1669
|
|
|
1670
1670
|
addRowVector(vector) {
|
|
1671
1671
|
vector = checkRowVector(this, vector);
|
|
1672
|
-
for (
|
|
1673
|
-
for (
|
|
1672
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1673
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1674
1674
|
this.set(i, j, this.get(i, j) + vector[j]);
|
|
1675
1675
|
}
|
|
1676
1676
|
}
|
|
@@ -1679,8 +1679,8 @@ class AbstractMatrix {
|
|
|
1679
1679
|
|
|
1680
1680
|
subRowVector(vector) {
|
|
1681
1681
|
vector = checkRowVector(this, vector);
|
|
1682
|
-
for (
|
|
1683
|
-
for (
|
|
1682
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1683
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1684
1684
|
this.set(i, j, this.get(i, j) - vector[j]);
|
|
1685
1685
|
}
|
|
1686
1686
|
}
|
|
@@ -1689,8 +1689,8 @@ class AbstractMatrix {
|
|
|
1689
1689
|
|
|
1690
1690
|
mulRowVector(vector) {
|
|
1691
1691
|
vector = checkRowVector(this, vector);
|
|
1692
|
-
for (
|
|
1693
|
-
for (
|
|
1692
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1693
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1694
1694
|
this.set(i, j, this.get(i, j) * vector[j]);
|
|
1695
1695
|
}
|
|
1696
1696
|
}
|
|
@@ -1699,8 +1699,8 @@ class AbstractMatrix {
|
|
|
1699
1699
|
|
|
1700
1700
|
divRowVector(vector) {
|
|
1701
1701
|
vector = checkRowVector(this, vector);
|
|
1702
|
-
for (
|
|
1703
|
-
for (
|
|
1702
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1703
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1704
1704
|
this.set(i, j, this.get(i, j) / vector[j]);
|
|
1705
1705
|
}
|
|
1706
1706
|
}
|
|
@@ -1709,8 +1709,8 @@ class AbstractMatrix {
|
|
|
1709
1709
|
|
|
1710
1710
|
addColumnVector(vector) {
|
|
1711
1711
|
vector = checkColumnVector(this, vector);
|
|
1712
|
-
for (
|
|
1713
|
-
for (
|
|
1712
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1713
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1714
1714
|
this.set(i, j, this.get(i, j) + vector[i]);
|
|
1715
1715
|
}
|
|
1716
1716
|
}
|
|
@@ -1719,8 +1719,8 @@ class AbstractMatrix {
|
|
|
1719
1719
|
|
|
1720
1720
|
subColumnVector(vector) {
|
|
1721
1721
|
vector = checkColumnVector(this, vector);
|
|
1722
|
-
for (
|
|
1723
|
-
for (
|
|
1722
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1723
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1724
1724
|
this.set(i, j, this.get(i, j) - vector[i]);
|
|
1725
1725
|
}
|
|
1726
1726
|
}
|
|
@@ -1729,8 +1729,8 @@ class AbstractMatrix {
|
|
|
1729
1729
|
|
|
1730
1730
|
mulColumnVector(vector) {
|
|
1731
1731
|
vector = checkColumnVector(this, vector);
|
|
1732
|
-
for (
|
|
1733
|
-
for (
|
|
1732
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1733
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1734
1734
|
this.set(i, j, this.get(i, j) * vector[i]);
|
|
1735
1735
|
}
|
|
1736
1736
|
}
|
|
@@ -1739,8 +1739,8 @@ class AbstractMatrix {
|
|
|
1739
1739
|
|
|
1740
1740
|
divColumnVector(vector) {
|
|
1741
1741
|
vector = checkColumnVector(this, vector);
|
|
1742
|
-
for (
|
|
1743
|
-
for (
|
|
1742
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1743
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1744
1744
|
this.set(i, j, this.get(i, j) / vector[i]);
|
|
1745
1745
|
}
|
|
1746
1746
|
}
|
|
@@ -1749,7 +1749,7 @@ class AbstractMatrix {
|
|
|
1749
1749
|
|
|
1750
1750
|
mulRow(index, value) {
|
|
1751
1751
|
checkRowIndex(this, index);
|
|
1752
|
-
for (
|
|
1752
|
+
for (let i = 0; i < this.columns; i++) {
|
|
1753
1753
|
this.set(index, i, this.get(index, i) * value);
|
|
1754
1754
|
}
|
|
1755
1755
|
return this;
|
|
@@ -1757,16 +1757,16 @@ class AbstractMatrix {
|
|
|
1757
1757
|
|
|
1758
1758
|
mulColumn(index, value) {
|
|
1759
1759
|
checkColumnIndex(this, index);
|
|
1760
|
-
for (
|
|
1760
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1761
1761
|
this.set(i, index, this.get(i, index) * value);
|
|
1762
1762
|
}
|
|
1763
1763
|
return this;
|
|
1764
1764
|
}
|
|
1765
1765
|
|
|
1766
1766
|
max() {
|
|
1767
|
-
|
|
1768
|
-
for (
|
|
1769
|
-
for (
|
|
1767
|
+
let v = this.get(0, 0);
|
|
1768
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1769
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1770
1770
|
if (this.get(i, j) > v) {
|
|
1771
1771
|
v = this.get(i, j);
|
|
1772
1772
|
}
|
|
@@ -1776,10 +1776,10 @@ class AbstractMatrix {
|
|
|
1776
1776
|
}
|
|
1777
1777
|
|
|
1778
1778
|
maxIndex() {
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
for (
|
|
1782
|
-
for (
|
|
1779
|
+
let v = this.get(0, 0);
|
|
1780
|
+
let idx = [0, 0];
|
|
1781
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1782
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1783
1783
|
if (this.get(i, j) > v) {
|
|
1784
1784
|
v = this.get(i, j);
|
|
1785
1785
|
idx[0] = i;
|
|
@@ -1791,9 +1791,9 @@ class AbstractMatrix {
|
|
|
1791
1791
|
}
|
|
1792
1792
|
|
|
1793
1793
|
min() {
|
|
1794
|
-
|
|
1795
|
-
for (
|
|
1796
|
-
for (
|
|
1794
|
+
let v = this.get(0, 0);
|
|
1795
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1796
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1797
1797
|
if (this.get(i, j) < v) {
|
|
1798
1798
|
v = this.get(i, j);
|
|
1799
1799
|
}
|
|
@@ -1803,10 +1803,10 @@ class AbstractMatrix {
|
|
|
1803
1803
|
}
|
|
1804
1804
|
|
|
1805
1805
|
minIndex() {
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
for (
|
|
1809
|
-
for (
|
|
1806
|
+
let v = this.get(0, 0);
|
|
1807
|
+
let idx = [0, 0];
|
|
1808
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1809
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1810
1810
|
if (this.get(i, j) < v) {
|
|
1811
1811
|
v = this.get(i, j);
|
|
1812
1812
|
idx[0] = i;
|
|
@@ -1819,8 +1819,8 @@ class AbstractMatrix {
|
|
|
1819
1819
|
|
|
1820
1820
|
maxRow(row) {
|
|
1821
1821
|
checkRowIndex(this, row);
|
|
1822
|
-
|
|
1823
|
-
for (
|
|
1822
|
+
let v = this.get(row, 0);
|
|
1823
|
+
for (let i = 1; i < this.columns; i++) {
|
|
1824
1824
|
if (this.get(row, i) > v) {
|
|
1825
1825
|
v = this.get(row, i);
|
|
1826
1826
|
}
|
|
@@ -1830,9 +1830,9 @@ class AbstractMatrix {
|
|
|
1830
1830
|
|
|
1831
1831
|
maxRowIndex(row) {
|
|
1832
1832
|
checkRowIndex(this, row);
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
for (
|
|
1833
|
+
let v = this.get(row, 0);
|
|
1834
|
+
let idx = [row, 0];
|
|
1835
|
+
for (let i = 1; i < this.columns; i++) {
|
|
1836
1836
|
if (this.get(row, i) > v) {
|
|
1837
1837
|
v = this.get(row, i);
|
|
1838
1838
|
idx[1] = i;
|
|
@@ -1843,8 +1843,8 @@ class AbstractMatrix {
|
|
|
1843
1843
|
|
|
1844
1844
|
minRow(row) {
|
|
1845
1845
|
checkRowIndex(this, row);
|
|
1846
|
-
|
|
1847
|
-
for (
|
|
1846
|
+
let v = this.get(row, 0);
|
|
1847
|
+
for (let i = 1; i < this.columns; i++) {
|
|
1848
1848
|
if (this.get(row, i) < v) {
|
|
1849
1849
|
v = this.get(row, i);
|
|
1850
1850
|
}
|
|
@@ -1854,9 +1854,9 @@ class AbstractMatrix {
|
|
|
1854
1854
|
|
|
1855
1855
|
minRowIndex(row) {
|
|
1856
1856
|
checkRowIndex(this, row);
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
for (
|
|
1857
|
+
let v = this.get(row, 0);
|
|
1858
|
+
let idx = [row, 0];
|
|
1859
|
+
for (let i = 1; i < this.columns; i++) {
|
|
1860
1860
|
if (this.get(row, i) < v) {
|
|
1861
1861
|
v = this.get(row, i);
|
|
1862
1862
|
idx[1] = i;
|
|
@@ -1867,8 +1867,8 @@ class AbstractMatrix {
|
|
|
1867
1867
|
|
|
1868
1868
|
maxColumn(column) {
|
|
1869
1869
|
checkColumnIndex(this, column);
|
|
1870
|
-
|
|
1871
|
-
for (
|
|
1870
|
+
let v = this.get(0, column);
|
|
1871
|
+
for (let i = 1; i < this.rows; i++) {
|
|
1872
1872
|
if (this.get(i, column) > v) {
|
|
1873
1873
|
v = this.get(i, column);
|
|
1874
1874
|
}
|
|
@@ -1878,9 +1878,9 @@ class AbstractMatrix {
|
|
|
1878
1878
|
|
|
1879
1879
|
maxColumnIndex(column) {
|
|
1880
1880
|
checkColumnIndex(this, column);
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
for (
|
|
1881
|
+
let v = this.get(0, column);
|
|
1882
|
+
let idx = [0, column];
|
|
1883
|
+
for (let i = 1; i < this.rows; i++) {
|
|
1884
1884
|
if (this.get(i, column) > v) {
|
|
1885
1885
|
v = this.get(i, column);
|
|
1886
1886
|
idx[0] = i;
|
|
@@ -1891,8 +1891,8 @@ class AbstractMatrix {
|
|
|
1891
1891
|
|
|
1892
1892
|
minColumn(column) {
|
|
1893
1893
|
checkColumnIndex(this, column);
|
|
1894
|
-
|
|
1895
|
-
for (
|
|
1894
|
+
let v = this.get(0, column);
|
|
1895
|
+
for (let i = 1; i < this.rows; i++) {
|
|
1896
1896
|
if (this.get(i, column) < v) {
|
|
1897
1897
|
v = this.get(i, column);
|
|
1898
1898
|
}
|
|
@@ -1902,9 +1902,9 @@ class AbstractMatrix {
|
|
|
1902
1902
|
|
|
1903
1903
|
minColumnIndex(column) {
|
|
1904
1904
|
checkColumnIndex(this, column);
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
for (
|
|
1905
|
+
let v = this.get(0, column);
|
|
1906
|
+
let idx = [0, column];
|
|
1907
|
+
for (let i = 1; i < this.rows; i++) {
|
|
1908
1908
|
if (this.get(i, column) < v) {
|
|
1909
1909
|
v = this.get(i, column);
|
|
1910
1910
|
idx[0] = i;
|
|
@@ -1914,21 +1914,21 @@ class AbstractMatrix {
|
|
|
1914
1914
|
}
|
|
1915
1915
|
|
|
1916
1916
|
diag() {
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
for (
|
|
1917
|
+
let min = Math.min(this.rows, this.columns);
|
|
1918
|
+
let diag = [];
|
|
1919
|
+
for (let i = 0; i < min; i++) {
|
|
1920
1920
|
diag.push(this.get(i, i));
|
|
1921
1921
|
}
|
|
1922
1922
|
return diag;
|
|
1923
1923
|
}
|
|
1924
1924
|
|
|
1925
1925
|
norm(type = 'frobenius') {
|
|
1926
|
-
|
|
1926
|
+
let result = 0;
|
|
1927
1927
|
if (type === 'max') {
|
|
1928
1928
|
return this.max();
|
|
1929
1929
|
} else if (type === 'frobenius') {
|
|
1930
|
-
for (
|
|
1931
|
-
for (
|
|
1930
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1931
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1932
1932
|
result = result + this.get(i, j) * this.get(i, j);
|
|
1933
1933
|
}
|
|
1934
1934
|
}
|
|
@@ -1939,9 +1939,9 @@ class AbstractMatrix {
|
|
|
1939
1939
|
}
|
|
1940
1940
|
|
|
1941
1941
|
cumulativeSum() {
|
|
1942
|
-
|
|
1943
|
-
for (
|
|
1944
|
-
for (
|
|
1942
|
+
let sum = 0;
|
|
1943
|
+
for (let i = 0; i < this.rows; i++) {
|
|
1944
|
+
for (let j = 0; j < this.columns; j++) {
|
|
1945
1945
|
sum += this.get(i, j);
|
|
1946
1946
|
this.set(i, j, sum);
|
|
1947
1947
|
}
|
|
@@ -1951,12 +1951,12 @@ class AbstractMatrix {
|
|
|
1951
1951
|
|
|
1952
1952
|
dot(vector2) {
|
|
1953
1953
|
if (AbstractMatrix.isMatrix(vector2)) vector2 = vector2.to1DArray();
|
|
1954
|
-
|
|
1954
|
+
let vector1 = this.to1DArray();
|
|
1955
1955
|
if (vector1.length !== vector2.length) {
|
|
1956
1956
|
throw new RangeError('vectors do not have the same size');
|
|
1957
1957
|
}
|
|
1958
|
-
|
|
1959
|
-
for (
|
|
1958
|
+
let dot = 0;
|
|
1959
|
+
for (let i = 0; i < vector1.length; i++) {
|
|
1960
1960
|
dot += vector1[i] * vector2[i];
|
|
1961
1961
|
}
|
|
1962
1962
|
return dot;
|
|
@@ -1965,21 +1965,21 @@ class AbstractMatrix {
|
|
|
1965
1965
|
mmul(other) {
|
|
1966
1966
|
other = Matrix.checkMatrix(other);
|
|
1967
1967
|
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1968
|
+
let m = this.rows;
|
|
1969
|
+
let n = this.columns;
|
|
1970
|
+
let p = other.columns;
|
|
1971
1971
|
|
|
1972
|
-
|
|
1972
|
+
let result = new Matrix(m, p);
|
|
1973
1973
|
|
|
1974
|
-
|
|
1975
|
-
for (
|
|
1976
|
-
for (
|
|
1974
|
+
let Bcolj = new Float64Array(n);
|
|
1975
|
+
for (let j = 0; j < p; j++) {
|
|
1976
|
+
for (let k = 0; k < n; k++) {
|
|
1977
1977
|
Bcolj[k] = other.get(k, j);
|
|
1978
1978
|
}
|
|
1979
1979
|
|
|
1980
|
-
for (
|
|
1981
|
-
|
|
1982
|
-
for (k = 0; k < n; k++) {
|
|
1980
|
+
for (let i = 0; i < m; i++) {
|
|
1981
|
+
let s = 0;
|
|
1982
|
+
for (let k = 0; k < n; k++) {
|
|
1983
1983
|
s += this.get(i, k) * Bcolj[k];
|
|
1984
1984
|
}
|
|
1985
1985
|
|
|
@@ -1991,7 +1991,7 @@ class AbstractMatrix {
|
|
|
1991
1991
|
|
|
1992
1992
|
strassen2x2(other) {
|
|
1993
1993
|
other = Matrix.checkMatrix(other);
|
|
1994
|
-
|
|
1994
|
+
let result = new Matrix(2, 2);
|
|
1995
1995
|
const a11 = this.get(0, 0);
|
|
1996
1996
|
const b11 = other.get(0, 0);
|
|
1997
1997
|
const a12 = this.get(0, 1);
|
|
@@ -2025,7 +2025,7 @@ class AbstractMatrix {
|
|
|
2025
2025
|
|
|
2026
2026
|
strassen3x3(other) {
|
|
2027
2027
|
other = Matrix.checkMatrix(other);
|
|
2028
|
-
|
|
2028
|
+
let result = new Matrix(3, 3);
|
|
2029
2029
|
|
|
2030
2030
|
const a00 = this.get(0, 0);
|
|
2031
2031
|
const a01 = this.get(0, 1);
|
|
@@ -2095,27 +2095,27 @@ class AbstractMatrix {
|
|
|
2095
2095
|
|
|
2096
2096
|
mmulStrassen(y) {
|
|
2097
2097
|
y = Matrix.checkMatrix(y);
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2098
|
+
let x = this.clone();
|
|
2099
|
+
let r1 = x.rows;
|
|
2100
|
+
let c1 = x.columns;
|
|
2101
|
+
let r2 = y.rows;
|
|
2102
|
+
let c2 = y.columns;
|
|
2103
2103
|
if (c1 !== r2) {
|
|
2104
2104
|
// eslint-disable-next-line no-console
|
|
2105
2105
|
console.warn(
|
|
2106
|
-
`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match
|
|
2106
|
+
`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`,
|
|
2107
2107
|
);
|
|
2108
2108
|
}
|
|
2109
2109
|
|
|
2110
2110
|
// Put a matrix into the top left of a matrix of zeros.
|
|
2111
2111
|
// `rows` and `cols` are the dimensions of the output matrix.
|
|
2112
2112
|
function embed(mat, rows, cols) {
|
|
2113
|
-
|
|
2114
|
-
|
|
2113
|
+
let r = mat.rows;
|
|
2114
|
+
let c = mat.columns;
|
|
2115
2115
|
if (r === rows && c === cols) {
|
|
2116
2116
|
return mat;
|
|
2117
2117
|
} else {
|
|
2118
|
-
|
|
2118
|
+
let resultat = AbstractMatrix.zeros(rows, cols);
|
|
2119
2119
|
resultat = resultat.setSubMatrix(mat, 0, 0);
|
|
2120
2120
|
return resultat;
|
|
2121
2121
|
}
|
|
@@ -2125,8 +2125,8 @@ class AbstractMatrix {
|
|
|
2125
2125
|
// This is exclusively for simplicity:
|
|
2126
2126
|
// this algorithm can be implemented with matrices of different sizes.
|
|
2127
2127
|
|
|
2128
|
-
|
|
2129
|
-
|
|
2128
|
+
let r = Math.max(r1, r2);
|
|
2129
|
+
let c = Math.max(c1, c2);
|
|
2130
2130
|
x = embed(x, r, c);
|
|
2131
2131
|
y = embed(y, r, c);
|
|
2132
2132
|
|
|
@@ -2149,57 +2149,57 @@ class AbstractMatrix {
|
|
|
2149
2149
|
b = embed(b, rows, cols + 1);
|
|
2150
2150
|
}
|
|
2151
2151
|
|
|
2152
|
-
|
|
2153
|
-
|
|
2152
|
+
let halfRows = parseInt(a.rows / 2, 10);
|
|
2153
|
+
let halfCols = parseInt(a.columns / 2, 10);
|
|
2154
2154
|
// Subdivide input matrices.
|
|
2155
|
-
|
|
2156
|
-
|
|
2155
|
+
let a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1);
|
|
2156
|
+
let b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1);
|
|
2157
2157
|
|
|
2158
|
-
|
|
2159
|
-
|
|
2158
|
+
let a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1);
|
|
2159
|
+
let b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1);
|
|
2160
2160
|
|
|
2161
|
-
|
|
2162
|
-
|
|
2161
|
+
let a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1);
|
|
2162
|
+
let b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1);
|
|
2163
2163
|
|
|
2164
|
-
|
|
2165
|
-
|
|
2164
|
+
let a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1);
|
|
2165
|
+
let b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1);
|
|
2166
2166
|
|
|
2167
2167
|
// Compute intermediate values.
|
|
2168
|
-
|
|
2168
|
+
let m1 = blockMult(
|
|
2169
2169
|
AbstractMatrix.add(a11, a22),
|
|
2170
2170
|
AbstractMatrix.add(b11, b22),
|
|
2171
2171
|
halfRows,
|
|
2172
|
-
halfCols
|
|
2172
|
+
halfCols,
|
|
2173
2173
|
);
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2174
|
+
let m2 = blockMult(AbstractMatrix.add(a21, a22), b11, halfRows, halfCols);
|
|
2175
|
+
let m3 = blockMult(a11, AbstractMatrix.sub(b12, b22), halfRows, halfCols);
|
|
2176
|
+
let m4 = blockMult(a22, AbstractMatrix.sub(b21, b11), halfRows, halfCols);
|
|
2177
|
+
let m5 = blockMult(AbstractMatrix.add(a11, a12), b22, halfRows, halfCols);
|
|
2178
|
+
let m6 = blockMult(
|
|
2179
2179
|
AbstractMatrix.sub(a21, a11),
|
|
2180
2180
|
AbstractMatrix.add(b11, b12),
|
|
2181
2181
|
halfRows,
|
|
2182
|
-
halfCols
|
|
2182
|
+
halfCols,
|
|
2183
2183
|
);
|
|
2184
|
-
|
|
2184
|
+
let m7 = blockMult(
|
|
2185
2185
|
AbstractMatrix.sub(a12, a22),
|
|
2186
2186
|
AbstractMatrix.add(b21, b22),
|
|
2187
2187
|
halfRows,
|
|
2188
|
-
halfCols
|
|
2188
|
+
halfCols,
|
|
2189
2189
|
);
|
|
2190
2190
|
|
|
2191
2191
|
// Combine intermediate values into the output.
|
|
2192
|
-
|
|
2192
|
+
let c11 = AbstractMatrix.add(m1, m4);
|
|
2193
2193
|
c11.sub(m5);
|
|
2194
2194
|
c11.add(m7);
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2195
|
+
let c12 = AbstractMatrix.add(m3, m5);
|
|
2196
|
+
let c21 = AbstractMatrix.add(m2, m4);
|
|
2197
|
+
let c22 = AbstractMatrix.sub(m1, m2);
|
|
2198
2198
|
c22.add(m3);
|
|
2199
2199
|
c22.add(m6);
|
|
2200
2200
|
|
|
2201
2201
|
// Crop output to the desired size (undo dynamic padding).
|
|
2202
|
-
|
|
2202
|
+
let resultat = AbstractMatrix.zeros(2 * c11.rows, 2 * c11.columns);
|
|
2203
2203
|
resultat = resultat.setSubMatrix(c11, 0, 0);
|
|
2204
2204
|
resultat = resultat.setSubMatrix(c12, c11.rows, 0);
|
|
2205
2205
|
resultat = resultat.setSubMatrix(c21, 0, c11.columns);
|
|
@@ -2217,8 +2217,8 @@ class AbstractMatrix {
|
|
|
2217
2217
|
if (!Number.isFinite(min)) throw new TypeError('min must be a number');
|
|
2218
2218
|
if (!Number.isFinite(max)) throw new TypeError('max must be a number');
|
|
2219
2219
|
if (min >= max) throw new RangeError('min must be smaller than max');
|
|
2220
|
-
|
|
2221
|
-
for (
|
|
2220
|
+
let newMatrix = new Matrix(this.rows, this.columns);
|
|
2221
|
+
for (let i = 0; i < this.rows; i++) {
|
|
2222
2222
|
const row = this.getRow(i);
|
|
2223
2223
|
rescale(row, { min, max, output: row });
|
|
2224
2224
|
newMatrix.setRow(i, row);
|
|
@@ -2234,13 +2234,13 @@ class AbstractMatrix {
|
|
|
2234
2234
|
if (!Number.isFinite(min)) throw new TypeError('min must be a number');
|
|
2235
2235
|
if (!Number.isFinite(max)) throw new TypeError('max must be a number');
|
|
2236
2236
|
if (min >= max) throw new RangeError('min must be smaller than max');
|
|
2237
|
-
|
|
2238
|
-
for (
|
|
2237
|
+
let newMatrix = new Matrix(this.rows, this.columns);
|
|
2238
|
+
for (let i = 0; i < this.columns; i++) {
|
|
2239
2239
|
const column = this.getColumn(i);
|
|
2240
2240
|
rescale(column, {
|
|
2241
2241
|
min: min,
|
|
2242
2242
|
max: max,
|
|
2243
|
-
output: column
|
|
2243
|
+
output: column,
|
|
2244
2244
|
});
|
|
2245
2245
|
newMatrix.setColumn(i, column);
|
|
2246
2246
|
}
|
|
@@ -2249,10 +2249,10 @@ class AbstractMatrix {
|
|
|
2249
2249
|
|
|
2250
2250
|
flipRows() {
|
|
2251
2251
|
const middle = Math.ceil(this.columns / 2);
|
|
2252
|
-
for (
|
|
2253
|
-
for (
|
|
2254
|
-
|
|
2255
|
-
|
|
2252
|
+
for (let i = 0; i < this.rows; i++) {
|
|
2253
|
+
for (let j = 0; j < middle; j++) {
|
|
2254
|
+
let first = this.get(i, j);
|
|
2255
|
+
let last = this.get(i, this.columns - 1 - j);
|
|
2256
2256
|
this.set(i, j, last);
|
|
2257
2257
|
this.set(i, this.columns - 1 - j, first);
|
|
2258
2258
|
}
|
|
@@ -2262,10 +2262,10 @@ class AbstractMatrix {
|
|
|
2262
2262
|
|
|
2263
2263
|
flipColumns() {
|
|
2264
2264
|
const middle = Math.ceil(this.rows / 2);
|
|
2265
|
-
for (
|
|
2266
|
-
for (
|
|
2267
|
-
|
|
2268
|
-
|
|
2265
|
+
for (let j = 0; j < this.columns; j++) {
|
|
2266
|
+
for (let i = 0; i < middle; i++) {
|
|
2267
|
+
let first = this.get(i, j);
|
|
2268
|
+
let last = this.get(this.rows - 1 - i, j);
|
|
2269
2269
|
this.set(i, j, last);
|
|
2270
2270
|
this.set(this.rows - 1 - i, j, first);
|
|
2271
2271
|
}
|
|
@@ -2276,16 +2276,16 @@ class AbstractMatrix {
|
|
|
2276
2276
|
kroneckerProduct(other) {
|
|
2277
2277
|
other = Matrix.checkMatrix(other);
|
|
2278
2278
|
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2279
|
+
let m = this.rows;
|
|
2280
|
+
let n = this.columns;
|
|
2281
|
+
let p = other.rows;
|
|
2282
|
+
let q = other.columns;
|
|
2283
2283
|
|
|
2284
|
-
|
|
2285
|
-
for (
|
|
2286
|
-
for (
|
|
2287
|
-
for (
|
|
2288
|
-
for (
|
|
2284
|
+
let result = new Matrix(m * p, n * q);
|
|
2285
|
+
for (let i = 0; i < m; i++) {
|
|
2286
|
+
for (let j = 0; j < n; j++) {
|
|
2287
|
+
for (let k = 0; k < p; k++) {
|
|
2288
|
+
for (let l = 0; l < q; l++) {
|
|
2289
2289
|
result.set(p * i + k, q * j + l, this.get(i, j) * other.get(k, l));
|
|
2290
2290
|
}
|
|
2291
2291
|
}
|
|
@@ -2295,9 +2295,9 @@ class AbstractMatrix {
|
|
|
2295
2295
|
}
|
|
2296
2296
|
|
|
2297
2297
|
transpose() {
|
|
2298
|
-
|
|
2299
|
-
for (
|
|
2300
|
-
for (
|
|
2298
|
+
let result = new Matrix(this.columns, this.rows);
|
|
2299
|
+
for (let i = 0; i < this.rows; i++) {
|
|
2300
|
+
for (let j = 0; j < this.columns; j++) {
|
|
2301
2301
|
result.set(j, i, this.get(i, j));
|
|
2302
2302
|
}
|
|
2303
2303
|
}
|
|
@@ -2305,14 +2305,14 @@ class AbstractMatrix {
|
|
|
2305
2305
|
}
|
|
2306
2306
|
|
|
2307
2307
|
sortRows(compareFunction = compareNumbers) {
|
|
2308
|
-
for (
|
|
2308
|
+
for (let i = 0; i < this.rows; i++) {
|
|
2309
2309
|
this.setRow(i, this.getRow(i).sort(compareFunction));
|
|
2310
2310
|
}
|
|
2311
2311
|
return this;
|
|
2312
2312
|
}
|
|
2313
2313
|
|
|
2314
2314
|
sortColumns(compareFunction = compareNumbers) {
|
|
2315
|
-
for (
|
|
2315
|
+
for (let i = 0; i < this.columns; i++) {
|
|
2316
2316
|
this.setColumn(i, this.getColumn(i).sort(compareFunction));
|
|
2317
2317
|
}
|
|
2318
2318
|
return this;
|
|
@@ -2320,12 +2320,12 @@ class AbstractMatrix {
|
|
|
2320
2320
|
|
|
2321
2321
|
subMatrix(startRow, endRow, startColumn, endColumn) {
|
|
2322
2322
|
checkRange(this, startRow, endRow, startColumn, endColumn);
|
|
2323
|
-
|
|
2323
|
+
let newMatrix = new Matrix(
|
|
2324
2324
|
endRow - startRow + 1,
|
|
2325
|
-
endColumn - startColumn + 1
|
|
2325
|
+
endColumn - startColumn + 1,
|
|
2326
2326
|
);
|
|
2327
|
-
for (
|
|
2328
|
-
for (
|
|
2327
|
+
for (let i = startRow; i <= endRow; i++) {
|
|
2328
|
+
for (let j = startColumn; j <= endColumn; j++) {
|
|
2329
2329
|
newMatrix.set(i - startRow, j - startColumn, this.get(i, j));
|
|
2330
2330
|
}
|
|
2331
2331
|
}
|
|
@@ -2345,9 +2345,9 @@ class AbstractMatrix {
|
|
|
2345
2345
|
throw new RangeError('Argument out of range');
|
|
2346
2346
|
}
|
|
2347
2347
|
|
|
2348
|
-
|
|
2349
|
-
for (
|
|
2350
|
-
for (
|
|
2348
|
+
let newMatrix = new Matrix(indices.length, endColumn - startColumn + 1);
|
|
2349
|
+
for (let i = 0; i < indices.length; i++) {
|
|
2350
|
+
for (let j = startColumn; j <= endColumn; j++) {
|
|
2351
2351
|
if (indices[i] < 0 || indices[i] >= this.rows) {
|
|
2352
2352
|
throw new RangeError(`Row index out of range: ${indices[i]}`);
|
|
2353
2353
|
}
|
|
@@ -2370,9 +2370,9 @@ class AbstractMatrix {
|
|
|
2370
2370
|
throw new RangeError('Argument out of range');
|
|
2371
2371
|
}
|
|
2372
2372
|
|
|
2373
|
-
|
|
2374
|
-
for (
|
|
2375
|
-
for (
|
|
2373
|
+
let newMatrix = new Matrix(endRow - startRow + 1, indices.length);
|
|
2374
|
+
for (let i = 0; i < indices.length; i++) {
|
|
2375
|
+
for (let j = startRow; j <= endRow; j++) {
|
|
2376
2376
|
if (indices[i] < 0 || indices[i] >= this.columns) {
|
|
2377
2377
|
throw new RangeError(`Column index out of range: ${indices[i]}`);
|
|
2378
2378
|
}
|
|
@@ -2384,11 +2384,11 @@ class AbstractMatrix {
|
|
|
2384
2384
|
|
|
2385
2385
|
setSubMatrix(matrix, startRow, startColumn) {
|
|
2386
2386
|
matrix = Matrix.checkMatrix(matrix);
|
|
2387
|
-
|
|
2388
|
-
|
|
2387
|
+
let endRow = startRow + matrix.rows - 1;
|
|
2388
|
+
let endColumn = startColumn + matrix.columns - 1;
|
|
2389
2389
|
checkRange(this, startRow, endRow, startColumn, endColumn);
|
|
2390
|
-
for (
|
|
2391
|
-
for (
|
|
2390
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
2391
|
+
for (let j = 0; j < matrix.columns; j++) {
|
|
2392
2392
|
this.set(startRow + i, startColumn + j, matrix.get(i, j));
|
|
2393
2393
|
}
|
|
2394
2394
|
}
|
|
@@ -2396,12 +2396,12 @@ class AbstractMatrix {
|
|
|
2396
2396
|
}
|
|
2397
2397
|
|
|
2398
2398
|
selection(rowIndices, columnIndices) {
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
for (
|
|
2402
|
-
|
|
2403
|
-
for (
|
|
2404
|
-
|
|
2399
|
+
let indices = checkIndices(this, rowIndices, columnIndices);
|
|
2400
|
+
let newMatrix = new Matrix(rowIndices.length, columnIndices.length);
|
|
2401
|
+
for (let i = 0; i < indices.row.length; i++) {
|
|
2402
|
+
let rowIndex = indices.row[i];
|
|
2403
|
+
for (let j = 0; j < indices.column.length; j++) {
|
|
2404
|
+
let columnIndex = indices.column[j];
|
|
2405
2405
|
newMatrix.set(i, j, this.get(rowIndex, columnIndex));
|
|
2406
2406
|
}
|
|
2407
2407
|
}
|
|
@@ -2409,18 +2409,18 @@ class AbstractMatrix {
|
|
|
2409
2409
|
}
|
|
2410
2410
|
|
|
2411
2411
|
trace() {
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
for (
|
|
2412
|
+
let min = Math.min(this.rows, this.columns);
|
|
2413
|
+
let trace = 0;
|
|
2414
|
+
for (let i = 0; i < min; i++) {
|
|
2415
2415
|
trace += this.get(i, i);
|
|
2416
2416
|
}
|
|
2417
2417
|
return trace;
|
|
2418
2418
|
}
|
|
2419
2419
|
|
|
2420
2420
|
clone() {
|
|
2421
|
-
|
|
2422
|
-
for (
|
|
2423
|
-
for (
|
|
2421
|
+
let newMatrix = new Matrix(this.rows, this.columns);
|
|
2422
|
+
for (let row = 0; row < this.rows; row++) {
|
|
2423
|
+
for (let column = 0; column < this.columns; column++) {
|
|
2424
2424
|
newMatrix.set(row, column, this.get(row, column));
|
|
2425
2425
|
}
|
|
2426
2426
|
}
|
|
@@ -2520,7 +2520,7 @@ class AbstractMatrix {
|
|
|
2520
2520
|
if (by === undefined) {
|
|
2521
2521
|
return Math.sqrt(variance);
|
|
2522
2522
|
} else {
|
|
2523
|
-
for (
|
|
2523
|
+
for (let i = 0; i < variance.length; i++) {
|
|
2524
2524
|
variance[i] = Math.sqrt(variance[i]);
|
|
2525
2525
|
}
|
|
2526
2526
|
return variance;
|
|
@@ -2550,7 +2550,8 @@ class AbstractMatrix {
|
|
|
2550
2550
|
}
|
|
2551
2551
|
centerByColumn(this, center);
|
|
2552
2552
|
return this;
|
|
2553
|
-
}
|
|
2553
|
+
}
|
|
2554
|
+
case undefined: {
|
|
2554
2555
|
if (typeof center !== 'number') {
|
|
2555
2556
|
throw new TypeError('center must be a number');
|
|
2556
2557
|
}
|
|
@@ -2648,7 +2649,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2648
2649
|
nColumns = arrayData[0].length;
|
|
2649
2650
|
if (typeof nColumns !== 'number' || nColumns === 0) {
|
|
2650
2651
|
throw new TypeError(
|
|
2651
|
-
'Data must be a 2D array with at least one element'
|
|
2652
|
+
'Data must be a 2D array with at least one element',
|
|
2652
2653
|
);
|
|
2653
2654
|
}
|
|
2654
2655
|
this.data = [];
|
|
@@ -2660,7 +2661,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2660
2661
|
}
|
|
2661
2662
|
} else {
|
|
2662
2663
|
throw new TypeError(
|
|
2663
|
-
'First argument must be a positive number or an array'
|
|
2664
|
+
'First argument must be a positive number or an array',
|
|
2664
2665
|
);
|
|
2665
2666
|
}
|
|
2666
2667
|
this.rows = nRows;
|
|
@@ -2704,7 +2705,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2704
2705
|
if (this.columns === 1) {
|
|
2705
2706
|
throw new RangeError('A matrix cannot have less than one column');
|
|
2706
2707
|
}
|
|
2707
|
-
for (
|
|
2708
|
+
for (let i = 0; i < this.rows; i++) {
|
|
2708
2709
|
const newRow = new Float64Array(this.columns - 1);
|
|
2709
2710
|
for (let j = 0; j < index; j++) {
|
|
2710
2711
|
newRow[j] = this.data[i][j];
|
|
@@ -2725,7 +2726,7 @@ class Matrix extends AbstractMatrix {
|
|
|
2725
2726
|
}
|
|
2726
2727
|
checkColumnIndex(this, index, true);
|
|
2727
2728
|
array = checkColumnVector(this, array);
|
|
2728
|
-
for (
|
|
2729
|
+
for (let i = 0; i < this.rows; i++) {
|
|
2729
2730
|
const newRow = new Float64Array(this.columns + 1);
|
|
2730
2731
|
let j = 0;
|
|
2731
2732
|
for (; j < index; j++) {
|
|
@@ -2851,54 +2852,54 @@ class MatrixRowSelectionView extends BaseView {
|
|
|
2851
2852
|
}
|
|
2852
2853
|
}
|
|
2853
2854
|
|
|
2854
|
-
class MatrixSelectionView extends BaseView {
|
|
2855
|
-
constructor(matrix, rowIndices, columnIndices) {
|
|
2856
|
-
|
|
2857
|
-
super(matrix, indices.row.length, indices.column.length);
|
|
2858
|
-
this.rowIndices = indices.row;
|
|
2859
|
-
this.columnIndices = indices.column;
|
|
2860
|
-
}
|
|
2861
|
-
|
|
2862
|
-
set(rowIndex, columnIndex, value) {
|
|
2863
|
-
this.matrix.set(
|
|
2864
|
-
this.rowIndices[rowIndex],
|
|
2865
|
-
this.columnIndices[columnIndex],
|
|
2866
|
-
value
|
|
2867
|
-
);
|
|
2868
|
-
return this;
|
|
2869
|
-
}
|
|
2870
|
-
|
|
2871
|
-
get(rowIndex, columnIndex) {
|
|
2872
|
-
return this.matrix.get(
|
|
2873
|
-
this.rowIndices[rowIndex],
|
|
2874
|
-
this.columnIndices[columnIndex]
|
|
2875
|
-
);
|
|
2876
|
-
}
|
|
2855
|
+
class MatrixSelectionView extends BaseView {
|
|
2856
|
+
constructor(matrix, rowIndices, columnIndices) {
|
|
2857
|
+
let indices = checkIndices(matrix, rowIndices, columnIndices);
|
|
2858
|
+
super(matrix, indices.row.length, indices.column.length);
|
|
2859
|
+
this.rowIndices = indices.row;
|
|
2860
|
+
this.columnIndices = indices.column;
|
|
2861
|
+
}
|
|
2862
|
+
|
|
2863
|
+
set(rowIndex, columnIndex, value) {
|
|
2864
|
+
this.matrix.set(
|
|
2865
|
+
this.rowIndices[rowIndex],
|
|
2866
|
+
this.columnIndices[columnIndex],
|
|
2867
|
+
value,
|
|
2868
|
+
);
|
|
2869
|
+
return this;
|
|
2870
|
+
}
|
|
2871
|
+
|
|
2872
|
+
get(rowIndex, columnIndex) {
|
|
2873
|
+
return this.matrix.get(
|
|
2874
|
+
this.rowIndices[rowIndex],
|
|
2875
|
+
this.columnIndices[columnIndex],
|
|
2876
|
+
);
|
|
2877
|
+
}
|
|
2877
2878
|
}
|
|
2878
2879
|
|
|
2879
|
-
class MatrixSubView extends BaseView {
|
|
2880
|
-
constructor(matrix, startRow, endRow, startColumn, endColumn) {
|
|
2881
|
-
checkRange(matrix, startRow, endRow, startColumn, endColumn);
|
|
2882
|
-
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);
|
|
2883
|
-
this.startRow = startRow;
|
|
2884
|
-
this.startColumn = startColumn;
|
|
2885
|
-
}
|
|
2886
|
-
|
|
2887
|
-
set(rowIndex, columnIndex, value) {
|
|
2888
|
-
this.matrix.set(
|
|
2889
|
-
this.startRow + rowIndex,
|
|
2890
|
-
this.startColumn + columnIndex,
|
|
2891
|
-
value
|
|
2892
|
-
);
|
|
2893
|
-
return this;
|
|
2894
|
-
}
|
|
2895
|
-
|
|
2896
|
-
get(rowIndex, columnIndex) {
|
|
2897
|
-
return this.matrix.get(
|
|
2898
|
-
this.startRow + rowIndex,
|
|
2899
|
-
this.startColumn + columnIndex
|
|
2900
|
-
);
|
|
2901
|
-
}
|
|
2880
|
+
class MatrixSubView extends BaseView {
|
|
2881
|
+
constructor(matrix, startRow, endRow, startColumn, endColumn) {
|
|
2882
|
+
checkRange(matrix, startRow, endRow, startColumn, endColumn);
|
|
2883
|
+
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);
|
|
2884
|
+
this.startRow = startRow;
|
|
2885
|
+
this.startColumn = startColumn;
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
set(rowIndex, columnIndex, value) {
|
|
2889
|
+
this.matrix.set(
|
|
2890
|
+
this.startRow + rowIndex,
|
|
2891
|
+
this.startColumn + columnIndex,
|
|
2892
|
+
value,
|
|
2893
|
+
);
|
|
2894
|
+
return this;
|
|
2895
|
+
}
|
|
2896
|
+
|
|
2897
|
+
get(rowIndex, columnIndex) {
|
|
2898
|
+
return this.matrix.get(
|
|
2899
|
+
this.startRow + rowIndex,
|
|
2900
|
+
this.startColumn + columnIndex,
|
|
2901
|
+
);
|
|
2902
|
+
}
|
|
2902
2903
|
}
|
|
2903
2904
|
|
|
2904
2905
|
class MatrixTransposeView extends BaseView {
|
|
@@ -2930,13 +2931,13 @@ class WrapperMatrix1D extends AbstractMatrix {
|
|
|
2930
2931
|
}
|
|
2931
2932
|
|
|
2932
2933
|
set(rowIndex, columnIndex, value) {
|
|
2933
|
-
|
|
2934
|
+
let index = this._calculateIndex(rowIndex, columnIndex);
|
|
2934
2935
|
this.data[index] = value;
|
|
2935
2936
|
return this;
|
|
2936
2937
|
}
|
|
2937
2938
|
|
|
2938
2939
|
get(rowIndex, columnIndex) {
|
|
2939
|
-
|
|
2940
|
+
let index = this._calculateIndex(rowIndex, columnIndex);
|
|
2940
2941
|
return this.data[index];
|
|
2941
2942
|
}
|
|
2942
2943
|
|
|
@@ -2979,13 +2980,13 @@ class LuDecomposition {
|
|
|
2979
2980
|
constructor(matrix) {
|
|
2980
2981
|
matrix = WrapperMatrix2D.checkMatrix(matrix);
|
|
2981
2982
|
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2983
|
+
let lu = matrix.clone();
|
|
2984
|
+
let rows = lu.rows;
|
|
2985
|
+
let columns = lu.columns;
|
|
2986
|
+
let pivotVector = new Float64Array(rows);
|
|
2987
|
+
let pivotSign = 1;
|
|
2988
|
+
let i, j, k, p, s, t, v;
|
|
2989
|
+
let LUcolj, kmax;
|
|
2989
2990
|
|
|
2990
2991
|
for (i = 0; i < rows; i++) {
|
|
2991
2992
|
pivotVector[i] = i;
|
|
@@ -3042,9 +3043,9 @@ class LuDecomposition {
|
|
|
3042
3043
|
}
|
|
3043
3044
|
|
|
3044
3045
|
isSingular() {
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
for (
|
|
3046
|
+
let data = this.LU;
|
|
3047
|
+
let col = data.columns;
|
|
3048
|
+
for (let j = 0; j < col; j++) {
|
|
3048
3049
|
if (data.get(j, j) === 0) {
|
|
3049
3050
|
return true;
|
|
3050
3051
|
}
|
|
@@ -3055,8 +3056,8 @@ class LuDecomposition {
|
|
|
3055
3056
|
solve(value) {
|
|
3056
3057
|
value = Matrix.checkMatrix(value);
|
|
3057
3058
|
|
|
3058
|
-
|
|
3059
|
-
|
|
3059
|
+
let lu = this.LU;
|
|
3060
|
+
let rows = lu.rows;
|
|
3060
3061
|
|
|
3061
3062
|
if (rows !== value.rows) {
|
|
3062
3063
|
throw new Error('Invalid matrix dimensions');
|
|
@@ -3065,10 +3066,10 @@ class LuDecomposition {
|
|
|
3065
3066
|
throw new Error('LU matrix is singular');
|
|
3066
3067
|
}
|
|
3067
3068
|
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3069
|
+
let count = value.columns;
|
|
3070
|
+
let X = value.subMatrixRow(this.pivotVector, 0, count - 1);
|
|
3071
|
+
let columns = lu.columns;
|
|
3072
|
+
let i, j, k;
|
|
3072
3073
|
|
|
3073
3074
|
for (k = 0; k < columns; k++) {
|
|
3074
3075
|
for (i = k + 1; i < columns; i++) {
|
|
@@ -3091,25 +3092,25 @@ class LuDecomposition {
|
|
|
3091
3092
|
}
|
|
3092
3093
|
|
|
3093
3094
|
get determinant() {
|
|
3094
|
-
|
|
3095
|
+
let data = this.LU;
|
|
3095
3096
|
if (!data.isSquare()) {
|
|
3096
3097
|
throw new Error('Matrix must be square');
|
|
3097
3098
|
}
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
for (
|
|
3099
|
+
let determinant = this.pivotSign;
|
|
3100
|
+
let col = data.columns;
|
|
3101
|
+
for (let j = 0; j < col; j++) {
|
|
3101
3102
|
determinant *= data.get(j, j);
|
|
3102
3103
|
}
|
|
3103
3104
|
return determinant;
|
|
3104
3105
|
}
|
|
3105
3106
|
|
|
3106
3107
|
get lowerTriangularMatrix() {
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
for (
|
|
3112
|
-
for (
|
|
3108
|
+
let data = this.LU;
|
|
3109
|
+
let rows = data.rows;
|
|
3110
|
+
let columns = data.columns;
|
|
3111
|
+
let X = new Matrix(rows, columns);
|
|
3112
|
+
for (let i = 0; i < rows; i++) {
|
|
3113
|
+
for (let j = 0; j < columns; j++) {
|
|
3113
3114
|
if (i > j) {
|
|
3114
3115
|
X.set(i, j, data.get(i, j));
|
|
3115
3116
|
} else if (i === j) {
|
|
@@ -3123,12 +3124,12 @@ class LuDecomposition {
|
|
|
3123
3124
|
}
|
|
3124
3125
|
|
|
3125
3126
|
get upperTriangularMatrix() {
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
for (
|
|
3131
|
-
for (
|
|
3127
|
+
let data = this.LU;
|
|
3128
|
+
let rows = data.rows;
|
|
3129
|
+
let columns = data.columns;
|
|
3130
|
+
let X = new Matrix(rows, columns);
|
|
3131
|
+
for (let i = 0; i < rows; i++) {
|
|
3132
|
+
for (let j = 0; j < columns; j++) {
|
|
3132
3133
|
if (i <= j) {
|
|
3133
3134
|
X.set(i, j, data.get(i, j));
|
|
3134
3135
|
} else {
|
|
@@ -3145,7 +3146,7 @@ class LuDecomposition {
|
|
|
3145
3146
|
}
|
|
3146
3147
|
|
|
3147
3148
|
function hypotenuse(a, b) {
|
|
3148
|
-
|
|
3149
|
+
let r = 0;
|
|
3149
3150
|
if (Math.abs(a) > Math.abs(b)) {
|
|
3150
3151
|
r = b / a;
|
|
3151
3152
|
return Math.abs(a) * Math.sqrt(1 + r * r);
|
|
@@ -3161,14 +3162,14 @@ class QrDecomposition {
|
|
|
3161
3162
|
constructor(value) {
|
|
3162
3163
|
value = WrapperMatrix2D.checkMatrix(value);
|
|
3163
3164
|
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3165
|
+
let qr = value.clone();
|
|
3166
|
+
let m = value.rows;
|
|
3167
|
+
let n = value.columns;
|
|
3168
|
+
let rdiag = new Float64Array(n);
|
|
3169
|
+
let i, j, k, s;
|
|
3169
3170
|
|
|
3170
3171
|
for (k = 0; k < n; k++) {
|
|
3171
|
-
|
|
3172
|
+
let nrm = 0;
|
|
3172
3173
|
for (i = k; i < m; i++) {
|
|
3173
3174
|
nrm = hypotenuse(nrm, qr.get(i, k));
|
|
3174
3175
|
}
|
|
@@ -3201,8 +3202,8 @@ class QrDecomposition {
|
|
|
3201
3202
|
solve(value) {
|
|
3202
3203
|
value = Matrix.checkMatrix(value);
|
|
3203
3204
|
|
|
3204
|
-
|
|
3205
|
-
|
|
3205
|
+
let qr = this.QR;
|
|
3206
|
+
let m = qr.rows;
|
|
3206
3207
|
|
|
3207
3208
|
if (value.rows !== m) {
|
|
3208
3209
|
throw new Error('Matrix row dimensions must agree');
|
|
@@ -3211,10 +3212,10 @@ class QrDecomposition {
|
|
|
3211
3212
|
throw new Error('Matrix is rank deficient');
|
|
3212
3213
|
}
|
|
3213
3214
|
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3215
|
+
let count = value.columns;
|
|
3216
|
+
let X = value.clone();
|
|
3217
|
+
let n = qr.columns;
|
|
3218
|
+
let i, j, k, s;
|
|
3218
3219
|
|
|
3219
3220
|
for (k = 0; k < n; k++) {
|
|
3220
3221
|
for (j = 0; j < count; j++) {
|
|
@@ -3243,8 +3244,8 @@ class QrDecomposition {
|
|
|
3243
3244
|
}
|
|
3244
3245
|
|
|
3245
3246
|
isFullRank() {
|
|
3246
|
-
|
|
3247
|
-
for (
|
|
3247
|
+
let columns = this.QR.columns;
|
|
3248
|
+
for (let i = 0; i < columns; i++) {
|
|
3248
3249
|
if (this.Rdiag[i] === 0) {
|
|
3249
3250
|
return false;
|
|
3250
3251
|
}
|
|
@@ -3253,10 +3254,10 @@ class QrDecomposition {
|
|
|
3253
3254
|
}
|
|
3254
3255
|
|
|
3255
3256
|
get upperTriangularMatrix() {
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3257
|
+
let qr = this.QR;
|
|
3258
|
+
let n = qr.columns;
|
|
3259
|
+
let X = new Matrix(n, n);
|
|
3260
|
+
let i, j;
|
|
3260
3261
|
for (i = 0; i < n; i++) {
|
|
3261
3262
|
for (j = 0; j < n; j++) {
|
|
3262
3263
|
if (i < j) {
|
|
@@ -3272,11 +3273,11 @@ class QrDecomposition {
|
|
|
3272
3273
|
}
|
|
3273
3274
|
|
|
3274
3275
|
get orthogonalMatrix() {
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3276
|
+
let qr = this.QR;
|
|
3277
|
+
let rows = qr.rows;
|
|
3278
|
+
let columns = qr.columns;
|
|
3279
|
+
let X = new Matrix(rows, columns);
|
|
3280
|
+
let i, j, k, s;
|
|
3280
3281
|
|
|
3281
3282
|
for (k = columns - 1; k >= 0; k--) {
|
|
3282
3283
|
for (i = 0; i < rows; i++) {
|
|
@@ -3290,7 +3291,7 @@ class QrDecomposition {
|
|
|
3290
3291
|
s += qr.get(i, k) * X.get(i, j);
|
|
3291
3292
|
}
|
|
3292
3293
|
|
|
3293
|
-
s = -s / qr
|
|
3294
|
+
s = -s / qr.get(k, k);
|
|
3294
3295
|
|
|
3295
3296
|
for (i = k; i < rows; i++) {
|
|
3296
3297
|
X.set(i, j, X.get(i, j) + s * qr.get(i, k));
|
|
@@ -3306,33 +3307,33 @@ class SingularValueDecomposition {
|
|
|
3306
3307
|
constructor(value, options = {}) {
|
|
3307
3308
|
value = WrapperMatrix2D.checkMatrix(value);
|
|
3308
3309
|
|
|
3309
|
-
|
|
3310
|
-
|
|
3310
|
+
let m = value.rows;
|
|
3311
|
+
let n = value.columns;
|
|
3311
3312
|
|
|
3312
3313
|
const {
|
|
3313
3314
|
computeLeftSingularVectors = true,
|
|
3314
3315
|
computeRightSingularVectors = true,
|
|
3315
|
-
autoTranspose = false
|
|
3316
|
+
autoTranspose = false,
|
|
3316
3317
|
} = options;
|
|
3317
3318
|
|
|
3318
|
-
|
|
3319
|
-
|
|
3319
|
+
let wantu = Boolean(computeLeftSingularVectors);
|
|
3320
|
+
let wantv = Boolean(computeRightSingularVectors);
|
|
3320
3321
|
|
|
3321
|
-
|
|
3322
|
-
|
|
3322
|
+
let swapped = false;
|
|
3323
|
+
let a;
|
|
3323
3324
|
if (m < n) {
|
|
3324
3325
|
if (!autoTranspose) {
|
|
3325
3326
|
a = value.clone();
|
|
3326
3327
|
// eslint-disable-next-line no-console
|
|
3327
3328
|
console.warn(
|
|
3328
|
-
'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose'
|
|
3329
|
+
'Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose',
|
|
3329
3330
|
);
|
|
3330
3331
|
} else {
|
|
3331
3332
|
a = value.transpose();
|
|
3332
3333
|
m = a.rows;
|
|
3333
3334
|
n = a.columns;
|
|
3334
3335
|
swapped = true;
|
|
3335
|
-
|
|
3336
|
+
let aux = wantu;
|
|
3336
3337
|
wantu = wantv;
|
|
3337
3338
|
wantv = aux;
|
|
3338
3339
|
}
|
|
@@ -3340,21 +3341,21 @@ class SingularValueDecomposition {
|
|
|
3340
3341
|
a = value.clone();
|
|
3341
3342
|
}
|
|
3342
3343
|
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3344
|
+
let nu = Math.min(m, n);
|
|
3345
|
+
let ni = Math.min(m + 1, n);
|
|
3346
|
+
let s = new Float64Array(ni);
|
|
3347
|
+
let U = new Matrix(m, nu);
|
|
3348
|
+
let V = new Matrix(n, n);
|
|
3348
3349
|
|
|
3349
|
-
|
|
3350
|
-
|
|
3350
|
+
let e = new Float64Array(n);
|
|
3351
|
+
let work = new Float64Array(m);
|
|
3351
3352
|
|
|
3352
|
-
|
|
3353
|
+
let si = new Float64Array(ni);
|
|
3353
3354
|
for (let i = 0; i < ni; i++) si[i] = i;
|
|
3354
3355
|
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3356
|
+
let nct = Math.min(m - 1, n);
|
|
3357
|
+
let nrt = Math.max(0, Math.min(n - 2, m));
|
|
3358
|
+
let mrc = Math.max(nct, nrt);
|
|
3358
3359
|
|
|
3359
3360
|
for (let k = 0; k < mrc; k++) {
|
|
3360
3361
|
if (k < nct) {
|
|
@@ -3501,8 +3502,8 @@ class SingularValueDecomposition {
|
|
|
3501
3502
|
}
|
|
3502
3503
|
}
|
|
3503
3504
|
|
|
3504
|
-
|
|
3505
|
-
|
|
3505
|
+
let pp = p - 1;
|
|
3506
|
+
let eps = Number.EPSILON;
|
|
3506
3507
|
while (p > 0) {
|
|
3507
3508
|
let k, kase;
|
|
3508
3509
|
for (k = p - 2; k >= -1; k--) {
|
|
@@ -3593,7 +3594,7 @@ class SingularValueDecomposition {
|
|
|
3593
3594
|
Math.abs(s[p - 2]),
|
|
3594
3595
|
Math.abs(e[p - 2]),
|
|
3595
3596
|
Math.abs(s[k]),
|
|
3596
|
-
Math.abs(e[k])
|
|
3597
|
+
Math.abs(e[k]),
|
|
3597
3598
|
);
|
|
3598
3599
|
const sp = s[p - 1] / scale;
|
|
3599
3600
|
const spm1 = s[p - 2] / scale;
|
|
@@ -3692,7 +3693,7 @@ class SingularValueDecomposition {
|
|
|
3692
3693
|
}
|
|
3693
3694
|
|
|
3694
3695
|
if (swapped) {
|
|
3695
|
-
|
|
3696
|
+
let tmp = V;
|
|
3696
3697
|
V = U;
|
|
3697
3698
|
U = tmp;
|
|
3698
3699
|
}
|
|
@@ -3705,10 +3706,10 @@ class SingularValueDecomposition {
|
|
|
3705
3706
|
}
|
|
3706
3707
|
|
|
3707
3708
|
solve(value) {
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3709
|
+
let Y = value;
|
|
3710
|
+
let e = this.threshold;
|
|
3711
|
+
let scols = this.s.length;
|
|
3712
|
+
let Ls = Matrix.zeros(scols, scols);
|
|
3712
3713
|
|
|
3713
3714
|
for (let i = 0; i < scols; i++) {
|
|
3714
3715
|
if (Math.abs(this.s[i]) <= e) {
|
|
@@ -3718,13 +3719,13 @@ class SingularValueDecomposition {
|
|
|
3718
3719
|
}
|
|
3719
3720
|
}
|
|
3720
3721
|
|
|
3721
|
-
|
|
3722
|
-
|
|
3722
|
+
let U = this.U;
|
|
3723
|
+
let V = this.rightSingularVectors;
|
|
3723
3724
|
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3725
|
+
let VL = V.mmul(Ls);
|
|
3726
|
+
let vrows = V.rows;
|
|
3727
|
+
let urows = U.rows;
|
|
3728
|
+
let VLU = Matrix.zeros(vrows, urows);
|
|
3728
3729
|
|
|
3729
3730
|
for (let i = 0; i < vrows; i++) {
|
|
3730
3731
|
for (let j = 0; j < urows; j++) {
|
|
@@ -3744,11 +3745,11 @@ class SingularValueDecomposition {
|
|
|
3744
3745
|
}
|
|
3745
3746
|
|
|
3746
3747
|
inverse() {
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3748
|
+
let V = this.V;
|
|
3749
|
+
let e = this.threshold;
|
|
3750
|
+
let vrows = V.rows;
|
|
3751
|
+
let vcols = V.columns;
|
|
3752
|
+
let X = new Matrix(vrows, this.s.length);
|
|
3752
3753
|
|
|
3753
3754
|
for (let i = 0; i < vrows; i++) {
|
|
3754
3755
|
for (let j = 0; j < vcols; j++) {
|
|
@@ -3758,11 +3759,11 @@ class SingularValueDecomposition {
|
|
|
3758
3759
|
}
|
|
3759
3760
|
}
|
|
3760
3761
|
|
|
3761
|
-
|
|
3762
|
+
let U = this.U;
|
|
3762
3763
|
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3764
|
+
let urows = U.rows;
|
|
3765
|
+
let ucols = U.columns;
|
|
3766
|
+
let Y = new Matrix(vrows, urows);
|
|
3766
3767
|
|
|
3767
3768
|
for (let i = 0; i < vrows; i++) {
|
|
3768
3769
|
for (let j = 0; j < urows; j++) {
|
|
@@ -3786,10 +3787,10 @@ class SingularValueDecomposition {
|
|
|
3786
3787
|
}
|
|
3787
3788
|
|
|
3788
3789
|
get rank() {
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
for (
|
|
3790
|
+
let tol = Math.max(this.m, this.n) * this.s[0] * Number.EPSILON;
|
|
3791
|
+
let r = 0;
|
|
3792
|
+
let s = this.s;
|
|
3793
|
+
for (let i = 0, ii = s.length; i < ii; i++) {
|
|
3793
3794
|
if (s[i] > tol) {
|
|
3794
3795
|
r++;
|
|
3795
3796
|
}
|
|
@@ -3842,7 +3843,7 @@ function solve(leftHandSide, rightHandSide, useSVD = false) {
|
|
|
3842
3843
|
function determinant(matrix) {
|
|
3843
3844
|
matrix = Matrix.checkMatrix(matrix);
|
|
3844
3845
|
if (matrix.isSquare()) {
|
|
3845
|
-
|
|
3846
|
+
let a, b, c, d;
|
|
3846
3847
|
if (matrix.columns === 2) {
|
|
3847
3848
|
// 2 x 2 matrix
|
|
3848
3849
|
a = matrix.get(0, 0);
|
|
@@ -3853,7 +3854,7 @@ function determinant(matrix) {
|
|
|
3853
3854
|
return a * d - b * c;
|
|
3854
3855
|
} else if (matrix.columns === 3) {
|
|
3855
3856
|
// 3 x 3 matrix
|
|
3856
|
-
|
|
3857
|
+
let subMatrix0, subMatrix1, subMatrix2;
|
|
3857
3858
|
subMatrix0 = new MatrixSelectionView(matrix, [1, 2], [1, 2]);
|
|
3858
3859
|
subMatrix1 = new MatrixSelectionView(matrix, [1, 2], [0, 2]);
|
|
3859
3860
|
subMatrix2 = new MatrixSelectionView(matrix, [1, 2], [0, 1]);
|
|
@@ -3876,8 +3877,8 @@ function determinant(matrix) {
|
|
|
3876
3877
|
}
|
|
3877
3878
|
|
|
3878
3879
|
function xrange(n, exception) {
|
|
3879
|
-
|
|
3880
|
-
for (
|
|
3880
|
+
let range = [];
|
|
3881
|
+
for (let i = 0; i < n; i++) {
|
|
3881
3882
|
if (i !== exception) {
|
|
3882
3883
|
range.push(i);
|
|
3883
3884
|
}
|
|
@@ -3890,13 +3891,13 @@ function dependenciesOneRow(
|
|
|
3890
3891
|
matrix,
|
|
3891
3892
|
index,
|
|
3892
3893
|
thresholdValue = 10e-10,
|
|
3893
|
-
thresholdError = 10e-10
|
|
3894
|
+
thresholdError = 10e-10,
|
|
3894
3895
|
) {
|
|
3895
3896
|
if (error > thresholdError) {
|
|
3896
3897
|
return new Array(matrix.rows + 1).fill(0);
|
|
3897
3898
|
} else {
|
|
3898
|
-
|
|
3899
|
-
for (
|
|
3899
|
+
let returnArray = matrix.addRow(index, [0]);
|
|
3900
|
+
for (let i = 0; i < returnArray.rows; i++) {
|
|
3900
3901
|
if (Math.abs(returnArray.get(i, 0)) < thresholdValue) {
|
|
3901
3902
|
returnArray.set(i, 0, 0);
|
|
3902
3903
|
}
|
|
@@ -3909,20 +3910,20 @@ function linearDependencies(matrix, options = {}) {
|
|
|
3909
3910
|
const { thresholdValue = 10e-10, thresholdError = 10e-10 } = options;
|
|
3910
3911
|
matrix = Matrix.checkMatrix(matrix);
|
|
3911
3912
|
|
|
3912
|
-
|
|
3913
|
-
|
|
3913
|
+
let n = matrix.rows;
|
|
3914
|
+
let results = new Matrix(n, n);
|
|
3914
3915
|
|
|
3915
|
-
for (
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3916
|
+
for (let i = 0; i < n; i++) {
|
|
3917
|
+
let b = Matrix.columnVector(matrix.getRow(i));
|
|
3918
|
+
let Abis = matrix.subMatrixRow(xrange(n, i)).transpose();
|
|
3919
|
+
let svd = new SingularValueDecomposition(Abis);
|
|
3920
|
+
let x = svd.solve(b);
|
|
3921
|
+
let error = Matrix.sub(b, Abis.mmul(x))
|
|
3921
3922
|
.abs()
|
|
3922
3923
|
.max();
|
|
3923
3924
|
results.setRow(
|
|
3924
3925
|
i,
|
|
3925
|
-
dependenciesOneRow(error, x, i, thresholdValue, thresholdError)
|
|
3926
|
+
dependenciesOneRow(error, x, i, thresholdValue, thresholdError),
|
|
3926
3927
|
);
|
|
3927
3928
|
}
|
|
3928
3929
|
return results;
|
|
@@ -3930,13 +3931,13 @@ function linearDependencies(matrix, options = {}) {
|
|
|
3930
3931
|
|
|
3931
3932
|
function pseudoInverse(matrix, threshold = Number.EPSILON) {
|
|
3932
3933
|
matrix = Matrix.checkMatrix(matrix);
|
|
3933
|
-
|
|
3934
|
+
let svdSolution = new SingularValueDecomposition(matrix, { autoTranspose: true });
|
|
3934
3935
|
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
3936
|
+
let U = svdSolution.leftSingularVectors;
|
|
3937
|
+
let V = svdSolution.rightSingularVectors;
|
|
3938
|
+
let s = svdSolution.diagonal;
|
|
3938
3939
|
|
|
3939
|
-
for (
|
|
3940
|
+
for (let i = 0; i < s.length; i++) {
|
|
3940
3941
|
if (Math.abs(s[i]) > threshold) {
|
|
3941
3942
|
s[i] = 1.0 / s[i];
|
|
3942
3943
|
} else {
|
|
@@ -3950,7 +3951,11 @@ function pseudoInverse(matrix, threshold = Number.EPSILON) {
|
|
|
3950
3951
|
function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
3951
3952
|
xMatrix = Matrix.checkMatrix(xMatrix);
|
|
3952
3953
|
let yIsSame = false;
|
|
3953
|
-
if (
|
|
3954
|
+
if (
|
|
3955
|
+
typeof yMatrix === 'object' &&
|
|
3956
|
+
!Matrix.isMatrix(yMatrix) &&
|
|
3957
|
+
!Array.isArray(yMatrix)
|
|
3958
|
+
) {
|
|
3954
3959
|
options = yMatrix;
|
|
3955
3960
|
yMatrix = xMatrix;
|
|
3956
3961
|
yIsSame = true;
|
|
@@ -3967,19 +3972,23 @@ function covariance(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
3967
3972
|
yMatrix = yMatrix.center('column');
|
|
3968
3973
|
}
|
|
3969
3974
|
}
|
|
3970
|
-
const
|
|
3971
|
-
for (let i = 0; i <
|
|
3972
|
-
for (let j = 0; j <
|
|
3973
|
-
|
|
3975
|
+
const cov = xMatrix.transpose().mmul(yMatrix);
|
|
3976
|
+
for (let i = 0; i < cov.rows; i++) {
|
|
3977
|
+
for (let j = 0; j < cov.columns; j++) {
|
|
3978
|
+
cov.set(i, j, cov.get(i, j) * (1 / (xMatrix.rows - 1)));
|
|
3974
3979
|
}
|
|
3975
3980
|
}
|
|
3976
|
-
return
|
|
3981
|
+
return cov;
|
|
3977
3982
|
}
|
|
3978
3983
|
|
|
3979
3984
|
function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
3980
3985
|
xMatrix = Matrix.checkMatrix(xMatrix);
|
|
3981
3986
|
let yIsSame = false;
|
|
3982
|
-
if (
|
|
3987
|
+
if (
|
|
3988
|
+
typeof yMatrix === 'object' &&
|
|
3989
|
+
!Matrix.isMatrix(yMatrix) &&
|
|
3990
|
+
!Array.isArray(yMatrix)
|
|
3991
|
+
) {
|
|
3983
3992
|
options = yMatrix;
|
|
3984
3993
|
yMatrix = xMatrix;
|
|
3985
3994
|
yIsSame = true;
|
|
@@ -4005,15 +4014,21 @@ function correlation(xMatrix, yMatrix = xMatrix, options = {}) {
|
|
|
4005
4014
|
}
|
|
4006
4015
|
|
|
4007
4016
|
const sdx = xMatrix.standardDeviation('column', { unbiased: true });
|
|
4008
|
-
const sdy = yIsSame
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4017
|
+
const sdy = yIsSame
|
|
4018
|
+
? sdx
|
|
4019
|
+
: yMatrix.standardDeviation('column', { unbiased: true });
|
|
4020
|
+
|
|
4021
|
+
const corr = xMatrix.transpose().mmul(yMatrix);
|
|
4022
|
+
for (let i = 0; i < corr.rows; i++) {
|
|
4023
|
+
for (let j = 0; j < corr.columns; j++) {
|
|
4024
|
+
corr.set(
|
|
4025
|
+
i,
|
|
4026
|
+
j,
|
|
4027
|
+
corr.get(i, j) * (1 / (sdx[i] * sdy[j])) * (1 / (xMatrix.rows - 1)),
|
|
4028
|
+
);
|
|
4014
4029
|
}
|
|
4015
4030
|
}
|
|
4016
|
-
return
|
|
4031
|
+
return corr;
|
|
4017
4032
|
}
|
|
4018
4033
|
|
|
4019
4034
|
class EigenvalueDecomposition {
|
|
@@ -4025,14 +4040,14 @@ class EigenvalueDecomposition {
|
|
|
4025
4040
|
throw new Error('Matrix is not a square matrix');
|
|
4026
4041
|
}
|
|
4027
4042
|
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4043
|
+
let n = matrix.columns;
|
|
4044
|
+
let V = new Matrix(n, n);
|
|
4045
|
+
let d = new Float64Array(n);
|
|
4046
|
+
let e = new Float64Array(n);
|
|
4047
|
+
let value = matrix;
|
|
4048
|
+
let i, j;
|
|
4034
4049
|
|
|
4035
|
-
|
|
4050
|
+
let isSymmetric = false;
|
|
4036
4051
|
if (assumeSymmetric) {
|
|
4037
4052
|
isSymmetric = true;
|
|
4038
4053
|
} else {
|
|
@@ -4048,8 +4063,8 @@ class EigenvalueDecomposition {
|
|
|
4048
4063
|
tred2(n, e, d, V);
|
|
4049
4064
|
tql2(n, e, d, V);
|
|
4050
4065
|
} else {
|
|
4051
|
-
|
|
4052
|
-
|
|
4066
|
+
let H = new Matrix(n, n);
|
|
4067
|
+
let ort = new Float64Array(n);
|
|
4053
4068
|
for (j = 0; j < n; j++) {
|
|
4054
4069
|
for (i = 0; i < n; i++) {
|
|
4055
4070
|
H.set(i, j, value.get(i, j));
|
|
@@ -4078,11 +4093,11 @@ class EigenvalueDecomposition {
|
|
|
4078
4093
|
}
|
|
4079
4094
|
|
|
4080
4095
|
get diagonalMatrix() {
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4096
|
+
let n = this.n;
|
|
4097
|
+
let e = this.e;
|
|
4098
|
+
let d = this.d;
|
|
4099
|
+
let X = new Matrix(n, n);
|
|
4100
|
+
let i, j;
|
|
4086
4101
|
for (i = 0; i < n; i++) {
|
|
4087
4102
|
for (j = 0; j < n; j++) {
|
|
4088
4103
|
X.set(i, j, 0);
|
|
@@ -4099,7 +4114,7 @@ class EigenvalueDecomposition {
|
|
|
4099
4114
|
}
|
|
4100
4115
|
|
|
4101
4116
|
function tred2(n, e, d, V) {
|
|
4102
|
-
|
|
4117
|
+
let f, g, h, i, j, k, hh, scale;
|
|
4103
4118
|
|
|
4104
4119
|
for (j = 0; j < n; j++) {
|
|
4105
4120
|
d[j] = V.get(n - 1, j);
|
|
@@ -4208,7 +4223,7 @@ function tred2(n, e, d, V) {
|
|
|
4208
4223
|
}
|
|
4209
4224
|
|
|
4210
4225
|
function tql2(n, e, d, V) {
|
|
4211
|
-
|
|
4226
|
+
let g, h, i, j, k, l, m, p, r, dl1, c, c2, c3, el1, s, s2;
|
|
4212
4227
|
|
|
4213
4228
|
for (i = 1; i < n; i++) {
|
|
4214
4229
|
e[i - 1] = e[i];
|
|
@@ -4216,9 +4231,9 @@ function tql2(n, e, d, V) {
|
|
|
4216
4231
|
|
|
4217
4232
|
e[n - 1] = 0;
|
|
4218
4233
|
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4234
|
+
let f = 0;
|
|
4235
|
+
let tst1 = 0;
|
|
4236
|
+
let eps = Number.EPSILON;
|
|
4222
4237
|
|
|
4223
4238
|
for (l = 0; l < n; l++) {
|
|
4224
4239
|
tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l]));
|
|
@@ -4309,10 +4324,10 @@ function tql2(n, e, d, V) {
|
|
|
4309
4324
|
}
|
|
4310
4325
|
|
|
4311
4326
|
function orthes(n, H, ort, V) {
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4327
|
+
let low = 0;
|
|
4328
|
+
let high = n - 1;
|
|
4329
|
+
let f, g, h, i, j, m;
|
|
4330
|
+
let scale;
|
|
4316
4331
|
|
|
4317
4332
|
for (m = low + 1; m <= high - 1; m++) {
|
|
4318
4333
|
scale = 0;
|
|
@@ -4392,21 +4407,21 @@ function orthes(n, H, ort, V) {
|
|
|
4392
4407
|
}
|
|
4393
4408
|
|
|
4394
4409
|
function hqr2(nn, e, d, V, H) {
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
+
let n = nn - 1;
|
|
4411
|
+
let low = 0;
|
|
4412
|
+
let high = nn - 1;
|
|
4413
|
+
let eps = Number.EPSILON;
|
|
4414
|
+
let exshift = 0;
|
|
4415
|
+
let norm = 0;
|
|
4416
|
+
let p = 0;
|
|
4417
|
+
let q = 0;
|
|
4418
|
+
let r = 0;
|
|
4419
|
+
let s = 0;
|
|
4420
|
+
let z = 0;
|
|
4421
|
+
let iter = 0;
|
|
4422
|
+
let i, j, k, l, m, t, w, x, y;
|
|
4423
|
+
let ra, sa, vr, vi;
|
|
4424
|
+
let notlast, cdivres;
|
|
4410
4425
|
|
|
4411
4426
|
for (i = 0; i < nn; i++) {
|
|
4412
4427
|
if (i < low || i > high) {
|
|
@@ -4671,7 +4686,7 @@ function hqr2(nn, e, d, V, H) {
|
|
|
4671
4686
|
H.set(
|
|
4672
4687
|
i + 1,
|
|
4673
4688
|
n,
|
|
4674
|
-
Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z
|
|
4689
|
+
Math.abs(x) > Math.abs(z) ? (-r - w * t) / x : (-s - y * t) / z,
|
|
4675
4690
|
);
|
|
4676
4691
|
}
|
|
4677
4692
|
|
|
@@ -4736,7 +4751,7 @@ function hqr2(nn, e, d, V, H) {
|
|
|
4736
4751
|
x * r - z * ra + q * sa,
|
|
4737
4752
|
x * s - z * sa - q * ra,
|
|
4738
4753
|
vr,
|
|
4739
|
-
vi
|
|
4754
|
+
vi,
|
|
4740
4755
|
);
|
|
4741
4756
|
H.set(i, n - 1, cdivres[0]);
|
|
4742
4757
|
H.set(i, n, cdivres[1]);
|
|
@@ -4744,19 +4759,19 @@ function hqr2(nn, e, d, V, H) {
|
|
|
4744
4759
|
H.set(
|
|
4745
4760
|
i + 1,
|
|
4746
4761
|
n - 1,
|
|
4747
|
-
(-ra - w * H.get(i, n - 1) + q * H.get(i, n)) / x
|
|
4762
|
+
(-ra - w * H.get(i, n - 1) + q * H.get(i, n)) / x,
|
|
4748
4763
|
);
|
|
4749
4764
|
H.set(
|
|
4750
4765
|
i + 1,
|
|
4751
4766
|
n,
|
|
4752
|
-
(-sa - w * H.get(i, n) - q * H.get(i, n - 1)) / x
|
|
4767
|
+
(-sa - w * H.get(i, n) - q * H.get(i, n - 1)) / x,
|
|
4753
4768
|
);
|
|
4754
4769
|
} else {
|
|
4755
4770
|
cdivres = cdiv(
|
|
4756
4771
|
-r - y * H.get(i, n - 1),
|
|
4757
4772
|
-s - y * H.get(i, n),
|
|
4758
4773
|
z,
|
|
4759
|
-
q
|
|
4774
|
+
q,
|
|
4760
4775
|
);
|
|
4761
4776
|
H.set(i + 1, n - 1, cdivres[0]);
|
|
4762
4777
|
H.set(i + 1, n, cdivres[1]);
|
|
@@ -4795,7 +4810,7 @@ function hqr2(nn, e, d, V, H) {
|
|
|
4795
4810
|
}
|
|
4796
4811
|
|
|
4797
4812
|
function cdiv(xr, xi, yr, yi) {
|
|
4798
|
-
|
|
4813
|
+
let r, d;
|
|
4799
4814
|
if (Math.abs(yr) > Math.abs(yi)) {
|
|
4800
4815
|
r = yi / yr;
|
|
4801
4816
|
d = yr + r * yi;
|
|
@@ -4814,16 +4829,16 @@ class CholeskyDecomposition {
|
|
|
4814
4829
|
throw new Error('Matrix is not symmetric');
|
|
4815
4830
|
}
|
|
4816
4831
|
|
|
4817
|
-
|
|
4818
|
-
|
|
4819
|
-
|
|
4820
|
-
|
|
4821
|
-
|
|
4832
|
+
let a = value;
|
|
4833
|
+
let dimension = a.rows;
|
|
4834
|
+
let l = new Matrix(dimension, dimension);
|
|
4835
|
+
let positiveDefinite = true;
|
|
4836
|
+
let i, j, k;
|
|
4822
4837
|
|
|
4823
4838
|
for (j = 0; j < dimension; j++) {
|
|
4824
|
-
|
|
4839
|
+
let d = 0;
|
|
4825
4840
|
for (k = 0; k < j; k++) {
|
|
4826
|
-
|
|
4841
|
+
let s = 0;
|
|
4827
4842
|
for (i = 0; i < k; i++) {
|
|
4828
4843
|
s += l.get(k, i) * l.get(j, i);
|
|
4829
4844
|
}
|
|
@@ -4841,26 +4856,30 @@ class CholeskyDecomposition {
|
|
|
4841
4856
|
}
|
|
4842
4857
|
}
|
|
4843
4858
|
|
|
4844
|
-
if (!positiveDefinite) {
|
|
4845
|
-
throw new Error('Matrix is not positive definite');
|
|
4846
|
-
}
|
|
4847
|
-
|
|
4848
4859
|
this.L = l;
|
|
4860
|
+
this.positiveDefinite = Boolean(positiveDefinite);
|
|
4861
|
+
}
|
|
4862
|
+
|
|
4863
|
+
isPositiveDefinite() {
|
|
4864
|
+
return this.positiveDefinite;
|
|
4849
4865
|
}
|
|
4850
4866
|
|
|
4851
4867
|
solve(value) {
|
|
4852
4868
|
value = WrapperMatrix2D.checkMatrix(value);
|
|
4853
4869
|
|
|
4854
|
-
|
|
4855
|
-
|
|
4870
|
+
let l = this.L;
|
|
4871
|
+
let dimension = l.rows;
|
|
4856
4872
|
|
|
4857
4873
|
if (value.rows !== dimension) {
|
|
4858
4874
|
throw new Error('Matrix dimensions do not match');
|
|
4859
4875
|
}
|
|
4876
|
+
if (this.isPositiveDefinite() === false) {
|
|
4877
|
+
throw new Error('Matrix is not positive definite');
|
|
4878
|
+
}
|
|
4860
4879
|
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
4880
|
+
let count = value.columns;
|
|
4881
|
+
let B = value.clone();
|
|
4882
|
+
let i, j, k;
|
|
4864
4883
|
|
|
4865
4884
|
for (k = 0; k < dimension; k++) {
|
|
4866
4885
|
for (j = 0; j < count; j++) {
|
|
@@ -4888,6 +4907,139 @@ class CholeskyDecomposition {
|
|
|
4888
4907
|
}
|
|
4889
4908
|
}
|
|
4890
4909
|
|
|
4910
|
+
class nipals {
|
|
4911
|
+
constructor(X, options = {}) {
|
|
4912
|
+
X = WrapperMatrix2D.checkMatrix(X);
|
|
4913
|
+
let { Y } = options;
|
|
4914
|
+
const {
|
|
4915
|
+
scaleScores = false,
|
|
4916
|
+
maxIterations = 1000,
|
|
4917
|
+
terminationCriteria = 1e-10,
|
|
4918
|
+
} = options;
|
|
4919
|
+
|
|
4920
|
+
let u;
|
|
4921
|
+
if (Y) {
|
|
4922
|
+
if (Array.isArray(Y) && typeof Y[0] === 'number') {
|
|
4923
|
+
Y = Matrix.columnVector(Y);
|
|
4924
|
+
} else {
|
|
4925
|
+
Y = WrapperMatrix2D.checkMatrix(Y);
|
|
4926
|
+
}
|
|
4927
|
+
if (!Y.isColumnVector() || Y.rows !== X.rows) {
|
|
4928
|
+
throw new Error('Y must be a column vector of length X.rows');
|
|
4929
|
+
}
|
|
4930
|
+
u = Y;
|
|
4931
|
+
} else {
|
|
4932
|
+
u = X.getColumnVector(0);
|
|
4933
|
+
}
|
|
4934
|
+
|
|
4935
|
+
let diff = 1;
|
|
4936
|
+
let t, q, w, tOld;
|
|
4937
|
+
|
|
4938
|
+
for (
|
|
4939
|
+
let counter = 0;
|
|
4940
|
+
counter < maxIterations && diff > terminationCriteria;
|
|
4941
|
+
counter++
|
|
4942
|
+
) {
|
|
4943
|
+
w = X.transpose()
|
|
4944
|
+
.mmul(u)
|
|
4945
|
+
.div(
|
|
4946
|
+
u
|
|
4947
|
+
.transpose()
|
|
4948
|
+
.mmul(u)
|
|
4949
|
+
.get(0, 0),
|
|
4950
|
+
);
|
|
4951
|
+
w = w.div(w.norm());
|
|
4952
|
+
|
|
4953
|
+
t = X.mmul(w).div(
|
|
4954
|
+
w
|
|
4955
|
+
.transpose()
|
|
4956
|
+
.mmul(w)
|
|
4957
|
+
.get(0, 0),
|
|
4958
|
+
);
|
|
4959
|
+
|
|
4960
|
+
if (counter > 0) {
|
|
4961
|
+
diff = t
|
|
4962
|
+
.clone()
|
|
4963
|
+
.sub(tOld)
|
|
4964
|
+
.pow(2)
|
|
4965
|
+
.sum();
|
|
4966
|
+
}
|
|
4967
|
+
tOld = t.clone();
|
|
4968
|
+
|
|
4969
|
+
if (Y) {
|
|
4970
|
+
q = Y.transpose()
|
|
4971
|
+
.mmul(t)
|
|
4972
|
+
.div(
|
|
4973
|
+
t
|
|
4974
|
+
.transpose()
|
|
4975
|
+
.mmul(t)
|
|
4976
|
+
.get(0, 0),
|
|
4977
|
+
);
|
|
4978
|
+
q = q.div(q.norm());
|
|
4979
|
+
|
|
4980
|
+
u = Y.mmul(q).div(
|
|
4981
|
+
q
|
|
4982
|
+
.transpose()
|
|
4983
|
+
.mmul(q)
|
|
4984
|
+
.get(0, 0),
|
|
4985
|
+
);
|
|
4986
|
+
} else {
|
|
4987
|
+
u = t;
|
|
4988
|
+
}
|
|
4989
|
+
}
|
|
4990
|
+
|
|
4991
|
+
if (Y) {
|
|
4992
|
+
let p = X.transpose()
|
|
4993
|
+
.mmul(t)
|
|
4994
|
+
.div(
|
|
4995
|
+
t
|
|
4996
|
+
.transpose()
|
|
4997
|
+
.mmul(t)
|
|
4998
|
+
.get(0, 0),
|
|
4999
|
+
);
|
|
5000
|
+
p = p.div(p.norm());
|
|
5001
|
+
let xResidual = X.clone().sub(t.clone().mmul(p.transpose()));
|
|
5002
|
+
let residual = u
|
|
5003
|
+
.transpose()
|
|
5004
|
+
.mmul(t)
|
|
5005
|
+
.div(
|
|
5006
|
+
t
|
|
5007
|
+
.transpose()
|
|
5008
|
+
.mmul(t)
|
|
5009
|
+
.get(0, 0),
|
|
5010
|
+
);
|
|
5011
|
+
let yResidual = Y.clone().sub(
|
|
5012
|
+
t
|
|
5013
|
+
.clone()
|
|
5014
|
+
.mulS(residual.get(0, 0))
|
|
5015
|
+
.mmul(q.transpose()),
|
|
5016
|
+
);
|
|
5017
|
+
|
|
5018
|
+
this.t = t;
|
|
5019
|
+
this.p = p.transpose();
|
|
5020
|
+
this.w = w.transpose();
|
|
5021
|
+
this.q = q;
|
|
5022
|
+
this.u = u;
|
|
5023
|
+
this.s = t.transpose().mmul(t);
|
|
5024
|
+
this.xResidual = xResidual;
|
|
5025
|
+
this.yResidual = yResidual;
|
|
5026
|
+
this.betas = residual;
|
|
5027
|
+
} else {
|
|
5028
|
+
this.w = w.transpose();
|
|
5029
|
+
this.s = t
|
|
5030
|
+
.transpose()
|
|
5031
|
+
.mmul(t)
|
|
5032
|
+
.sqrt();
|
|
5033
|
+
if (scaleScores) {
|
|
5034
|
+
this.t = t.clone().div(this.s.get(0, 0));
|
|
5035
|
+
} else {
|
|
5036
|
+
this.t = t;
|
|
5037
|
+
}
|
|
5038
|
+
this.xResidual = X.sub(t.mmul(w.transpose()));
|
|
5039
|
+
}
|
|
5040
|
+
}
|
|
5041
|
+
}
|
|
5042
|
+
|
|
4891
5043
|
exports.AbstractMatrix = AbstractMatrix;
|
|
4892
5044
|
exports.CHO = CholeskyDecomposition;
|
|
4893
5045
|
exports.CholeskyDecomposition = CholeskyDecomposition;
|
|
@@ -4905,6 +5057,8 @@ exports.MatrixRowView = MatrixRowView;
|
|
|
4905
5057
|
exports.MatrixSelectionView = MatrixSelectionView;
|
|
4906
5058
|
exports.MatrixSubView = MatrixSubView;
|
|
4907
5059
|
exports.MatrixTransposeView = MatrixTransposeView;
|
|
5060
|
+
exports.NIPALS = nipals;
|
|
5061
|
+
exports.Nipals = nipals;
|
|
4908
5062
|
exports.QR = QrDecomposition;
|
|
4909
5063
|
exports.QrDecomposition = QrDecomposition;
|
|
4910
5064
|
exports.SVD = SingularValueDecomposition;
|