21static double dist(
int dim,
double *x,
double *y){
24 for (k = 0; k <
dim; k++) d += (x[k] - y[k])*(x[k]-y[k]);
33 for (i = 0; i < k; i++){
42 for (i = 0; i < k; i++){
56void
furtherest_point(
int k,
int dim,
double *wgt,
double *pts,
double *
center,
double width,
int max_level,
double *dist_max,
double **argmax){
84 for (
int i = 0; i < k; i++) wmax =
MAX(wgt[i], wmax);
92 if (!(*argmax)) *argmax =
gv_calloc(
dim,
sizeof(
double));
93 memcpy(*argmax,
center,
sizeof(
double)*
dim);
95 qt_list_t candidates = {0};
96 qt_list_t candidates2 = {0};
97 qt_list_append(&candidates, qt);
102 while (level++ < max_level){
104 fprintf(stderr,
"level=%d=================\n", level);
106 qt_list_clear(&candidates2);
107 for (
size_t i = 0; i < qt_list_size(&candidates); i++){
110 qt = qt_list_get(&candidates, i);
114 fprintf(stderr,
"candidate %" PRISIZE_T " at {", i);
115 for (j = 0; j <
dim; j++) fprintf(stderr,
"%f, ", qt->
center[j]);
122 for (ii = 0; ii < 1<<
dim; ii++) {
129 fprintf(stderr,
"new distmax=%f, pt={", *dist_max);
130 for (j = 0; j <
dim; j++) fprintf(stderr,
"%f, ", qt->
qts[ii]->
center[j]);
131 fprintf(stderr,
"}\n");
133 memcpy(*argmax, qt->
qts[ii]->
center,
sizeof(
double)*
dim);
138 qt_list_append(&candidates2, qt->
qts[ii]);
144 qt_list_t ctmp = candidates;
145 candidates = candidates2;
152 qt_list_free(&candidates);
153 qt_list_free(&candidates2);
157 double *dist_max,
double **argmax){
189 for (
int i = 0; i < k; i++) wmax =
MAX(wgt[i], wmax);
196 if (!(*argmax)) *argmax =
gv_calloc(
dim,
sizeof(
double));
197 memcpy(*argmax, average,
sizeof(
double)*
dim);
199 qt_list_t candidates = {0};
200 qt_list_t candidates2 = {0};
201 qt_list_append(&candidates, qt);
206 while (level++ < max_level){
208 fprintf(stderr,
"level=%d=================\n", level);
210 qt_list_clear(&candidates2);
211 for (
size_t i = 0; i < qt_list_size(&candidates); i++){
212 qt = qt_list_get(&candidates, i);
215 fprintf(stderr,
"candidate %" PRISIZE_T " at {", i);
216 for (j = 0; j <
dim; j++) fprintf(stderr,
"%f, ", qt->
center[j]);
221 if (qt->
n == 1 ||
distance + wmax*2*sqrt(((
double)
dim))*qt->
width < *dist_max)
continue;
224 if (!(qt->
qts))
continue;
226 for (ii = 0; ii < 1<<
dim; ii++) {
227 if (!(qt->
qts[ii]))
continue;
233 fprintf(stderr,
"new distmax=%f, pt={", *dist_max);
234 for (j = 0; j <
dim; j++) fprintf(stderr,
"%f, ", qt->
qts[ii]->
average[j]);
235 fprintf(stderr,
"}\n");
242 qt_list_append(&candidates2, qt->
qts[ii]);
248 qt_list_t ctmp = candidates;
249 candidates = candidates2;
254 qt_list_free(&candidates);
255 qt_list_free(&candidates2);
QuadTree QuadTree_new_in_quadrant(int dim, double *center, double width, int max_level, int i)
QuadTree QuadTree_new(int dim, double *center, double width, int max_level)
void QuadTree_delete(QuadTree q)
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
void furtherest_point_in_list(int k, int dim, double *wgt, double *pts, QuadTree qt, int max_level, double *dist_max, double **argmax)
static double dist(int dim, double *x, double *y)
static double distance_to_group(int k, int dim, double *wgt, double *pts, double *center)
void furtherest_point(int k, int dim, double *wgt, double *pts, double *center, double width, int max_level, double *dist_max, double **argmax)
double distance(double *x, int dim, int i, int j)
#define DEFINE_LIST(name, type)
#define PRISIZE_T
PRIu64 alike for printing size_t
static point center(point vertex[], size_t n)