*/ public function compatibility() : array { return [ DataType::continuous(), ]; } /** * Compute the distance between two vectors. * * @internal * * @param list $a * @param list $b * @return float */ public function compute(array $a, array $b) : float { $sigma = $ssA = $ssB = 0.0; foreach ($a as $i => $valueA) { $valueB = $b[$i]; $sigma += $valueA * $valueB; $ssA += $valueA ** 2; $ssB += $valueB ** 2; } if ($ssA === 0.0 and $ssB === 0.0) { return 0.0; } if ($ssA === 0.0 or $ssB === 0.0) { return 2.0; } return 1.0 - ($sigma / sqrt($ssA * $ssB)); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'Cosine'; } }