int mergecb(float *B,float **cb,unsigned char *P,int npt,int N,float thresh,int dim) { int i,j,newN,*count,l,ei,debug=0; float **dist; if (N==1) return N; count=(int *)malloc(sizeof(int)*N); for (l=0;l<npt;l++) count[P[l]]++; dist=(float **)fmatrix(N,N); ei = N-1; for (i=0;i<ei;i++) { for (j=i+1;j<N;j++) { dist[i][j] = distance2(cb[i],cb[j],dim); dist[j][i] = dist[i][j]; } } if (debug) printf("thresh %f ",thresh); newN=mergecb1(dist,B,cb,N,thresh,dim,count); free_fmatrix(dist,N); free(count); return newN; }
int mergecb(float *B,float **cb,unsigned char *P,int npt,int N,float thresh,int dim) { int i,j,newN,*count,l,ei,*count2; float **dist,**dist2,**cb2; if (N==1) return 1; count=(int *)calloc(N,sizeof(int)); for (l=0;l<npt;l++) count[P[l]]++; dist=(float **)fmatrix(N,N); ei = N-1; for (i=0;i<ei;i++) { for (j=i+1;j<N;j++) { dist[i][j] = distance2(cb[i],cb[j],dim); dist[j][i] = dist[i][j]; } } if (thresh<0) { cb2 = (float **)fmatrix(N,dim); for (i=0;i<N;i++) for (j=0;j<dim;j++) cb2[i][j]=cb[i][j]; count2=(int *)calloc(N,sizeof(int)); for (i=0;i<N;i++) count2[i]=count[i]; dist2=(float **)fmatrix(N,N); for (i=0;i<N;i++) for (j=0;j<N;j++) dist2[i][j]=dist[i][j]; if (dim==3) thresh=400; else if (dim==1) thresh=800; mergecb1(dist2,B,cb2,P,npt,N,&thresh,dim,count2,1); free_fmatrix(cb2,N); free(count2); free_fmatrix(dist2,N); } printf("thresh %f ",thresh); newN=mergecb1(dist,B,cb,P,npt,N,&thresh,dim,count,0); free_fmatrix(dist,N); free(count); return newN; }
/* Alloue de l'espace en mémoire pour créer une nouvelle matrice. * lin et col indiquent le nombre de lignes et de colonnes respectivement. * * Renvoie un pointeur vers la matrice. En cas d'erreur, retourne NULL. */ fmatrix *allocate_fmatrix(long lin, long col) { /* Vérifie que les dimensions sont cohérentes */ if ((lin < 1) || (col < 1)) { fprintf(stderr, "Error calling allocate_fmatrix :" " your fmatrix has dimensions %ldx%ld\n", lin, col); return NULL; } /* Crée la matrice */ fmatrix *mat = (fmatrix *)malloc(sizeof(fmatrix)); if (!mat) { fprintf(stderr, "Error calling allocate_fmatrix :" " malloc failed\n"); return NULL; } /* Crée le tableau */ long **tab = (long **)malloc(lin*sizeof(long *)); if (!tab) { fprintf(stderr, "Error calling allocate_fmatrix :" " malloc failed\n"); return NULL; } /* Initialisation de toutes les listes chaînées */ long i; for (i=0; i<lin; i++) { tab[i] = (long *)malloc(col*sizeof(long)); if (!tab[i]) { fprintf(stderr, "Error calling allocate_fmatrix :" " malloc to initialize line %ld failed\n", i); free_fmatrix(mat); return NULL; } } /* Données de la matrice */ mat->n = lin; mat->m = col; mat->pointers = tab; /* Retourne une matrice initialisée */ return mat; }
int greedy(float *A,int nvec,int ndim,int N,float **codebook,float t,unsigned char *P, float *weight) { int iv,in,jn,imax,nsplit,*index2,k,i,retgla,kn; float *totalw,*d,**buf, *variance; buf=fmatrix(N,ndim); d=(float *)calloc(ndim,sizeof(float)); variance=(float *)calloc(N,sizeof(float)); totalw=(float *)calloc(N,sizeof(float)); index2=(int *)calloc(N,sizeof(int)); /* Calculate the initial centroid */ for (k=0;k<ndim;k++) codebook[0][k]=0.0; totalw[0]=0; i=0; for (iv=0;iv<nvec;iv++) { P[iv]= 0; totalw[0]+=weight[iv]; for (k=0;k<ndim;k++) codebook[0][k] += weight[iv]*A[i++]; } for (k=0;k<ndim;k++) codebook[0][k]/=totalw[0]; in=1; while (in<N) { /* find the maximum variance */ for (jn=0;jn<in;jn++) { variance[jn]=0; totalw[jn]=0; for (k=0;k<ndim;k++) buf[jn][k]=0.0; } i = 0; for (iv=0;iv<nvec;iv++) { for (k=0;k<ndim;k++) d[k] = codebook[P[iv]][k] - A[i++]; for (k=0;k<ndim;k++) { buf[P[iv]][k] += weight[iv]*sqr(d[k]); variance[P[iv]]+= buf[P[iv]][k]; } totalw[P[iv]] += weight[iv]; } for (jn=0;jn<in;jn++) { for (k=0;k<ndim;k++) buf[jn][k] = sqrt(buf[jn][k]/totalw[jn]); } piksrt(in,variance,index2); /* split */ nsplit=in/2+1; if ((nsplit+in)>N) nsplit=N-in; for (jn=0;jn<nsplit;jn++) { imax=index2[jn]; for (k=0;k<ndim;k++) codebook[in+jn][k] = codebook[imax][k] - buf[imax][k]; for (k=0;k<ndim;k++) codebook[imax] [k] = codebook[imax][k] + buf[imax][k]; } /* run gla on the codebook */ in=in+nsplit; retgla=gla(A,nvec,ndim,in,codebook,t,P,weight); /* find the code vectors same, remove them and stop */ if (retgla) { for (jn=0;jn<in-1;jn++) { for (kn=jn+1;kn<in;kn++) { if (distance2(codebook[jn],codebook[kn],ndim)==0) { for (i=kn;i<in-1;i++) for (k=0;k<ndim;k++) codebook[i][k]=codebook[i+1][k]; in--; } } } break; } } free_fmatrix(buf,N); free (d); free (index2); free(variance); free(totalw); return in; }