-
Notifications
You must be signed in to change notification settings - Fork 229
/
splittree.cpp
268 lines (222 loc) · 7.12 KB
/
splittree.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
#include <cmath>
#include <cfloat>
#include <cstdlib>
#include <cstdio>
#include "splittree.h"
// Checks whether a point lies in a cell
bool Cell::containsPoint(double point[])
{
for (int i = 0; i< n_dims; ++i) {
if (abs_d(center[i] - point[i]) > width[i]) {
return false;
}
}
return true;
}
// Default constructor for quadtree -- build tree, too!
SplitTree::SplitTree(double* inp_data, int N, int no_dims)
{
QT_NO_DIMS = no_dims;
num_children = 1 << no_dims;
// Compute mean, width, and height of current map (boundaries of SplitTree)
double* mean_Y = new double[QT_NO_DIMS];
for (int d = 0; d < QT_NO_DIMS; d++) {
mean_Y[d] = .0;
}
double* min_Y = new double[QT_NO_DIMS];
for (int d = 0; d < QT_NO_DIMS; d++) {
min_Y[d] = DBL_MAX;
}
double* max_Y = new double[QT_NO_DIMS];
for (int d = 0; d < QT_NO_DIMS; d++) {
max_Y[d] = -DBL_MAX;
}
for (int n = 0; n < N; n++) {
for (int d = 0; d < QT_NO_DIMS; d++) {
mean_Y[d] += inp_data[n * QT_NO_DIMS + d];
min_Y[d] = min(min_Y[d], inp_data[n * QT_NO_DIMS + d]);
max_Y[d] = max(max_Y[d], inp_data[n * QT_NO_DIMS + d]);
}
}
double* width_Y = new double[QT_NO_DIMS];
for (int d = 0; d < QT_NO_DIMS; d++) {
mean_Y[d] /= (double) N;
width_Y[d] = max(max_Y[d] - mean_Y[d], mean_Y[d] - min_Y[d]) + 1e-5;
}
// Construct SplitTree
init(NULL, inp_data, mean_Y, width_Y);
fill(N);
delete[] max_Y; delete[] min_Y;
}
// Constructor for SplitTree with particular size and parent (do not fill the tree)
SplitTree::SplitTree(SplitTree* inp_parent, double* inp_data, double* mean_Y, double* width_Y)
{
QT_NO_DIMS = inp_parent->QT_NO_DIMS;
num_children = 1 << QT_NO_DIMS;
init(inp_parent, inp_data, mean_Y, width_Y);
}
// Main initialization function
void SplitTree::init(SplitTree* inp_parent, double* inp_data, double* mean_Y, double* width_Y)
{
// parent = inp_parent;
data = inp_data;
is_leaf = true;
size = 0;
cum_size = 0;
boundary.center = mean_Y;
boundary.width = width_Y;
boundary.n_dims = QT_NO_DIMS;
index[0] = 0;
center_of_mass = new double[QT_NO_DIMS];
for (int i = 0; i < QT_NO_DIMS; i++) {
center_of_mass[i] = .0;
}
}
// Destructor for SplitTree
SplitTree::~SplitTree()
{
for(unsigned int i = 0; i != children.size(); i++) {
delete children[i];
}
delete[] center_of_mass;
}
// Insert a point into the SplitTree
bool SplitTree::insert(int new_index)
{
// Ignore objects which do not belong in this quad tree
double* point = data + new_index * QT_NO_DIMS;
if (!boundary.containsPoint(point)) {
return false;
}
// Online update of cumulative size and center-of-mass
cum_size++;
double mult1 = (double) (cum_size - 1) / (double) cum_size;
double mult2 = 1.0 / (double) cum_size;
for (int d = 0; d < QT_NO_DIMS; d++) {
center_of_mass[d] = center_of_mass[d] * mult1 + mult2 * point[d];
}
// If there is space in this quad tree and it is a leaf, add the object here
if (is_leaf && size < QT_NODE_CAPACITY) {
index[size] = new_index;
size++;
return true;
}
// Don't add duplicates for now (this is not very nice)
bool any_duplicate = false;
for (int n = 0; n < size; n++) {
bool duplicate = true;
for (int d = 0; d < QT_NO_DIMS; d++) {
if (point[d] != data[index[n] * QT_NO_DIMS + d]) { duplicate = false; break; }
}
any_duplicate = any_duplicate | duplicate;
}
if (any_duplicate) {
return true;
}
// Otherwise, we need to subdivide the current cell
if (is_leaf) {
subdivide();
}
// Find out where the point can be inserted
for (int i = 0; i < num_children; ++i) {
if (children[i]->insert(new_index)) {
return true;
}
}
// Otherwise, the point cannot be inserted (this should never happen)
// printf("%s\n", "No no, this should not happen");
return false;
}
int *get_bits(int n, int bitswanted){
int *bits = new int[bitswanted];
int k;
for(k=0; k<bitswanted; k++) {
int mask = 1 << k;
int masked_n = n & mask;
int thebit = masked_n >> k;
bits[k] = thebit;
}
return bits;
}
// Create four children which fully divide this cell into four quads of equal area
void SplitTree::subdivide() {
// Create children
double* new_centers = new double[2 * QT_NO_DIMS];
for(int i = 0; i < QT_NO_DIMS; ++i) {
new_centers[i*2] = boundary.center[i] - .5 * boundary.width[i];
new_centers[i*2 + 1] = boundary.center[i] + .5 * boundary.width[i];
}
for (int i = 0; i < num_children; ++i) {
int *bits = get_bits(i, QT_NO_DIMS);
double* mean_Y = new double[QT_NO_DIMS];
double* width_Y = new double[QT_NO_DIMS];
// fill the means and width
for (int d = 0; d < QT_NO_DIMS; d++) {
mean_Y[d] = new_centers[d*2 + bits[d]];
width_Y[d] = .5*boundary.width[d];
}
SplitTree* qt = new SplitTree(this, data, mean_Y, width_Y);
children.push_back(qt);
delete[] bits;
}
delete[] new_centers;
// Move existing points to correct children
for (int i = 0; i < size; i++) {
// bool flag = false;
for (int j = 0; j < num_children; j++) {
if (children[j]->insert(index[i])) {
// flag = true;
break;
}
}
// if (flag == false) {
index[i] = -1;
// }
}
// This node is not leaf now
// Empty it
size = 0;
is_leaf = false;
}
// Build SplitTree on dataset
void SplitTree::fill(int N)
{
for (int i = 0; i < N; i++) {
insert(i);
}
}
// Compute non-edge forces using Barnes-Hut algorithm
void SplitTree::computeNonEdgeForces(int point_index, double theta, double* neg_f, double* sum_Q)
{
// Make sure that we spend no time on empty nodes or self-interactions
if (cum_size == 0 || (is_leaf && size == 1 && index[0] == point_index)) {
return;
}
// Compute distance between point and center-of-mass
double D = .0;
int ind = point_index * QT_NO_DIMS;
for (int d = 0; d < QT_NO_DIMS; d++) {
double t = data[ind + d] - center_of_mass[d];
D += t * t;
}
// Check whether we can use this node as a "summary"
double m = -1;
for (int i = 0; i < QT_NO_DIMS; ++i) {
m = max(m, boundary.width[i]);
}
if (is_leaf || m / sqrt(D) < theta) {
// Compute and add t-SNE force between point and current node
double Q = 1.0 / (1.0 + D);
*sum_Q += cum_size * Q;
double mult = cum_size * Q * Q;
for (int d = 0; d < QT_NO_DIMS; d++) {
neg_f[d] += mult * (data[ind + d] - center_of_mass[d]);
}
}
else {
// Recursively apply Barnes-Hut to children
for (int i = 0; i < num_children; ++i) {
children[i]->computeNonEdgeForces(point_index, theta, neg_f, sum_Q);
}
}
}