예제 #1
0
int igraph_2dgrid_init(igraph_2dgrid_t *grid, igraph_matrix_t *coords,
                       igraph_real_t minx, igraph_real_t maxx, igraph_real_t deltax,
                       igraph_real_t miny, igraph_real_t maxy, igraph_real_t deltay) {
    long int i;

    grid->coords=coords;
    grid->minx=minx;
    grid->maxx=maxx;
    grid->deltax=deltax;
    grid->miny=miny;
    grid->maxy=maxy;
    grid->deltay=deltay;

    grid->stepsx=(long int) ceil((maxx-minx)/deltax);
    grid->stepsy=(long int) ceil((maxy-miny)/deltay);

    IGRAPH_CHECK(igraph_matrix_init(&grid->startidx,
                                    grid->stepsx, grid->stepsy));
    IGRAPH_FINALLY(igraph_matrix_destroy, &grid->startidx);
    IGRAPH_VECTOR_INIT_FINALLY(&grid->next, igraph_matrix_nrow(coords));
    IGRAPH_VECTOR_INIT_FINALLY(&grid->prev, igraph_matrix_nrow(coords));

    for (i=0; i<igraph_vector_size(&grid->next); i++) {
        VECTOR(grid->next)[i]=-1;
    }

    grid->massx=0;
    grid->massy=0;
    grid->vertices=0;

    IGRAPH_FINALLY_CLEAN(3);
    return 0;
}
예제 #2
0
igraph_bool_t check_ev(const igraph_matrix_t *A, 
		       const igraph_vector_t *values_real,
		       const igraph_vector_t *values_imag,
		       const igraph_matrix_t *vectors_left, 
		       const igraph_matrix_t *vectors_right, 
		       igraph_real_t tol) {

  int n=igraph_matrix_nrow(A);
  igraph_vector_t v_real, v_imag;
  igraph_vector_t AV_real, AV_imag, lv_real, lv_imag;
  igraph_vector_t null;
  int i;
  
  if (igraph_matrix_ncol(A)             != n) { return 1; }
  if (igraph_vector_size(values_real)   != n) { return 1; }
  if (igraph_vector_size(values_imag)   != n) { return 1; }
  if (igraph_matrix_nrow(vectors_left)  != n) { return 1; }
  if (igraph_matrix_ncol(vectors_left)  != n) { return 1; }
  if (igraph_matrix_nrow(vectors_right) != n) { return 1; }
  if (igraph_matrix_ncol(vectors_right) != n) { return 1; }

  igraph_vector_init(&AV_real, n);
  igraph_vector_init(&AV_imag, n);
  igraph_vector_init(&lv_real, n);
  igraph_vector_init(&lv_imag, n);
  igraph_vector_init(&null, n);
  igraph_vector_null(&null);

  for (i=0; i<n; i++) {
    if (VECTOR(*values_imag)[i]==0.0) {
      igraph_vector_view(&v_real, &MATRIX(*vectors_right, 0, i), n);
      igraph_vector_view(&v_imag, VECTOR(null), n);
    } else if (VECTOR(*values_imag)[i] > 0.0) {
      igraph_vector_view(&v_real, &MATRIX(*vectors_right, 0, i), n);
      igraph_vector_view(&v_imag, &MATRIX(*vectors_right, 0, i+1), n);
    } else if (VECTOR(*values_imag)[i] < 0.0) {
      igraph_vector_view(&v_real, &MATRIX(*vectors_right, 0, i-1), n);
      igraph_vector_view(&v_imag, &MATRIX(*vectors_right, 0, i), n);
      igraph_vector_scale(&v_imag, -1.0);
    }
    real_cplx_mult(A, &v_real, &v_imag, &AV_real, &AV_imag);
    sc_cplx_cplx_mult(VECTOR(*values_real)[i], VECTOR(*values_imag)[i],
		      &v_real, &v_imag, &lv_real, &lv_imag);
    
    if (igraph_vector_maxdifference(&AV_real, &lv_real) > tol ||
	igraph_vector_maxdifference(&AV_imag, &lv_imag) > tol) {
      igraph_vector_print(&AV_real); igraph_vector_print(&AV_imag);
      igraph_vector_print(&lv_real); igraph_vector_print(&lv_imag);      
      return 1;
    }
  }

  igraph_vector_destroy(&null);
  igraph_vector_destroy(&AV_imag);
  igraph_vector_destroy(&AV_real);
  igraph_vector_destroy(&lv_imag);
  igraph_vector_destroy(&lv_real);
  
  return 0;
}
예제 #3
0
int igraph_lapack_dgetrs(igraph_bool_t transpose, const igraph_matrix_t *a,
			 igraph_vector_int_t *ipiv, igraph_matrix_t *b) {
  char trans = transpose ? 'T' : 'N';
  int n=(int) igraph_matrix_nrow(a);
  int nrhs=(int) igraph_matrix_ncol(b);
  int lda= n > 0 ? n : 1;
  int ldb= n > 0 ? n : 1;
  int info;

  if (n != igraph_matrix_ncol(a)) {
    IGRAPH_ERROR("Cannot LU solve matrix", IGRAPH_NONSQUARE);
  }
  if (n != igraph_matrix_nrow(b)) {
    IGRAPH_ERROR("Cannot LU solve matrix, RHS of wrong size", IGRAPH_EINVAL);
  }

  igraphdgetrs_(&trans, &n, &nrhs, VECTOR(a->data), &lda, VECTOR(*ipiv),
		VECTOR(b->data), &ldb, &info);

  if (info < 0) {
    switch(info) { 
    case -1:
      IGRAPH_ERROR("Invalid transpose argument", IGRAPH_ELAPACK);
      break;
    case -2:
      IGRAPH_ERROR("Invalid number of rows/columns", IGRAPH_ELAPACK);
      break;
    case -3:
      IGRAPH_ERROR("Invalid number of RHS vectors", IGRAPH_ELAPACK);
      break;
    case -4:
      IGRAPH_ERROR("Invalid LU matrix", IGRAPH_ELAPACK);
      break;
    case -5: 
      IGRAPH_ERROR("Invalid LDA parameter", IGRAPH_ELAPACK);
      break;
    case -6:
      IGRAPH_ERROR("Invalid pivot vector", IGRAPH_ELAPACK);
      break;
    case -7:
      IGRAPH_ERROR("Invalid RHS matrix", IGRAPH_ELAPACK);
      break;
    case -8:
      IGRAPH_ERROR("Invalid LDB parameter", IGRAPH_ELAPACK);
      break;
    case -9:
      IGRAPH_ERROR("Invalid info argument", IGRAPH_ELAPACK);
      break;
    default:
      IGRAPH_ERROR("Unknown LAPACK error", IGRAPH_ELAPACK);
      break;
    }
  }
		
  return 0;
}
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 check_ev(const igraph_matrix_t *A, const igraph_vector_t *values,
	     const igraph_matrix_t *vectors) {

  int i, n=igraph_matrix_nrow(A);
  int ne=igraph_matrix_ncol(vectors);
  igraph_vector_t v, lhs, rhs;

  if (ne != igraph_vector_size(values)) {
    printf("'values' and 'vectors' sizes do not match\n");
    exit(1);
  }

  igraph_vector_init(&lhs, n);
  igraph_vector_init(&rhs, n);

  for (i=0; i<ne; i++) {
    igraph_vector_view(&v, &MATRIX(*vectors, 0, i), n);
    igraph_blas_dgemv(/*transpose=*/ 0, /*alpha=*/ 1, A, &v, 
		      /*beta=*/ 0, &lhs);
    igraph_vector_update(&rhs, &v);
    igraph_vector_scale(&rhs, VECTOR(*values)[i]);
    if (igraph_vector_maxdifference(&lhs, &rhs) > 1e-10) { 
      printf("LHS: "); igraph_vector_print(&lhs);
      printf("RHS: "); igraph_vector_print(&rhs);
      exit(2);
    }
  }
  
  igraph_vector_destroy(&rhs);
  igraph_vector_destroy(&lhs);
  
  return 0;
}
예제 #6
0
파일: eigen.c 프로젝트: AlessiaWent/igraph
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;
  
}
예제 #7
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;

}
예제 #8
0
int real_cplx_mult(const igraph_matrix_t *A,
		   const igraph_vector_t *v_real,
		   const igraph_vector_t *v_imag, 
		   igraph_vector_t *res_real,
		   igraph_vector_t *res_imag) {

  int n=igraph_vector_size(v_real);
  int r, c;

  if (igraph_matrix_nrow(A) != n || 
      igraph_matrix_ncol(A) != n || 
      igraph_vector_size(v_imag) != n) {
    printf("Wrong matrix or vector size");
    return 1;
  }

  igraph_vector_resize(res_real, n);
  igraph_vector_resize(res_imag, n);

  for (r=0; r<n; r++) {
    igraph_real_t s_real=0.0;
    igraph_real_t s_imag=0.0;
    for (c=0; c<n; c++) {
      s_real += MATRIX(*A, r, c) * VECTOR(*v_real)[c];
      s_imag += MATRIX(*A, r, c) * VECTOR(*v_imag)[c];
    }
    VECTOR(*res_real)[r]=s_real;
    VECTOR(*res_imag)[r]=s_imag;
  }
  
  return 0;
}
예제 #9
0
/* call-seq:
 *   matrix.nrow -> Integer
 *
 * Returns the number of rows in the matrix.
 */
