Skip to content
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
6 changes: 5 additions & 1 deletion lib/Clustering/SquaredDistance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions tests/SquaredDistanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @copyright Copyright (c) 2026 The Recognize contributors.
* @license AGPL-3.0-or-later
*
* This file is licensed under the Affero General Public License version 3 or later.
* See the COPYING file.
*/
declare(strict_types=1);

use OCA\Recognize\Clustering\SquaredDistance;
use PHPUnit\Framework\TestCase;

class SquaredDistanceTest extends TestCase {
private SquaredDistance $kernel;

public function setUp(): void {
$this->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([], []));
}
}