-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.cpp
285 lines (259 loc) · 6.44 KB
/
util.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "util.h"
using namespace std;
// -----------------------------------------------------------------------------
int read_data( // read data/query set from disk
int n, // number of data/query objects
int d, // dimensionality
const char *fname, // address of data/query set
float **data) // data/query objects (return)
{
FILE *fp = fopen(fname, "r");
if (!fp) {
printf("Could not open %s\n", fname);
return 1;
}
int i = 0;
int tmp = -1;
while (!feof(fp) && i < n) {
fscanf(fp, "%d", &tmp);
for (int j = 0; j < d; ++j) {
fscanf(fp, " %f", &data[i][j]);
}
// fscanf(fp, "\n");
++i;
}
// assert(feof(fp) && i == n);
fclose(fp);
return 0;
}
// -----------------------------------------------------------------------------
int read_ground_truth( // read ground truth results from disk
int qn, // number of query objects
const char *fname, // address of truth set
Result **R) // ground truth results (return)
{
FILE *fp = fopen(fname, "r");
if (!fp) {
printf("Could not open %s\n", fname);
return 1;
}
int tmp1 = -1;
int tmp2 = -1;
fscanf(fp, "%d %d\n", &tmp1, &tmp2);
// assert(tmp1 == qn && tmp2 == MAXK);
assert(tmp2 == MAXK);
for (int i = 0; i < qn; ++i) {
for (int j = 0; j < MAXK; ++j) {
fscanf(fp, "%d %f ", &R[i][j].id_, &R[i][j].key_);
}
fscanf(fp, "\n");
}
fclose(fp);
return 0;
}
// -----------------------------------------------------------------------------
float calc_inner_product( // calc inner product
int dim, // dimension
const float *p1, // 1st point
const float *p2) // 2nd point
{
double ret = 0.0f;
for (int i = 0; i < dim; ++i) {
ret += (p1[i] * p2[i]);
}
return ret;
}
// -----------------------------------------------------------------------------
float calc_angle( // calc angle
int dim, // dimension
const float *p1, // 1st point
const float *p2) // 2nd point
{
return acos(calc_cosangle(dim, p1, p2));
}
// -----------------------------------------------------------------------------
float calc_cosangle( // calc cos(angle)
int dim, // dimension
const float *p1, // 1st point
const float *p2) // 2nd point
{
double ret = 0.0f;
double norm0 = 0., norm1 = 0.;
for (int i = 0; i < dim; ++i) {
ret += (p1[i] * p2[i]);
norm0 += p1[i] * p1[i];
norm1 += p2[i] * p2[i];
}
if(norm0==0 || norm1==0){
return 0;
}
return ret/sqrt(norm0*norm1);
}
// -----------------------------------------------------------------------------
float calc_l2_sqr( // calc L2 square distance
int dim, // dimension
const float *p1, // 1st point
const float *p2) // 2nd point
{
float diff = 0.0f;
float ret = 0.0f;
for (int i = 0; i < dim; ++i) {
diff = p1[i] - p2[i];
ret += diff * diff;
}
return ret;
}
// -----------------------------------------------------------------------------
float calc_l2_dist( // calc L2 distance
int dim, // dimension
const float *p1, // 1st point
const float *p2) // 2nd point
{
return sqrt(calc_l2_sqr(dim, p1, p2));
}
// -----------------------------------------------------------------------------
float calc_l1_dist( // calc L1 distance
int dim, // dimension
const float *p1, // 1st point
const float *p2) // 2nd point
{
float ret = 0.0f;
for (int i = 0; i < dim; ++i) {
ret += fabs(p1[i] - p2[i]);
}
return ret;
}
// -----------------------------------------------------------------------------
float calc_recall( // calc recall (percentage)
int k, // top-k value
const Result *R, // ground truth results
MaxK_List *list) // results returned by algorithms
{
int i = list->size()-1;
int last = k - 1;
//loop until list->ithkey >= R[last].key
while (i >= 0 && R[last].key_ - list->ith_key(i) > EPS) {
i--;
}
return (i + 1) * 100.0f / k;
}
// -----------------------------------------------------------------------------
float calc_recall( // calc recall (percentage)
int k, // top-k value
const Result *R, // ground truth results
MinK_List *list) // results returned by algorithms
{
int i = list->size()-1;
int last = k - 1;
//loop until list->ithkey <= R[last].key
while (i >= 0 && R[last].key_ - list->ith_key(i) < -EPS) {
i--;
}
return (i + 1) * 100.0f / k;
}
// -----------------------------------------------------------------------------
int get_hits( // get the number of hits between two ID list
int k, // top-k value
int t, // top-t value
const Result *R, // ground truth results
MaxK_List *list) // results returned by algorithms
{
int i = k - 1;
int last = t - 1;
while (i >= 0 && R[last].key_ - list->ith_key(i) > EPS) {
i--;
}
return min(t, i + 1);
}
float calc_weighted_dist2( // calc inner product
int dim, // dimension
const float *w,
const float *p1, // 1st point
const float *p2) // 2nd point
{
double ret = 0.0f;
for (int i = 0; i < dim; ++i) {
ret += w[i]*(p1[i]-p2[i])*(p1[i]-p2[i]);
}
return ret;
}
int calc_hamming_dist( // calc inner product
int dim,
const uint8_t *p1, // 1st point
const uint8_t *p2) // 2nd point
{
int tail = dim%8;
int ret = 0;
for(int i=0;i<tail;i++){
ret += get_num_bits8(p1[i]^p2[i]);
}
ret += calc_hamming_dist(dim/8, (const uint64_t*)(p1+tail), (const uint64_t*)(p2+tail));
return ret;
}
int calc_hamming_dist( // calc inner product
int dim,
const uint64_t *p1, // 1st point
const uint64_t *p2) // 2nd point
{
int ret = 0;
for(int i=0;i<dim;i++){
ret += get_num_bits64(p1[i]^p2[i]);
}
return ret;
}
float calc_ratio(
int k,
const Result *Rs,
MinK_List *list)
{
if(list->size()<k){
return sqrt(1e9);
}
double ret = (list->ith_key(k-1)+1e-9) / (Rs[k-1].key_+1e-9);
if(ret<0){
ret = 1e9;
} else if(ret<1){
ret = 1/ret;
}
return sqrt(ret);
}
float calc_ratio(
int k,
const Result *Rs,
MaxK_List *list)
{
if(list->size()<k){
return sqrt(1e9);
}
double ret = (list->ith_key(k-1)+1e-9) / (Rs[k-1].key_+1e-9);
if(ret<0){
ret = 1e9;
} else if(ret<1){
ret = 1/ret;
}
return sqrt(ret);
}
void calc_min_max(
int n,
int qn,
int d,
const float** data,
const float** query,
float& maxx,
float& minx)
{
maxx=0;
minx=1e10;
for(int i=0;i<n;i++){
for(int j=0;j<d;j++){
maxx = std::max(data[i][j], maxx);
minx = std::min(data[i][j], minx);
}
}
for(int i=0;i<qn;i++){
for(int j=0;j<d;j++){
maxx = std::max(query[i][j], maxx);
minx = std::min(query[i][j], minx);
}
}
}