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

31 lines
611 B
PHP
Raw Normal View History

2016-04-11 19:44:48 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-04-11 19:44:48 +00:00
2016-04-20 21:56:33 +00:00
namespace Phpml\Math\Distance;
2016-04-11 19:44:48 +00:00
use Phpml\Exception\InvalidArgumentException;
2016-04-20 21:56:33 +00:00
use Phpml\Math\Distance;
2016-04-11 19:44:48 +00:00
class Chebyshev implements Distance
{
/**
* @throws InvalidArgumentException
*/
public function distance(array $a, array $b) : float
2016-04-11 19:44:48 +00:00
{
if (count($a) !== count($b)) {
throw InvalidArgumentException::arraySizeNotMatch();
2016-04-11 19:44:48 +00:00
}
$differences = [];
$count = count($a);
for ($i = 0; $i < $count; ++$i) {
$differences[] = abs($a[$i] - $b[$i]);
}
return max($differences);
}
}