int main() { igraph_matrix_t A; igraph_vector_t values; igraph_matrix_t vectors; int i, j; igraph_eigen_which_t which; igraph_rng_seed(igraph_rng_default(), 42 * 42); igraph_matrix_init(&A, DIM, DIM); igraph_matrix_init(&vectors, 0, 0); igraph_vector_init(&values, 0); /* All eigenvalues and eigenvectors */ for (i=0; i<DIM; i++) { for (j=i; j<DIM; j++) { MATRIX(A, i, j) = MATRIX(A, j, i) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } which.pos=IGRAPH_EIGEN_LM; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.howmany=8; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.pos=IGRAPH_EIGEN_BE; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.pos=IGRAPH_EIGEN_SM; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); igraph_vector_destroy(&values); igraph_matrix_destroy(&vectors); igraph_matrix_destroy(&A); return 0; }
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 }
int main () { long int i; igraph_matrix_t m; igraph_real_t x, y, z, r; srand(time(0)); /* 2D */ igraph_matrix_init(&m, 1000, 2); for (i=0; i<igraph_matrix_nrow(&m); i++) { MATRIX(m,i,0)=rand()/(double)RAND_MAX; MATRIX(m,i,1)=rand()/(double)RAND_MAX; } igraph_i_layout_sphere_2d(&m, &x, &y, &r); for (i=0; i<igraph_matrix_nrow(&m); i++) { igraph_real_t dist=sqrt((MATRIX(m,i,0)-x)*(MATRIX(m,i,0)-x) + (MATRIX(m,i,1)-y)*(MATRIX(m,i,1)-y)); if (dist > r) { printf("x: %f y: %f r: %f\n", x, y, r); printf("x: %f y: %f dist: %f (%li)\n", MATRIX(m,i,0), MATRIX(m,i,1), dist, i); return 1; } } igraph_matrix_destroy(&m); /* 3D */ igraph_matrix_init(&m, 1000, 3); for (i=0; i<igraph_matrix_nrow(&m); i++) { MATRIX(m,i,0)=rand()/(double)RAND_MAX; MATRIX(m,i,1)=rand()/(double)RAND_MAX; MATRIX(m,i,2)=rand()/(double)RAND_MAX; } igraph_i_layout_sphere_3d(&m, &x, &y, &z, &r); for (i=0; i<igraph_matrix_nrow(&m); i++) { igraph_real_t dist=sqrt((MATRIX(m,i,0)-x)*(MATRIX(m,i,0)-x) + (MATRIX(m,i,1)-y)*(MATRIX(m,i,1)-y) + (MATRIX(m,i,2)-z)*(MATRIX(m,i,2)-z)); if (dist > r) { printf("x: %f y: %f z: %f r: %f\n", x, y, z, r); printf("x: %f y: %f z: %f dist: %f (%li)\n", MATRIX(m,i,0), MATRIX(m,i,1), MATRIX(m,i,2), dist, i); return 1; } } igraph_matrix_destroy(&m); return 0; }
int main() { igraph_matrix_t A; igraph_matrix_t vectors_left, vectors_right; igraph_vector_t values_real, values_imag; int i, j; int info=1; int ilo, ihi; igraph_real_t abnrm; igraph_rng_seed(igraph_rng_default(), 42); igraph_matrix_init(&A, DIM, DIM); igraph_matrix_init(&vectors_left, 0, 0); igraph_matrix_init(&vectors_right, 0, 0); igraph_vector_init(&values_real, 0); igraph_vector_init(&values_imag, 0); for (i=0; i<DIM; i++) { for (j=0; j<DIM; j++) { MATRIX(A, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } igraph_lapack_dgeevx(IGRAPH_LAPACK_DGEEVX_BALANCE_BOTH, &A, &values_real, &values_imag, &vectors_left, &vectors_right, &ilo, &ihi, /*scale=*/ 0, &abnrm, /*rconde=*/ 0, /*rcondv=*/ 0, &info); if (check_ev(&A, &values_real, &values_imag, &vectors_left, &vectors_right, /*tol=*/ 1e-8)) { return 1; } /* igraph_matrix_print(&A); */ /* igraph_vector_print(&values_real); */ /* igraph_vector_print(&values_imag); */ /* igraph_matrix_print(&vectors_left); */ /* igraph_matrix_print(&vectors_right); */ igraph_vector_destroy(&values_imag); igraph_vector_destroy(&values_real); igraph_matrix_destroy(&vectors_right); igraph_matrix_destroy(&vectors_left); igraph_matrix_destroy(&A); return 0; }
/* call-seq: * graph.get_adjacency(type) -> Array * * Returns the adjacency matrix of a graph * */ VALUE cIGraph_get_adjacency(VALUE self, VALUE mode){ igraph_t *graph; igraph_get_adjacency_t pmode = NUM2INT(mode); igraph_matrix_t res; int i; int j; VALUE row; VALUE path_length; VALUE matrix = rb_ary_new(); int n; Data_Get_Struct(self, igraph_t, graph); n = igraph_vcount(graph); //matrix to hold the results of the calculations igraph_matrix_init(&res,n,n); igraph_get_adjacency(graph,&res,pmode); for(i=0; i<igraph_matrix_nrow(&res); i++){ row = rb_ary_new(); rb_ary_push(matrix,row); for(j=0; j<igraph_matrix_ncol(&res); j++){ path_length = INT2NUM(MATRIX(res,i,j)); rb_ary_push(row,path_length); } } igraph_matrix_destroy(&res); return matrix; }
int main() { const int nodes=10, skip=3; igraph_matrix_t mat2; igraph_vector_complex_t values, values2; igraph_matrix_complex_t vectors, vectors2; igraph_eigen_which_t which; int i; igraph_rng_seed(igraph_rng_default(), 42); igraph_matrix_init(&mat2, nodes, nodes); for (i=0; i<nodes; i++) { int j; for (j=0; j<nodes; j++) { MATRIX(mat2, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } igraph_vector_complex_init(&values, 0); igraph_matrix_complex_init(&vectors, 0, 0); which.pos=IGRAPH_EIGEN_LI; which.howmany=nodes; igraph_eigen_matrix(&mat2, /*sparsemat=*/ 0, /*fun=*/ 0, nodes, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_complex_init(&values2, 0); igraph_matrix_complex_init(&vectors2, 0, 0); which.pos=IGRAPH_EIGEN_SI; which.howmany=nodes; igraph_eigen_matrix(&mat2, /*sparsemat=*/ 0, /*fun=*/ 0, nodes, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values2, &vectors2); igraph_vector_complex_print(&values); igraph_vector_complex_print(&values2); for (i=0; i<nodes; i++) { int j; igraph_real_t d= igraph_complex_abs(igraph_complex_sub(VECTOR(values)[i], VECTOR(values2)[nodes-i-1])); if (d > 1e-15) { DUMP(); return 2; } for (j=0; j<nodes; j++) { igraph_real_t d= igraph_complex_abs(igraph_complex_sub(MATRIX(vectors, j, i), MATRIX(vectors2, j, nodes-i-1))); if (d > 1e-15) { DUMP(); return 3; } } } igraph_vector_complex_destroy(&values); igraph_matrix_complex_destroy(&vectors); igraph_vector_complex_destroy(&values2); igraph_matrix_complex_destroy(&vectors2); igraph_matrix_destroy(&mat2); return 0; }
int test_unnormalized_laplacian(igraph_bool_t dir) { igraph_t g; igraph_matrix_t m; igraph_vector_t vec; igraph_matrix_init(&m, 1, 1); /* No loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, 0); print_matrix(&m); printf("===\n"); /* Add some loop edges */ igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 0); print_matrix(&m); printf("===\n"); /* Duplicate some edges */ igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 0); print_matrix(&m); igraph_destroy(&g); igraph_matrix_destroy(&m); return 0; }
int main() { igraph_t g; igraph_vector_t weights; igraph_real_t weights_data[] = { 0,2,1, 0,5,2, 1,1,0, 2,2,8, 1,1,3, 1,1,4, 2,1 }; igraph_matrix_t res; igraph_small(&g, 10, IGRAPH_DIRECTED, 0,1, 0,2, 0,3, 1,2, 1,4, 1,5, 2,3, 2,6, 3,2, 3,6, 4,5, 4,7, 5,6, 5,8, 5,9, 7,5, 7,8, 8,9, 5,2, 2,1, -1); igraph_vector_view(&weights, weights_data, sizeof(weights_data)/sizeof(igraph_real_t)); igraph_matrix_init(&res, 0, 0); igraph_shortest_paths_dijkstra(&g, &res, igraph_vss_all(), igraph_vss_all(), &weights, IGRAPH_OUT); print_matrix(&res); igraph_matrix_destroy(&res); igraph_destroy(&g); return 0; }
int igraph_i_eigen_matrix_lapack_common(const igraph_matrix_t *A, const igraph_eigen_which_t *which, igraph_vector_complex_t *values, igraph_matrix_complex_t *vectors) { igraph_vector_t valuesreal, valuesimag; igraph_matrix_t vectorsright, *myvectors= vectors ? &vectorsright : 0; int n=(int) igraph_matrix_nrow(A); int info=1; IGRAPH_VECTOR_INIT_FINALLY(&valuesreal, n); IGRAPH_VECTOR_INIT_FINALLY(&valuesimag, n); if (vectors) { IGRAPH_MATRIX_INIT_FINALLY(&vectorsright, n, n); } IGRAPH_CHECK(igraph_lapack_dgeev(A, &valuesreal, &valuesimag, /*vectorsleft=*/ 0, myvectors, &info)); IGRAPH_CHECK(igraph_i_eigen_matrix_lapack_reorder(&valuesreal, &valuesimag, myvectors, which, values, vectors)); if (vectors) { igraph_matrix_destroy(&vectorsright); IGRAPH_FINALLY_CLEAN(1); } igraph_vector_destroy(&valuesimag); igraph_vector_destroy(&valuesreal); IGRAPH_FINALLY_CLEAN(2); return 0; }
/* call-seq: * graph.dijkstra_shortest_paths(varray,weights,mode) -> Array * * Calculates the length of the shortest paths from each of the vertices in * the varray Array to all of the other vertices in the graph given a set of * edge weights given in the weights Array. The result * is returned as an Array of Array. Each top-level Array contains the results * for a vertex in the varray Array. Each entry in the Array is the path length * to another vertex in the graph in vertex order (the order the vertices were * added to the graph. (This should probalby be changed to give a Hash of Hash * to allow easier look up.) */ VALUE cIGraph_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE weights, VALUE mode){ igraph_t *graph; igraph_vs_t vids; igraph_vector_t vidv; igraph_vector_t wghts; igraph_neimode_t pmode = NUM2INT(mode); igraph_matrix_t res; int i; int j; VALUE row; VALUE path_length; VALUE matrix = rb_ary_new(); int n_row; int n_col; Data_Get_Struct(self, igraph_t, graph); n_row = NUM2INT(rb_funcall(from,rb_intern("length"),0)); n_col = igraph_vcount(graph); //matrix to hold the results of the calculations igraph_matrix_init(&res,n_row,n_col); igraph_vector_init(&wghts,RARRAY_LEN(weights)); for(i=0;i<RARRAY_LEN(weights);i++){ VECTOR(wghts)[i] = NUM2DBL(RARRAY_PTR(weights)[i]); } //Convert an array of vertices to a vector of vertex ids igraph_vector_init_int(&vidv,0); cIGraph_vertex_arr_to_id_vec(self,from,&vidv); //create vertex selector from the vecotr of ids igraph_vs_vector(&vids,&vidv); igraph_dijkstra_shortest_paths(graph,&res,vids,&wghts,pmode); for(i=0; i<igraph_matrix_nrow(&res); i++){ row = rb_ary_new(); rb_ary_push(matrix,row); for(j=0; j<igraph_matrix_ncol(&res); j++){ path_length = MATRIX(res,i,j) == n_col ? Qnil : rb_float_new(MATRIX(res,i,j)); rb_ary_push(row,path_length); } } igraph_vector_destroy(&vidv); igraph_matrix_destroy(&res); igraph_vs_destroy(&vids); igraph_vector_destroy(&wghts); return matrix; }
symmetric_matrix<double, lower> Graph::adjacency() const { double x; int i,j; igraph_matrix_t m; symmetric_matrix<double, lower> adjmatrix(size, size); igraph_matrix_init(&m, size, size); igraph_get_adjacency(graph, &m,IGRAPH_GET_ADJACENCY_LOWER); /* IGRAPH_GET_ADJACENCY_BOTH);*/ for(i = 0; i < size; i++){ for(j = 0; j <=i; j++) { x = MATRIX(m,i,j) ? 1 : 0; adjmatrix(i,j) = x; } } igraph_matrix_destroy(&m); return adjmatrix; }
/* call-seq: * graph.cocitation(varray) -> Array * * Cocitation coupling. * * Two vertices are cocited if there is another vertex citing both of them. * igraph_cocitation() simply counts how many types two vertices are cocited. * The cocitation score for each given vertex and all other vertices in the * graph will be calculated. * */ VALUE cIGraph_cocitation(VALUE self, VALUE vs){ igraph_t *graph; igraph_vs_t vids; igraph_vector_t vidv; igraph_matrix_t res; int i; int j; VALUE row; VALUE path_length; VALUE matrix = rb_ary_new(); int n_row; int n_col; Data_Get_Struct(self, igraph_t, graph); n_row = NUM2INT(rb_funcall(vs,rb_intern("length"),0)); n_col = igraph_vcount(graph); //matrix to hold the results of the calculations igraph_matrix_init(&res,n_row,n_col); //Convert an array of vertices to a vector of vertex ids igraph_vector_init_int(&vidv,0); cIGraph_vertex_arr_to_id_vec(self,vs,&vidv); //create vertex selector from the vecotr of ids igraph_vs_vector(&vids,&vidv); igraph_cocitation(graph,&res,vids); for(i=0; i<igraph_matrix_nrow(&res); i++){ row = rb_ary_new(); rb_ary_push(matrix,row); for(j=0; j<igraph_matrix_ncol(&res); j++){ path_length = INT2NUM(MATRIX(res,i,j)); rb_ary_push(row,path_length); } } igraph_vector_destroy(&vidv); igraph_matrix_destroy(&res); igraph_vs_destroy(&vids); return matrix; }
int main() { igraph_matrix_t m; igraph_matrix_init(&m, 10, 10); if (igraph_matrix_capacity(&m) != 100) { return 1; } igraph_matrix_add_cols(&m, 5); igraph_matrix_resize(&m, 5, 5); igraph_matrix_resize_min(&m); if (igraph_matrix_capacity(&m) != igraph_matrix_size(&m)) { return 2; } igraph_matrix_destroy(&m); return 0; }
int main() { igraph_t graph; igraph_matrix_t coords; int i; igraph_matrix_init(&coords, 0, 0); for (i=0; i<10; i++) { igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, /*n=*/ 100, /*p=*/ 2.0/100, IGRAPH_UNDIRECTED, /*loops=*/ 0); igraph_layout_mds(&graph, &coords, /*dist=*/ 0, /*dim=*/ 2, /*options=*/ 0); igraph_destroy(&graph); } igraph_matrix_destroy(&coords); return 0; }
int main() { igraph_matrix_t mat; igraph_sparsemat_t spmat, spmat2; int i; igraph_real_t m1, m2; igraph_rng_seed(igraph_rng_default(), 42); igraph_sparsemat_init(&spmat, DIM1, DIM2, 20); igraph_sparsemat_entry(&spmat, 1, 2, -1.0); igraph_sparsemat_entry(&spmat, 3, 2, 10.0); for (i=0; i<10; i++) { igraph_sparsemat_entry(&spmat, INT(DIM1-1), INT(DIM2-1), 1.0); } igraph_sparsemat_entry(&spmat, 1, 2, -1.0); igraph_sparsemat_entry(&spmat, 3, 2, 10.0); igraph_sparsemat_compress(&spmat, &spmat2); igraph_matrix_init(&mat, 0, 0); igraph_sparsemat_as_matrix(&mat, &spmat2); m1=igraph_sparsemat_min(&spmat2); m2=igraph_matrix_min(&mat); if (m1 != m2) { printf("%f %f\n", m1, m2); return 1; } m1=igraph_sparsemat_max(&spmat2); m2=igraph_matrix_max(&mat); if (m1 != m2) { printf("%f %f\n", m1, m2); return 2; } igraph_sparsemat_minmax(&spmat2, &m1, &m2); if (m1 != igraph_matrix_min(&mat)) { return 3; } if (m2 != igraph_matrix_max(&mat)) { return 4; } igraph_matrix_destroy(&mat); igraph_sparsemat_destroy(&spmat); igraph_sparsemat_destroy(&spmat2); return 0; }
int main() { igraph_t g; igraph_matrix_t m; igraph_small(&g, 0, IGRAPH_DIRECTED, 0,1, 2,1, 2,0, 3,0, -1); igraph_matrix_init(&m, 0, 0); igraph_bibcoupling(&g, &m, igraph_vss_all()); print_matrix(&m, stdout); igraph_cocitation(&g, &m, igraph_vss_all()); print_matrix(&m, stdout); igraph_matrix_destroy(&m); igraph_destroy(&g); return 0; }
int test_normalized_laplacian(igraph_bool_t dir) { igraph_t g; igraph_matrix_t m; igraph_vector_t vec; igraph_matrix_init(&m, 1, 1); igraph_bool_t ok = 1; /* Undirected graph, no loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); /* Add some loop edges */ igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); /* Duplicate some edges */ igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); igraph_destroy(&g); igraph_matrix_destroy(&m); if (ok) printf("OK\n"); return !ok; }
int igraph_lapack_dsyevr(const igraph_matrix_t *A, igraph_lapack_dsyev_which_t which, igraph_real_t vl, igraph_real_t vu, int vestimate, int il, int iu, igraph_real_t abstol, igraph_vector_t *values, igraph_matrix_t *vectors, igraph_vector_int_t *support) { igraph_matrix_t Acopy; char jobz = vectors ? 'V' : 'N', range, uplo='U'; int n=(int) igraph_matrix_nrow(A), lda=n, ldz=n; int m, info; igraph_vector_t *myvalues=values, vvalues; igraph_vector_int_t *mysupport=support, vsupport; igraph_vector_t work; igraph_vector_int_t iwork; int lwork=-1, liwork=-1; if (n != igraph_matrix_ncol(A)) { IGRAPH_ERROR("Cannot find eigenvalues/vectors", IGRAPH_NONSQUARE); } if (which==IGRAPH_LAPACK_DSYEV_INTERVAL && (vestimate < 1 || vestimate > n)) { IGRAPH_ERROR("Estimated (upper bound) number of eigenvalues must be " "between 1 and n", IGRAPH_EINVAL); } if (which==IGRAPH_LAPACK_DSYEV_SELECT && iu-il < 0) { IGRAPH_ERROR("Invalid 'il' and/or 'iu' values", IGRAPH_EINVAL); } IGRAPH_CHECK(igraph_matrix_copy(&Acopy, A)); IGRAPH_FINALLY(igraph_matrix_destroy, &Acopy); IGRAPH_VECTOR_INIT_FINALLY(&work, 1); IGRAPH_CHECK(igraph_vector_int_init(&iwork, 1)); IGRAPH_FINALLY(igraph_vector_int_destroy, &iwork); if (!values) { IGRAPH_VECTOR_INIT_FINALLY(&vvalues, 0); myvalues=&vvalues; } if (!support) { IGRAPH_CHECK(igraph_vector_int_init(&vsupport, 0)); IGRAPH_FINALLY(igraph_vector_int_destroy, &vsupport); mysupport=&vsupport; } switch (which) { case IGRAPH_LAPACK_DSYEV_ALL: range = 'A'; IGRAPH_CHECK(igraph_vector_resize(myvalues, n)); IGRAPH_CHECK(igraph_vector_int_resize(mysupport, 2*n)); if (vectors) { IGRAPH_CHECK(igraph_matrix_resize(vectors, n, n)); } break; case IGRAPH_LAPACK_DSYEV_INTERVAL: range = 'V'; IGRAPH_CHECK(igraph_vector_resize(myvalues, vestimate)); IGRAPH_CHECK(igraph_vector_int_resize(mysupport, 2*vestimate)); if (vectors) { IGRAPH_CHECK(igraph_matrix_resize(vectors,n, vestimate)); } break; case IGRAPH_LAPACK_DSYEV_SELECT: range = 'I'; IGRAPH_CHECK(igraph_vector_resize(myvalues, iu-il+1)); IGRAPH_CHECK(igraph_vector_int_resize(mysupport, 2*(iu-il+1))); if (vectors) { IGRAPH_CHECK(igraph_matrix_resize(vectors, n, iu-il+1)); } break; } igraphdsyevr_(&jobz, &range, &uplo, &n, &MATRIX(Acopy,0,0), &lda, &vl, &vu, &il, &iu, &abstol, &m, VECTOR(*myvalues), vectors ? &MATRIX(*vectors,0,0) : 0, &ldz, VECTOR(*mysupport), VECTOR(work), &lwork, VECTOR(iwork), &liwork, &info); lwork=(int) VECTOR(work)[0]; liwork=VECTOR(iwork)[0]; IGRAPH_CHECK(igraph_vector_resize(&work, lwork)); IGRAPH_CHECK(igraph_vector_int_resize(&iwork, liwork)); igraphdsyevr_(&jobz, &range, &uplo, &n, &MATRIX(Acopy,0,0), &lda, &vl, &vu, &il, &iu, &abstol, &m, VECTOR(*myvalues), vectors ? &MATRIX(*vectors,0,0) : 0, &ldz, VECTOR(*mysupport), VECTOR(work), &lwork, VECTOR(iwork), &liwork, &info); if (values) { IGRAPH_CHECK(igraph_vector_resize(values, m)); } if (vectors) { IGRAPH_CHECK(igraph_matrix_resize(vectors, n, m)); } if (support) { IGRAPH_CHECK(igraph_vector_int_resize(support, m)); } if (!support) { igraph_vector_int_destroy(&vsupport); IGRAPH_FINALLY_CLEAN(1); } if (!values) { igraph_vector_destroy(&vvalues); IGRAPH_FINALLY_CLEAN(1); } igraph_vector_int_destroy(&iwork); igraph_vector_destroy(&work); igraph_matrix_destroy(&Acopy); IGRAPH_FINALLY_CLEAN(3); return 0; }
/* Constructors and destructors */ Matrix::~Matrix() { if (owner()) igraph_matrix_destroy(ptr()); }
int igraph_i_eigen_matrix_symmetric_lapack_lm(const igraph_matrix_t *A, const igraph_eigen_which_t *which, igraph_vector_t *values, igraph_matrix_t *vectors) { igraph_matrix_t vec1, vec2; igraph_vector_t val1, val2; int n=(int) igraph_matrix_nrow(A); int p1=0, p2=which->howmany-1, pr=0; IGRAPH_VECTOR_INIT_FINALLY(&val1, 0); IGRAPH_VECTOR_INIT_FINALLY(&val2, 0); if (vectors) { IGRAPH_CHECK(igraph_matrix_init(&vec1, 0, 0)); IGRAPH_FINALLY(igraph_matrix_destroy, &vec1); IGRAPH_CHECK(igraph_matrix_init(&vec2, 0, 0)); IGRAPH_FINALLY(igraph_matrix_destroy, &vec1); } IGRAPH_CHECK(igraph_lapack_dsyevr(A, IGRAPH_LAPACK_DSYEV_SELECT, /*vl=*/ 0, /*vu=*/ 0, /*vestimate=*/ 0, /*il=*/ 1, /*iu=*/ which->howmany, /*abstol=*/ 1e-14, &val1, vectors ? &vec1 : 0, /*support=*/ 0)); IGRAPH_CHECK(igraph_lapack_dsyevr(A, IGRAPH_LAPACK_DSYEV_SELECT, /*vl=*/ 0, /*vu=*/ 0, /*vestimate=*/ 0, /*il=*/ n-which->howmany+1, /*iu=*/ n, /*abstol=*/ 1e-14, &val2, vectors ? &vec2 : 0, /*support=*/ 0)); if (values) { IGRAPH_CHECK(igraph_vector_resize(values, which->howmany)); } if (vectors) { IGRAPH_CHECK(igraph_matrix_resize(vectors, n, which->howmany)); } while (pr < which->howmany) { if (p2 < 0 || fabs(VECTOR(val1)[p1]) > fabs(VECTOR(val2)[p2])) { if (values) { VECTOR(*values)[pr]=VECTOR(val1)[p1]; } if (vectors) { memcpy(&MATRIX(*vectors,0,pr), &MATRIX(vec1,0,p1), sizeof(igraph_real_t) * (size_t) n); } p1++; pr++; } else { if (values) { VECTOR(*values)[pr]=VECTOR(val2)[p2]; } if (vectors) { memcpy(&MATRIX(*vectors,0,pr), &MATRIX(vec2,0,p2), sizeof(igraph_real_t) * (size_t) n); } p2--; pr++; } } if (vectors) { igraph_matrix_destroy(&vec2); igraph_matrix_destroy(&vec1); IGRAPH_FINALLY_CLEAN(2); } igraph_vector_destroy(&val2); igraph_vector_destroy(&val1); IGRAPH_FINALLY_CLEAN(2); return 0; }
//------------------------------------------------------------------------------------------------- int main(int argc, char **argv) { igraph_set_error_handler(igraph_error_handler_printignore); igraph_i_set_attribute_table(&igraph_cattribute_table); drawingcontrol=1; runningcontrol=0; cleared=1; graphisloaded=0; newgraph=1; usesteady=0; ispii=0; haveout=0; isrelaxed=0; havecstop=0; isdissipating=0; //RANDOM NUMBER GENERATOR INITIALISE init_genrand(0); //srand(3); //preloaded graph: { latticedim=2; latticeside=5; istoro=1; islattice=1; isclustered=0; israndomER1=0; generatelattice(2,5,1,0,0,0,0); //inizializations igraph_matrix_init(&state, 0, 0); igraph_matrix_init(&density, 0, 0); igraph_matrix_init(&densityold,0,0); igraph_matrix_init(&statenew, 0, 0); igraph_matrix_init(&flux, 0, 0); igraph_matrix_init(&loss, 0, 0); beginner=(nodesnumber/2); InitialStateTAS(beginner,0,totrun); igraph_vector_init(&gain,nodesnumber); graphisloaded=1; newgraph=1; haveloadedstate=0; drnodes=1; drlinks=1; drfluxes=0; } havepath=0; sprintf(path,"No path"); logDebug("\n DEFAULT ADJ MATRIX:\n"); print_matrix_ur(&admatrix,stdout); printf("\n"); printf("\n"); rewrite=1; CreateMyWindow(); //Fl::add_timeout(1.0,mainidle_cb); Fl::add_idle(mainidle_cb, 0); Fl::run(); histdist.Clear(); igraph_destroy(&graph); igraph_destroy(&sgraph); igraph_matrix_destroy(&admatrix); igraph_matrix_destroy(&layout); igraph_matrix_destroy(&density); igraph_matrix_destroy(&densityold); igraph_matrix_destroy(&state); igraph_matrix_destroy(&loadedstate); igraph_matrix_destroy(&statenew); igraph_matrix_destroy(&flux); igraph_vector_destroy(&gain); igraph_matrix_destroy(&loss); igraph_vector_destroy(&statstate); igraph_matrix_destroy(&estates); closeout(); return 0; }
int igraph_lapack_dgehrd(const igraph_matrix_t *A, int ilo, int ihi, igraph_matrix_t *result) { int n=(int) igraph_matrix_nrow(A); int lda=n; int lwork=-1; igraph_vector_t work; igraph_real_t optwork; igraph_vector_t tau; igraph_matrix_t Acopy; int info=0; int i; if (igraph_matrix_ncol(A) != n) { IGRAPH_ERROR("Hessenberg reduction failed", IGRAPH_NONSQUARE); } if (ilo < 1 || ihi > n || ilo > ihi) { IGRAPH_ERROR("Invalid `ilo' and/or `ihi'", IGRAPH_EINVAL); } if (n <= 1) { IGRAPH_CHECK(igraph_matrix_update(result, A)); return 0; } IGRAPH_CHECK(igraph_matrix_copy(&Acopy, A)); IGRAPH_FINALLY(igraph_matrix_destroy, &Acopy); IGRAPH_VECTOR_INIT_FINALLY(&tau, n-1); igraphdgehrd_(&n, &ilo, &ihi, &MATRIX(Acopy, 0, 0), &lda, VECTOR(tau), &optwork, &lwork, &info); if (info != 0) { IGRAPH_ERROR("Internal Hessenberg transformation error", IGRAPH_EINTERNAL); } lwork=(int) optwork; IGRAPH_VECTOR_INIT_FINALLY(&work, lwork); igraphdgehrd_(&n, &ilo, &ihi, &MATRIX(Acopy, 0, 0), &lda, VECTOR(tau), VECTOR(work), &lwork, &info); if (info != 0) { IGRAPH_ERROR("Internal Hessenberg transformation error", IGRAPH_EINTERNAL); } igraph_vector_destroy(&work); igraph_vector_destroy(&tau); IGRAPH_FINALLY_CLEAN(2); IGRAPH_CHECK(igraph_matrix_update(result, &Acopy)); igraph_matrix_destroy(&Acopy); IGRAPH_FINALLY_CLEAN(1); for (i=0; i<n-2; i++) { int j; for (j=i+2; j<n; j++) { MATRIX(*result, j, i) = 0.0; } } return 0; }
int igraph_lapack_dgeev(const igraph_matrix_t *A, igraph_vector_t *valuesreal, igraph_vector_t *valuesimag, igraph_matrix_t *vectorsleft, igraph_matrix_t *vectorsright, int *info) { char jobvl= vectorsleft ? 'V' : 'N'; char jobvr= vectorsright ? 'V' : 'N'; int n=(int) igraph_matrix_nrow(A); int lda=n, ldvl=n, ldvr=n, lwork=-1; igraph_vector_t work; igraph_vector_t *myreal=valuesreal, *myimag=valuesimag, vreal, vimag; igraph_matrix_t Acopy; int error=*info; if (igraph_matrix_ncol(A) != n) { IGRAPH_ERROR("Cannot calculate eigenvalues (dgeev)", IGRAPH_NONSQUARE); } IGRAPH_CHECK(igraph_matrix_copy(&Acopy, A)); IGRAPH_FINALLY(igraph_matrix_destroy, &Acopy); IGRAPH_VECTOR_INIT_FINALLY(&work, 1); if (!valuesreal) { IGRAPH_VECTOR_INIT_FINALLY(&vreal, n); myreal=&vreal; } else { IGRAPH_CHECK(igraph_vector_resize(myreal, n)); } if (!valuesimag) { IGRAPH_VECTOR_INIT_FINALLY(&vimag, n); myimag=&vimag; } else { IGRAPH_CHECK(igraph_vector_resize(myimag, n)); } if (vectorsleft) { IGRAPH_CHECK(igraph_matrix_resize(vectorsleft, n, n)); } if (vectorsright) { IGRAPH_CHECK(igraph_matrix_resize(vectorsright, n, n)); } igraphdgeev_(&jobvl, &jobvr, &n, &MATRIX(Acopy,0,0), &lda, VECTOR(*myreal), VECTOR(*myimag), vectorsleft ? &MATRIX(*vectorsleft ,0,0) : 0, &ldvl, vectorsright ? &MATRIX(*vectorsright,0,0) : 0, &ldvr, VECTOR(work), &lwork, info); lwork=(int) VECTOR(work)[0]; IGRAPH_CHECK(igraph_vector_resize(&work, lwork)); igraphdgeev_(&jobvl, &jobvr, &n, &MATRIX(Acopy,0,0), &lda, VECTOR(*myreal), VECTOR(*myimag), vectorsleft ? &MATRIX(*vectorsleft ,0,0) : 0, &ldvl, vectorsright ? &MATRIX(*vectorsright,0,0) : 0, &ldvr, VECTOR(work), &lwork, info); if (*info < 0) { IGRAPH_ERROR("Cannot calculate eigenvalues (dgeev)", IGRAPH_ELAPACK); } else if (*info > 0) { if (error) { IGRAPH_ERROR("Cannot calculate eigenvalues (dgeev)", IGRAPH_ELAPACK); } else { IGRAPH_WARNING("Cannot calculate eigenvalues (dgeev)"); } } if (!valuesimag) { igraph_vector_destroy(&vimag); IGRAPH_FINALLY_CLEAN(1); } if (!valuesreal) { igraph_vector_destroy(&vreal); IGRAPH_FINALLY_CLEAN(1); } igraph_vector_destroy(&work); igraph_matrix_destroy(&Acopy); IGRAPH_FINALLY_CLEAN(2); return 0; }
int igraph_i_community_spinglass_negative(const igraph_t *graph, const igraph_vector_t *weights, igraph_real_t *modularity, igraph_real_t *temperature, igraph_vector_t *membership, igraph_vector_t *csize, igraph_integer_t spins, igraph_bool_t parupdate, igraph_real_t starttemp, igraph_real_t stoptemp, igraph_real_t coolfact, igraph_spincomm_update_t update_rule, igraph_real_t gamma, /* igraph_matrix_t *adhesion, */ /* igraph_matrix_t *normalised_adhesion, */ /* igraph_real_t *polarization, */ igraph_real_t gamma_minus) { unsigned long changes, runs; igraph_bool_t use_weights=0; bool zeroT; double kT, acc; ClusterList<NNode*> *cl_cur; network *net; PottsModelN *pm; igraph_real_t d_n; igraph_real_t d_p; /* Check arguments */ if (parupdate) { IGRAPH_ERROR("Parallel spin update not implemented with " "negative gamma", IGRAPH_UNIMPLEMENTED); } if (spins < 2 || spins > 500) { IGRAPH_ERROR("Invalid number of spins", IGRAPH_EINVAL); } if (update_rule != IGRAPH_SPINCOMM_UPDATE_SIMPLE && update_rule != IGRAPH_SPINCOMM_UPDATE_CONFIG) { IGRAPH_ERROR("Invalid update rule", IGRAPH_EINVAL); } if (weights) { if (igraph_vector_size(weights) != igraph_ecount(graph)) { IGRAPH_ERROR("Invalid weight vector length", IGRAPH_EINVAL); } use_weights=1; } if (coolfact < 0 || coolfact>=1.0) { IGRAPH_ERROR("Invalid cooling factor", IGRAPH_EINVAL); } if (gamma < 0.0) { IGRAPH_ERROR("Invalid gamma value", IGRAPH_EINVAL); } if (starttemp/stoptemp<1.0) { IGRAPH_ERROR("starttemp should be larger in absolute value than stoptemp", IGRAPH_EINVAL); } /* Check whether we have a single component */ igraph_bool_t conn; IGRAPH_CHECK(igraph_is_connected(graph, &conn, IGRAPH_WEAK)); if (!conn) { IGRAPH_ERROR("Cannot work with unconnected graph", IGRAPH_EINVAL); } igraph_vector_minmax(weights, &d_n, &d_p); if (d_n > 0) { d_n=0; } if (d_p < 0) { d_p=0; } d_n = -d_n; net = new network; net->node_list =new DL_Indexed_List<NNode*>(); net->link_list =new DL_Indexed_List<NLink*>(); net->cluster_list=new DL_Indexed_List<ClusterList<NNode*>*>(); /* Transform the igraph_t */ IGRAPH_CHECK(igraph_i_read_network(graph, weights, net, use_weights, 0)); bool directed = igraph_is_directed(graph); pm=new PottsModelN(net,(unsigned int)spins, directed); /* initialize the random number generator */ RNG_BEGIN(); if ((stoptemp==0.0) && (starttemp==0.0)) zeroT=true; else zeroT=false; //Begin at a high enough temperature kT=pm->FindStartTemp(gamma, gamma_minus, starttemp); /* assign random initial configuration */ pm->assign_initial_conf(true); runs=0; changes=1; acc = 0; while (changes>0 && (kT/stoptemp>1.0 || (zeroT && runs<150))) { IGRAPH_ALLOW_INTERRUPTION(); /* This is not clean.... */ runs++; kT = kT*coolfact; acc=pm->HeatBathLookup(gamma, gamma_minus, kT, 50); if (acc<(1.0-1.0/double(spins))*0.001) changes=0; else changes=1; } /* while loop */ /* These are needed, otherwise 'modularity' is not calculated */ igraph_matrix_t adhesion, normalized_adhesion; igraph_real_t polarization; IGRAPH_MATRIX_INIT_FINALLY(&adhesion, 0, 0); IGRAPH_MATRIX_INIT_FINALLY(&normalized_adhesion, 0, 0); pm->WriteClusters(modularity, temperature, csize, membership, &adhesion, &normalized_adhesion, &polarization, kT, d_p, d_n, gamma, gamma_minus); igraph_matrix_destroy(&normalized_adhesion); igraph_matrix_destroy(&adhesion); IGRAPH_FINALLY_CLEAN(2); while (net->link_list->Size()) delete net->link_list->Pop(); while (net->node_list->Size()) delete net->node_list->Pop(); while (net->cluster_list->Size()) { cl_cur=net->cluster_list->Pop(); while (cl_cur->Size()) cl_cur->Pop(); delete cl_cur; } RNG_END(); return 0; }
int igraph_lapack_dgeevx(igraph_lapack_dgeevx_balance_t balance, const igraph_matrix_t *A, igraph_vector_t *valuesreal, igraph_vector_t *valuesimag, igraph_matrix_t *vectorsleft, igraph_matrix_t *vectorsright, int *ilo, int *ihi, igraph_vector_t *scale, igraph_real_t *abnrm, igraph_vector_t *rconde, igraph_vector_t *rcondv, int *info) { char balanc; char jobvl= vectorsleft ? 'V' : 'N'; char jobvr= vectorsright ? 'V' : 'N'; char sense; int n=(int) igraph_matrix_nrow(A); int lda=n, ldvl=n, ldvr=n, lwork=-1; igraph_vector_t work; igraph_vector_int_t iwork; igraph_matrix_t Acopy; int error=*info; igraph_vector_t *myreal=valuesreal, *myimag=valuesimag, vreal, vimag; igraph_vector_t *myscale=scale, vscale; if (igraph_matrix_ncol(A) != n) { IGRAPH_ERROR("Cannot calculate eigenvalues (dgeevx)", IGRAPH_NONSQUARE); } switch (balance) { case IGRAPH_LAPACK_DGEEVX_BALANCE_NONE: balanc='N'; break; case IGRAPH_LAPACK_DGEEVX_BALANCE_PERM: balanc='P'; break; case IGRAPH_LAPACK_DGEEVX_BALANCE_SCALE: balanc='S'; break; case IGRAPH_LAPACK_DGEEVX_BALANCE_BOTH: balanc='B'; break; default: IGRAPH_ERROR("Invalid 'balance' argument", IGRAPH_EINVAL); break; } if (!rconde && !rcondv) { sense='N'; } else if (rconde && !rcondv) { sense='E'; } else if (!rconde && rcondv) { sense='V'; } else { sense='B'; } IGRAPH_CHECK(igraph_matrix_copy(&Acopy, A)); IGRAPH_FINALLY(igraph_matrix_destroy, &Acopy); IGRAPH_VECTOR_INIT_FINALLY(&work, 1); IGRAPH_CHECK(igraph_vector_int_init(&iwork, n)); IGRAPH_FINALLY(igraph_vector_int_destroy, &iwork); if (!valuesreal) { IGRAPH_VECTOR_INIT_FINALLY(&vreal, n); myreal=&vreal; } else { IGRAPH_CHECK(igraph_vector_resize(myreal, n)); } if (!valuesimag) { IGRAPH_VECTOR_INIT_FINALLY(&vimag, n); myimag=&vimag; } else { IGRAPH_CHECK(igraph_vector_resize(myimag, n)); } if (!scale) { IGRAPH_VECTOR_INIT_FINALLY(&vscale, n); myscale=&vscale; } else { IGRAPH_CHECK(igraph_vector_resize(scale, n)); } if (vectorsleft) { IGRAPH_CHECK(igraph_matrix_resize(vectorsleft, n, n)); } if (vectorsright) { IGRAPH_CHECK(igraph_matrix_resize(vectorsright, n, n)); } igraphdgeevx_(&balanc, &jobvl, &jobvr, &sense, &n, &MATRIX(Acopy,0,0), &lda, VECTOR(*myreal), VECTOR(*myimag), vectorsleft ? &MATRIX(*vectorsleft ,0,0) : 0, &ldvl, vectorsright ? &MATRIX(*vectorsright,0,0) : 0, &ldvr, ilo, ihi, VECTOR(*myscale), abnrm, rconde ? VECTOR(*rconde) : 0, rcondv ? VECTOR(*rcondv) : 0, VECTOR(work), &lwork, VECTOR(iwork), info); lwork=(int) VECTOR(work)[0]; IGRAPH_CHECK(igraph_vector_resize(&work, lwork)); igraphdgeevx_(&balanc, &jobvl, &jobvr, &sense, &n, &MATRIX(Acopy,0,0), &lda, VECTOR(*myreal), VECTOR(*myimag), vectorsleft ? &MATRIX(*vectorsleft ,0,0) : 0, &ldvl, vectorsright ? &MATRIX(*vectorsright,0,0) : 0, &ldvr, ilo, ihi, VECTOR(*myscale), abnrm, rconde ? VECTOR(*rconde) : 0, rcondv ? VECTOR(*rcondv) : 0, VECTOR(work), &lwork, VECTOR(iwork), info); if (*info < 0) { IGRAPH_ERROR("Cannot calculate eigenvalues (dgeev)", IGRAPH_ELAPACK); } else if (*info > 0) { if (error) { IGRAPH_ERROR("Cannot calculate eigenvalues (dgeev)", IGRAPH_ELAPACK); } else { IGRAPH_WARNING("Cannot calculate eigenvalues (dgeev)"); } } if (!scale) { igraph_vector_destroy(&vscale); IGRAPH_FINALLY_CLEAN(1); } if (!valuesimag) { igraph_vector_destroy(&vimag); IGRAPH_FINALLY_CLEAN(1); } if (!valuesreal) { igraph_vector_destroy(&vreal); IGRAPH_FINALLY_CLEAN(1); } igraph_vector_int_destroy(&iwork); igraph_vector_destroy(&work); igraph_matrix_destroy(&Acopy); IGRAPH_FINALLY_CLEAN(3); return 0; }
int igraph_i_eigen_matrix_symmetric_lapack_sm(const igraph_matrix_t *A, const igraph_eigen_which_t *which, igraph_vector_t *values, igraph_matrix_t *vectors) { igraph_vector_t val; igraph_matrix_t vec; int i, w=0, n=(int) igraph_matrix_nrow(A); igraph_real_t small; int p1, p2, pr=0; IGRAPH_VECTOR_INIT_FINALLY(&val, 0); if (vectors) { IGRAPH_MATRIX_INIT_FINALLY(&vec, 0, 0); } IGRAPH_CHECK(igraph_lapack_dsyevr(A, IGRAPH_LAPACK_DSYEV_ALL, /*vl=*/ 0, /*vu=*/ 0, /*vestimate=*/ 0, /*il=*/ 0, /*iu=*/ 0, /*abstol=*/ 1e-14, &val, vectors ? &vec : 0, /*support=*/ 0)); /* Look for smallest value */ small=fabs(VECTOR(val)[0]); for (i=1; i<n; i++) { igraph_real_t v=fabs(VECTOR(val)[i]); if (v < small) { small=v; w=i; } } p1=w-1; p2=w; if (values) { IGRAPH_CHECK(igraph_vector_resize(values, which->howmany)); } if (vectors) { IGRAPH_CHECK(igraph_matrix_resize(vectors, n, which->howmany)); } while (pr < which->howmany) { if (p2 == n-1 || fabs(VECTOR(val)[p1]) < fabs(VECTOR(val)[p2])) { if (values) { VECTOR(*values)[pr]=VECTOR(val)[p1]; } if (vectors) { memcpy(&MATRIX(*vectors,0,pr), &MATRIX(vec,0,p1), sizeof(igraph_real_t) * (size_t) n); } p1--; pr++; } else { if (values) { VECTOR(*values)[pr]=VECTOR(val)[p2]; } if (vectors) { memcpy(&MATRIX(*vectors,0,pr), &MATRIX(vec,0,p2), sizeof(igraph_real_t) * (size_t) n); } p2++; pr++; } } if (vectors) { igraph_matrix_destroy(&vec); IGRAPH_FINALLY_CLEAN(1); } igraph_vector_destroy(&val); IGRAPH_FINALLY_CLEAN(1); return 0; }
/*__________________________________________________________________________ MAIN CYCLE (FLTK CYCLE) ____*/ void mainidle_cb(void*){ //this routine updates the program. //thus, it computes the EVOLUTION double shooted; double dens, err; double totdens, toterr, totimerr; char s[100]; // ---- running controls AND PRINTING if( (amstepping==0 && runningcontrol==1 && graphisloaded==1 && ticks<=maxtime ) || (amstepping==1 && runningcontrol==1 && graphisloaded==1 && ticks<=maxtime && tickstep<=step-1) ) { Evolution(deltat); //PRINTS if((int)printdatabutton->value()==1){ //if have steady state if(usesteady==1){ igraph_matrix_t activation; igraph_matrix_init(&activation,nodesnumber,totrun); igraph_matrix_null(&activation); igraph_vector_t correlation; igraph_vector_init(&correlation,(nodesnumber*nodesnumber)); igraph_vector_null(&correlation); fprintf(output1,"%i ", ticks); fprintf(output2,"%i ", ticks); fprintf(output5,"%i ", ticks); fprintf(output6,"%i ", ticks); totdens=0; toterr=0; for(int i=0;i<nodesnumber;++i){ shooted=0; dens=0; err=0; for(int j=0; j<totrun; ++j){ dens=dens+MATRIX(density,i,j); err=err+((VECTOR(statstate)[i]-MATRIX(density,i,j))*(VECTOR(statstate)[i]-MATRIX(density,i,j))); shooted=shooted+MATRIX(loss,i,j); if(MATRIX(loss,i,j)!=0){++MATRIX(activation,i,j);} } dens=dens/totrun; err=sqrt(err)/totrun; shooted=shooted/totrun; totdens=totdens+dens; toterr=toterr+err; fprintf(output1,"%f ",dens); fprintf(output2,"%f ",err); fprintf(output5,"%f ",shooted); } totdens=totdens/nodesnumber; toterr=toterr/nodesnumber; fprintf(output1,"%f ",totdens); fprintf(output2,"%f ",toterr); // printf("\n\n ACTIVATION MATRIX \n \n"); print_matrix_ur(&activation,stdout); printf("\n\n"); // ---- CORRELATION --- igraph_vector_t meanactivation; igraph_vector_init(&meanactivation,nodesnumber); igraph_vector_null(&meanactivation); //calculate mean activation for (int j=0; j<totrun; ++j) { for (int i=0; i<nodesnumber; ++i) { VECTOR(meanactivation)[i]=VECTOR(meanactivation)[i]+MATRIX(activation,i,j); } } igraph_vector_scale(&meanactivation,1./totrun); //calculate actual correlation for (int x=0; x<nodesnumber ; ++x) { for(int y=0; y<nodesnumber; ++y){ double prod=0; for (int j=0; j<totrun; ++j) { prod=prod+ ( MATRIX(activation,x,j)*MATRIX(activation,y,j) ); } prod=prod/totrun; VECTOR(correlation)[(x*nodesnumber+y)] = prod - (VECTOR(meanactivation)[x]*VECTOR(meanactivation)[y]); } } igraph_vector_destroy(&meanactivation); for (int i=0; i<(nodesnumber*nodesnumber); ++i) { fprintf(output6,"%f ",VECTOR(correlation)[i]); } //calculate error on run igraph_matrix_t distl1; igraph_matrix_t distimel1; igraph_matrix_init(&distl1,nodesnumber,totrun); igraph_matrix_init(&distimel1,nodesnumber,totrun); igraph_matrix_null(&distl1); igraph_matrix_null(&distimel1); igraph_vector_t rundistl1; igraph_vector_t rundistimel1; igraph_vector_init(&rundistl1,totrun); igraph_vector_init(&rundistimel1,totrun); igraph_vector_null(&rundistl1); igraph_vector_null(&rundistimel1); toterr=0; totimerr=0; //for every run for(int j=0;j<totrun;++j){ //i evaluate the distance between the state and the stationary state (toterr) and the distance between old and new density (totimerr) for(int i=0; i<nodesnumber; ++i) { //L1 DISTANCE WRT STATSTATE & DENSITY MATRIX(distl1,i,j)=fabs(VECTOR(statstate)[i]-MATRIX(density,i,j)); //L1 DISTANCE WRT OLD DENSITY & DENSITY MATRIX(distimel1,i,j)=fabs(MATRIX(densityold,i,j)-MATRIX(density,i,j)); } } igraph_matrix_rowsum(&distl1,&rundistl1); igraph_matrix_rowsum(&distimel1,&rundistimel1); igraph_vector_scale(&rundistl1,(1./nodesnumber)); igraph_vector_scale(&rundistimel1,(1./nodesnumber)); toterr= (double)( igraph_vector_sum(&rundistl1) ) / (double)totrun ; totimerr= (double)( igraph_vector_sum(&rundistimel1)) / (double)totrun; igraph_vector_destroy(&rundistl1); igraph_vector_destroy(&rundistimel1); igraph_matrix_destroy(&distl1); igraph_matrix_destroy(&distimel1); fprintf(output2,"%f %f",toterr, totimerr); fprintf(output1,"\n"); fprintf(output2,"\n"); fprintf(output5,"\n"); fprintf(output6,"\n"); //if i have BRIDGES ("clustered" graph), I print the traffic on the BRIDGES, using "output3" file if(isclustered==1){ //for each bridge fprintf(output3,"%i ",ticks); for(int nbri=0; nbri<igraph_matrix_ncol(&bridgeslinks); ++nbri){ for(int i=0; i<igraph_matrix_nrow(&bridgeslinks);++i){ double tfl=0; for(int j=0; j<totrun; ++j){ int beid; beid=(int)MATRIX(bridgeslinks,i,nbri); tfl=tfl+MATRIX(flux,beid,j); } fprintf(output3,"%f ",tfl); } } fprintf(output3,"\n"); } igraph_matrix_destroy(&activation); igraph_vector_destroy(&correlation); } //if i HAVENT STEADY STATE else { fprintf(output1,"%i ", ticks); fprintf(output5,"%i ", ticks); for(int i=0;i<nodesnumber;++i){ shooted=0; dens=0; for(int j=0; j<totrun; ++j){ dens=dens+MATRIX(density,i,j); shooted=shooted+MATRIX(loss,i,j); } dens=dens/totrun; shooted=shooted/totrun; fprintf(output1,"%f " ,dens); fprintf(output5,"%f " ,shooted); } fprintf(output1,"\n"); fprintf(output5,"\n"); } } } if((ticks==maxtime || tickstep==step) && runningcontrol==1){ run(); } // ---- no graph loaded if(graphisloaded==0 && rewrite==1) { runbutton->deactivate(); sprintf(s,"No\nnetwork\nloaded"); databuff->text(s); } // ---- graph loaded else { runbutton->activate(); if(islattice==1 && rewrite==1){ if(istoro==1){ sprintf(s,"Nodes=%i\nToroidal\nLattice\n%iD Side=%i",nodesnumber, latticedim, latticeside); } else{ sprintf(s,"Nodes=%i\nLattice\n%iD Side=%i",nodesnumber, latticedim, latticeside); } databuff->text(s); } else if(rewrite==1){ sprintf(s,"Nodes=%i",nodesnumber); databuff->text(s); } } //have path if(havepath==1 && rewrite==1){pathbuff->text(path); } else if(havepath==0 && rewrite==1){pathbuff->text("No Path");} if(error==1 && rewrite==1){ sprintf(s,errorstring); databuff->text(s); } if (ticks<=maxtime){ scene->redraw(); datascene->redraw(); } rewrite=0; //Fl::repeat_timeout(1.0, mainidle_cb); }
int igraph_i_eigen_matrix_symmetric_lapack(const igraph_matrix_t *A, const igraph_sparsemat_t *sA, igraph_arpack_function_t *fun, int n, void *extra, const igraph_eigen_which_t *which, igraph_vector_t *values, igraph_matrix_t *vectors) { const igraph_matrix_t *myA=A; igraph_matrix_t mA; /* First we need to create a dense square matrix */ if (A) { n=(int) igraph_matrix_nrow(A); } else if (sA) { n=(int) igraph_sparsemat_nrow(sA); IGRAPH_CHECK(igraph_matrix_init(&mA, 0, 0)); IGRAPH_FINALLY(igraph_matrix_destroy, &mA); IGRAPH_CHECK(igraph_sparsemat_as_matrix(&mA, sA)); myA=&mA; } else if (fun) { IGRAPH_CHECK(igraph_i_eigen_arpackfun_to_mat(fun, n, extra, &mA)); IGRAPH_FINALLY(igraph_matrix_destroy, &mA); myA=&mA; } switch (which->pos) { case IGRAPH_EIGEN_LM: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_lm(myA, which, values, vectors)); break; case IGRAPH_EIGEN_SM: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_sm(myA, which, values, vectors)); break; case IGRAPH_EIGEN_LA: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_la(myA, which, values, vectors)); break; case IGRAPH_EIGEN_SA: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_sa(myA, which, values, vectors)); break; case IGRAPH_EIGEN_BE: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_be(myA, which, values, vectors)); break; case IGRAPH_EIGEN_ALL: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_all(myA, values, vectors)); break; case IGRAPH_EIGEN_INTERVAL: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_iv(myA, which, values, vectors)); break; case IGRAPH_EIGEN_SELECT: IGRAPH_CHECK(igraph_i_eigen_matrix_symmetric_lapack_sel(myA, which, values, vectors)); break; default: /* This cannot happen */ break; } if (!A) { igraph_matrix_destroy(&mA); IGRAPH_FINALLY_CLEAN(1); } return 0; }
int main() { igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t adj, V; igraph_vector_t groups; igraph_eigen_which_t which; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&adj, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_vector_init(&groups, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_get_adjacency(&g, &adj, IGRAPH_GET_ADJACENCY_BOTH, /*eids=*/ 0); which.pos=IGRAPH_EIGEN_LM; which.howmany=1; igraph_eigen_matrix_symmetric(&adj, /*sparsemat=*/ 0, /*fun=*/ 0, igraph_vcount(&g), /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_SYMMETRIC, &L, &R, \ &Lsparse, &Rsparse, /*p=*/ 0, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_vector_destroy(&groups); igraph_matrix_destroy(&V); igraph_matrix_destroy(&adj); igraph_destroy(&g); return 0; }
int igraph_i_eigen_matrix_symmetric_arpack_be(const igraph_matrix_t *A, const igraph_sparsemat_t *sA, igraph_arpack_function_t *fun, int n, void *extra, const igraph_eigen_which_t *which, igraph_arpack_options_t *options, igraph_arpack_storage_t *storage, igraph_vector_t *values, igraph_matrix_t *vectors) { igraph_vector_t tmpvalues, tmpvalues2; igraph_matrix_t tmpvectors, tmpvectors2; igraph_i_eigen_matrix_sym_arpack_data_t myextra = { A, sA }; int low=(int) floor(which->howmany/2.0), high=(int) ceil(which->howmany/2.0); int l1, l2, w; if (low + high >= n) { IGRAPH_ERROR("Requested too many eigenvalues/vectors", IGRAPH_EINVAL); } if (!fun) { fun=igraph_i_eigen_matrix_sym_arpack_cb; extra=(void*) &myextra; } IGRAPH_VECTOR_INIT_FINALLY(&tmpvalues, high); IGRAPH_MATRIX_INIT_FINALLY(&tmpvectors, n, high); IGRAPH_VECTOR_INIT_FINALLY(&tmpvalues2, low); IGRAPH_MATRIX_INIT_FINALLY(&tmpvectors2, n, low); options->n=n; options->nev=high; options->ncv= 2*options->nev < n ? 2*options->nev : n; options->which[0]='L'; options->which[1]='A'; IGRAPH_CHECK(igraph_arpack_rssolve(fun, extra, options, storage, &tmpvalues, &tmpvectors)); options->nev=low; options->ncv= 2*options->nev < n ? 2*options->nev : n; options->which[0]='S'; options->which[1]='A'; IGRAPH_CHECK(igraph_arpack_rssolve(fun, extra, options, storage, &tmpvalues2, &tmpvectors2)); IGRAPH_CHECK(igraph_vector_resize(values, low+high)); IGRAPH_CHECK(igraph_matrix_resize(vectors, n, low+high)); l1=0; l2=0; w=0; while (w < which->howmany) { VECTOR(*values)[w] = VECTOR(tmpvalues)[l1]; memcpy(&MATRIX(*vectors, 0, w), &MATRIX(tmpvectors, 0, l1), (size_t) n * sizeof(igraph_real_t)); w++; l1++; if (w < which->howmany) { VECTOR(*values)[w] = VECTOR(tmpvalues2)[l2]; memcpy(&MATRIX(*vectors, 0, w), &MATRIX(tmpvectors2, 0, l2), (size_t) n * sizeof(igraph_real_t)); w++; l2++; } } igraph_matrix_destroy(&tmpvectors2); igraph_vector_destroy(&tmpvalues2); igraph_matrix_destroy(&tmpvectors); igraph_vector_destroy(&tmpvalues); IGRAPH_FINALLY_CLEAN(4); return 0; }