-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.h
125 lines (102 loc) · 2.39 KB
/
check.h
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
/*
this is a check unit to check the clustering centers
*/
#include"flann/util/matrix.h"
#include<vector>
#include<cmath>
namespace flann{
template<typename T>
void show_vector(std::vector<T*> & m,int len);
bool checks_clustering_centers(std::vector<double*> ¢ers,
std::vector<double*> &feature,
std::vector<int> &indices,int len)
{
for(int i=0;i<indices.size();i++)
{
int pos = 0;
double dist = 0;
for(int j=0;j<len;j++)
{
double sub = (centers[0][j]-feature[i][j]);
dist += sub*sub;
}
//std::cout<<i<<" th"<<" pos: "<<indices[i]<<std::endl;
//std::cout<<0<<" dist: "<<dist<<std::endl;
for(int k=1;k<centers.size();k++)
{
double subdist = 0;
for(int j=0;j<len;j++)
{
double sub = centers[k][j] - feature[i][j];
subdist += sub*sub;
}
//std::cout<<k<<" dist: "<<subdist<<std::endl;
if(subdist < dist)
{
dist = subdist;
pos = k;
}
}
//std::cout<<"new pos: "<<pos<<std::endl;
if(pos != indices[i])
return false;
}
return true;
}
bool is_perfect_cluster(std::vector<double*> ¢ers,
std::vector<double*> &feature,
std::vector<int> &indices,int len)
{
std::vector<double*> dcenters(centers.size());
for(int i=0;i<centers.size();i++)
{
dcenters[i] = new double[len];
memset(dcenters[i],0,sizeof(double)*len);
}
std::vector<int> count(dcenters.size(),0);
for(int i=0;i<indices.size();i++)
{
int belong = indices[i];
count[belong]++;
for(int j=0;j<len;j++)
{
dcenters[belong][j] += feature[i][j];
}
}
for(int i=0;i<centers.size();i++){
double factor = 1.0/count[i];
for(int j=0;j<len;j++)
dcenters[i][j] *= factor;
}
//show_vector(centers, len);
//std::cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~`"<<std::endl;
//show_vector(dcenters, len);
for(int i=0;i<centers.size();i++)
for(int j=0;j<len;j++)
if(abs(dcenters[i][j]-centers[i][j])>1e-5)
return false;
for(int i=0;i<centers.size();i++)
delete[i] dcenters[i];
return true;
}
template<typename T>
void show_matrix(Matrix<T> & m)
{
for(int i=0;i<m.rows;i++)
{
for(int j=0;j<m.cols;j++)
std::cout<<m[i][j]<<' ';
std::cout<<std::endl;
}
}
template<typename T>
void show_vector(std::vector<T*> & m,int len)
{
for(int i=0;i<m.size();i++)
{
for(int j=0;j<len;j++)
std::cout<<m[i][j]<<' ';
std::cout<<std::endl;
}
}
}