2016-04-12 19:43:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types = 1);
|
|
|
|
|
|
|
|
namespace Phpml\Metric\Distance;
|
|
|
|
|
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
|
|
|
use Phpml\Metric\Distance;
|
|
|
|
|
|
|
|
class Manhattan implements Distance
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param array $a
|
|
|
|
* @param array $b
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function distance(array $a, array $b): float
|
|
|
|
{
|
|
|
|
if (count($a) !== count($b)) {
|
2016-04-18 20:58:43 +00:00
|
|
|
throw InvalidArgumentException::arraySizeNotMatch();
|
2016-04-12 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$distance = 0;
|
|
|
|
$count = count($a);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
|
|
$distance += abs($a[$i] - $b[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $distance;
|
|
|
|
}
|
|
|
|
}
|