MCPcopy Index your code
hub / github.com/forloopcodes/contextplus / cosine

Function cosine

src/core/clustering.ts:10–19  ·  view source on GitHub ↗
(a: number[], b: number[])

Source from the content-addressed store, hash-verified

8}
9
10function cosine(a: number[], b: number[]): number {
11 let dot = 0, normA = 0, normB = 0;
12 for (let i = 0; i < a.length; i++) {
13 dot += a[i] * b[i];
14 normA += a[i] * a[i];
15 normB += b[i] * b[i];
16 }
17 const denom = Math.sqrt(normA) * Math.sqrt(normB);
18 return denom === 0 ? 0 : dot / denom;
19}
20
21function buildAffinityMatrix(vectors: number[][]): Matrix {
22 const n = vectors.length;

Callers 1

buildAffinityMatrixFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected