-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruteforce.c
106 lines (83 loc) · 1.86 KB
/
bruteforce.c
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
#include <stdio.h>
#include <float.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <xmmintrin.h>
#define MATRIX float*
#define VECTOR float*
float distance(VECTOR x1,VECTOR x2,int d){
float diff,sum = 0;
int i;
// Si lavora su gruppi di 2
for(i=0; i<=d-2; i+=2){
diff = x1[i]-x2[i];
sum += diff*diff;
diff = x1[i+1]-x2[i+1];
sum += diff*diff;
}
// Processiamo un eventuale residual_centroids
for(i;i<d;i++){
diff = x1[i]-x2[i];
sum += diff*diff;
}
return sum;
// TESTATA
}//distance
void brute_force(float* ds, float* qs, int n,int nq, int d, int knn){
float* v1=malloc(d*sizeof(float));
float* v2=malloc(d*sizeof(float));
int* NN=malloc(nq*knn*sizeof(float));
int l;
float dist;
for(int i=0;i<nq;i++){ // punto del qs i
for(int j=0;j<d;j++){
v1[j]=qs[i*d+j];
}
VECTOR distances = (VECTOR) malloc(knn*sizeof(float));
for(int j=0;j<knn;j++)
distances[j] = FLT_MAX;
for(int j=0;j<n;j++){ // punto del ds j
for(int k=0;k<d;k++){
v2[k]=ds[j*d+k];
}
dist=distance(v1,v2,d);
//printf("Distanza al QUADRATO tra la query %d ed il punto del dataset %d: %f\n",i,j, dist);
if(distances[0]>dist){
l = 0;
while(l<knn-1 && distances[l+1]>dist){
distances[l] = distances [l+1];
NN[i*knn+l] = NN[i*knn+l+1];
l++;
}// while
distances[l] = dist;
NN[i*knn+l] = j;
}// if
}
}
for(int i=0;i<nq;i++){
printf("I %d punti più vicini alla query %d sono: ",knn,i);
for(int j=0;j<knn;j++){
printf("%d\t",NN[i*knn+j]);
}
printf("\n");
}
}
/*
int main() {
int n=4;
int d=2;
int nq=2;
int knn=2;
float* ds=malloc(n*d*sizeof(float));
float* qs=malloc(nq*d*sizeof(float));
ds[0]=1.0; ds[1]=0.0;
ds[2]=3.3; ds[3]=4.0;
ds[4]=2.0; ds[5]=5.0;
ds[6]=7,0; ds[7]=3.0;
qs[0]=0.5; qs[1]=1.0;
qs[2]=3.0; qs[3]=2.5;
brute_force(ds,qs,n,nq,d,knn);
}
*/