function cosineSimilarity(a, b) {
if (a.length !== b.length) {
throw new Error("Vectors must have the same dimensions");
}
const magA = magnitude(a);
const magB = magnitude(b);
if (magA === 0 || magB === 0) {
throw new Error(
"Magnitude of one of the vectors is zero, cannot calculate cosine similarity"
);
}
return dotProduct(a, b) / (magA * magB);
}