$v */ public static function encode(array $v): string { return base64_encode(pack('g*', ...$v)); } /** @return list */ public static function decode(string $s, int $dimension): array { $raw = base64_decode($s, true); if ($raw === false || strlen($raw) !== $dimension * 4) { throw new \UnexpectedValueException('Stored vector has the wrong dimension.'); } return array_values(unpack('g*', $raw)); } /** @param list $a @param list $b */ public static function dot(array $a, array $b): float { $s = 0.0; foreach ($a as $i => $x) { $s += $x * $b[$i]; } return $s; } }