From fdd25bf8149181a2d5db3e50ca3c835c42229a86 Mon Sep 17 00:00:00 2001 From: RanaKhan77 Date: Tue, 10 Nov 2026 20:57:28 +0000 Subject: [PATCH] fix(clustering): stop undefined-key warnings in SquaredDistance on ragged vectors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit compute() walked count($a) dimensions but read $b[$i] unguarded, so any pair of vectors with different lengths (e.g. a truncated embedding row) flooded the log with "Undefined array key N" warnings during the cron clustering. The loop now walks min(count($a), count($b)) — the dimensions both vectors share — which is also what the old loop already did whenever $a happened to be the shorter side, so the kernel is now symmetric on ragged input and unchanged for well-formed vectors. Adds a small unit test for the equal-length value, the ragged case from the report, symmetry, and empty input. Fixes #1611 Signed-off-by: RanaKhan77 --- lib/Clustering/SquaredDistance.php | 6 +++- tests/SquaredDistanceTest.php | 49 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/SquaredDistanceTest.php diff --git a/lib/Clustering/SquaredDistance.php b/lib/Clustering/SquaredDistance.php index ce1ed9591..e64eda7d2 100644 --- a/lib/Clustering/SquaredDistance.php +++ b/lib/Clustering/SquaredDistance.php @@ -45,7 +45,11 @@ public function compatibility(): array { public function compute(array $a, array $b): float { $distance = 0.0; - $count = count($a); + // Only walk the dimensions both vectors share: vectors of different + // lengths (e.g. a truncated embedding row) previously triggered + // "Undefined array key" warnings on every comparison while the cron + // clustering ran (#1611). + $count = min(count($a), count($b)); for ($i = 0; $i < $count; $i++) { $diff = (float)$a[$i] - (float)$b[$i]; $distance += $diff * $diff; diff --git a/tests/SquaredDistanceTest.php b/tests/SquaredDistanceTest.php new file mode 100644 index 000000000..9d6cd0adb --- /dev/null +++ b/tests/SquaredDistanceTest.php @@ -0,0 +1,49 @@ +kernel = new SquaredDistance(); + } + + public function testComputesSquaredEuclideanDistance(): void { + // sqrt-free euclidean: (1-4)^2 + (2-5)^2 = 18 + self::assertSame(18.0, $this->kernel->compute([1, 2], [4, 5])); + } + + public function testZeroDistanceToItself(): void { + self::assertSame(0.0, $this->kernel->compute([0.5, 1.5, -2.5], [0.5, 1.5, -2.5])); + } + + public function testRaggedSecondVectorReturnsNoWarning(): void { + // #1611: $b shorter than $a used to emit "Undefined array key" + // warnings. The distance now covers only the shared dimensions + // (here: (0-3)^2 = 9). + self::assertSame(9.0, $this->kernel->compute([0, 3, 7], [3])); + } + + public function testRaggedFirstVectorIsSymmetric(): void { + // whichever side is shorter, the result must be the same + self::assertSame( + $this->kernel->compute([0, 3, 7], [3]), + $this->kernel->compute([3], [0, 3, 7]) + ); + } + + public function testEmptyVectorsReturnZero(): void { + self::assertSame(0.0, $this->kernel->compute([], [])); + } +}