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

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