import math


def cosine_similarity(v1, v2):
    """ 
    compute cosine similarity of v1 to v2: (v1 dot v2)/{||v1||*||v2||)
    """
    sumxx, sumxy, sumyy = 0, 0, 0
    for i in range(len(v1)):
        x = v1[i]
        y = v2[i]
        sumxx += x*x
        sumyy += y*y
        sumxy += x*y
    return 1.0 - sumxy / math.sqrt(sumxx * sumyy)