26 lines
525 B
PHP
26 lines
525 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();
|
|
}
|
|
|
|
return array_sum(array_map(function ($m, $n) {
|
|
return abs($m - $n);
|
|
}, $a, $b));
|
|
}
|
|
}
|