inference-server
Version:
Libraries and server to build AI applications. Adapters to various native bindings allowing local inference. Integrate it with your application, or use as a microservice.
30 lines • 950 B
JavaScript
/**
* Calculates the dot product of two vectors.
* @param vecA - The first vector.
* @param vecB - The second vector.
* @returns The dot product of vecA and vecB.
*/
function dotProduct(vecA, vecB) {
return vecA.reduce((sum, value, index) => sum + value * vecB[index], 0);
}
/**
* Calculates the magnitude of a vector.
* @param vec - The vector.
* @returns The magnitude of the vector.
*/
function magnitude(vec) {
return Math.sqrt(vec.reduce((sum, value) => sum + value * value, 0));
}
/**
* Calculates the cosine similarity between two vectors.
* @param vecA - The first vector.
* @param vecB - The second vector.
* @returns The cosine similarity between vecA and vecB.
*/
export function cosineSimilarity(vecA, vecB) {
const dotProd = dotProduct(vecA, vecB);
const magnitudeA = magnitude(vecA);
const magnitudeB = magnitude(vecB);
return dotProd / (magnitudeA * magnitudeB);
}
//# sourceMappingURL=math.js.map