php-ml/src/Phpml/Math/Distance/Manhattan.php
Tomáš Votruba f4650c696c [coding standard] fix imports order and drop unused docs typehints (#145)
* fix imports order

* drop unused docs typehints, make use of return types where possible
2017-11-06 08:56:37 +01:00

31 lines
595 B
PHP

<?php
declare(strict_types=1);
namespace Phpml\Math\Distance;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance;
class Manhattan implements Distance
{
/**
* @throws InvalidArgumentException
*/
public function distance(array $a, array $b) : float
{
if (count($a) !== count($b)) {
throw InvalidArgumentException::arraySizeNotMatch();
}
$distance = 0;
$count = count($a);
for ($i = 0; $i < $count; ++$i) {
$distance += abs($a[$i] - $b[$i]);
}
return $distance;
}
}