diff --git a/Math/Topology/MetricsND.php b/Math/Topology/MetricsND.php index 772964bdd..d892711a9 100755 --- a/Math/Topology/MetricsND.php +++ b/Math/Topology/MetricsND.php @@ -52,7 +52,7 @@ final class MetricsND */ public static function manhattan(array $a, array $b) : float { - if (\count($a) !== \count($b)) { + if (\count($a) > \count($b)) { throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } @@ -78,6 +78,10 @@ final class MetricsND */ public static function euclidean(array $a, array $b) : float { + if (\count($a) > \count($b)) { + throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); + } + $dist = 0.0; foreach ($a as $key => $e) { $dist += \abs($e - $b[$key]) ** 2; @@ -94,10 +98,16 @@ final class MetricsND * * @return float * + * @throws InvalidDimensionException + * * @since 1.0.0 */ public static function cosine(array $a, array $b) : float { + if (\count($a) > \count($b)) { + throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); + } + $dotProduct = 0; foreach ($a as $id => $_) { $dotProduct += $a[$id] * $b[$id]; @@ -138,7 +148,7 @@ final class MetricsND */ public static function chebyshev(array $a, array $b) : float { - if (\count($a) !== \count($b)) { + if (\count($a) > \count($b)) { throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } @@ -167,7 +177,7 @@ final class MetricsND */ public static function minkowski(array $a, array $b, int $lambda) : float { - if (\count($a) !== \count($b)) { + if (\count($a) > \count($b)) { throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } @@ -195,7 +205,7 @@ final class MetricsND */ public static function canberra(array $a, array $b) : float { - if (\count($a) !== \count($b)) { + if (\count($a) > \count($b)) { throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } @@ -223,7 +233,7 @@ final class MetricsND */ public static function brayCurtis(array $a, array $b) : float { - if (\count($a) !== \count($b)) { + if (\count($a) > \count($b)) { throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } @@ -253,7 +263,7 @@ final class MetricsND */ public static function angularSeparation(array $a, array $b) : float { - if (\count($a) !== \count($b)) { + if (\count($a) > \count($b)) { throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } diff --git a/tests/Math/Topology/MetricsNDTest.php b/tests/Math/Topology/MetricsNDTest.php index 074b865de..0b6bb4cdb 100755 --- a/tests/Math/Topology/MetricsNDTest.php +++ b/tests/Math/Topology/MetricsNDTest.php @@ -126,7 +126,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::manhattan([3, 6, 4], [4, 6, 8, 3]); + MetricsND::manhattan([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -135,7 +135,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::euclidean([3, 6, 4], [4, 6, 8, 3]); + MetricsND::euclidean([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -144,7 +144,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::chebyshev([3, 6, 4], [4, 6, 8, 3]); + MetricsND::chebyshev([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -153,7 +153,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::minkowski([3, 6, 4], [4, 6, 8, 3], 2); + MetricsND::minkowski([4, 6, 8, 3], [3, 6, 4], 2); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -162,7 +162,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::canberra([3, 6, 4], [4, 6, 8, 3]); + MetricsND::canberra([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -171,7 +171,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::cosine([3, 6, 4], [4, 6, 8, 3]); + MetricsND::cosine([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -180,7 +180,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::brayCurtis([3, 6, 4], [4, 6, 8, 3]); + MetricsND::brayCurtis([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -189,7 +189,7 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::angularSeparation([3, 6, 4], [4, 6, 8, 3]); + MetricsND::angularSeparation([4, 6, 8, 3], [3, 6, 4]); } #[\PHPUnit\Framework\Attributes\Group('framework')] @@ -198,6 +198,6 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase { $this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); - MetricsND::hamming([3, 6, 4], [4, 6, 8, 3]); + MetricsND::hamming([4, 6, 8, 3], [3, 6, 4]); } }