Total Complexity | 6 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
13 | abstract class Distance implements DistanceInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var float|int |
||
17 | */ |
||
18 | public $norm; |
||
19 | |||
20 | /** |
||
21 | * Distance constructor. |
||
22 | */ |
||
23 | public function __construct(float $norm = 3.0) |
||
24 | { |
||
25 | $this->norm = $norm; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @throws InvalidArgumentException |
||
30 | */ |
||
31 | public function distance(array $a, array $b): float |
||
32 | { |
||
33 | $distance = 0; |
||
34 | |||
35 | foreach ($this->deltas($a, $b) as $delta) { |
||
36 | $distance += $delta ** $this->norm; |
||
37 | } |
||
38 | |||
39 | return $distance ** (1 / $this->norm); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @throws InvalidArgumentException |
||
44 | */ |
||
45 | protected function deltas(array $a, array $b): array |
||
60 | } |
||
61 | } |
||
62 |