34 #define DELTA_ERR_MAX 0.1
39 typedef struct cell_s {
64 for (i=0; i<
dim; i++) {
65 dist += (a[i] - b[i])*(a[i] - b[i]);
80 memcpy(res, vect, dim*
sizeof(
int));
87 for (; cells; cells=cells->
next)
95 int i, pick=0, diff, diff_min = INT_MAX;
96 for (i=0; i<elbg->
numCB; i++)
99 if (diff < diff_min) {
140 int numpoints[2] = {0,0};
141 int *newcentroid[2] = {
147 memset(newcentroid[0], 0, 2 * dim *
sizeof(*newcentroid[0]));
152 for (tempcell = cells; tempcell; tempcell=tempcell->
next) {
156 for (i=0; i<
dim; i++)
157 newcentroid[idx][i] += points[tempcell->
index*dim + i];
160 vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
161 vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
163 for (tempcell = cells; tempcell; tempcell=tempcell->
next) {
166 int idx = dist[0] > dist[1];
167 newutility[idx] += dist[idx];
170 return newutility[0] + newutility[1];
177 int *
min = newcentroid_i;
178 int *max = newcentroid_p;
181 for (i=0; i< elbg->
dim; i++) {
186 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->
next)
187 for(i=0; i<elbg->
dim; i++) {
192 for (i=0; i<elbg->
dim; i++) {
193 int ni = min[i] + (max[i] - min[i])/3;
194 int np = min[i] + (2*(max[i] - min[i]))/3;
195 newcentroid_i[i] = ni;
196 newcentroid_p[i] = np;
218 *pp = elbg->
cells[indexes[0]];
220 elbg->
cells[indexes[0]] = NULL;
221 tempdata = elbg->
cells[indexes[1]];
222 elbg->
cells[indexes[1]] = NULL;
227 newcentroid[0], elbg->
dim, INT_MAX) >
229 newcentroid[1], elbg->
dim, INT_MAX);
231 tempdata->
next = elbg->
cells[indexes[idx]];
232 elbg->
cells[indexes[idx]] = tempdata;
233 tempdata = tempcell2;
242 for (i=0; i < elbg->
numCB; i++) {
254 elbg->
utility[idx] = newutility;
255 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->
next)
268 int j, k, olderror=0, newerror, cont=0;
270 int *newcentroid[3] = {
278 olderror += elbg->
utility[idx[j]];
280 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
283 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->
next) {
285 for (j=0; j<elbg->
dim; j++)
296 newerror = newutility[2];
299 elbg->
cells[idx[1]]);
301 if (olderror > newerror) {
304 elbg->
error += newerror - olderror;
322 for (idx[0]=0; idx[0] < elbg->
numCB; idx[0]++)
330 if (idx[1] != idx[0] && idx[1] != idx[2])
335 #define BIG_PRIME 433494437LL
338 int numCB,
int max_steps,
int *closest_cb,
343 if (numpoints > 24*numCB) {
347 for (i=0; i<numpoints/8; i++) {
349 memcpy(temp_points + i*dim, points + k*dim, dim*
sizeof(
int));
352 avpriv_init_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state);
353 avpriv_do_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state);
358 for (i=0; i < numCB; i++)
359 memcpy(codebook + i*dim, points + ((i*
BIG_PRIME)%numpoints)*dim,
365 int numCB,
int max_steps,
int *closest_cb,
371 int i, j, k, last_error, steps=0;
376 int best_dist, best_idx = 0;
378 elbg->
error = INT_MAX;
392 free_cells = list_buffer;
393 last_error = elbg->
error;
395 memset(elbg->
utility, 0, numCB*
sizeof(
int));
396 memset(elbg->
cells, 0, numCB*
sizeof(
cell *));
402 for (i=0; i < numpoints; i++) {
404 for (k=0; k < elbg->
numCB; k++) {
406 if (dist < best_dist) {
412 dist_cb[i] = best_dist;
413 elbg->
error += dist_cb[i];
415 free_cells->
index = i;
423 memset(size_part, 0, numCB*
sizeof(
int));
427 for (i=0; i < numpoints; i++) {
429 for (j=0; j < elbg->
dim; j++)
434 for (i=0; i < elbg->
numCB; i++)
439 (steps < max_steps));