Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DistanceMatrix): fromCompact with an empty array #188

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/__tests__/distance_matrix/creation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/distance_matrix/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/matrix/utility.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/distanceMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/inspect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const indent = ' '.repeat(2);
const indentData = ' '.repeat(4);

/**
* @this {Matrix}
* @returns {string}
*/
export function inspectMatrix() {
return inspectMatrixWithOptions(this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/mathOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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);
Expand Down