This extension provides four functions to work with hex representation of feature vector.
Every function asserts that both arguments have the same length.
Computes absolute hamming distance between to FVs
SELECT hamming_distance('1d3f', '1110');
hamming_distance
------------------
7
(1 row)
This expands as
a = '1d3f' -> 0001 1101 0011 1111
b = '1110' -> 0001 0001 0001 0000
c = a ^ b -> 0000 1100 0010 1111
popcount(c) -> 0 + 2 + 1 + 4 = 7
SELECT hamming_similarity('1d3f', '1110');
hamming_similarity
--------------------
9
(1 row)
Can be defined as
HAMMING_SIMILARITY = len(a) * 4 - hamming_distance(a, b);
SELECT hamming_distance_normalized('1d3f', '1110');
hamming_distance_normalized
-----------------------------
0.4375
(1 row)
Can be defined as
HAMMING_DISTANCE_NORM = hamming_distance(a, b) / len(a) * 4;
SELECT hamming_similarity_normalized('1d3f', '1110');
hamming_similarity_normalized
-------------------------------
0.5625
(1 row)
Can be defined as
HAMMING_SIMILARITY_NORM = hamming_similarity(a, b) / len(a) * 4;
cd .../hamming_distance
sudo make install
make installcheck