MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / fast_symmetric_distance

Method fast_symmetric_distance

nodedb-codec/src/vector_quant/rabitq.rs:335–346  ·  view source on GitHub ↗

Symmetric distance estimate: both `q` and `v` are quantized. `approx_l2 = ‖v-c‖² + ‖q-c‖² − 2·‖v-c‖·‖q-c‖·(1 − 2·hamming/D)` The angular factor `1 − 2·hamming/D` approximates `cos(θ)` between the two sign-vectors. Error bound: O(1/√D) MSE.

(&self, q: &Self::Quantized, v: &Self::Quantized)

Source from the content-addressed store, hash-verified

333 /// The angular factor `1 − 2·hamming/D` approximates `cos(θ)` between
334 /// the two sign-vectors. Error bound: O(1/√D) MSE.
335 fn fast_symmetric_distance(&self, q: &Self::Quantized, v: &Self::Quantized) -> f32 {
336 let qh = q.0.header();
337 let vh = v.0.header();
338 let qb = q.0.packed_bits();
339 let vb = v.0.packed_bits();
340 let h = hamming_distance(qb, vb);
341 let dim = self.dim as f32;
342 let dot_estimate = 1.0 - 2.0 * h as f32 / dim;
343 let approx = qh.residual_norm * qh.residual_norm + vh.residual_norm * vh.residual_norm
344 - 2.0 * qh.residual_norm * vh.residual_norm * dot_estimate;
345 approx.max(0.0)
346 }
347
348 /// Asymmetric distance estimate: `q` is a prepared [`RaBitQQuery`], `v`
349 /// is a stored quantized vector.

Callers 1

Calls 3

packed_bitsMethod · 0.80
hamming_distanceFunction · 0.70
headerMethod · 0.45

Tested by 1