hnsw-lite
Version:
A lightweight HNSW implementation for nearest neighbor search.
23 lines • 873 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateCosineSimilarity = void 0;
const calculateCosineSimilarity = (v1, v2) => {
if (v1.length !== v2.length) {
throw new Error("Vectors must have the same length.");
}
let dotProduct = 0;
let magnitudeV1 = 0;
let magnitudeV2 = 0;
for (let i = 0; i < v1.length; i++) {
dotProduct += v1[i] * v2[i];
magnitudeV1 += v1[i] * v1[i];
magnitudeV2 += v2[i] * v2[i];
}
const denominator = Math.sqrt(magnitudeV1) * Math.sqrt(magnitudeV2);
if (denominator === 0) {
throw new Error("One of the vectors has zero magnitude.");
}
return dotProduct / denominator; // Returns value between -1 and 1
};
exports.calculateCosineSimilarity = calculateCosineSimilarity;
//# sourceMappingURL=calculateCosineSimilarity.js.map