From df8386f75b0538075d72d52693836bb8878f505b Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Mon, 19 Oct 2015 16:23:00 +0100 Subject: First commit of MAMMULT code --- dynamics/ising/Makefile | 11 + dynamics/ising/iltree.c | 201 +++++++++++++ dynamics/ising/iltree.h | 55 ++++ dynamics/ising/multiplex_ising.c | 313 ++++++++++++++++++++ dynamics/ising/utils.c | 494 ++++++++++++++++++++++++++++++++ dynamics/ising/utils.h | 58 ++++ dynamics/randomwalks/Makefile | 20 ++ dynamics/randomwalks/entropyrate2add.c | 147 ++++++++++ dynamics/randomwalks/entropyrate2int.c | 151 ++++++++++ dynamics/randomwalks/entropyrate2mult.c | 149 ++++++++++ dynamics/randomwalks/iltree.c | 201 +++++++++++++ dynamics/randomwalks/iltree.h | 55 ++++ dynamics/randomwalks/statdistr2.c | 231 +++++++++++++++ dynamics/randomwalks/utils.c | 494 ++++++++++++++++++++++++++++++++ dynamics/randomwalks/utils.h | 58 ++++ 15 files changed, 2638 insertions(+) create mode 100644 dynamics/ising/Makefile create mode 100755 dynamics/ising/iltree.c create mode 100755 dynamics/ising/iltree.h create mode 100644 dynamics/ising/multiplex_ising.c create mode 100755 dynamics/ising/utils.c create mode 100755 dynamics/ising/utils.h create mode 100644 dynamics/randomwalks/Makefile create mode 100755 dynamics/randomwalks/entropyrate2add.c create mode 100755 dynamics/randomwalks/entropyrate2int.c create mode 100755 dynamics/randomwalks/entropyrate2mult.c create mode 100755 dynamics/randomwalks/iltree.c create mode 100755 dynamics/randomwalks/iltree.h create mode 100755 dynamics/randomwalks/statdistr2.c create mode 100755 dynamics/randomwalks/utils.c create mode 100755 dynamics/randomwalks/utils.h (limited to 'dynamics') diff --git a/dynamics/ising/Makefile b/dynamics/ising/Makefile new file mode 100644 index 0000000..1c09fa1 --- /dev/null +++ b/dynamics/ising/Makefile @@ -0,0 +1,11 @@ +CFLAGS="-O3" +CC="gcc" +MFLAG=-lm + +all: multiplex_ising + +multiplex_ising: multiplex_ising.c + $(CC) $(CFLAGS) -o multiplex_ising multiplex_ising.c utils.c iltree.c $(MFLAG) + +clean: + rm multiplex_ising diff --git a/dynamics/ising/iltree.c b/dynamics/ising/iltree.c new file mode 100755 index 0000000..5a693d0 --- /dev/null +++ b/dynamics/ising/iltree.c @@ -0,0 +1,201 @@ +/* + * + * A simple insert-lookup static btree datatype + * + */ + +#include +#include "iltree.h" +#include + + +void __recursive_preorder(node_t *cur, ilfunc_t *funs){ + + if(cur->left){ + __recursive_preorder(cur->left, funs); + } + funs->print(cur->info, funs->fileout); + if(cur->right){ + __recursive_preorder(cur->right, funs); + } +} + +/* + * + * Recursive push of nodes in the nodecache :-) + * + */ + +void __recursive_destroy(node_t *cur, ilfunc_t *funs){ + if(cur->left){ + __recursive_destroy(cur->left, funs); + cur->left = NULL; + } + if(cur->right){ + __recursive_destroy(cur->right, funs); + cur->right = NULL; + } +} + + +int __recursive_insert(node_t *cur, node_t *elem, ilfunc_t *f){ + + int res ; + res = f->compare(cur->info, elem->info); + /* printf("res: %d\n", res); */ + if ( res > 0){ + if (cur->left){ + return __recursive_insert(cur->left, elem, f); + } + else{ + cur->left = elem; + return 0; + } + } + else if (res < 0){ + if (cur->right){ + return __recursive_insert(cur->right, elem, f); + } + else{ + cur->right = elem; + return 0; + } + } + printf("warning!!!!! duplicate entry!!!!!!\n\n"); + return -1; +} + + + +void* __recursive_lookup(node_t *cur, void *v, ilfunc_t *f){ + + int res; + + res = f->compare(cur->info, v); + + if (res > 0){ + if(cur->left) + return __recursive_lookup(cur->left, v, f); + else + return NULL; + + } + else if (res < 0){ + if(cur->right) + return __recursive_lookup(cur->right, v, f); + else + return NULL; + } + else + return cur->info; +} + +void __recursive_map(node_t *cur, void (*func)(void*)){ + + if (cur->left) + __recursive_map(cur->left, func); + func(cur->info); + if (cur->right) + __recursive_map(cur->right, func); +} + +void __recursive_map_args(node_t *cur, void (*func)(void*, void*), void *args){ + + if (cur->left) + __recursive_map_args(cur->left, func, args); + func(cur->info, args); + if (cur->right) + __recursive_map_args(cur->right, func, args); +} + + + +iltree_t iltree_create(iltree_t t){ + if (!t) { + t = (iltree_t)malloc(sizeof(iltree_struct_t)); + } + t->root = NULL; + return t; +} + + +void iltree_set_funs(iltree_t t, ilfunc_t *funs){ + + t->funs = *funs; +} + + +void iltree_insert(iltree_t t, void *elem){ + + node_t *n; + + n = (node_t*)malloc(sizeof(node_t)); + n->info = t->funs.alloc(); + t->funs.copy(elem, n->info); + n->left = n->right = NULL; + if (t->root == NULL){ + t->root = n; + } + else{ + __recursive_insert(t->root, n, & (t->funs)); + } +} + + +void iltree_destroy(iltree_t t){ + + if(t->root) + __recursive_destroy(t->root, & (t->funs)); + free(t); +} + + + + +void iltree_view_pre(iltree_t t){ + + if (t->root){ + /*printf("----\n");*/ + __recursive_preorder(t->root, & (t->funs)); + /*printf("----\n");*/ + } + else + printf("----- Empty tree!!!! -----\n"); + +} + + + +void* iltree_lookup(iltree_t t , void *elem){ + + node_t n; + + if(t->root) + return __recursive_lookup(t->root, elem, & (t->funs) ); + else + return NULL; +} + + +void iltree_map(iltree_t t, void (*func)(void*)){ + + __recursive_map(t->root, func); + +} + + +void iltree_map_args(iltree_t t, void (*func)(void*, void*), void *args){ + + __recursive_map_args(t->root, func, args); + +} + +void* iltree_get_fileout(iltree_t t){ + + return t->funs.fileout; +} + +void iltree_set_fileout(iltree_t t, void *f){ + + t->funs.fileout = f; +} diff --git a/dynamics/ising/iltree.h b/dynamics/ising/iltree.h new file mode 100755 index 0000000..3e835e4 --- /dev/null +++ b/dynamics/ising/iltree.h @@ -0,0 +1,55 @@ +#ifndef __ILTREE_H__ +#define __ILTREE_H__ + + +typedef struct node{ + void* info; + struct node* left; + struct node* right; +} node_t; + +typedef struct{ + void* (*alloc)(); + void (*dealloc)(void*); + void (*copy)(void *src, void *dst); + int (*compare)(void*, void*); + void (*print)(void*, void*); + void *fileout; +} ilfunc_t; + + +typedef struct { + node_t* root; + ilfunc_t funs; +} iltree_struct_t; + + + +typedef iltree_struct_t* iltree_t; + + +void iltree_set_funs(iltree_t, ilfunc_t *); + +void iltree_destroy(iltree_t); + +void iltree_empty(iltree_t); + +void iltree_insert(iltree_t, void*); + +void* iltree_lookup(iltree_t, void*); + +void iltree_view_pre(iltree_t); + +iltree_t iltree_create(iltree_t); + +void iltree_empty_cache(iltree_t); + +void iltree_map(iltree_t, void (*func)(void*)); + +void iltree_map_args(iltree_t, void (*func)(void*, void*), void*); + +void* iltree_get_fileout(iltree_t t); + +void iltree_set_fileout(iltree_t t, void *f); + +#endif /* __ILTREE_H__*/ diff --git a/dynamics/ising/multiplex_ising.c b/dynamics/ising/multiplex_ising.c new file mode 100644 index 0000000..8dbbf68 --- /dev/null +++ b/dynamics/ising/multiplex_ising.c @@ -0,0 +1,313 @@ +#include +#include +#include +#include + +#include "utils.h" +#include "iltree.h" + + + +typedef struct{ + double T; + double J; + double Jcoup; + double p0; + double p1; + double h0; + double h1; + unsigned int num_epochs; +} param_t; + + +typedef struct{ + unsigned int N; + unsigned int K; + unsigned int *J_slap; + unsigned int *r_slap; + int *s; +} net_t; + +typedef struct{ + double m0; + double m1; + double C; + double b0; + double b1; + double q0; + double q1; + double IFC0; + double IFC1; + double M; +} stats_t; + +void init_spins(int *s, int N, double p){ + + int i; + double val; + + for (i=0; ip0); + init_spins(layers[1].s, N, sys->p1); + } + +void shuffle_ids(int * v, int N){ + + int tmp, val, i; + + for (i=N-1; i >=0; i --){ + val = rand() % (i+1); + tmp = v[i]; + v[i] = v[val]; + v[val] = tmp; + } +} + +void compute_stats (net_t *layers, stats_t *stats, int *spinold0, int *spinold1){ + + int i, N; + + N = layers[0].N; + + stats->M =stats->m0 = stats->m1 = stats->C = stats->b0 = stats->b1 = stats->q0 = stats->q1 = stats->IFC0 = stats->IFC1 = 0; + + double *bubblevec0, *bubblevec1; + bubblevec0 = (double *)malloc(N * sizeof(double)); + bubblevec1 = (double *)malloc(N * sizeof(double)); + + + double bubble0, bubble1, deg; + int j; + for (i=0; im0 += layers[0].s[i]; + stats->m1 += layers[1].s[i]; + stats->C += layers[0].s[i] * layers[1].s[i]; + + + bubble0=0; + for(j=layers[0].r_slap[i]; + j< layers[0].r_slap[i + 1]; + j ++){ + bubble0+= layers[0].s[i] * layers[0].s[ layers[0].J_slap[j] ] ; + stats->IFC0 += fabs(layers[0].s[i] * layers[0].s[ layers[0].J_slap[j] ]-1.)/2.; + } + deg = (layers[0].r_slap[i + 1] - layers[0].r_slap[i])*1.0; + bubblevec0[i]=bubble0/deg; + bubble1=0; + for(j=layers[1].r_slap[i]; + j< layers[1].r_slap[i + 1]; + j ++){ + bubble1+= layers[1].s[i] * layers[1].s[ layers[1].J_slap[j] ] ; + stats->IFC1 += fabs(layers[1].s[i] * layers[1].s[ layers[1].J_slap[j] ]-1.)/2.; + } + deg = (layers[1].r_slap[i + 1] - layers[1].r_slap[i])*1.0; + bubblevec1[i]=bubble1/deg; + + stats->q0 += layers[0].s[i]*spinold0[i]; + stats->q1 += layers[1].s[i]*spinold1[i]; + } + + stats->b0=0; + for (i=0; ib0 = stats->b0 + bubblevec0[i]; + } + stats->b0 /= N; + stats->b1=0; + for (i=0; ib1 = stats->b1 + bubblevec1[i]; + } + stats->b1 /= N; + + stats->m0 /= N; + stats->m1 /= N; + stats->C /= N; + stats->q0 /= N; + stats->q1 /= N; + stats->IFC0 /= layers[0].K; + stats->IFC1 /= layers[1].K; + + stats->M = (fabs(stats->m0)+fabs(stats->m1))/2.0; + } + +void dump_spins(int *s1, int *s2, int N){ + + int i; + + for(i=0; inum_epochs; e++){ + num_flips = 0; + shuffle_ids(ids, N2); + for (i=0; i< N2; i++){ + id = ids[i] % N; + l = ids[i]/N; + //printf("i: %d id: %d l:%d\n", ids[i], id, l); + E_old = 0; + E_new = 0; + for(j=layers[l].r_slap[id]; + j< layers[l].r_slap[id + 1]; + j ++){ + E_old -= layers[l].s[id] * layers[l].s[ layers[l].J_slap[j] ]; + E_new -= -layers[l].s[id] * layers[l].s[ layers[l].J_slap[j] ]; + } + E_old *= sys->J; + E_new *= sys->J; + + if (l==0) { + E_old -= sys->h0 * layers[l].s[id]; + E_new -= sys->h0 * (-layers[l].s[id]); + } + else { + E_old -= sys->h1 * layers[l].s[id]; + E_new -= sys->h1 * (-layers[l].s[id]); + } + + E_old -= sys->Jcoup * layers[l].s[id] * layers[1-l].s[id]; + E_new -= sys->Jcoup * -(layers[l].s[id]) * layers[1-l].s[id]; + + E_old = 2*E_old; + E_new = 2*E_new; + if (E_new <= E_old){ /* The new configuration has smaller energy -> flip! */ + layers[l].s[id] = - layers[l].s[id]; + } + else if (sys->T > 0){ /* The new conf has higher energy -> flop with e-(\Delta E)/T */ + val = 1.0 * rand() / RAND_MAX; + exp_val = exp( - (1.0*(E_new - E_old)) / sys->T); + if (val < exp_val){ + layers[l].s[id] = - layers[l].s[id]; + } + } + } + + if (e==(sys->num_epochs-2)) { + int u; + for (u=0; uT, sys->J, sys->Jcoup, sys->h0, sys->h1, sys->p0, + sys->p1, s->m0, s->m1, s->C); + fflush(stdout); +} + + +int main(int argc, char *argv[]){ + + + net_t layers[2]; + param_t sys; + stats_t stats; + unsigned int *J_slap, *r_slap; + + FILE *fin; + + if (argc < 10){ + printf("Usage: %s