VALUE cIGraph_matrix_nrow(VALUE self){

  igraph_matrix_t *m;

  Data_Get_Struct(self, igraph_matrix_t, m);
  return LONG2FIX(igraph_matrix_nrow(m));

}
예제 #10
0
void byrow(igraph_matrix_t *m) {
  long int r=igraph_matrix_nrow(m), c=igraph_matrix_ncol(m);
  long int n=0, i, j;
  for (i=0; i<r; i++) {
    for (j=0; j<c; j++) {
      MATRIX(*m, i, j) = n++;
    }
  }
}
예제 #11
0
void print_matrix(igraph_matrix_t *m) {
  long int i, j;
  for (i=0; i<igraph_matrix_nrow(m); i++) {
    for (j=0; j<igraph_matrix_ncol(m); j++) {
      printf(" %g", MATRIX(*m, i, j));
    }
    printf("\n");
  }  
}
void print_matrix(igraph_matrix_t *m, FILE *f) {
  long int i, j;
  for (i=0; i<igraph_matrix_nrow(m); i++) {
    for (j=0; j<igraph_matrix_ncol(m); j++) {
      fprintf(f, " %li", (long int) MATRIX(*m, i, j));
    }
    fprintf(f, "\n");
  }
}
int print_matrix(const igraph_matrix_t *m) {
  long int i, j, nrow=igraph_matrix_nrow(m), ncol=igraph_matrix_ncol(m);
  for (i=0; i<nrow; i++) {
    for (j=0; j<ncol; j++) {
      printf("%.2g", (double)MATRIX(*m, i, j));
      if (j!=ncol-1) { printf(" "); }
    }
    printf("\n");
  }
  return 0;
}
예제 #14
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;

}
void print_matrix(igraph_matrix_t* m) {
  long int nr=igraph_matrix_nrow(m);
  long int nc=igraph_matrix_ncol(m);
  long int i, j;
  for (i=0; i<nr; i++) {
    for (j=0; j<nc; j++) {
      if (j!=0) { putchar(' '); }
      printf("%d", (int)MATRIX(*m, i, j));
    }
    printf("\n");
  }
}
예제 #16
0
int print_matrix(const igraph_matrix_t *m) {
  long int nrow=igraph_matrix_nrow(m);
  long int ncol=igraph_matrix_ncol(m);
  long int i, j;
  for (i=0; i<nrow; i++) {
    printf("%li:", i);
    for (j=0; j<ncol; j++) {
      printf(" %3.0F", MATRIX(*m, i, j));
    }
    printf("\n");
  }
  return 0;
}
예제 #17
0
int igraph_lapack_dgetrf(igraph_matrix_t *a, igraph_vector_int_t *ipiv, 
			 int *info) {
  int m=(int) igraph_matrix_nrow(a);
  int n=(int) igraph_matrix_ncol(a);
  int lda=m > 0 ? m : 1;
  igraph_vector_int_t *myipiv=ipiv, vipiv;

  if (!ipiv) {
    IGRAPH_CHECK(igraph_vector_int_init(&vipiv, m<n ? m : n));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &vipiv);
    myipiv=&vipiv;
  }

  igraphdgetrf_(&m, &n, VECTOR(a->data), &lda, VECTOR(*myipiv), info);

  if (*info > 0) {
    IGRAPH_WARNING("LU: factor is exactly singular");
  } else if (*info < 0) {
    switch(*info) { 
    case -1:
      IGRAPH_ERROR("Invalid number of rows", IGRAPH_ELAPACK);
      break;
    case -2:
      IGRAPH_ERROR("Invalid number of columns", IGRAPH_ELAPACK);
      break;
    case -3:
      IGRAPH_ERROR("Invalid input matrix", IGRAPH_ELAPACK);
      break;
    case -4:
      IGRAPH_ERROR("Invalid LDA parameter", IGRAPH_ELAPACK);
      break;
    case -5:
      IGRAPH_ERROR("Invalid pivot vector", IGRAPH_ELAPACK);
      break;
    case -6:
      IGRAPH_ERROR("Invalid info argument", IGRAPH_ELAPACK);
      break;
    default:
      IGRAPH_ERROR("Unknown LAPACK error", IGRAPH_ELAPACK);
      break;
    }
  }

  if (!ipiv) {
    igraph_vector_int_destroy(&vipiv);
    IGRAPH_FINALLY_CLEAN(1);
  }
  
  return 0;
}
예제 #18
0
파일: dotproduct.c 프로젝트: abeham/igraph
int igraph_dot_product_game(igraph_t *graph, const igraph_matrix_t *vecs,
			    igraph_bool_t directed) {

  igraph_integer_t nrow=igraph_matrix_nrow(vecs);
  igraph_integer_t ncol=igraph_matrix_ncol(vecs);
  int i, j;
  igraph_vector_t edges;
  igraph_bool_t warned_neg=0, warned_big=0;
  
  IGRAPH_VECTOR_INIT_FINALLY(&edges, 0);
    
  RNG_BEGIN();

  for (i = 0; i < ncol; i++) {
    int from=directed ? 0 : i+1;
    igraph_vector_t v1;
    igraph_vector_view(&v1, &MATRIX(*vecs, 0, i), nrow);
    for (j = from; j < ncol; j++) {
      igraph_real_t prob;
      igraph_vector_t v2;
      if (i==j) { continue; }
      igraph_vector_view(&v2, &MATRIX(*vecs, 0, j), nrow);
      igraph_lapack_ddot(&v1, &v2, &prob);
      if (prob < 0 && ! warned_neg) {
	warned_neg=1;
	IGRAPH_WARNING("Negative connection probability in "
		       "dot-product graph");
      } else if (prob > 1 && ! warned_big) {
	warned_big=1;
	IGRAPH_WARNING("Greater than 1 connection probability in "
		       "dot-product graph");
	IGRAPH_CHECK(igraph_vector_push_back(&edges, i));
	IGRAPH_CHECK(igraph_vector_push_back(&edges, j));
      } else if (RNG_UNIF01() < prob) { 
	IGRAPH_CHECK(igraph_vector_push_back(&edges, i));
	IGRAPH_CHECK(igraph_vector_push_back(&edges, j));
      }
    }
  }

  RNG_END();
  
  igraph_create(graph, &edges, ncol, directed);
  igraph_vector_destroy(&edges);
  IGRAPH_FINALLY_CLEAN(1);

  return 0;
}
예제 #19
0
파일: eigen.c 프로젝트: AlessiaWent/igraph
int igraph_i_eigen_matrix_symmetric_lapack_la(const igraph_matrix_t *A,
			      const igraph_eigen_which_t *which,
			      igraph_vector_t *values,
			      igraph_matrix_t *vectors) {

  /* TODO: ordering? */
  
  int n=(int) igraph_matrix_nrow(A);
  int il=n-which->howmany+1;
  IGRAPH_CHECK(igraph_lapack_dsyevr(A, IGRAPH_LAPACK_DSYEV_SELECT,
				    /*vl=*/ 0, /*vu=*/ 0, /*vestimate=*/ 0,
				    /*il=*/ il, /*iu=*/ n, 
				    /*abstol=*/ 1e-14, values, vectors, 
				    /*support=*/ 0));
  return 0;
}
예제 #20
0
/* 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;

}
예제 #21
0
/**
 * \ingroup structural
 * \function igraph_similarity_dice
 * \brief Dice similarity coefficient.
 *
 * </para><para>
 * The Dice similarity coefficient of two vertices is twice the number of common
 * neighbors divided by the sum of the degrees of the vertices. This function
 * calculates the pairwise Dice similarities for some (or all) of the vertices.
 *
 * \param graph The graph object to analyze
 * \param res Pointer to a matrix, the result of the calculation will
 *        be stored here. The number of its rows and columns is the same
 *        as the number of vertex ids in \p vids.
 * \param vids The vertex ids of the vertices for which the
 *        calculation will be done.
 * \param mode The type of neighbors to be used for the calculation in
 *        directed graphs. Possible values:
 *        \clist
 *        \cli IGRAPH_OUT
 *          the outgoing edges will be considered for each node.
 *        \cli IGRAPH_IN
 *          the incoming edges will be considered for each node.
 *        \cli IGRAPH_ALL
 *          the directed graph is considered as an undirected one for the
 *          computation.
 *        \endclist
 * \param loops Whether to include the vertices themselves as their own
 *        neighbors.
 * \return Error code:
 *        \clist
 *        \cli IGRAPH_ENOMEM
 *           not enough memory for temporary data.
 *        \cli IGRAPH_EINVVID
 *           invalid vertex id passed.
 *        \cli IGRAPH_EINVMODE
 *           invalid mode argument.
 *        \endclist
 * 
 * Time complexity: O(|V|^2 d),
 * |V| is the number of vertices in the vertex iterator given, d is the
 * (maximum) degree of the vertices in the graph.
 *
 * \sa \ref igraph_similarity_jaccard(), a measure very similar to the Dice
 *   coefficient
 * 
 * \example examples/simple/igraph_similarity.c
 */
