Skip to content

Commit

Permalink
fix(DistanceMatrix): fromCompact with an empty array
Browse files Browse the repository at this point in the history
It was creating a distance matrix
with `1` as `diagonalSize` instead `0`
  • Loading branch information
tpoisseau authored and targos committed Jun 10, 2024
1 parent 4b632bb commit 637598e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
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
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

0 comments on commit 637598e

Please sign in to comment.