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

Function distance

nodedb-vector/src/distance/compute.rs:12–33  ·  view source on GitHub ↗
(a: &[f32], b: &[f32], metric: DistanceMetric)

Source from the content-addressed store, hash-verified

10/// falls back to scalar implementations on other architectures.
11#[inline]
12pub fn distance(a: &[f32], b: &[f32], metric: DistanceMetric) -> f32 {
13 assert_eq!(
14 a.len(),
15 b.len(),
16 "distance: length mismatch (a.len()={}, b.len()={})",
17 a.len(),
18 b.len()
19 );
20 let rt = simd::runtime();
21 match metric {
22 DistanceMetric::L2 => (rt.l2_squared)(a, b),
23 DistanceMetric::Cosine => (rt.cosine_distance)(a, b),
24 DistanceMetric::InnerProduct => (rt.neg_inner_product)(a, b),
25 DistanceMetric::Manhattan => manhattan(a, b),
26 DistanceMetric::Chebyshev => chebyshev(a, b),
27 DistanceMetric::Hamming => hamming_f32(a, b),
28 DistanceMetric::Jaccard => jaccard(a, b),
29 DistanceMetric::Pearson => pearson(a, b),
30 // DistanceMetric is #[non_exhaustive]; unknown future variants fall back to L2.
31 _ => (rt.l2_squared)(a, b),
32 }
33}
34
35/// Batch distance: compute distances from `query` to each candidate.
36///

Callers 1

batch_distancesFunction · 0.70

Calls 6

manhattanFunction · 0.85
chebyshevFunction · 0.85
hamming_f32Function · 0.85
jaccardFunction · 0.85
pearsonFunction · 0.85
runtimeFunction · 0.50

Tested by

no test coverage detected