norm = $norm; } /** * @throws InvalidArgumentException */ public function distance(array $a, array $b): float { $distance = 0; foreach ($this->deltas($a, $b) as $delta) { $distance += $delta ** $this->norm; } return $distance ** (1 / $this->norm); } /** * @throws InvalidArgumentException */ protected function deltas(array $a, array $b): array { $count = count($a); if ($count !== count($b)) { throw new InvalidArgumentException('Size of given arrays does not match'); } $deltas = []; for ($i = 0; $i < $count; $i++) { $deltas[] = abs($a[$i] - $b[$i]); } return $deltas; } }