Go · Set Similarity

Set Similarity

import "github.com/ashvardanian/NumKong" 8.0.0-rc.1 · b1f866ab

Hamming and Jaccard distances for binary and set-hash vectors.

// Byte-level Hamming distance
a := []uint8{1, 2, 3, 4}
b := []uint8{1, 0, 3, 5}
dist := nk.HammingU8(a, b) // 2

// Bit-level Hamming distance (packed binary vectors)
x := []byte{0xFF}
y := []byte{0x0F}
bits := nk.HammingU1(x, y, 8) // 4

// Bit-level Jaccard distance
jd := nk.JaccardU1(x, y, 8) // 0.5

// Set-hash Jaccard (MinHash-style)
h16a := []uint16{1, 2, 3, 4}
h16b := []uint16{3, 4, 5, 6}
nk.JaccardU16(h16a, h16b)

h32a := []uint32{10, 20, 30}
h32b := []uint32{30, 40, 50}
nk.JaccardU32(h32a, h32b)

API

Functions

HammingU1HammingU1 computes the Hamming distance between two binary vectors of depth dimensions, a multiple of 8, and both slices hold at least [DimensionsToValues]("u1", depth) bytes.
numkong.go:28 ↗
func HammingU1(a, b []byte, depth int) uint32
HammingU8HammingU8 counts the positions at which two uint8 vectors differ.
numkong.go:14 ↗
func HammingU8(a, b []uint8) uint32

Both slices must have the same length.

JaccardU1JaccardU1 computes the Jaccard distance between two binary vectors of depth dimensions, a multiple of 8, and both slices hold at least [DimensionsToValues]("u1", depth) bytes.
numkong.go:44 ↗
func JaccardU1(a, b []byte, depth int) float32
JaccardU16JaccardU16 computes the Jaccard distance between two uint16 set-hash vectors.
numkong.go:60 ↗
func JaccardU16(a, b []uint16) float32

Both slices must have the same length.

JaccardU32JaccardU32 computes the Jaccard distance between two uint32 set-hash vectors.
numkong.go:74 ↗
func JaccardU32(a, b []uint32) float32

Both slices must have the same length.