1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 from math import sqrt
36 import numpy as np
37
38
39
40 hamming_table = np.zeros(256,dtype=np.int32)
41 for i in range(256):
42 bits = (i&1>0) + (i&2>0) + (i&4>0) + (i&8>0) + (i&16>0) + (i&32>0) + (i&64>0) + (i&128>0)
43 hamming_table[i] = bits
44
45
47 "Convert a boolean vector to a ubyte vector which is much more space efficient."
48 assert isinstance(x,np.ndarray)
49 assert x.dtype in [np.bool]
50 assert len(x.shape) == 1
51 assert x.shape[0] % 8 == 0
52
53 out = 1*x[7::8] + \
54 2*x[6::8] + \
55 4*x[5::8] + \
56 8*x[4::8] + \
57 16*x[3::8] + \
58 32*x[2::8] + \
59 64*x[1::8] + \
60 128*x[0::8]
61
62 out = out.astype(np.ubyte)
63 return out
64
65
67 "Convert a byte vector to a bool vector."
68 assert isinstance(x,np.ndarray)
69 assert x.dtype in [np.ubyte]
70 assert len(x.shape) == 1
71
72 out = np.zeros(x.shape[0]*8,dtype=np.bool)
73
74 out[7::8] = 1&x > 0
75 out[6::8] = 2&x > 0
76 out[5::8] = 4&x > 0
77 out[4::8] = 8&x > 0
78 out[3::8] = 16&x > 0
79 out[2::8] = 32&x > 0
80 out[1::8] = 64&x > 0
81 out[0::8] = 128&x > 0
82
83 return out
84
85
87 if a.dtype == np.bool and b.dtype == bool:
88 return (a ^ b).sum()
89 elif a.dtype == np.ubyte and b.dtype == np.ubyte:
90 return hamming_table[a^b].sum()
91 else:
92 raise NotImplementedError("Unsupported array types %s and %s",a.dtype,b.dtype)
93
94
96 ''' Compute the l1 distance measure '''
97 return abs(a - b).sum()
98
99
101 ''' compute the l2 distance '''
102 d = (a - b)
103 return sqrt( (d*d).sum() )
104
105
107 ''' Compute the correlation of two vectors '''
108
109 a = a - a.mean()
110 b = b - b.mean()
111
112
113 a = (1.0/sqrt((a*a).sum()+0.000001))*a
114 b = (1.0/sqrt((b*b).sum()+0.000001))*b
115
116
117 c = (a*b).sum()
118
119 return c
120