# Author: Junjun # Date: 2025/9/23 import math def cosine_similarity(vec_a, vec_b): if len(vec_a) != len(vec_b): raise ValueError("The vector dimension must be the same") dot_product = sum(a * b for a, b in zip(vec_a, vec_b)) norm_a = math.sqrt(sum(a * a for a in vec_a)) norm_b = math.sqrt(sum(b * b for b in vec_b)) if norm_a == 0 or norm_b == 0: return 0.0 return dot_product / (norm_a * norm_b)