pub trait BinarySimilarity: Hamming + Jaccard Implementors
impl<Scalar: Hamming + Jaccard> BinarySimilarity for Scalar
Packed-binary metrics work on packed words instead of boolean slices. That is the right model once the workload is “semantic hash” rather than “array of booleans”.
use numkong::{Dot, Hamming, Jaccard, u1x8};
let a = [u1x8(0b10101010), u1x8(0b11110000)];
let b = [u1x8(0b10101110), u1x8(0b11000000)];
let hamming = u1x8::hamming(&a, &b).unwrap();
let jaccard = u1x8::jaccard(&a, &b).unwrap();
let overlap = u1x8::dot(&a, &b).unwrap(); // set bits in commonDot on u1x8 is the intersection count Jaccard normalises, so a ranking pass can skip the division.
Integer set Jaccard works on sorted arrays of integer identifiers.
let set_a = [1_u32, 3, 5, 7, 9];
let set_b = [3_u32, 5, 8, 9, 10];
let jaccard_sets = u32::jaccard(&set_a, &set_b).unwrap();
assert!(jaccard_sets > 0.0 && jaccard_sets < 1.0); // |A ∩ B| / |A ∪ B|BinarySimilarityBinarySimilarity bundles binary distance metrics: Hamming and Jaccard.pub trait BinarySimilarity: Hamming + Jaccard impl<Scalar: Hamming + Jaccard> BinarySimilarity for ScalarHammingComputes the Hamming distance between two binary vectors.2pub trait Hamming: StorageElement Counts differing bits for u1x8, or differing bytes for u8.
Range: [0, n]. Fails with Error::ShapeMismatch if lengths differ, or
Error::KernelFailed carrying the kernel's status.
Implemented for: u1x8, u8.
OutputResult type of the Hamming distance.type Output; hammingComputes the Hamming distance between two equally long slices.fn hamming(a: &[Self], b: &[Self]) -> Result<Self::Output, Error> JaccardComputes the Jaccard distance between two sets represented as bit/integer vectors.2pub trait Jaccard: StorageElement d = 1 − |A ∩ B| / |A ∪ B|
Range: [0, 1]. Fails with Error::ShapeMismatch if lengths differ, or
Error::KernelFailed carrying the kernel's status.
Implemented for: u1x8, u16, u32.
OutputResult type of the Jaccard distance.type Output; jaccardComputes the Jaccard distance between two equally long slices.fn jaccard(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>