(query: number[], q: QuantizedVec)
| 83 | * Running the dot product in a tight loop over the Int8Array is the hot path. |
| 84 | */ |
| 85 | export function cosineQuantized(query: number[], q: QuantizedVec): number { |
| 86 | const { scale, bytes } = q; |
| 87 | const len = Math.min(query.length, bytes.length); |
| 88 | let dot = 0, nq = 0, ns = 0; |
| 89 | for (let i = 0; i < len; i++) { |
| 90 | const s = bytes[i]! * scale; |
| 91 | dot += query[i]! * s; |
| 92 | nq += query[i]! * query[i]!; |
| 93 | ns += s * s; |
| 94 | } |
| 95 | if (nq === 0 || ns === 0) return 0; |
| 96 | return dot / (Math.sqrt(nq) * Math.sqrt(ns)); |
| 97 | } |
| 98 | |
| 99 | // ── Build / open / search ───────────────────────────────────────────────────── |
| 100 |
no outgoing calls
no test coverage detected