Skip to main content

Module distance

Module distance 

Source
Expand description

Distance metrics for vector search.

Three metrics are supported: Distance::Euclidean (L2), Distance::Cosine, and Distance::DotProduct. All routines operate on f32 slices and run in scalar code; portable SIMD is not used because the workspace forbids unsafe_code and std::simd is still nightly-only.

For ANN search, smaller scores mean closer. Cosine and euclidean already have that orientation; dot product is negated so the same comparator works across metrics.

§Examples

use dynvec::distance::Distance;
let a = [1.0, 0.0, 0.0];
let b = [0.0, 1.0, 0.0];
assert!((Distance::Euclidean.score(&a, &b) - 2.0_f32.sqrt()).abs() < 1e-6);
assert!((Distance::Cosine.score(&a, &b) - 1.0).abs() < 1e-6);

Enums§

Distance
A distance / similarity metric over f32 vectors.