diff --git a/src/__tests__/distance_matrix/creation.test.js b/src/__tests__/distance_matrix/creation.test.js index 1b59287..ee72a79 100644 --- a/src/__tests__/distance_matrix/creation.test.js +++ b/src/__tests__/distance_matrix/creation.test.js @@ -83,6 +83,10 @@ describe('DistanceMatrix creation', () => { ); }); + it('fromCompact empty', () => { + expect(DistanceMatrix.fromCompact([]).to2DArray()).toStrictEqual([]); + }); + it('zeros', () => { expect(DistanceMatrix.zeros(3).to2DArray()).toStrictEqual([ [0, 0, 0], diff --git a/src/__tests__/distance_matrix/export.test.js b/src/__tests__/distance_matrix/export.test.js index fbdf1ff..4aced5f 100644 --- a/src/__tests__/distance_matrix/export.test.js +++ b/src/__tests__/distance_matrix/export.test.js @@ -13,6 +13,10 @@ describe('DistanceMatrix export', () => { expect(matrix.toCompact()).toStrictEqual([1, 2, 3, 2, 3, 3]); }); + it('toCompact empty', () => { + expect(new DistanceMatrix(0).toCompact()).toStrictEqual([]); + }); + it('toSymmetricMatrix', () => { const matrix = new DistanceMatrix([ [0, 1, 2, 3], diff --git a/src/__tests__/matrix/utility.test.js b/src/__tests__/matrix/utility.test.js index 7bf7b50..e8328fc 100644 --- a/src/__tests__/matrix/utility.test.js +++ b/src/__tests__/matrix/utility.test.js @@ -45,6 +45,11 @@ describe('utility methods', () => { matrix.set(0, 0, 10); let called = 0; + /** + * @this {Matrix} + * @param i + * @param j + */ function cb(i, j) { called++; expect(this).toBeInstanceOf(Matrix); diff --git a/src/distanceMatrix.js b/src/distanceMatrix.js index 75c620b..babdd29 100644 --- a/src/distanceMatrix.js +++ b/src/distanceMatrix.js @@ -97,6 +97,12 @@ export class DistanceMatrix extends SymmetricMatrix { */ static fromCompact(compact) { const compactSize = compact.length; + + if (compactSize === 0) { + return new this(0); + } + + // compactSize in Natural integer range ]0;∞] // compactSize = (sideSize * (sideSize - 1)) / 2 // sideSize = (Sqrt(8 × compactSize + 1) + 1) / 2 const diagonalSize = (Math.sqrt(8 * compactSize + 1) + 1) / 2; diff --git a/src/inspect.js b/src/inspect.js index 66927dd..79500a4 100644 --- a/src/inspect.js +++ b/src/inspect.js @@ -1,6 +1,10 @@ const indent = ' '.repeat(2); const indentData = ' '.repeat(4); +/** + * @this {Matrix} + * @returns {string} + */ export function inspectMatrix() { return inspectMatrixWithOptions(this); } diff --git a/src/mathOperations.js b/src/mathOperations.js index 221886b..2b8c306 100644 --- a/src/mathOperations.js +++ b/src/mathOperations.js @@ -801,7 +801,7 @@ export function installMathOperations(AbstractMatrix, Matrix) { AbstractMatrix.prototype.powS = function powS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { - this.set(i, j, Math.pow(this.get(i, j), value)); + this.set(i, j, this.get(i, j) ** value); } } return this; @@ -815,7 +815,7 @@ export function installMathOperations(AbstractMatrix, Matrix) { } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { - this.set(i, j, Math.pow(this.get(i, j), matrix.get(i, j))); + this.set(i, j, this.get(i, j) ** matrix.get(i, j)); } } return this; diff --git a/src/stat.js b/src/stat.js index dc9d705..dbe558d 100644 --- a/src/stat.js +++ b/src/stat.js @@ -157,7 +157,7 @@ export function getScaleByRow(matrix) { for (let i = 0; i < matrix.rows; i++) { let sum = 0; for (let j = 0; j < matrix.columns; j++) { - sum += Math.pow(matrix.get(i, j), 2) / (matrix.columns - 1); + sum += matrix.get(i, j) ** 2 / (matrix.columns - 1); } scale.push(Math.sqrt(sum)); } @@ -177,7 +177,7 @@ export function getScaleByColumn(matrix) { for (let j = 0; j < matrix.columns; j++) { let sum = 0; for (let i = 0; i < matrix.rows; i++) { - sum += Math.pow(matrix.get(i, j), 2) / (matrix.rows - 1); + sum += matrix.get(i, j) ** 2 / (matrix.rows - 1); } scale.push(Math.sqrt(sum)); } @@ -197,7 +197,7 @@ export function getScaleAll(matrix) { let sum = 0; for (let j = 0; j < matrix.columns; j++) { for (let i = 0; i < matrix.rows; i++) { - sum += Math.pow(matrix.get(i, j), 2) / divider; + sum += matrix.get(i, j) ** 2 / divider; } } return Math.sqrt(sum);