php-ml/src/Phpml/Math/Distance/Manhattan.php

31 lines
595 B
PHP
Raw Normal View History

<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-04-20 21:56:33 +00:00
namespace Phpml\Math\Distance;
use Phpml\Exception\InvalidArgumentException;
2016-04-20 21:56:33 +00:00
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;
}
}