Given a highly sparse matrix representing user-item interactions, write a function to compute cosine similarity efficiently.