(a: number[], b: number[])
| 8 | } |
| 9 | |
| 10 | function 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 | |
| 21 | function buildAffinityMatrix(vectors: number[][]): Matrix { |
| 22 | const n = vectors.length; |
no outgoing calls
no test coverage detected