22standardize(
double* orthog,
int nvtxs)
26 for (i=0; i<nvtxs; i++)
31 for (i=0; i<nvtxs; i++)
38 if (fabs(
len) < DBL_EPSILON) {
46mat_mult_vec_orthog(
float** mat,
int dim1,
int dim2,
double* vec,
47 double* result,
double* orthog)
53 for (i=0; i<dim1; i++) {
55 for (j=0; j<dim2; j++) {
56 sum += mat[i][j]*vec[j];
60 assert(orthog !=
NULL);
66power_iteration_orthog(
float** square_mat,
int n,
int neigs,
73 double *tmp_vec =
gv_calloc(n,
sizeof(
double));
74 double *last_vec =
gv_calloc(n,
sizeof(
double));
88 for (i=0; i<neigs; i++) {
89 curr_vector = eigs[i];
93 curr_vector[j] = rand()%100;
96 assert(orthog !=
NULL);
100 for (j=0; j<i; j++) {
113 mat_mult_vec_orthog(square_mat,n,n,curr_vector,tmp_vec,orthog);
117 for (j=0; j<i; j++) {
131 }
while (fabs(angle)<
tol);
138 for (; i<neigs; i++) {
143 curr_vector = eigs[i];
146 curr_vector[j] = rand()%100;
148 for (j=0; j<i; j++) {
159 for (i=0; i<neigs-1; i++) {
161 largest_eval=evals[largest_index];
162 for (j=i+1; j<neigs; j++) {
163 if (largest_eval<evals[j]) {
165 largest_eval=evals[largest_index];
168 if (largest_index!=i) {
173 evals[largest_index]=evals[i];
174 evals[i]=largest_eval;
183compute_avgs(
DistType** Dij,
int n,
float* all_avg)
185 float* row_avg =
gv_calloc(n,
sizeof(
float));
187 double sum=0, sum_row;
189 for (i=0; i<n; i++) {
191 for (j=0; j<n; j++) {
192 sum+=(double)Dij[i][j]*(
double)Dij[i][j];
193 sum_row+=(double)Dij[i][j]*(
double)Dij[i][j];
195 row_avg[i]=(float)sum_row/n;
197 *all_avg=(float)sum/(n*n);
205 float *storage =
gv_calloc(n * n,
sizeof(
float));
206 float **Bij =
gv_calloc(n,
sizeof(
float *));
211 Bij[i] = storage+i*n;
213 row_avg = compute_avgs(Dij, n, &all_avg);
214 for (i=0; i<n; i++) {
215 for (j=0; j<=i; j++) {
216 Bij[i][j]=-(float)Dij[i][j]*Dij[i][j]+row_avg[i]+row_avg[j]-all_avg;
225CMDS_orthog(
int n,
int dim,
double** eigs,
double tol,
229 float** Bij = compute_Bij(Dij, n);
232 assert(orthog !=
NULL);
233 double *orthog_aux =
gv_calloc(n,
sizeof(
double));
234 for (i=0; i<n; i++) {
235 orthog_aux[i]=orthog[i];
237 standardize(orthog_aux,n);
238 power_iteration_orthog(Bij, n,
dim, eigs, evals, orthog_aux,
tol);
240 for (i=0; i<
dim; i++) {
241 for (j=0; j<n; j++) {
242 eigs[i][j]*=sqrt(fabs(evals[i]));
249#define SCALE_FACTOR 256
251int IMDS_given_dim(
vtx_data*
graph,
int n,
double* given_coords,
252 double* new_coords,
double conj_tol)
257 double* x = given_coords;
259 double* y = new_coords;
260 float **lap =
gv_calloc(n,
sizeof(
float *));
263 double *balance =
gv_calloc(n,
sizeof(
double));
274 Dij[i][j]*=SCALE_FACTOR;
280 for (sum1=sum2=0,i=1; i<n; i++) {
281 for (j=0; j<i; j++) {
282 sum1+=1.0/(Dij[i][j])*fabs(x[i]-x[j]);
283 sum2+=1.0/(Dij[i][j]*Dij[i][j])*fabs(x[i]-x[j])*fabs(x[i]-x[j]);
286 uniLength = isinf(sum2) ? 0 : sum1 / sum2;
292 CMDS_orthog(n, 1, &y, conj_tol, x, Dij);
295 float *f_storage =
gv_calloc(n * n,
sizeof(
float));
297 for (i=0; i<n; i++) {
298 lap[i]=f_storage+i*n;
300 for (j=0; j<n; j++) {
303 degree-=lap[i][j]=-1.0f/((float)Dij[i][j]*(
float)Dij[i][j]);
314 for (i=1; i<n; i++) {
316 for (j=0; j<i; j++) {
317 diff=(double)Dij[i][j]*(
double)Dij[i][j]-(pos_i-x[j])*(pos_i-x[j]);
318 Dij[i][j]=Dij[j][i]=diff>0 ? (
DistType)sqrt(diff) : 0;
324 for (i=0; i<n; i++) {
327 for (j=0; j<n; j++) {
331 balance[i]+=Dij[i][j]*(-lap[i][j]);
334 balance[i]-=Dij[i][j]*(-lap[i][j]);
339 for (converged=
false,iterations2=0; iterations2<200 && !converged; iterations2++) {
345 for (i=0; i<n; i++) {
348 for (j=0; j<n; j++) {
352 b+=Dij[i][j]*(-lap[i][j]);
356 b-=Dij[i][j]*(-lap[i][j]);
360 if ((b != balance[i]) && (fabs(1-b/balance[i])>1e-5)) {
367 for (i = 0; !(fabs(uniLength) < DBL_EPSILON) && i < n; i++) {
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
int conjugate_gradient_f(float **A, double *x, double *b, int n, double tol, int max_iterations, bool ortho1)
static double norm(int n, const double *x)
static double len(glCompPoint p)
static void cleanup(void)
Agraph_t * graph(char *name)
DistType ** compute_apsp(vtx_data *graph, int n)
double vectors_inner_product(int n, const double *vector1, const double *vector2)
static double p_iteration_threshold
void scadd(double *vec1, int end, double fac, double *vec2)
void copy_vector(int n, const double *source, double *dest)
void vectors_scalar_mult(int n, const double *vector, double alpha, double *result)