int igraph_similarity_dice(const igraph_t *graph, igraph_matrix_t *res,
    const igraph_vs_t vids, igraph_neimode_t mode, igraph_bool_t loops) {
  long int i, j, nr, nc;

  IGRAPH_CHECK(igraph_similarity_jaccard(graph, res, vids, mode, loops));

  nr = igraph_matrix_nrow(res);
  nc = igraph_matrix_ncol(res);
  for (i = 0; i < nr; i++) {
    for (j = 0; j < nc; j++) {
      igraph_real_t x = MATRIX(*res, i, j);
      MATRIX(*res, i, j) = 2*x / (1+x);
    }
  }

  return IGRAPH_SUCCESS;
}
예제 #22
0
파일: eigen.c 프로젝트: AlessiaWent/igraph
int igraph_i_eigen_checks(const igraph_matrix_t *A, 
			  const igraph_sparsemat_t *sA,
			  igraph_arpack_function_t *fun, int n) {
  
  if ( (A?1:0)+(sA?1:0)+(fun?1:0) != 1) {
    IGRAPH_ERROR("Exactly one of 'A', 'sA' and 'fun' must be given", 
		 IGRAPH_EINVAL);
  }

  if (A) {
    if (n != igraph_matrix_ncol(A) || n != igraph_matrix_nrow(A)) {
      IGRAPH_ERROR("Invalid matrix", IGRAPH_NONSQUARE);
    }
  } else if (sA) {
    if (n != igraph_sparsemat_ncol(sA) || n != igraph_sparsemat_nrow(sA)) {
      IGRAPH_ERROR("Invalid matrix", IGRAPH_NONSQUARE);
    }
  }

  return 0;
}
예제 #23
0
void show_results(igraph_t *g, igraph_vector_t *membership, igraph_matrix_t *memberships, igraph_vector_t *modularity, FILE* f) {
  long int i, j, no_of_nodes = igraph_vcount(g);

  j=igraph_vector_which_max(modularity);
  for (i=0; i<igraph_vector_size(membership); i++) {
    if (VECTOR(*membership)[i] != MATRIX(*memberships, j, i)) {
      fprintf(f, "WARNING: best membership vector element %li does not match the best one in the membership matrix\n", i);
    }
  }

  fprintf(f, "Modularities:\n");
  igraph_vector_print(modularity);

  for (i=0; i < igraph_matrix_nrow(memberships); i++) {
    for (j=0; j < no_of_nodes; j++) {
      fprintf(f, "%ld ", (long int)MATRIX(*memberships, i, j));
    }
    fprintf(f, "\n");
  }

  fprintf(f, "\n");
}
예제 #24
0
파일: Main.cpp 프로젝트: ETRu/Laura
/*__________________________________________________________________________ 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);
    
}
예제 #25
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;
}
예제 #26
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;
}
예제 #27
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;
}
예제 #28
0
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;
}
예제 #29
0
int igraph_lapack_dgesv(igraph_matrix_t *a, igraph_vector_int_t *ipiv,
			igraph_matrix_t *b, int *info) {

  int n=(int) igraph_matrix_nrow(a);
  int nrhs=(int) igraph_matrix_ncol(b);
  int lda= n > 0 ? n : 1;
  int ldb= n > 0 ? n : 1;
  igraph_vector_int_t *myipiv=ipiv, vipiv;

  if (n != igraph_matrix_ncol(a)) {
    IGRAPH_ERROR("Cannot LU solve matrix", IGRAPH_NONSQUARE);
  }
  if (n != igraph_matrix_nrow(b)) {
    IGRAPH_ERROR("Cannot LU solve matrix, RHS of wrong size", IGRAPH_EINVAL);
  }

  if (!ipiv) {
    IGRAPH_CHECK(igraph_vector_int_init(&vipiv, n));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &vipiv);
    myipiv=&vipiv;
  }
  
  igraphdgesv_(&n, &nrhs, VECTOR(a->data), &lda, VECTOR(*myipiv),
	       VECTOR(b->data), &ldb, info);

  if (*info > 0) {
    IGRAPH_WARNING("LU: factor is exactly singular");
  } else if (*info < 0) {
    switch(*info) { 
    case -1:
      IGRAPH_ERROR("Invalid number of rows/column", IGRAPH_ELAPACK);
      break;
    case -2:
      IGRAPH_ERROR("Invalid number of RHS vectors", IGRAPH_ELAPACK);
      break;
    case -3:
      IGRAPH_ERROR("Invalid input matrix", IGRAPH_ELAPACK);
      break;
    case -4:
      IGRAPH_ERROR("Invalid LDA parameter", IGRAPH_ELAPACK);
      break;
    case -5:
      IGRAPH_ERROR("Invalid pivot vector", IGRAPH_ELAPACK);
      break;
    case -6:
      IGRAPH_ERROR("Invalid RHS matrix", IGRAPH_ELAPACK);
      break;
    case -7:
      IGRAPH_ERROR("Invalid LDB parameter", IGRAPH_ELAPACK);
      break;
    case -8:
      IGRAPH_ERROR("Invalid info argument", IGRAPH_ELAPACK);
      break;
    default:
      IGRAPH_ERROR("Unknown LAPACK error", IGRAPH_ELAPACK);
      break;
    }
  }
		
  if (!ipiv) {
    igraph_vector_int_destroy(&vipiv);
    IGRAPH_FINALLY_CLEAN(1);
  }
  
  return 0;
}
예제 #30
0
long int Matrix::nrow() const noexcept { return igraph_matrix_nrow(ptr()); }