21 standardize(
double* orthog,
int nvtxs)
25 for (i=0; i<nvtxs; i++)
30 for (i=0; i<nvtxs; i++)
34 len =
norm(orthog, 0, nvtxs-1);
35 vecscale(orthog, 0, nvtxs-1, 1.0 / len, orthog);
39 mat_mult_vec_orthog(
float** mat,
int dim1,
int dim2,
double* vec,
40 double* result,
double* orthog)
46 for (i=0; i<dim1; i++) {
48 for (j=0; j<dim2; j++) {
49 sum += mat[i][j]*vec[j];
54 double alpha=-
dot(result,0,dim1-1,orthog);
55 scadd(result, 0, dim1-1, alpha, orthog);
60 power_iteration_orthog(
float** square_mat,
int n,
int neigs,
61 double** eigs,
double* evals,
double* orthog,
double p_iteration_threshold)
68 double *tmp_vec =
N_GNEW(n,
double);
69 double *last_vec =
N_GNEW(n,
double);
78 double tol=1-p_iteration_threshold;
84 for (i=0; i<neigs; i++) {
85 curr_vector = eigs[i];
89 curr_vector[j] = rand()%100;
93 alpha=-
dot(orthog,0,n-1,curr_vector);
94 scadd(curr_vector, 0, n-1, alpha, orthog);
98 alpha = -
dot(eigs[j], 0, n-1, curr_vector);
99 scadd(curr_vector, 0, n-1, alpha, eigs[j]);
101 len =
norm(curr_vector, 0, n-1);
106 vecscale(curr_vector, 0, n-1, 1.0 / len, curr_vector);
110 cpvec(last_vec,0,n-1,curr_vector);
112 mat_mult_vec_orthog(square_mat,n,n,curr_vector,tmp_vec,orthog);
113 cpvec(curr_vector,0,n-1,tmp_vec);
116 for (j=0; j<i; j++) {
117 alpha = -
dot(eigs[j], 0, n-1, curr_vector);
118 scadd(curr_vector, 0, n-1, alpha, eigs[j]);
120 len =
norm(curr_vector, 0, n-1);
128 vecscale(curr_vector, 0, n-1, 1.0 / len, curr_vector);
129 angle =
dot(curr_vector, 0, n-1, last_vec);
130 }
while (fabs(angle)<tol);
137 for (; i<neigs; i++) {
142 curr_vector = eigs[i];
145 curr_vector[j] = rand()%100;
147 for (j=0; j<i; j++) {
148 alpha = -
dot(eigs[j], 0, n-1, curr_vector);
149 scadd(curr_vector, 0, n-1, alpha, eigs[j]);
151 len =
norm(curr_vector, 0, n-1);
152 vecscale(curr_vector, 0, n-1, 1.0 / len, curr_vector);
158 for (i=0; i<neigs-1; i++) {
160 largest_eval=evals[largest_index];
161 for (j=i+1; j<neigs; j++) {
162 if (largest_eval<evals[j]) {
164 largest_eval=evals[largest_index];
167 if (largest_index!=i) {
168 cpvec(tmp_vec,0,n-1,eigs[i]);
169 cpvec(eigs[i],0,n-1,eigs[largest_index]);
170 cpvec(eigs[largest_index],0,n-1,tmp_vec);
172 evals[largest_index]=evals[i];
173 evals[i]=largest_eval;
177 free (tmp_vec); free (last_vec);
182 compute_avgs(
DistType** Dij,
int n,
float* all_avg)
184 float* row_avg =
N_GNEW(n,
float);
186 double sum=0, sum_row;
188 for (i=0; i<n; i++) {
190 for (j=0; j<n; j++) {
191 sum+=(double)Dij[i][j]*(
double)Dij[i][j];
192 sum_row+=(double)Dij[i][j]*(
double)Dij[i][j];
194 row_avg[i]=(float)sum_row/n;
196 *all_avg=(float)sum/(n*n);
204 float* storage =
N_GNEW(n*n,
float);
205 float** Bij =
N_GNEW(n,
float*);
210 Bij[i] = storage+i*n;
212 row_avg = compute_avgs(Dij, n, &all_avg);
213 for (i=0; i<n; i++) {
214 for (j=0; j<=i; j++) {
215 Bij[i][j]=-(float)Dij[i][j]*Dij[i][j]+row_avg[i]+row_avg[j]-all_avg;
224 CMDS_orthog(
vtx_data*
graph,
int n,
int dim,
double** eigs,
double tol,
228 float** Bij = compute_Bij(Dij, n);
229 double* evals=
N_GNEW(dim,
double);
231 double * orthog_aux =
NULL;
233 orthog_aux =
N_GNEW(n,
double);
234 for (i=0; i<n; i++) {
235 orthog_aux[i]=orthog[i];
237 standardize(orthog_aux,n);
239 power_iteration_orthog(Bij, n, dim, eigs, evals, orthog_aux, tol);
241 for (i=0; i<dim; i++) {
242 for (j=0; j<n; j++) {
243 eigs[i][j]*=sqrt(fabs(evals[i]));
246 free (Bij[0]); free (Bij);
247 free (evals); free (orthog_aux);
250 #define SCALE_FACTOR 256
252 int IMDS_given_dim(
vtx_data* graph,
int n,
double* given_coords,
253 double* new_coords,
double conj_tol)
258 float* f_storage =
NULL;
259 double* x = given_coords;
261 double* orthog_aux =
NULL;
262 double* y = new_coords;
263 float** lap =
N_GNEW(n,
float*);
266 double* balance =
N_GNEW(n,
double);
271 iterations1=mat_mult_count1=0;
281 Dij[i][j]*=SCALE_FACTOR;
287 orthog_aux =
N_GNEW(n,
double);
288 for (i=0; i<n; i++) {
291 standardize(orthog_aux,n);
293 for (sum1=sum2=0,i=1; i<n; i++) {
294 for (j=0; j<i; j++) {
295 sum1+=1.0/(Dij[i][j])*fabs(x[i]-x[j]);
296 sum2+=1.0/(Dij[i][j]*Dij[i][j])*fabs(x[i]-x[j])*fabs(x[i]-x[j]);
305 CMDS_orthog(graph, n, 1, &y, conj_tol, x, Dij);
308 f_storage =
N_GNEW(n*n,
float);
310 for (i=0; i<n; i++) {
311 lap[i]=f_storage+i*n;
313 for (j=0; j<n; j++) {
316 degree-=lap[i][j]=-1.0f/((float)Dij[i][j]*(
float)Dij[i][j]);
327 for (i=1; i<n; i++) {
329 for (j=0; j<i; j++) {
330 diff=(double)Dij[i][j]*(
double)Dij[i][j]-(pos_i-x[j])*(pos_i-x[j]);
331 Dij[i][j]=Dij[j][i]=diff>0 ? (
DistType)sqrt(diff) : 0;
337 for (i=0; i<n; i++) {
340 for (j=0; j<n; j++) {
344 balance[i]+=Dij[i][j]*(-lap[i][j]);
347 balance[i]-=Dij[i][j]*(-lap[i][j]);
352 for (converged=
FALSE,iterations2=0; iterations2<200 && !converged; iterations2++) {
358 for (i=0; i<n; i++) {
361 for (j=0; j<n; j++) {
365 b+=Dij[i][j]*(-lap[i][j]);
369 b-=Dij[i][j]*(-lap[i][j]);
373 if ((b != balance[i]) && (fabs(1-b/balance[i])>1e-5)) {
380 for (i=0; i<n; i++) {
387 free (Dij[0]); free (Dij);
388 free (lap[0]); free (lap);
389 free (orthog_aux); free (balance);
void vecscale(double *vec1, int beg, int end, double alpha, double *vec2)
void cpvec(double *copy, int beg, int end, double *vec)
DistType ** compute_apsp(vtx_data *graph, int n)
Agraph_t * graph(char *name)
double norm(double *vec, int beg, int end)
int conjugate_gradient_f(float **A, double *x, double *b, int n, double tol, int max_iterations, boolean ortho1)
void scadd(double *vec1, int beg, int end, double fac, double *vec2)