int main (void){ int i, j, k = 0; gsl_matrix *m = gsl_matrix_alloc (10, 10); gsl_matrix *a = gsl_matrix_alloc (10, 10); for (i = 0; i < 10; i++){ for (j = 0; j < 10; j++){ gsl_matrix_set (m, i, j, i*10+j); } } FILE *f = fopen ("test.dat", "w"); gsl_matrix_fwrite (f, m); fclose (f); FILE *F = fopen ("test.dat", "r"); gsl_matrix_fread (F, a); fclose (F); for (i = 0; i < 10; i++) for (j = 0; j < 10; j++){ double mij = gsl_matrix_get (m, i, j); double aij = gsl_matrix_get (a, i, j); if (mij != aij) k++; } gsl_matrix_free (m); gsl_matrix_free (a); printf ("differences = %d (should be zero)\n", k); return (k > 0); }
gsl_matrix * pca_read_matrix(const char *filename) { FILE *fp; gsl_matrix *m; size_t size1, size2; fp = fopen(filename, "r"); if (!fp) { fprintf(stderr, "pca_read_matrix: unable to open %s: %s\n", filename, strerror(errno)); return NULL; } fread(&size1, sizeof(size_t), 1, fp); fread(&size2, sizeof(size_t), 1, fp); m = gsl_matrix_alloc(size1, size2); gsl_matrix_fread(fp, m); fclose(fp); return m; }
int main (void) { int i, j, k = 0; gsl_matrix * m = gsl_matrix_alloc (100, 100); gsl_matrix * a = gsl_matrix_alloc (100, 100); for (i = 0; i < 100; i++) for (j = 0; j < 100; j++) gsl_matrix_set (m, i, j, 0.23 + i + j); { FILE * f = fopen ("test.dat", "wb"); gsl_matrix_fwrite (f, m); fclose (f); } { FILE * f = fopen ("test.dat", "rb"); gsl_matrix_fread (f, a); fclose (f); } for (i = 0; i < 100; i++) for (j = 0; j < 100; j++) { double mij = gsl_matrix_get (m, i, j); double aij = gsl_matrix_get (a, i, j); if (mij != aij) k++; } printf ("differences = %d (should be zero)\n", k); return (k > 0); }
//Reads coupling matrix from file and computes its LU decomposition void read_coupling_matrix(char *fname_in,int nbins_in, gsl_matrix **coupling_matrix_b_out, gsl_permutation **perm_out, int pol1,int pol2) { int sig,n_cl,stat; FILE *fi; gsl_permutation *perm; gsl_matrix *coupling_matrix_b; if(pol1) { if(pol2) n_cl=4; else n_cl=2; } else { if(pol2) n_cl=2; else n_cl=1; } perm=gsl_permutation_alloc(n_cl*nbins_in); coupling_matrix_b=gsl_matrix_alloc(n_cl*nbins_in,n_cl*nbins_in); fi=my_fopen(fname_in,"rb"); stat=gsl_matrix_fread(fi,coupling_matrix_b); if(stat==GSL_EFAILED) report_error(1,"Error reading matrix from file %s\n",fname_in); fclose(fi); gsl_linalg_LU_decomp(coupling_matrix_b,perm,&sig); *coupling_matrix_b_out=coupling_matrix_b; *perm_out=perm; }
void chapeau_loadstate ( chapeau * ch, char * filename ) { int i,m,N; chapeau * chaux = (chapeau*)malloc(sizeof(chapeau)); FILE * ofs; ofs=fopen(filename,"r"); if (!ch) { fprintf(stderr,"CFACV) ERROR in load chapeau file because holding object was not allocated\n"); exit(-1); } fread(&i,sizeof(int),1,ofs); fprintf(stdout,"CFACV) Recovering chapeau state of step %i of some previous simulation\n",i); // With chaux we prevent override of addresses (hits, mask, etc) when // reading integers (N,M,et), that is only what we want... better way to do // this? fread(chaux, sizeof(*chaux), 1, ofs); ch->N = chaux->N ; ch->m = chaux->m ; ch->nsample = chaux->nsample ; ch->nupdate = chaux->nupdate ; ch->alpha = chaux->alpha ; strcpy(ch->filename,chaux->filename); ch->outputFreq = chaux->outputFreq ; ch->outputLevel = chaux->outputLevel; ch->rmin = chaux->rmin ; ch->rmax = chaux->rmax ; ch->dr = chaux->dr ; ch->idr = chaux->idr ; N=ch->N; m=ch->m; fread(ch->hits,sizeof(*(ch->hits)),ch->m,ofs); fread(ch->mask,sizeof(*(ch->mask)),ch->N,ofs); gsl_matrix_fread(ofs,ch->A); gsl_vector_fread(ofs,ch->b); gsl_matrix_fread(ofs,ch->Afull); gsl_vector_fread(ofs,ch->bfull); gsl_vector_fread(ofs,ch->lam); fclose(ofs); free(chaux); }
static int read_matrix(const char dir[], const char fname[], gsl_matrix *m) { size_t size = strlen(dir) + strlen(fname) + 2; char *path = XLALMalloc(size); snprintf(path, size, "%s/%s", dir, fname); FILE *f = fopen(path, "rb"); if (!f) XLAL_ERROR(XLAL_EIO, "Could not find ROM data file at path `%s'", path); int ret = gsl_matrix_fread(f, m); if (ret != 0) XLAL_ERROR(XLAL_EIO, "Error reading data from `%s'", path); fclose(f); XLAL_PRINT_INFO("Sucessfully read data file `%s'", path); XLALFree(path); return(XLAL_SUCCESS); }
int lls_load(const char *filename, lls_workspace *w) { int s = 0; FILE *fp; fp = fopen(filename, "r"); if (!fp) { fprintf(stderr, "lls_load: unable to open %s: %s\n", filename, strerror(errno)); return GSL_FAILURE; } fread(&(w->bTb), sizeof(double), 1, fp); s += gsl_matrix_fread(fp, w->ATA); s += gsl_vector_fread(fp, w->ATb); fclose(fp); return s; } /* lls_load() */
void crypt_vector(int *a, int n, int factor, int seed) { /* Opening the coresponding files as streams */ int i = 0; FILE *fp_s; FILE *fp_m1; FILE *fp_m2; if(factor == 0) { fp_s = fopen("s.key", "r"); fp_m1 = fopen("m1_t.key", "r"); fp_m2 = fopen("m2_t.key", "r"); } else { fp_s = fopen("s_i.key", "r"); fp_m1 = fopen("m1_i.key", "r"); fp_m2 = fopen("m2_i.key", "r"); } /* Allocating memories for the datastructures */ gsl_vector *org_vector = gsl_vector_alloc(n); gsl_vector *s = gsl_vector_alloc(n); gsl_matrix *m1 = gsl_matrix_alloc(n,n); gsl_matrix *m2 = gsl_matrix_alloc(n,n); /* Getting them into memory */ gsl_vector_fread(fp_s, s); gsl_matrix_fread(fp_m1, m1); gsl_matrix_fread(fp_m2, m2); for(i = 0; i<n; i++) gsl_vector_set(org_vector, i, a[i]); init_rng(seed); if(factor == 1) { int scale = gsl_rng_uniform_int(r, 100); gsl_vector_scale(org_vector, scale); } gsl_vector *splt_vec_1 = gsl_vector_alloc(n); gsl_vector *splt_vec_2 = gsl_vector_alloc(n); split(org_vector, splt_vec_1, splt_vec_2, s, n); /*{ int j = 0; for(j = 0; j<n; j++) printf("%d", (int) gsl_vector_get(org_vector, j)); printf("\n\n\n"); //int i = 0, j = 0; for(j = 0; j<n; j++) printf("%d", (int) gsl_vector_get(splt_vec_1, j)); printf("\n\n\n"); for(j = 0; j<n; j++) printf("%d", (int) gsl_vector_get(splt_vec_2, j)); printf("\n\n"); }*/ gsl_vector *crp_vector_1 = gsl_vector_alloc(n); gsl_vector *crp_vector_2 = gsl_vector_alloc(n); compute_crp_vector(splt_vec_1, m1, crp_vector_1, n); compute_crp_vector(splt_vec_2, m2, crp_vector_2, n); FILE *f_crpt_1 = fopen("crypt_1.key", "w"); FILE *f_crpt_2 = fopen("crypt_2.key", "w"); gsl_vector_fwrite(f_crpt_1, crp_vector_1); gsl_vector_fwrite(f_crpt_2, crp_vector_2); gsl_vector_free(org_vector); gsl_vector_free(s); gsl_vector_free(splt_vec_1); gsl_vector_free(splt_vec_2); gsl_vector_free(crp_vector_1); gsl_vector_free(crp_vector_2); gsl_matrix_free(m1); gsl_matrix_free(m2); gsl_rng_free(r); fclose(fp_s); fclose(fp_m1); fclose(fp_m2); fclose(f_crpt_1); fclose(f_crpt_2); }