cactus183/patchbench-dev
0
1import math2 3 4def magnitude(v):5 """Return the magnitude (Euclidean norm) of vector v."""6 return math.sqrt(sum(x * x for x in v))7 8 9def normalize(v):10 """Return the unit vector in the same direction as v."""11 # BUG: divides by sum of components instead of magnitude12 s = sum(v)13 if s == 0:14 return [0.0] * len(v)15 return [x / s for x in v]16 17 18def cosine_similarity(a, b):19 """Return the cosine similarity between vectors a and b."""20 na = normalize(a)21 nb = normalize(b)22 return sum(x * y for x, y in zip(na, nb))23 