/** * \ingroup python_interface_arpack * \brief Allocates a new ARPACK parameters object */ PyObject* igraphmodule_ARPACKOptions_new() { igraphmodule_ARPACKOptionsObject* self; self=PyObject_New(igraphmodule_ARPACKOptionsObject, &igraphmodule_ARPACKOptionsType); if (self) { igraph_arpack_options_init(&self->params); igraph_arpack_options_init(&self->params_out); } return (PyObject*)self; }
int main() { igraph_t g; igraph_vector_t v, weights; long int i; igraph_real_t value; igraph_arpack_options_t options; igraph_star(&g, 100, IGRAPH_STAR_UNDIRECTED, 0); igraph_arpack_options_init(&options); igraph_vector_init(&v, 0); igraph_eigenvector_centrality(&g, &v, &value, /*directed=*/ 0, /*scale=*/0, /*weights=*/0, &options); if (options.info != 0) { return 1; } for (i=0; i<igraph_vector_size(&v); i++) { printf(" %.3f", fabs(VECTOR(v)[i])); } printf("\n"); igraph_destroy(&g); /* Special cases: check for empty graph */ igraph_empty(&g, 10, 0); igraph_eigenvector_centrality(&g, &v, &value, 0, 0, 0, &options); if (value != 0.0) { return 1; } for (i=0; i<igraph_vector_size(&v); i++) { printf(" %.2f", fabs(VECTOR(v)[i])); } printf("\n"); igraph_destroy(&g); /* Special cases: check for full graph, zero weights */ igraph_full(&g, 10, 0, 0); igraph_vector_init(&weights, 45); igraph_vector_fill(&weights, 0); igraph_eigenvector_centrality(&g, &v, &value, 0, 0, &weights, &options); igraph_vector_destroy(&weights); if (value != 0.0) { return 2; } for (i=0; i<igraph_vector_size(&v); i++) { printf(" %.2f", fabs(VECTOR(v)[i])); } printf("\n"); igraph_destroy(&g); igraph_vector_destroy(&v); return 0; }
VALUE cIGraph_community_leading_eigenvector_naive(VALUE self, VALUE steps){ igraph_t *graph; igraph_vector_t membership; igraph_matrix_t *merges = malloc(sizeof(igraph_matrix_t)); igraph_arpack_options_t arpack_opt; igraph_arpack_options_init(&arpack_opt); int i,groupid,max_groupid; VALUE groups, group, res; Data_Get_Struct(self, igraph_t, graph); igraph_matrix_init(merges,0,0); igraph_vector_init(&membership,0); igraph_community_leading_eigenvector_naive(graph,merges,&membership, NUM2INT(steps), &arpack_opt); max_groupid = 0; for(i=0;i<igraph_vector_size(&membership);i++){ if(VECTOR(membership)[i] > max_groupid) max_groupid = VECTOR(membership)[i]; } groups = rb_ary_new(); for(i=0;i<max_groupid+1;i++){ rb_ary_push(groups,rb_ary_new()); } for(i=0;i<igraph_vector_size(&membership);i++){ groupid = VECTOR(membership)[i]; if(groupid == -1) groupid = 0; group = RARRAY_PTR(groups)[groupid]; rb_ary_push(group,cIGraph_get_vertex_object(self, i)); } res = rb_ary_new3(2,groups, Data_Wrap_Struct(cIGraphMatrix, 0, cIGraph_matrix_free, merges)); igraph_vector_destroy(&membership); return res; }
int main() { igraph_t small, big; igraph_matrix_t small_coords, big_coords, merged_coords; igraph_vector_ptr_t graph_ptr, coords_ptr; igraph_arpack_options_t arpack_opts; /* To make things reproducible */ igraph_rng_seed(igraph_rng_default(), 42); igraph_small(&big, 10, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,0, -1); igraph_small(&small, 3, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,0, -1); igraph_arpack_options_init(&arpack_opts); igraph_matrix_init(&big_coords, 0, 0); igraph_layout_mds(&big, &big_coords, /*dist=*/ 0, /*dim=*/ 2, &arpack_opts); igraph_matrix_init(&small_coords, 0, 0); igraph_layout_mds(&small, &small_coords, /*dist=*/ 0, /*dim=*/ 2, &arpack_opts); igraph_vector_ptr_init(&graph_ptr, 2); igraph_vector_ptr_init(&coords_ptr, 2); igraph_matrix_init(&merged_coords, 0, 0); VECTOR(graph_ptr)[0] = &big; VECTOR(graph_ptr)[1] = &small; VECTOR(coords_ptr)[0] = &big_coords; VECTOR(coords_ptr)[1] = &small_coords; igraph_layout_merge_dla(&graph_ptr, &coords_ptr, &merged_coords); igraph_matrix_print(&merged_coords); igraph_matrix_destroy(&merged_coords); igraph_matrix_destroy(&small_coords); igraph_matrix_destroy(&big_coords); igraph_vector_ptr_destroy(&graph_ptr); igraph_vector_ptr_destroy(&coords_ptr); igraph_destroy(&small); igraph_destroy(&big); #ifdef __APPLE__ return 0; #else return 77; #endif }
/* call-seq: * graph.eigenvector_centrality(scale, weights) -> Array * * Returns a two-element arrar, the first element of which is an Array of * eigenvector centrality scores for graph, and the second of which is the * eigenvalue. * * scale is a boolean value. If true, the scores will be weighted so that the * absolute value of the maximum centrality is one. * * weights is an Array giving the weights of the edges. If nil, the edges are unweighted. */ VALUE cIGraph_eigenvector_centrality(VALUE self, VALUE scale, VALUE weights) { int i; igraph_t *graph; igraph_vector_t vec; igraph_real_t val; igraph_vector_t wgts; igraph_arpack_options_t arpack_opt; igraph_bool_t sc = 0; VALUE eigenvector = rb_ary_new(); VALUE rb_res = rb_ary_new(); IGRAPH_FINALLY(igraph_vector_destroy, &vec); IGRAPH_FINALLY(igraph_vector_destroy, &wgts); IGRAPH_CHECK(igraph_vector_init(&vec,0)); IGRAPH_CHECK(igraph_vector_init(&wgts,0)); igraph_arpack_options_init(&arpack_opt); if (scale == Qtrue) sc = 1; Data_Get_Struct(self, igraph_t, graph); if (weights == Qnil) { IGRAPH_CHECK(igraph_eigenvector_centrality(graph, &vec, &val, sc, NULL, &arpack_opt)); } else { for(i = 0; i < RARRAY_LEN(weights); i++) IGRAPH_CHECK(igraph_vector_push_back(&wgts, NUM2DBL(RARRAY_PTR(weights)[i]))); IGRAPH_CHECK(igraph_eigenvector_centrality(graph, &vec, &val, sc, &wgts, &arpack_opt)); } for(i = 0; i < igraph_vector_size(&vec); i++) rb_ary_push(eigenvector, rb_float_new(VECTOR(vec)[i])); igraph_vector_destroy(&vec); igraph_vector_destroy(&wgts); rb_ary_push(rb_res, eigenvector); rb_ary_push(rb_res, rb_float_new(val)); IGRAPH_FINALLY_CLEAN(2); return rb_res; }
int main() { igraph_t g; igraph_vector_t v, res, reset, weights; igraph_arpack_options_t arpack_options; igraph_real_t value; int ret; igraph_pagerank_power_options_t power_options; /* Test graphs taken from http://www.iprcom.com/papers/pagerank/ */ igraph_vector_init(&v, 10); VECTOR(v)[0]=0; VECTOR(v)[1]=1; VECTOR(v)[2]=1; VECTOR(v)[3]=2; VECTOR(v)[4]=2; VECTOR(v)[5]=0; VECTOR(v)[6]=3; VECTOR(v)[7]=2; VECTOR(v)[8]=0; VECTOR(v)[9]=2; igraph_create(&g, &v, 0, 1); igraph_vector_init(&res, 0); oldwarn=igraph_set_warning_handler(warning_handler_stdout); igraph_pagerank_old(&g, &res, igraph_vss_all(), 1, 1000, 0.001, 0.85, 0); print_vector(&res, stdout); igraph_vector_destroy(&res); igraph_vector_destroy(&v); igraph_destroy(&g); igraph_vector_init(&v, 28); VECTOR(v)[ 0]=0; VECTOR(v)[ 1]=1; VECTOR(v)[ 2]=0; VECTOR(v)[ 3]=2; VECTOR(v)[ 4]=0; VECTOR(v)[ 5]=3; VECTOR(v)[ 6]=1; VECTOR(v)[ 7]=0; VECTOR(v)[ 8]=2; VECTOR(v)[ 9]=0; VECTOR(v)[10]=3; VECTOR(v)[11]=0; VECTOR(v)[12]=3; VECTOR(v)[13]=4; VECTOR(v)[14]=3; VECTOR(v)[15]=5; VECTOR(v)[16]=3; VECTOR(v)[17]=6; VECTOR(v)[18]=3; VECTOR(v)[19]=7; VECTOR(v)[20]=4; VECTOR(v)[21]=0; VECTOR(v)[22]=5; VECTOR(v)[23]=0; VECTOR(v)[24]=6; VECTOR(v)[25]=0; VECTOR(v)[26]=7; VECTOR(v)[27]=0; igraph_create(&g, &v, 0, 1); igraph_vector_init(&res, 0); igraph_pagerank_old(&g, &res, igraph_vss_all(), 1, 10000, 0.0001, 0.85, 0); print_vector(&res, stdout); igraph_vector_destroy(&res); igraph_vector_destroy(&v); igraph_destroy(&g); igraph_set_warning_handler(oldwarn); /* New PageRank */ igraph_star(&g, 11, IGRAPH_STAR_UNDIRECTED, 0); igraph_vector_init(&res, 0); igraph_arpack_options_init(&arpack_options); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, 0); print_vector(&res, stdout); /* Check twice more for consistency */ igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, 0); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, 0); print_vector(&res, stdout); /* Check personalized PageRank */ igraph_personalized_pagerank_vs(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.5, igraph_vss_1(1), 0, &arpack_options); print_vector(&res, stdout); igraph_personalized_pagerank_vs(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.5, igraph_vss_1(1), 0, 0); print_vector(&res, stdout); /* Errors */ power_options.niter = -1; power_options.eps=0.0001; igraph_set_error_handler(igraph_error_handler_ignore); igraph_set_warning_handler(igraph_warning_handler_ignore); ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res, /*value=*/ 0, igraph_vss_all(), 1, 0.85, /*weights=*/ 0, &power_options); if (ret != IGRAPH_EINVAL) { return 1; } power_options.niter=10000; power_options.eps=-1; ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res, /*value=*/ 0, igraph_vss_all(), 1, 0.85, /*weights=*/ 0, &power_options); if (ret != IGRAPH_EINVAL) { return 2; } power_options.niter=10000; power_options.eps=0.0001; ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res, /*value=*/ 0, igraph_vss_all(), 1, 1.2, /*weights=*/ 0, &power_options); if (ret != IGRAPH_EINVAL) { return 3; } igraph_vector_init(&reset, 2); ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, &arpack_options); if (ret != IGRAPH_EINVAL) { return 4; } ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, 0); if (ret != IGRAPH_EINVAL) { return 4; } igraph_vector_resize(&reset, 10); igraph_vector_fill(&reset, 0); ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, &arpack_options); if (ret != IGRAPH_EINVAL) { return 5; } ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, 0); if (ret != IGRAPH_EINVAL) { return 5; } igraph_vector_destroy(&reset); igraph_destroy(&g); igraph_set_error_handler(igraph_error_handler_abort); /* Special cases: check for empty graph */ igraph_empty(&g, 10, 0); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, &value, igraph_vss_all(), 1, 0.85, 0, &arpack_options); if (value != 1.0) { return 6; } igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, &value, igraph_vss_all(), 1, 0.85, 0, 0); if (value != 1.0) { return 6; } print_vector(&res, stdout); igraph_destroy(&g); /* Special cases: check for full graph, zero weights */ igraph_full(&g, 10, 0, 0); igraph_vector_init(&v, 45); igraph_vector_fill(&v, 0); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, &value, igraph_vss_all(), 1, 0.85, &v, &arpack_options); if (value != 1.0) { return 7; } igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, &value, igraph_vss_all(), 1, 0.85, &v, 0); if (value != 1.0) { return 7; } igraph_vector_destroy(&v); print_vector(&res, stdout); igraph_destroy(&g); /* Another test case for PageRank (bug #792352) */ igraph_small(&g, 9, 1, 0, 5, 1, 5, 2, 0, 3, 1, 5, 4, 5, 7, 6, 0, 8, 0, 8, 1, -1); igraph_vector_init(&weights, 9); VECTOR(weights)[0] = 4; VECTOR(weights)[1] = 5; VECTOR(weights)[2] = 5; VECTOR(weights)[3] = 4; VECTOR(weights)[4] = 4; VECTOR(weights)[5] = 4; VECTOR(weights)[6] = 3; VECTOR(weights)[7] = 4; VECTOR(weights)[8] = 4; igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 1, 0.85, &weights, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 1, 0.85, &weights, 0); print_vector(&res, stdout); igraph_vector_destroy(&weights); igraph_destroy(&g); igraph_vector_destroy(&res); return 0; }
int main() { igraph_matrix_t A; igraph_matrix_t values, vectors; igraph_arpack_options_t options; cb2_data_t data = { &A }; int i, j; igraph_rng_seed(igraph_rng_default(), 42 * 42); igraph_matrix_init(&A, DIM, DIM); for (i=0; i<DIM; i++) { for (j=0; j<DIM; j++) { MATRIX(A, i, j) = igraph_rng_get_integer(igraph_rng_default(), -10, 10); } } igraph_arpack_options_init(&options); options.n=DIM; options.start=0; options.nev=4; options.ncv=9; options.which[0]='L' ; options.which[1]='M'; igraph_matrix_init(&values, 0, 0); igraph_matrix_init(&vectors, options.n, 1); igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); if (MATRIX(values, 2, 1) > 0) { MATRIX(values, 2, 1) = -MATRIX(values, 2, 1); MATRIX(values, 3, 1) = -MATRIX(values, 3, 1); } igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ options.nev=3; options.which[0]='L' ; options.which[1]='M'; igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); if (MATRIX(values, 2, 1) > 0) { MATRIX(values, 2, 1) = -MATRIX(values, 2, 1); } igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ options.nev=3; options.which[0]='S' ; options.which[1]='R'; igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ options.nev=3; options.which[0]='L' ; options.which[1]='I'; igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ igraph_matrix_destroy(&values); igraph_matrix_destroy(&vectors); igraph_matrix_destroy(&A); return 0; }
int main() { igraph_t g; igraph_matrix_t merges; igraph_vector_t membership; long int i, j; igraph_bool_t split; igraph_vector_t x; igraph_real_t val; igraph_arpack_options_t options; /* Zachary Karate club */ igraph_small(&g, 0, IGRAPH_UNDIRECTED, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 10, 0, 11, 0, 12, 0, 13, 0, 17, 0, 19, 0, 21, 0, 31, 1, 2, 1, 3, 1, 7, 1, 13, 1, 17, 1, 19, 1, 21, 1, 30, 2, 3, 2, 7, 2, 8, 2, 9, 2, 13, 2, 27, 2, 28, 2, 32, 3, 7, 3, 12, 3, 13, 4, 6, 4, 10, 5, 6, 5, 10, 5, 16, 6, 16, 8, 30, 8, 32, 8, 33, 9, 33, 13, 33, 14, 32, 14, 33, 15, 32, 15, 33, 18, 32, 18, 33, 19, 33, 20, 32, 20, 33, 22, 32, 22, 33, 23, 25, 23, 27, 23, 29, 23, 32, 23, 33, 24, 25, 24, 27, 24, 31, 25, 31, 26, 29, 26, 33, 27, 33, 28, 31, 28, 33, 29, 32, 29, 33, 30, 32, 30, 33, 31, 32, 31, 33, 32, 33, -1); /* Make one step with all methods */ igraph_matrix_init(&merges, 0, 0); igraph_vector_init(&membership, 0); igraph_vector_init(&x, 0); igraph_arpack_options_init(&options); igraph_community_leading_eigenvector_naive(&g, &merges, &membership, 1, &options); print_matrix(&merges); print_vector(&membership); igraph_community_leading_eigenvector(&g, &merges, &membership, 1, &options); print_matrix(&merges); print_vector(&membership); igraph_vector_null(&membership); igraph_community_leading_eigenvector_step(&g, &membership, 0, &split, &x, &val, &options, 0); print_vector(&membership); print_vector(&x); printf("\n"); /* Make all the steps */ igraph_community_leading_eigenvector(&g, &merges, &membership, igraph_vcount(&g), &options); print_matrix(&merges); print_vector(&membership); /* Try to make one more step from here, should fail */ for (i=0; i<igraph_matrix_nrow(&merges)+1; i++) { igraph_community_leading_eigenvector_step(&g, &membership, i, &split, &x, &val, &options, 0); if (split) { printf("Impossible, community %li splitted.\n", i); return 1; } } igraph_vector_destroy(&x); igraph_vector_destroy(&membership); igraph_matrix_destroy(&merges); igraph_destroy(&g); return 0; }
VALUE cIGraph_community_leading_eigenvector_step(VALUE self, VALUE membership, VALUE community){ igraph_t *graph; igraph_vector_t membership_vec; igraph_vector_t eigenvector; igraph_real_t eigenvalue; igraph_bool_t split; int i,j,groupid,max_groupid,vid; igraph_arpack_options_t arpack_opt; igraph_arpack_options_init(&arpack_opt); VALUE groups, group, res, eigenvector_a, obj; Data_Get_Struct(self, igraph_t, graph); igraph_vector_init(&membership_vec,igraph_vcount(graph)); igraph_vector_init(&eigenvector,0); for(i=0;i<RARRAY_LEN(membership);i++){ group = RARRAY_PTR(membership)[i]; for(j=0;j<RARRAY_LEN(group);j++){ obj = RARRAY_PTR(group)[j]; vid = cIGraph_get_vertex_id(self,obj); VECTOR(membership_vec)[vid] = i; } } igraph_community_leading_eigenvector_step(graph,&membership_vec, NUM2INT(community), &split,&eigenvector,&eigenvalue,&arpack_opt,NULL); max_groupid = 0; for(i=0;i<igraph_vector_size(&membership_vec);i++){ if(VECTOR(membership_vec)[i] > max_groupid) max_groupid = VECTOR(membership_vec)[i]; } groups = rb_ary_new(); for(i=0;i<max_groupid+1;i++){ rb_ary_push(groups,rb_ary_new()); } for(i=0;i<igraph_vector_size(&membership_vec);i++){ groupid = VECTOR(membership_vec)[i]; if(groupid == -1) groupid = 0; group = RARRAY_PTR(groups)[groupid]; rb_ary_push(group,cIGraph_get_vertex_object(self, i)); } eigenvector_a = rb_ary_new(); for(i=0;i<igraph_vector_size(&eigenvector);i++){ rb_ary_push(eigenvector_a,rb_float_new(VECTOR(eigenvector)[i])); } res = rb_ary_new3(4,groups,split==0 ? Qfalse : Qtrue, eigenvector_a,rb_float_new(eigenvalue)); igraph_vector_destroy(&membership_vec); igraph_vector_destroy(&eigenvector); return res; }
int main() { float dist[8][8] = { {0.00, 4.69, 6.79, 3.50, 3.11, 4.46, 5.57, 3.00}, {4.69, 0.00, 2.10, 2.27, 2.65, 2.36, 1.99, 1.74}, {6.79, 2.10, 0.00, 3.78, 4.53, 2.83, 2.44, 3.79}, {3.50, 2.27, 3.78, 0.00, 1.98, 4.35, 2.07, 0.53}, {3.11, 2.65, 4.53, 1.98, 0.00, 3.80, 3.31, 1.47}, {4.46, 2.36, 2.83, 4.35, 3.80, 0.00, 4.35, 3.82}, {5.57, 1.99, 2.44, 2.07, 3.31, 4.35, 0.00, 2.57}, {3.00, 1.74, 3.79, 0.53, 1.47, 3.82, 2.57, 0.00}, }; igraph_t g; igraph_matrix_t coords, dist_mat; igraph_real_t vc; igraph_arpack_options_t options; int i, j; srand(time(0)); igraph_arpack_options_init(&options); igraph_tree(&g, 10, 2, IGRAPH_TREE_UNDIRECTED); igraph_matrix_init(&coords, 0, 0); igraph_layout_mds(&g, &coords, 0, 2, &options); if (MATRIX(coords, 0, 0) > 0) { for (i = 0; i < igraph_matrix_nrow(&coords); i++) MATRIX(coords, i, 0) *= -1; } if (MATRIX(coords, 0, 1) < 0) { for (i = 0; i < igraph_matrix_nrow(&coords); i++) MATRIX(coords, i, 1) *= -1; } igraph_matrix_print(&coords); igraph_matrix_destroy(&coords); igraph_destroy(&g); igraph_full(&g, 8, IGRAPH_UNDIRECTED, 0); igraph_matrix_init(&coords, 8, 2); igraph_matrix_init(&dist_mat, 8, 8); for (i = 0; i < 8; i++) for (j = 0; j < 2; j++) MATRIX(coords, i, j) = rand() % 1000; for (i = 0; i < 8; i++) for (j = i+1; j < 8; j++) { double dist_sq = 0.0; dist_sq += sqr(MATRIX(coords, i, 0)-MATRIX(coords, j, 0)); dist_sq += sqr(MATRIX(coords, i, 1)-MATRIX(coords, j, 1)); MATRIX(dist_mat, i, j) = sqrt(dist_sq); MATRIX(dist_mat, j, i) = sqrt(dist_sq); } igraph_layout_mds(&g, &coords, &dist_mat, 2, &options); for (i = 0; i < 8; i++) for (j = i+1; j < 8; j++) { double dist_sq = 0.0; dist_sq += sqr(MATRIX(coords, i, 0)-MATRIX(coords, j, 0)); dist_sq += sqr(MATRIX(coords, i, 1)-MATRIX(coords, j, 1)); if (fabs(sqrt(dist_sq) - MATRIX(dist_mat, i, j)) > 1e-2) { printf("dist(%d,%d) should be %.4f, but it is %.4f\n", i, j, MATRIX(dist_mat, i, j), sqrt(dist_sq)); return 1; } } igraph_matrix_destroy(&dist_mat); igraph_matrix_destroy(&coords); igraph_destroy(&g); return 0; }