| @@ 10-35 (lines=26) @@ | ||
| 7 | use Phpml\Exception\InvalidArgumentException; |
|
| 8 | use Phpml\Metric\Distance; |
|
| 9 | ||
| 10 | class Chebyshev implements Distance |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @param array $a |
|
| 14 | * @param array $b |
|
| 15 | * |
|
| 16 | * @return float |
|
| 17 | * |
|
| 18 | * @throws InvalidArgumentException |
|
| 19 | */ |
|
| 20 | public function distance(array $a, array $b): float |
|
| 21 | { |
|
| 22 | if (count($a) !== count($b)) { |
|
| 23 | throw InvalidArgumentException::sizeNotMatch(); |
|
| 24 | } |
|
| 25 | ||
| 26 | $differences = []; |
|
| 27 | $count = count($a); |
|
| 28 | ||
| 29 | for ($i = 0; $i < $count; ++$i) { |
|
| 30 | $differences[] = abs($a[$i] - $b[$i]); |
|
| 31 | } |
|
| 32 | ||
| 33 | return max($differences); |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||
| @@ 10-35 (lines=26) @@ | ||
| 7 | use Phpml\Exception\InvalidArgumentException; |
|
| 8 | use Phpml\Metric\Distance; |
|
| 9 | ||
| 10 | class Euclidean implements Distance |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @param array $a |
|
| 14 | * @param array $b |
|
| 15 | * |
|
| 16 | * @return float |
|
| 17 | * |
|
| 18 | * @throws InvalidArgumentException |
|
| 19 | */ |
|
| 20 | public function distance(array $a, array $b): float |
|
| 21 | { |
|
| 22 | if (count($a) !== count($b)) { |
|
| 23 | throw InvalidArgumentException::sizeNotMatch(); |
|
| 24 | } |
|
| 25 | ||
| 26 | $distance = 0; |
|
| 27 | $count = count($a); |
|
| 28 | ||
| 29 | for ($i = 0; $i < $count; ++$i) { |
|
| 30 | $distance += pow($a[$i] - $b[$i], 2); |
|
| 31 | } |
|
| 32 | ||
| 33 | return sqrt($distance); |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||