Skip to content

Commit 9dfe983

Browse files
committed
fix(Matrix): use Float64Array to improve performance
1 parent 56bcb32 commit 9dfe983

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/matrix.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -756,20 +756,14 @@ export class AbstractMatrix {
756756

757757
mmul(other) {
758758
other = Matrix.checkMatrix(other);
759-
if (this.columns !== other.rows) {
760-
// eslint-disable-next-line no-console
761-
console.warn(
762-
'Number of columns of left matrix are not equal to number of rows of right matrix.'
763-
);
764-
}
765759

766760
var m = this.rows;
767761
var n = this.columns;
768762
var p = other.columns;
769763

770764
var result = new Matrix(m, p);
771765

772-
var Bcolj = new Array(n);
766+
var Bcolj = new Float64Array(n);
773767
for (var j = 0; j < p; j++) {
774768
for (var k = 0; k < n; k++) {
775769
Bcolj[k] = other.get(k, j);
@@ -1434,10 +1428,7 @@ export default class Matrix extends AbstractMatrix {
14341428
this.data = [];
14351429
if (Number.isInteger(nColumns) && nColumns > 0) {
14361430
for (let i = 0; i < nRows; i++) {
1437-
this.data.push([]);
1438-
for (let j = 0; j < nColumns; j++) {
1439-
this.data[i].push(0);
1440-
}
1431+
this.data.push(new Float64Array(nColumns));
14411432
}
14421433
} else {
14431434
throw new TypeError('nColumns must be a positive integer');
@@ -1457,7 +1448,7 @@ export default class Matrix extends AbstractMatrix {
14571448
if (arrayData[i].length !== nColumns) {
14581449
throw new RangeError('Inconsistent array dimensions');
14591450
}
1460-
this.data.push(arrayData[i].slice());
1451+
this.data.push(Float64Array.from(arrayData[i]));
14611452
}
14621453
} else {
14631454
throw new TypeError(
@@ -1494,7 +1485,7 @@ export default class Matrix extends AbstractMatrix {
14941485
index = this.rows;
14951486
}
14961487
checkRowIndex(this, index, true);
1497-
array = checkRowVector(this, array, true);
1488+
array = Float64Array.from(checkRowVector(this, array, true));
14981489
this.data.splice(index, 0, array);
14991490
this.rows += 1;
15001491
return this;
@@ -1506,7 +1497,14 @@ export default class Matrix extends AbstractMatrix {
15061497
throw new RangeError('A matrix cannot have less than one column');
15071498
}
15081499
for (var i = 0; i < this.rows; i++) {
1509-
this.data[i].splice(index, 1);
1500+
const newRow = new Float64Array(this.columns - 1);
1501+
for (let j = 0; j < index; j++) {
1502+
newRow[j] = this.data[i][j];
1503+
}
1504+
for (let j = index + 1; j < this.columns; j++) {
1505+
newRow[j - 1] = this.data[i][j];
1506+
}
1507+
this.data[i] = newRow;
15101508
}
15111509
this.columns -= 1;
15121510
return this;
@@ -1520,7 +1518,16 @@ export default class Matrix extends AbstractMatrix {
15201518
checkColumnIndex(this, index, true);
15211519
array = checkColumnVector(this, array);
15221520
for (var i = 0; i < this.rows; i++) {
1523-
this.data[i].splice(index, 0, array[i]);
1521+
const newRow = new Float64Array(this.columns + 1);
1522+
let j = 0;
1523+
for (; j < index; j++) {
1524+
newRow[j] = this.data[i][j];
1525+
}
1526+
newRow[j++] = array[i];
1527+
for (; j < this.columns + 1; j++) {
1528+
newRow[j] = this.data[i][j - 1];
1529+
}
1530+
this.data[i] = newRow;
15241531
}
15251532
this.columns += 1;
15261533
return this;

0 commit comments

Comments
 (0)