\n", argv[0]); + exit(1); + } + + sys.T = atof(argv[3]); + sys.J = atof(argv[4]); + sys.Jcoup = atof(argv[5]); + sys.p0 = atof(argv[8]); + sys.p1 = atof(argv[9]); + sys.h0 = atof(argv[6]); + sys.h1 = atof(argv[7]); + sys.num_epochs = atoi(argv[10]); + + srand(time(NULL)); + + J_slap = r_slap = NULL; + + fin = openfile_or_exit(argv[1], "r", 2); + read_slap(fin, &(layers[0].K), &(layers[0].N), &J_slap, &r_slap); + layers[0].J_slap = J_slap; + layers[0].r_slap = r_slap; + fclose(fin); + + J_slap = r_slap = NULL; + + fin = openfile_or_exit(argv[2], "r", 2); + read_slap(fin, &(layers[1].K), &(layers[1].N), &J_slap, &r_slap); + layers[1].J_slap = J_slap; + layers[1].r_slap = r_slap; + fclose(fin); + + if (layers[0].N != layers[1].N){ + printf("Error!!! Both layers must have the same number of nodes!!!!\n"); + exit(3); + } + + /* allocate space for the spins on each layer */ + layers[0].s = malloc(layers[0].N * sizeof(double)); + layers[1].s = malloc(layers[1].N * sizeof(double)); + + /* inizialize the spins */ + init_spins_once(&sys, layers); + + + make_simulation(&sys, layers, &stats); + + + +} diff --git a/dynamics/ising/utils.c b/dynamics/ising/utils.c new file mode 100755 index 0000000..952fcd7 --- /dev/null +++ b/dynamics/ising/utils.c @@ -0,0 +1,494 @@ +#include "iltree.h" +#include +#include +#include +#include + +#include "utils.h" + +void* alloc_double(){ + return malloc(sizeof(long double)); +} + +void dealloc_double(void *elem){ + free(elem); +} + +void copy_double(void *elem1, void *elem2){ + *((long double*)elem2) = *((long double*)elem1); +} + +int compare_double(void *elem1, void *elem2){ + return *((long double*)elem1) - *((long double*)elem2); +} + +void print_double(void *elem, void *fileout){ + + long double k, i, j; + long double x; + + k = *((long double*)elem); + + x = (1 + sqrtl(1 + 8 * (k-1))) / 2; + i = floorl(x) + 1; + j = k - ( (i-1)*1.0 * (i-2) ) /2; + //printf("x: %Lf\n i: %0.0Lf j: %0.0Lf\n", x, i, j); + fprintf((FILE*)fileout, "%0.0Lf %0.0Lf\n", i-1, j-1); +} + +iltree_t init_tree(iltree_t t, void *fileout){ + + ilfunc_t funs= { + .alloc = alloc_double, + .dealloc = dealloc_double, + .copy = copy_double, + .compare = compare_double, + .print = print_double, + .fileout = fileout + }; + + t = iltree_create(t); + iltree_set_funs(t, &funs); + return t; +} + + +/* @@@@ CAMBIARE IL PRIMO PARAMETRO IN UN FILE* PER RENDERLA COERENTE + ALLE ALTRE FUNZIONI DI READ!!!!*/ +int read_deg_distr(FILE *filein, unsigned int **degs, unsigned int **Nk, double **p){ + + int n_degs = 0; + int size = 10; + char *line; + char buff[256]; + int k_i, num_i; + double p_i; + char *ptr; + + line = NULL; + + *degs = realloc(*degs, size*sizeof(unsigned int)); + *Nk = realloc(*Nk, size*sizeof(unsigned int)); + *p = realloc(*p, size*sizeof(double)); + + + while(fgets(buff, 256, filein)){ + ptr = strtok(buff, " "); + if (ptr[0] == '#') + continue; + k_i = atoi(ptr); + ptr = strtok(NULL, " " ); + num_i = atoi(ptr); + ptr = strtok(NULL, " \n"); + p_i = atof(ptr); + if (n_degs == size){ + size += 10; + *degs = realloc(*degs, size*sizeof(unsigned int)); + *Nk = realloc(*Nk, size*sizeof(unsigned int)); + *p = realloc(*p, size*sizeof(double)); + } + (*degs)[n_degs] = k_i; + (*Nk)[n_degs] = num_i; + (*p)[n_degs] = p_i; + n_degs += 1; + } + *degs = realloc(*degs, n_degs*sizeof(unsigned int)); + *Nk = realloc(*Nk, n_degs*sizeof(unsigned int)); + *p = realloc(*p, n_degs*sizeof(double)); + return n_degs; +} + + +int read_deg_seq(FILE *filein, unsigned int **nodes){ + + int size, N, k; + char buff[256]; + char *ptr; + + N = 0; + size = 10; + + *nodes = (unsigned int*)malloc(size * sizeof(unsigned int)); + + while(fgets(buff, 256, filein)){ + ptr = strtok(buff, " "); + if (ptr[0] == '#') + continue; + k = atoi(ptr); + + if (N == size){ + size += 10; + *nodes = realloc(*nodes, size*sizeof(unsigned int)); + } + (*nodes)[N] = k; + N += 1; + } + *nodes = realloc(*nodes, N * sizeof(unsigned int)); + return N; +} + +int read_stubs(FILE *filein, unsigned int **S){ + + int size, K; + char buff[256]; + char *ptr; + + K=0; + size = 20; + *S = malloc(size * sizeof(unsigned int)); + + while(fgets(buff, 256, filein)){ + if (K == size){ + size += 20; + *S = realloc(*S, size*sizeof(unsigned int)); + } + ptr = strtok(buff, " "); /* read the first node */ + (*S)[K++] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the second node */ + (*S)[K++] = atoi(ptr); + } + *S = realloc(*S, K * sizeof(unsigned int)); + return K; +} + +/* + * Read a file in ij format + */ +int read_ij(FILE *filein, unsigned int **I, unsigned int **J){ + + unsigned int size, K; + char buff[256]; + char *ptr; + + size = 20; + K = 0; + + *I = malloc(size * sizeof(unsigned int)); + *J = malloc(size * sizeof(unsigned int)); + while(fgets(buff, 256, filein)){ + if (buff[0] == '#') + continue; + if (K == size){ + size += 20; + *I = realloc(*I, size*sizeof(unsigned int)); + *J = realloc(*J, size*sizeof(unsigned int)); + } + ptr = strtok(buff, " "); /* read the first node */ + (*I)[K] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the second node */ + (*J)[K] = atoi(ptr); + K += 1; + } + + *I = realloc(*I, K * sizeof(unsigned int)); + *J = realloc(*J, K * sizeof(unsigned int)); + return K; +} + +/*funzione pesata di moreno*/ + +int read_ij_w(FILE *filein, unsigned int **I, unsigned int **J , double **W){ + + unsigned int size, K; + char buff[256]; + char *ptr; + + size = 20; + K = 0; + + *I = malloc(size * sizeof(unsigned int)); + *J = malloc(size * sizeof(unsigned int)); + *W = malloc(size * sizeof(double)); + + while(fgets(buff, 256, filein)){ + if (buff[0] == '#') + continue; + if (K == size){ + size += 20; + *I = realloc(*I, size*sizeof(unsigned int)); + *J = realloc(*J, size*sizeof(unsigned int)); + *W = realloc(*W, size*sizeof(double)); + if (*W==NULL) { + printf ("Errore"); + exit(-1); + } + } + ptr = strtok(buff, " "); /* read the first node */ + (*I)[K] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the second node */ + (*J)[K] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the weight */ + (*W)[K] = atof(ptr); + K += 1; + } + + *I = realloc(*I, K * sizeof(unsigned int)); + *J = realloc(*J, K * sizeof(unsigned int)); + *W = realloc(*W, K * sizeof(double)); + return K; +} + + + +void read_slap(FILE *filein, unsigned int *K, unsigned int *N, + unsigned int **J_slap, unsigned int **r_slap){ + + unsigned int *I=NULL, *J=NULL; + unsigned int i, k; + + k = read_ij(filein, &I, &J); + *K = 2 * k; + I = realloc(I, 2*k * sizeof(unsigned int)); + J = realloc(J, 2*k * sizeof(unsigned int)); + for (i=k; i<2*k; i ++){ + I[i] = J[i-k]; + J[i] = I[i-k]; + } + + *N = convert_ij2slap(I, J, 2*k, r_slap, J_slap); + free(I); + free(J); + return; +} + +/*funzione pesata di moreno*/ + +void read_slap_w(FILE *filein, unsigned int *K, unsigned int *N, + unsigned int **J_slap, unsigned int **r_slap, double **w_slap){ + + unsigned int *I=NULL, *J=NULL; + double *W=NULL; + unsigned int i, k; + + k = read_ij_w(filein, &I, &J, &W); + *K = 2 * k; + I = realloc(I, 2*k * sizeof(unsigned int)); + J = realloc(J, 2*k * sizeof(unsigned int)); + W = realloc(W, 2*k * sizeof(double)); + + for (i=k; i<2*k; i ++){ + I[i] = J[i-k]; + J[i] = I[i-k]; + W[i] = W[i-k]; + } + + *N = convert_ij2slap_w(I, J, W, 2*k, r_slap, J_slap, w_slap); + free(I); + free(J); + free(W); + return; +} + +unsigned int find_max(unsigned int *v, unsigned int N){ + + unsigned int i, max; + + max = v[0]; + i = 0; + while(++i < N){ + if (v[i] > max) + max = v[i]; + } + return max; +} + + +int convert_ij2slap(unsigned int *I, unsigned int *J, unsigned int K, + unsigned int ** r_slap, unsigned int **J_slap){ + + unsigned int tmp, max; + unsigned int N; + unsigned int i, pos; + unsigned int *p; + + max = find_max(I, K) + 1; + tmp = find_max(J, K) + 1; + if (tmp > max){ + max = tmp ; + } + + *r_slap = malloc( (max+1) * sizeof(unsigned int)); + p = malloc(max * sizeof(unsigned int)); + + *J_slap = malloc(K * sizeof(unsigned int)); + + memset(*r_slap, 0, (max+1) * sizeof(unsigned int)); + for(i=0; i max){ + max = tmp ; + } + + *r_slap = malloc( (max+1) * sizeof(unsigned int)); + p = malloc(max * sizeof(unsigned int)); + + *J_slap = malloc(K * sizeof(unsigned int)); + *w_slap = malloc(K * sizeof(double)); + + memset(*r_slap, 0, (max+1) * sizeof(unsigned int)); + for(i=0; i i){ + fprintf(fileout, "%d %d\n", i, J_slap[j]); + } + } + } +} + + +/* Check if j is a neighbour of i */ +int is_neigh(unsigned int *J_slap, unsigned int *r_slap, unsigned int N, + unsigned int i, unsigned int j){ + + unsigned int l; + unsigned int count; + count = 0; + for(l=r_slap[i]; l +#include +#include +#include "utils.h" + + +int main(int argc, char *argv[]){ + + if (argc < 6){ + printf("Usage: %s \n", argv[0]); + exit(1); + } + + + FILE *filein0,*filein1, *filein, *fileout; + unsigned int N0, K0,N1, K1, N, K; + unsigned int *J_slap0, *r_slap0, *J_slap1, *r_slap1, *J_slap, *r_slap; + double *w_slap; + + + int i, j; + double c_i, d_i, f_i, f_i_2; + double alpha = (atof(argv[5])); + double beta = (atof(argv[6])); + + + int ov; + int deg0, deg1; + double degM, part, f_j, f_j_2; + double degMrid, maxdegM = 200.0; + double num1, num2, den, h; + int number_nodes=(atoi(argv[4])); + double M=2.0; + + filein0 = openfile_or_exit(argv[1], "r", 2); + read_slap(filein0, &K0, &N0, &J_slap0, &r_slap0); + + filein1 = openfile_or_exit(argv[2], "r", 2); + read_slap(filein1, &K1, &N1, &J_slap1, &r_slap1); + + + filein = openfile_or_exit(argv[3], "r", 2); + read_slap_w(filein, &K, &N, &J_slap, &r_slap,&w_slap); + + int r_slap0_n[N+1],r_slap1_n[N+1]; + for (i=0; i<=N; i++) { + if (i<=N0) { + r_slap0_n[i]=r_slap0[i]; + } + else { + r_slap0_n[i]=r_slap0[N0]; + } + if (i<=N1) { + r_slap1_n[i]=r_slap1[i]; + } + else { + r_slap1_n[i]=r_slap1[N1]; + } + + } + + + + double c_i_vec[N]; + double d_i_vec[N]; + double f_i_vec[N]; + + for (i=0; i0.0000000001) { + f_j = pow (deg0, alpha); + } + else { + f_j = 0; + } + if (deg1>0.0000000001) { + f_j_2 = pow (deg1, beta); + } + else { + f_j_2=0; + } + c_i+=ov*(f_j+f_j_2); + + d_i+=ov*(f_j+f_j_2)*log((ov*(f_j+f_j_2))); + + + + + } + c_i_vec[i]=c_i; + d_i_vec[i]=d_i; + deg0=r_slap0_n[i+1]-r_slap0_n[i]; + deg1=r_slap1_n[i+1]-r_slap1_n[i]; + + degM=(deg0+deg1)*1.0; + + part = (M/(M-1))* (1-(pow((deg0/degM),2))-(pow((deg1/degM),2)) ); + + if (deg0>0.0000000001) { + f_i = pow (deg0, alpha); + } + else { + f_i = 0; + } + if (deg1>0.0000000001) { + f_i_2 = pow (deg1, beta); + } + else { + f_i_2=0; + } + f_i_vec[i]=f_i+f_i_2; + + } + num1=0; + num2=0; + den=0; + for (i=0; i0.0) { + num1+=f_i_vec[i]*c_i_vec[i]*log(c_i_vec[i]); + } + + + num2=num2+f_i_vec[i]*d_i_vec[i]; + den=den+f_i_vec[i]*c_i_vec[i]; + + + + + } + + h=(num1-num2)/den; + printf("%f %f %f\n", h, alpha, beta); + +} diff --git a/dynamics/randomwalks/entropyrate2int.c b/dynamics/randomwalks/entropyrate2int.c new file mode 100755 index 0000000..757c7b7 --- /dev/null +++ b/dynamics/randomwalks/entropyrate2int.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include "utils.h" + + +int main(int argc, char *argv[]){ + + if (argc < 6){ + printf("Usage: %s \n", argv[0]); + exit(1); + } + + + FILE *filein0,*filein1,*filein2,*filein3,*filein4,*filein5,*filein6,*filein7,*filein8,*filein9, *filein, *fileout; + unsigned int N0, K0,N1, K1,N2, K2,N3, K3,N4, K4,N5, K5,N6, K6,N7, K7,N8, K8,N9, K9, N, K; + unsigned int *J_slap0, *r_slap0, *J_slap1, *r_slap1,*J_slap2, *r_slap2, *J_slap3, *r_slap3,*J_slap4, *r_slap4, *J_slap5, *r_slap5,*J_slap6, *r_slap6, *J_slap7, *r_slap7,*J_slap8, *r_slap8, *J_slap9, *r_slap9, *J_slap, *r_slap; + double *w_slap; + + + int i, j; + double c_i, d_i, f_i, f_i_2; + double alpha = (atof(argv[5])); + double beta = (atof(argv[6])); + + int ov; + int deg0, deg1,deg2, deg3,deg4, deg5,deg6, deg7,deg8, deg9; + double degM, part, f_j, f_j_2; + double degMrid, maxdegM = 200.0; + double num1, num2, den, h; + int number_nodes=(atoi(argv[4])); + double M=2.0; + + filein0 = openfile_or_exit(argv[1], "r", 2); + read_slap(filein0, &K0, &N0, &J_slap0, &r_slap0); + + filein1 = openfile_or_exit(argv[2], "r", 2); + read_slap(filein1, &K1, &N1, &J_slap1, &r_slap1); + + filein = openfile_or_exit(argv[3], "r", 2); + read_slap_w(filein, &K, &N, &J_slap, &r_slap,&w_slap); + + int r_slap0_n[N+1],r_slap1_n[N+1],r_slap2_n[N+1],r_slap3_n[N+1],r_slap4_n[N+1],r_slap5_n[N+1],r_slap6_n[N+1],r_slap7_n[N+1],r_slap8_n[N+1],r_slap9_n[N+1]; + for (i=0; i<=N; i++) { + if (i<=N0) { + r_slap0_n[i]=r_slap0[i]; + } + else { + r_slap0_n[i]=r_slap0[N0]; + } + if (i<=N1) { + r_slap1_n[i]=r_slap1[i]; + } + else { + r_slap1_n[i]=r_slap1[N1]; + } + + + } + + + + double c_i_vec[N]; + double d_i_vec[N]; + double f_i_vec[N]; + + + for (i=0; i0.0000000001) { + f_j = pow (part, alpha); + } + else { + f_j = 0; + } + f_j_2 = pow (degM, beta); + c_i+=ov*f_j*f_j_2; + if (part>0.0000000001) { + d_i+=ov*f_j*f_j_2*log((ov*f_j*f_j_2)); + } + + + + } + c_i_vec[i]=c_i; + d_i_vec[i]=d_i; + + deg0=r_slap0_n[i+1]-r_slap0_n[i]; + deg1=r_slap1_n[i+1]-r_slap1_n[i]; + + + + + + + + + + degM=(deg0+deg1)*1.0; + + part = (M/(M-1))* (1-(pow((deg0/degM),2))-(pow((deg1/degM),2))); + + + + if (part>0.0000000001) { + f_i = pow (part, alpha); + } + else { + f_i = 0; + } + f_i_2 = pow (degM, beta); + + f_i_vec[i]=f_i*f_i_2; + + } + num1=0; + num2=0; + den=0; + for (i=0; i0.0) { + + num1+=f_i_vec[i]*c_i_vec[i]*log(c_i_vec[i]); + } + + + num2=num2+f_i_vec[i]*d_i_vec[i]; + den=den+f_i_vec[i]*c_i_vec[i]; + + + } + + h=(num1-num2)/den*1.0; + printf("%f %f %f\n", h, alpha, beta); + +} diff --git a/dynamics/randomwalks/entropyrate2mult.c b/dynamics/randomwalks/entropyrate2mult.c new file mode 100755 index 0000000..cded4eb --- /dev/null +++ b/dynamics/randomwalks/entropyrate2mult.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include "utils.h" + + +int main(int argc, char *argv[]){ + + + if (argc < 6){ + printf("Usage: %s \n", argv[0]); + exit(1); + } + + + FILE *filein0,*filein1, *filein, *fileout; + unsigned int N0, K0,N1, K1, N, K; + unsigned int *J_slap0, *r_slap0, *J_slap1, *r_slap1, *J_slap, *r_slap; + double *w_slap; + + + int i, j; + double c_i, d_i, f_i, f_i_2; + double alpha = (atof(argv[5])); + double beta = (atof(argv[6])); + + + int ov; + int deg0, deg1; + double degM, part, f_j, f_j_2; + double degMrid, maxdegM = 200.0; + double num1, num2, den, h; + int number_nodes=(atoi(argv[4])); + double M=2.0; + + filein0 = openfile_or_exit(argv[1], "r", 2); + read_slap(filein0, &K0, &N0, &J_slap0, &r_slap0); + + filein1 = openfile_or_exit(argv[2], "r", 2); + read_slap(filein1, &K1, &N1, &J_slap1, &r_slap1); + + + filein = openfile_or_exit(argv[3], "r", 2); + read_slap_w(filein, &K, &N, &J_slap, &r_slap,&w_slap); + + int r_slap0_n[N+1],r_slap1_n[N+1]; + for (i=0; i<=N; i++) { + if (i<=N0) { + r_slap0_n[i]=r_slap0[i]; + } + else { + r_slap0_n[i]=r_slap0[N0]; + } + if (i<=N1) { + r_slap1_n[i]=r_slap1[i]; + } + else { + r_slap1_n[i]=r_slap1[N1]; + } + + } + + + + double c_i_vec[N]; + double d_i_vec[N]; + double f_i_vec[N]; + + + for (i=0; i0.0000000001) { + f_j = pow (deg0, alpha); + } + else { + f_j = 0; + } + if (deg1>0.0000000001) { + f_j_2 = pow (deg1, beta); + } + else { + f_j_2=0; + } + c_i+=ov*f_j*f_j_2; + if (deg0>0.0000000001 && deg1>0.0000000001) { + d_i+=ov*f_j*f_j_2*log((ov*f_j*f_j_2)); + } + + + /*chiudo il for*/ + } + c_i_vec[i]=c_i; + d_i_vec[i]=d_i; + deg0=r_slap0_n[i+1]-r_slap0_n[i]; + deg1=r_slap1_n[i+1]-r_slap1_n[i]; + + degM=(deg0+deg1)*1.0; + + part = (M/(M-1))* (1-(pow((deg0/degM),2))-(pow((deg1/degM),2)) ); + + if (deg0>0.0000000001) { + f_i = pow (deg0, alpha); + } + else { + f_i = 0; + } + if (deg1>0.0000000001) { + f_i_2 = pow (deg1, beta); + } + else { + f_i_2=0; + } + f_i_vec[i]=f_i*f_i_2; + + } + num1=0; + num2=0; + den=0; + for (i=0; i0.0) { + num1+=f_i_vec[i]*c_i_vec[i]*log(c_i_vec[i]); + } + + + num2=num2+f_i_vec[i]*d_i_vec[i]; + den=den+f_i_vec[i]*c_i_vec[i]; + + + + + } + + h=(num1-num2)/den; + printf("%f %f %f\n", h, alpha, beta); + +} diff --git a/dynamics/randomwalks/iltree.c b/dynamics/randomwalks/iltree.c new file mode 100755 index 0000000..5a693d0 --- /dev/null +++ b/dynamics/randomwalks/iltree.c @@ -0,0 +1,201 @@ +/* + * + * A simple insert-lookup static btree datatype + * + */ + +#include +#include "iltree.h" +#include + + +void __recursive_preorder(node_t *cur, ilfunc_t *funs){ + + if(cur->left){ + __recursive_preorder(cur->left, funs); + } + funs->print(cur->info, funs->fileout); + if(cur->right){ + __recursive_preorder(cur->right, funs); + } +} + +/* + * + * Recursive push of nodes in the nodecache :-) + * + */ + +void __recursive_destroy(node_t *cur, ilfunc_t *funs){ + if(cur->left){ + __recursive_destroy(cur->left, funs); + cur->left = NULL; + } + if(cur->right){ + __recursive_destroy(cur->right, funs); + cur->right = NULL; + } +} + + +int __recursive_insert(node_t *cur, node_t *elem, ilfunc_t *f){ + + int res ; + res = f->compare(cur->info, elem->info); + /* printf("res: %d\n", res); */ + if ( res > 0){ + if (cur->left){ + return __recursive_insert(cur->left, elem, f); + } + else{ + cur->left = elem; + return 0; + } + } + else if (res < 0){ + if (cur->right){ + return __recursive_insert(cur->right, elem, f); + } + else{ + cur->right = elem; + return 0; + } + } + printf("warning!!!!! duplicate entry!!!!!!\n\n"); + return -1; +} + + + +void* __recursive_lookup(node_t *cur, void *v, ilfunc_t *f){ + + int res; + + res = f->compare(cur->info, v); + + if (res > 0){ + if(cur->left) + return __recursive_lookup(cur->left, v, f); + else + return NULL; + + } + else if (res < 0){ + if(cur->right) + return __recursive_lookup(cur->right, v, f); + else + return NULL; + } + else + return cur->info; +} + +void __recursive_map(node_t *cur, void (*func)(void*)){ + + if (cur->left) + __recursive_map(cur->left, func); + func(cur->info); + if (cur->right) + __recursive_map(cur->right, func); +} + +void __recursive_map_args(node_t *cur, void (*func)(void*, void*), void *args){ + + if (cur->left) + __recursive_map_args(cur->left, func, args); + func(cur->info, args); + if (cur->right) + __recursive_map_args(cur->right, func, args); +} + + + +iltree_t iltree_create(iltree_t t){ + if (!t) { + t = (iltree_t)malloc(sizeof(iltree_struct_t)); + } + t->root = NULL; + return t; +} + + +void iltree_set_funs(iltree_t t, ilfunc_t *funs){ + + t->funs = *funs; +} + + +void iltree_insert(iltree_t t, void *elem){ + + node_t *n; + + n = (node_t*)malloc(sizeof(node_t)); + n->info = t->funs.alloc(); + t->funs.copy(elem, n->info); + n->left = n->right = NULL; + if (t->root == NULL){ + t->root = n; + } + else{ + __recursive_insert(t->root, n, & (t->funs)); + } +} + + +void iltree_destroy(iltree_t t){ + + if(t->root) + __recursive_destroy(t->root, & (t->funs)); + free(t); +} + + + + +void iltree_view_pre(iltree_t t){ + + if (t->root){ + /*printf("----\n");*/ + __recursive_preorder(t->root, & (t->funs)); + /*printf("----\n");*/ + } + else + printf("----- Empty tree!!!! -----\n"); + +} + + + +void* iltree_lookup(iltree_t t , void *elem){ + + node_t n; + + if(t->root) + return __recursive_lookup(t->root, elem, & (t->funs) ); + else + return NULL; +} + + +void iltree_map(iltree_t t, void (*func)(void*)){ + + __recursive_map(t->root, func); + +} + + +void iltree_map_args(iltree_t t, void (*func)(void*, void*), void *args){ + + __recursive_map_args(t->root, func, args); + +} + +void* iltree_get_fileout(iltree_t t){ + + return t->funs.fileout; +} + +void iltree_set_fileout(iltree_t t, void *f){ + + t->funs.fileout = f; +} diff --git a/dynamics/randomwalks/iltree.h b/dynamics/randomwalks/iltree.h new file mode 100755 index 0000000..3e835e4 --- /dev/null +++ b/dynamics/randomwalks/iltree.h @@ -0,0 +1,55 @@ +#ifndef __ILTREE_H__ +#define __ILTREE_H__ + + +typedef struct node{ + void* info; + struct node* left; + struct node* right; +} node_t; + +typedef struct{ + void* (*alloc)(); + void (*dealloc)(void*); + void (*copy)(void *src, void *dst); + int (*compare)(void*, void*); + void (*print)(void*, void*); + void *fileout; +} ilfunc_t; + + +typedef struct { + node_t* root; + ilfunc_t funs; +} iltree_struct_t; + + + +typedef iltree_struct_t* iltree_t; + + +void iltree_set_funs(iltree_t, ilfunc_t *); + +void iltree_destroy(iltree_t); + +void iltree_empty(iltree_t); + +void iltree_insert(iltree_t, void*); + +void* iltree_lookup(iltree_t, void*); + +void iltree_view_pre(iltree_t); + +iltree_t iltree_create(iltree_t); + +void iltree_empty_cache(iltree_t); + +void iltree_map(iltree_t, void (*func)(void*)); + +void iltree_map_args(iltree_t, void (*func)(void*, void*), void*); + +void* iltree_get_fileout(iltree_t t); + +void iltree_set_fileout(iltree_t t, void *f); + +#endif /* __ILTREE_H__*/ diff --git a/dynamics/randomwalks/statdistr2.c b/dynamics/randomwalks/statdistr2.c new file mode 100755 index 0000000..10528da --- /dev/null +++ b/dynamics/randomwalks/statdistr2.c @@ -0,0 +1,231 @@ +#include +#include +#include +#include "utils.h" + + +int main(int argc, char *argv[]){ + + + if (argc < 6){ + printf("Usage: %s \n", argv[0]); + exit(1); + } + + + /*dichiarazioni enzo*/ + FILE *filein0,*filein1, *filein, *fileout; + unsigned int N0, K0,N1, K1, N, K; + unsigned int *J_slap0, *r_slap0, *J_slap1, *r_slap1, *J_slap, *r_slap; + double *w_slap; + + /*dichiarazioni mie*/ + /*r_slap e' lungo N+1, da 0 a N compresi*/ + int i, j; + double f_j, f_j_2; + double alpha = (atof(argv[5])); + double beta = (atof(argv[6])); + + /*il bias*/ + int ov; + int deg0, deg1; + double degM, prodM, part, intM; + double degMrid, maxdegM = 200.0; + + int number_nodes=(atoi(argv[4])); + double M=2.0; + + filein0 = openfile_or_exit(argv[1], "r", 2); + read_slap(filein0, &K0, &N0, &J_slap0, &r_slap0); + + filein1 = openfile_or_exit(argv[2], "r", 2); + read_slap(filein1, &K1, &N1, &J_slap1, &r_slap1); + + + filein = openfile_or_exit(argv[3], "r", 2); + read_slap_w(filein, &K, &N, &J_slap, &r_slap,&w_slap); + + int r_slap0_n[N+1],r_slap1_n[N+1]; + for (i=0; i<=N; i++) { + if (i<=N0) { + r_slap0_n[i]=r_slap0[i]; + } + else { + r_slap0_n[i]=r_slap0[N0]; + } + if (i<=N1) { + r_slap1_n[i]=r_slap1[i]; + } + else { + r_slap1_n[i]=r_slap1[N1]; + } + + } + + + + double cf_i_vec_add[N]; + double cf_i_vec_mult[N]; + double cf_i_vec_part[N]; + double cf_i_vec_int[N]; + + double tot_add=0, tot_mult=0, tot_part=0, tot_int=0; + double c_i_add, c_i_mult, c_i_part, c_i_int; + double f_add, f_mult, f_int; + /*ciclo sui nodi dell'aggregato*/ + for (i=0; i0.0000000001) { + f_j = pow (deg0, alpha); + } + else { + f_j = 0; + } + if (deg1>0.0000000001) { + f_j_2 = pow (deg1, beta); + } + else { + f_j_2=0; + } + + c_i_add+=ov*(f_j+f_j_2); + + if (deg0>0.0000000001) { + f_j = pow (deg0, alpha); + } + else { + f_j = 0; + } + if (deg1>0.0000000001) { + f_j_2 = pow (deg1, beta); + } + else { + f_j_2=0; + } + + c_i_mult+=ov*(f_j*f_j_2); + //c_i_part+=ov*part; + + part = (M/(M-1))* (1-(pow((deg0/degM),2))-(pow((deg1/degM),2)) ); + if (part>0.0000000001) { + f_j = pow (part, alpha); + } + else { + f_j = 0; + } + f_j_2 = pow (degM, beta); + + c_i_int+=ov*(f_j*f_j_2); + + /*chiudo il for*/ + } + + deg0=r_slap0_n[i+1]-r_slap0_n[i]; + deg1=r_slap1_n[i+1]-r_slap1_n[i]; + + degM=(deg0+deg1)*1.0; + prodM=(deg0*deg1)*1.0; + part = (M/(M-1))* (1-(pow((deg0/degM),2))-(pow((deg1/degM),2)) ); + + intM=degM*part; + + if (deg0>0.0000000001) { + f_j = pow (deg0, alpha); + } + else { + f_j = 0; + } + if (deg1>0.0000000001) { + f_j_2 = pow (deg1, beta); + } + else { + f_j_2=0; + } + + + cf_i_vec_add[i]=c_i_add*(f_j+f_j_2); + + + if (deg0>0.0000000001) { + f_j = pow (deg0, alpha); + } + else { + f_j = 0; + } + if (deg1>0.0000000001) { + f_j_2 = pow (deg1, beta); + } + else { + f_j_2=0; + } + + + + cf_i_vec_mult[i]=c_i_mult*(f_j*f_j_2); + //cf_i_vec_part[i]=c_i_part*part; + if (part>0.0000000001) { + f_j = pow (part, alpha); + } + else { + f_j = 0; + } + f_j_2 = pow (degM, beta); + + + + cf_i_vec_int[i]=c_i_int*(f_j*f_j_2); + + tot_add+=cf_i_vec_add[i]; + tot_mult+=cf_i_vec_mult[i]; + tot_part+=cf_i_vec_part[i]; + tot_int+=cf_i_vec_int[i]; + } + + + double vec_add[N]; + double vec_mult[N]; + double vec_part[N]; + double vec_int[N]; + double tot_add_rid=0, tot_mult_rid=0, tot_part_rid=0, tot_int_rid=0; + + for (i=0; i +#include +#include +#include + +#include "utils.h" + +void* alloc_double(){ + return malloc(sizeof(long double)); +} + +void dealloc_double(void *elem){ + free(elem); +} + +void copy_double(void *elem1, void *elem2){ + *((long double*)elem2) = *((long double*)elem1); +} + +int compare_double(void *elem1, void *elem2){ + return *((long double*)elem1) - *((long double*)elem2); +} + +void print_double(void *elem, void *fileout){ + + long double k, i, j; + long double x; + + k = *((long double*)elem); + + x = (1 + sqrtl(1 + 8 * (k-1))) / 2; + i = floorl(x) + 1; + j = k - ( (i-1)*1.0 * (i-2) ) /2; + //printf("x: %Lf\n i: %0.0Lf j: %0.0Lf\n", x, i, j); + fprintf((FILE*)fileout, "%0.0Lf %0.0Lf\n", i-1, j-1); +} + +iltree_t init_tree(iltree_t t, void *fileout){ + + ilfunc_t funs= { + .alloc = alloc_double, + .dealloc = dealloc_double, + .copy = copy_double, + .compare = compare_double, + .print = print_double, + .fileout = fileout + }; + + t = iltree_create(t); + iltree_set_funs(t, &funs); + return t; +} + + +/* @@@@ CAMBIARE IL PRIMO PARAMETRO IN UN FILE* PER RENDERLA COERENTE + ALLE ALTRE FUNZIONI DI READ!!!!*/ +int read_deg_distr(FILE *filein, unsigned int **degs, unsigned int **Nk, double **p){ + + int n_degs = 0; + int size = 10; + char *line; + char buff[256]; + int k_i, num_i; + double p_i; + char *ptr; + + line = NULL; + + *degs = realloc(*degs, size*sizeof(unsigned int)); + *Nk = realloc(*Nk, size*sizeof(unsigned int)); + *p = realloc(*p, size*sizeof(double)); + + + while(fgets(buff, 256, filein)){ + ptr = strtok(buff, " "); + if (ptr[0] == '#') + continue; + k_i = atoi(ptr); + ptr = strtok(NULL, " " ); + num_i = atoi(ptr); + ptr = strtok(NULL, " \n"); + p_i = atof(ptr); + if (n_degs == size){ + size += 10; + *degs = realloc(*degs, size*sizeof(unsigned int)); + *Nk = realloc(*Nk, size*sizeof(unsigned int)); + *p = realloc(*p, size*sizeof(double)); + } + (*degs)[n_degs] = k_i; + (*Nk)[n_degs] = num_i; + (*p)[n_degs] = p_i; + n_degs += 1; + } + *degs = realloc(*degs, n_degs*sizeof(unsigned int)); + *Nk = realloc(*Nk, n_degs*sizeof(unsigned int)); + *p = realloc(*p, n_degs*sizeof(double)); + return n_degs; +} + + +int read_deg_seq(FILE *filein, unsigned int **nodes){ + + int size, N, k; + char buff[256]; + char *ptr; + + N = 0; + size = 10; + + *nodes = (unsigned int*)malloc(size * sizeof(unsigned int)); + + while(fgets(buff, 256, filein)){ + ptr = strtok(buff, " "); + if (ptr[0] == '#') + continue; + k = atoi(ptr); + + if (N == size){ + size += 10; + *nodes = realloc(*nodes, size*sizeof(unsigned int)); + } + (*nodes)[N] = k; + N += 1; + } + *nodes = realloc(*nodes, N * sizeof(unsigned int)); + return N; +} + +int read_stubs(FILE *filein, unsigned int **S){ + + int size, K; + char buff[256]; + char *ptr; + + K=0; + size = 20; + *S = malloc(size * sizeof(unsigned int)); + + while(fgets(buff, 256, filein)){ + if (K == size){ + size += 20; + *S = realloc(*S, size*sizeof(unsigned int)); + } + ptr = strtok(buff, " "); /* read the first node */ + (*S)[K++] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the second node */ + (*S)[K++] = atoi(ptr); + } + *S = realloc(*S, K * sizeof(unsigned int)); + return K; +} + +/* + * Read a file in ij format + */ +int read_ij(FILE *filein, unsigned int **I, unsigned int **J){ + + unsigned int size, K; + char buff[256]; + char *ptr; + + size = 20; + K = 0; + + *I = malloc(size * sizeof(unsigned int)); + *J = malloc(size * sizeof(unsigned int)); + while(fgets(buff, 256, filein)){ + if (buff[0] == '#') + continue; + if (K == size){ + size += 20; + *I = realloc(*I, size*sizeof(unsigned int)); + *J = realloc(*J, size*sizeof(unsigned int)); + } + ptr = strtok(buff, " "); /* read the first node */ + (*I)[K] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the second node */ + (*J)[K] = atoi(ptr); + K += 1; + } + + *I = realloc(*I, K * sizeof(unsigned int)); + *J = realloc(*J, K * sizeof(unsigned int)); + return K; +} + +/*funzione pesata di moreno*/ + +int read_ij_w(FILE *filein, unsigned int **I, unsigned int **J , double **W){ + + unsigned int size, K; + char buff[256]; + char *ptr; + + size = 20; + K = 0; + + *I = malloc(size * sizeof(unsigned int)); + *J = malloc(size * sizeof(unsigned int)); + *W = malloc(size * sizeof(double)); + + while(fgets(buff, 256, filein)){ + if (buff[0] == '#') + continue; + if (K == size){ + size += 20; + *I = realloc(*I, size*sizeof(unsigned int)); + *J = realloc(*J, size*sizeof(unsigned int)); + *W = realloc(*W, size*sizeof(double)); + if (*W==NULL) { + printf ("Errore"); + exit(-1); + } + } + ptr = strtok(buff, " "); /* read the first node */ + (*I)[K] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the second node */ + (*J)[K] = atoi(ptr); + ptr = strtok(NULL, " "); /* read the weight */ + (*W)[K] = atof(ptr); + K += 1; + } + + *I = realloc(*I, K * sizeof(unsigned int)); + *J = realloc(*J, K * sizeof(unsigned int)); + *W = realloc(*W, K * sizeof(double)); + return K; +} + + + +void read_slap(FILE *filein, unsigned int *K, unsigned int *N, + unsigned int **J_slap, unsigned int **r_slap){ + + unsigned int *I=NULL, *J=NULL; + unsigned int i, k; + + k = read_ij(filein, &I, &J); + *K = 2 * k; + I = realloc(I, 2*k * sizeof(unsigned int)); + J = realloc(J, 2*k * sizeof(unsigned int)); + for (i=k; i<2*k; i ++){ + I[i] = J[i-k]; + J[i] = I[i-k]; + } + + *N = convert_ij2slap(I, J, 2*k, r_slap, J_slap); + free(I); + free(J); + return; +} + +/*funzione pesata di moreno*/ + +void read_slap_w(FILE *filein, unsigned int *K, unsigned int *N, + unsigned int **J_slap, unsigned int **r_slap, double **w_slap){ + + unsigned int *I=NULL, *J=NULL; + double *W=NULL; + unsigned int i, k; + + k = read_ij_w(filein, &I, &J, &W); + *K = 2 * k; + I = realloc(I, 2*k * sizeof(unsigned int)); + J = realloc(J, 2*k * sizeof(unsigned int)); + W = realloc(W, 2*k * sizeof(double)); + + for (i=k; i<2*k; i ++){ + I[i] = J[i-k]; + J[i] = I[i-k]; + W[i] = W[i-k]; + } + + *N = convert_ij2slap_w(I, J, W, 2*k, r_slap, J_slap, w_slap); + free(I); + free(J); + free(W); + return; +} + +unsigned int find_max(unsigned int *v, unsigned int N){ + + unsigned int i, max; + + max = v[0]; + i = 0; + while(++i < N){ + if (v[i] > max) + max = v[i]; + } + return max; +} + + +int convert_ij2slap(unsigned int *I, unsigned int *J, unsigned int K, + unsigned int ** r_slap, unsigned int **J_slap){ + + unsigned int tmp, max; + unsigned int N; + unsigned int i, pos; + unsigned int *p; + + max = find_max(I, K) + 1; + tmp = find_max(J, K) + 1; + if (tmp > max){ + max = tmp ; + } + + *r_slap = malloc( (max+1) * sizeof(unsigned int)); + p = malloc(max * sizeof(unsigned int)); + + *J_slap = malloc(K * sizeof(unsigned int)); + + memset(*r_slap, 0, (max+1) * sizeof(unsigned int)); + for(i=0; i max){ + max = tmp ; + } + + *r_slap = malloc( (max+1) * sizeof(unsigned int)); + p = malloc(max * sizeof(unsigned int)); + + *J_slap = malloc(K * sizeof(unsigned int)); + *w_slap = malloc(K * sizeof(double)); + + memset(*r_slap, 0, (max+1) * sizeof(unsigned int)); + for(i=0; i i){ + fprintf(fileout, "%d %d\n", i, J_slap[j]); + } + } + } +} + + +/* Check if j is a neighbour of i */ +int is_neigh(unsigned int *J_slap, unsigned int *r_slap, unsigned int N, + unsigned int i, unsigned int j){ + + unsigned int l; + unsigned int count; + count = 0; + for(l=r_slap[i]; l