2016-04-12 19:43:25 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-04-12 19:43:25 +00:00
|
|
|
|
2016-04-20 21:56:33 +00:00
|
|
|
namespace Phpml\Math\Distance;
|
2016-04-12 19:43:25 +00:00
|
|
|
|
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2016-04-20 21:56:33 +00:00
|
|
|
use Phpml\Math\Distance;
|
2016-04-12 19:43:25 +00:00
|
|
|
|
|
|
|
class Manhattan implements Distance
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function distance(array $a, array $b): float
|
2016-04-12 19:43:25 +00:00
|
|
|
{
|
|
|
|
if (count($a) !== count($b)) {
|
2016-04-18 20:58:43 +00:00
|
|
|
throw InvalidArgumentException::arraySizeNotMatch();
|
2016-04-12 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:45:35 +00:00
|
|
|
return array_sum(array_map(function ($m, $n) {
|
|
|
|
return abs($m - $n);
|
|
|
|
}, $a, $b));
|
2016-04-12 19:43:25 +00:00
|
|
|
}
|
|
|
|
}
|