Module hnsw_rs::dist [−][src]
Expand description
Some standard distances as L1, L2, Cosine, Jaccard, Hamming and a structure to enable the user to implement its own distances. For the heavily used case (f32) we provide simd avx2 implementation.
Structs
A structure to implement Distance Api for type DistCFnPtr
It must be noted that this can be used in Julia via the macro @cfunction
to define interactiveley a distance function , compile it on the fly and sent it
to Rust via the init_hnsw_{f32, i32, u16, u32, u8} function
defined in libext
Cosine distance : implemented for f32, f64, i64, i32 , u16
This is essentially the Cosine distance but we suppose
all vectors (graph construction and request vectors have been l2 normalized to unity
BEFORE INSERTING in HNSW!.
No control is made, so it is the user responsability to send normalized vectors
everywhere in inserting and searching.
This structure is to let user define their own distance with closures.
Hamming distance. Implemented for u8, u16, u32, i32 and i16 The distance returned is normalized by length of slices, so it is between 0. and 1.
A structure to compute Hellinger distance between probalilities. Vector must be >= 0 and normalized to 1.
Jaccard distance. Implemented for u8, u16 , u32.
A structure to compute Jeffreys divergence between probalilities. If p and q are 2 probability distributions the “distance” is computed as: sum (p[i] - q[i]) * ln(p[i]/q[i])
Jensen-Shannon distance.
It is defined as the square root of the Jensen–Shannon divergence and is a metric.
Vector must be >= 0 and normalized to 1!
The distance computation does not check that.
L1 distance : implemented for i32, f64, i64, u32 , u16 , u8 and with Simd avx2 for f32
L2 distance : implemented for i32, f64, i64, u32 , u16 , u8 and with Simd avx2 for f32
Levenshtein distance. Implemented for u16
Special forbidden computation distance. It is associated to a unit NoData structure This is a special structure used when we want to only reload the graph from a previous computation possibly from an foreign language (and we do not have access to the original type of data from the foreign language).
Traits
This is the basic Trait describing a distance. The structure Hnsw can be instantiated by anything satisfying this Trait. The crate provides implmentations for L1, L2 , Cosine, Jaccard, Hamming. For other distances implement the trait possibly with the newtype pattern