Example #1
0
void scanf_matrix(char* filename, gsl_matrix * m)
{
    FILE* fileptr;
    fileptr = fopen(filename, "r");
    gsl_matrix_fscanf(fileptr, m);
    fclose(fileptr);
}
Example #2
0
static void load_data(mcmc * m, const char * filename) {
	FILE * input;
	int npoints = countlines(filename);
	int ndim = get_column_count(filename);
	gsl_matrix * data;
	IFDEBUGPARSER
	dump_i("lines", npoints);
	IFDEBUGPARSER
	dump_i("dimensions", ndim);

	data = gsl_matrix_alloc(npoints, ndim);

	input = openfile(filename);
	if (gsl_matrix_fscanf(input, data) != 0) {
		fprintf(stderr,
				"error reading input data. Perhaps inconsistent format?\n");
		fprintf(stderr, "tried to read %d x %d.\n", ndim, npoints);
		exit(3);
	}
	assert(fclose(input) == 0);

	m->data = data;

	IFDEBUGPARSER
	dump_i("loaded data points", npoints);
}
Example #3
0
File: io.c Project: diogro/evomod
void population_fscanf (Population *pop, FILE *stream)
{
    int i;
    for (i = 0; i < pop->n_e; i++){
        gsl_vector_fscanf(stream, pop->y[i]);
        gsl_matrix_fscanf(stream, pop->b[i]);
    }
    fscanf(stream, "%d", &pop->current_gen);
}
Example #4
0
int read_mat( gsl_matrix *a, const char * filename ) {
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    Log::lprintf(Log::LOG_LEVEL_NOTIFY, "Error opening file %s\n", filename);
    return 0;
  }
  gsl_matrix_fscanf(file, a);
  fclose(file);
  return 1;
}
Example #5
0
int main(int argc, char **argv) {

    FILE *in;
    FILE *out;
    int i,j;
    int n_rows, n_columns;
    float *mtiempo;


    in = fopen( argv[1] , "r");

    n_rows = contarlineas(argv[1]);
    n_columns = 2;


    /* incialisa las matrices y los vectores*/

    gsl_matrix *matriz = gsl_matrix_calloc (n_rows, n_columns);
    gsl_matrix *g = gsl_matrix_calloc (n_rows, 3);
    gsl_matrix *gTg = gsl_matrix_calloc (3,3);
    gsl_matrix *inversa = gsl_matrix_calloc (3,3);
    gsl_vector *t = gsl_vector_calloc (n_rows);
    gsl_vector *pos = gsl_vector_calloc (n_rows);
    gsl_vector *gTd = gsl_vector_calloc (3);
    gsl_vector *m = gsl_vector_calloc (3);



    // se crear la matriz con los datos del archivo
    gsl_matrix_fscanf (in, matriz);

    // se separa la matris en vectores
    gsl_matrix_get_col (t, matriz, 0);
    gsl_matrix_get_col (pos, matriz, 1);

    /* crea g*/
    crearg (t, g, n_rows);
    /* se crea (G^T) * G */
    gsl_blas_dgemm (CblasTrans, CblasNoTrans, 1.0, g, g, 0.0, gTg);
    /* se crea  [(G^T) * G]^(-1) */
    invertirmatriz (gTg, inversa, 3);
    /* se crea  (G^T) * d */
    gsl_blas_dgemv (CblasTrans, 1.0, g, pos, 0.0, gTd);
    /* se obtiene m */
    gsl_blas_dgemv (CblasNoTrans, 1.0, inversa, gTd, 0.0, m);


    /* imprime el archivo con los valores de m*/
    out = fopen("parametros_movimiento.dat", "w");
    fprintf(out, "%f %f %f" ,gsl_vector_get (m, 0), gsl_vector_get (m, 1) ,gsl_vector_get (m, 2));


    return 0;
}
void mtx_fscanf(const char* filename, gsl_matrix * m) {
	FILE* fileptr = fopen(filename, "r");
	
	outlog("reading %ld x %ld matrix from %s",
				 m->size1, m->size2, filename);
	if (!fileptr) {
		outlog("Error opening file %s. Failing.", filename);
		exit(1);
	}
	
	gsl_matrix_fscanf(fileptr, m);
	fclose(fileptr);
}
Example #7
0
int main(int argc, char **argv)
{
  gsl_matrix *m;
  FILE *f;
  size_t *result;
  double work;
  int rows, cols, keep, i;
  time_t start, end;


  if (argc != 3) {
    fprintf(stderr, "USAGE: %s dist_matrix keep_rows\n\n", argv[0]);
    fprintf(stderr, "dist_matrix should have one line with 'n_rows n_cols' ");
    fprintf(stderr, "followed by the matrix.\n");
    return 1;
  }

  keep = atoi(argv[2]);

  f = fopen(argv[1], "r");
  /* Read dimensions: width then height */
  assert(fscanf(f, "%d %d", &rows, &cols) == 2);
  m = gsl_matrix_alloc(rows, cols);
  assert(!gsl_matrix_fscanf(f, m));
  fclose(f);

  time(&start);
  result = pam(m, keep, NULL, &work);
  time(&end);

  for(i = 0; i < keep; i++)
    printf("%lu ", result[i]);
  printf("\n");
  printf("Total work: %f\n", work);
  printf("Took: %.2lfs\n", difftime(end, start));

  gsl_matrix_free(m);
  free(result);

  return 0;
}
Example #8
0
int main (int argc, char** argv)
{
 
  FILE *in;
  in = fopen(argv[1], "r");

  int n_rows;
  int n_columns;

  n_columns = 3;
  n_rows  = contarlineas(argv[1]);

  gsl_matrix *matriz = gsl_matrix_calloc (n_rows, n_columns);
  gsl_matrix* comatriz = gsl_matrix_calloc ( n_columns,  n_columns);
  gsl_matrix* eigenvectors = gsl_matrix_calloc ( n_columns,  n_columns);
  gsl_vector* eigenvalues = gsl_vector_calloc ( n_columns );

  gsl_matrix_fscanf (in, matriz);
  matrizcovariansa (matriz,  n_columns, n_rows,  comatriz);
  get_eigen ( comatriz , eigenvalues, eigenvectors,  n_columns);
  creararch (eigenvectors);

}
Example #9
0
File: pca.c Project: jbao/mds
int main(int argc, char **argv){
    int row = atoi(argv[2]);
    int col = atoi(argv[3]);
    printf("%d %d\n", row, col);
    gsl_matrix* data = gsl_matrix_alloc(row, col);
    //gsl_matrix* data = gsl_matrix_alloc(col, row);
    FILE* f = fopen(argv[1], "r");
    gsl_matrix_fscanf(f, data);
    //gsl_matrix_fread(f, data);
    //gsl_matrix_transpose_memcpy(data, data_raw);
    fclose(f);

    //printf("%f %f", gsl_matrix_get(data,0,0), gsl_matrix_get(data,0,1));
    //f = fopen("test.dat", "w");
    //gsl_matrix_fprintf(f, data, "%f");
    //fclose(f);
    
    // data centering, subtract the mean in each dimension (col.-wise)
    int i, j;
    double mean, sum, std;
    gsl_vector_view col_vector;
    for (i = 0; i < col; ++i){
        col_vector = gsl_matrix_column(data, i);
        mean = gsl_stats_mean((&col_vector.vector)->data, 1, 
            (&col_vector.vector)->size);
        gsl_vector_add_constant(&col_vector.vector, -mean);
        gsl_matrix_set_col(data, i, &col_vector.vector);
    }
    
    char filename[50];
    //sprintf(filename, "%s.zscore", argv[1]);
    //print2file(filename, data);

    gsl_matrix* u;
    if (col > row) {
        u = gsl_matrix_alloc(data->size2, data->size1);
        gsl_matrix_transpose_memcpy(u, data);
    } 
    else {
        u = gsl_matrix_alloc(data->size1, data->size2);
        gsl_matrix_memcpy(u, data);
    }

    // svd
    gsl_matrix* X = gsl_matrix_alloc(col, col);
    gsl_matrix* V = gsl_matrix_alloc(u->size2, u->size2);
    gsl_vector* S = gsl_vector_alloc(u->size2);
    gsl_vector* work = gsl_vector_alloc(u->size2);
    gsl_linalg_SV_decomp(u, V, S, work);
    //gsl_linalg_SV_decomp_jacobi(u, V, S);

    // mode coefficient
    //print2file("u.dat", u);
    /*
    // characteristic mode
    gsl_matrix* diag = diag_alloc(S);
    gsl_matrix* mode = gsl_matrix_alloc(diag->size1, V->size1);
    gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, diag, V, 0.0, mode);
    gsl_matrix_transpose(mode);
    print2file("mode.dat", mode);
    gsl_matrix_transpose(mode);
    */
    // reconstruction
    gsl_matrix *recons = gsl_matrix_alloc(u->size2, data->size1);
    if (col > row) {
        gsl_matrix_view data_sub = gsl_matrix_submatrix(data, 0, 0, 
            u->size2, u->size2);
        gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, V, 
            &data_sub.matrix, 0.0, recons);
    }
    else
        gsl_blas_dgemm(CblasTrans, CblasTrans, 1.0, V, data, 0.0, recons);

    //gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, u, mode, 0.0, 
    //    recons);
    gsl_matrix *recons_trans = gsl_matrix_alloc(recons->size2, 
        recons->size1);
    gsl_matrix_transpose_memcpy(recons_trans, recons);
    // take the first two eigenvectors
    gsl_matrix_view final = gsl_matrix_submatrix(recons_trans, 0, 0, 
            recons_trans->size1, 2);

    print2file(argv[4], &final.matrix);

    // eigenvalue
    gsl_vector_mul(S, S);
    f = fopen("eigenvalue.dat", "w");
    //gsl_vector_fprintf(f, S, "%f");
    fclose(f);

    gsl_matrix_free(data);
    gsl_matrix_free(X);
    gsl_matrix_free(V);
    //gsl_matrix_free(diag);
    //gsl_matrix_free(mode);
    gsl_matrix_free(recons);
    gsl_matrix_free(recons_trans);
    gsl_matrix_free(u);
    gsl_vector_free(S);
    gsl_vector_free(work);
    //gsl_vector_free(zero);
    //gsl_vector_free(corrcoef);
    //gsl_vector_free(corrcoef_mean);
    return 0;
}
Example #10
0
void experiment::run_experiment()
{
	parameter pdebug = get_param("debugprint");
	parameter pdebug_f = get_param("debugprint_file");
	pdebug.strvalue=pdebug_f.strvalue;
	std::string sverbfile=get_param_s("verbose_file");
	bool bverbose=(get_param_i("verbose_mode")==1);
	std::ofstream fverbose;
	if (sverbfile.compare("cout")==0)
		gout=&std::cout;
	else
	{
		fverbose.open(sverbfile.c_str());
	       	gout=&fverbose;
	};

	//used algorithms
	std::string sexp_type=get_param_s("exp_type");
	std::string salgo_match=get_param_s("algo");
	std::istrstream istr(salgo_match.c_str());
	std::vector<std::string>v_salgo_match;
	std::string stemp;
	while (istr>>stemp)
		v_salgo_match.push_back(stemp);
	//used initialization algorithms
	std::string salgo_init=get_param_s("algo_init_sol");
	std::istrstream istrinit(salgo_init.c_str());
	std::vector<std::string>v_salgo_init;
	while (istrinit>>stemp)
		v_salgo_init.push_back(stemp);
	if (v_salgo_init.size()!=v_salgo_match.size())
		{
			printf("Error: algo and algo_init_sol do not have the same size\n");
			exit(0);
		};

	double dalpha_ldh=get_param_d("alpha_ldh");
	if ((dalpha_ldh<0) ||(dalpha_ldh>1))
	{
		printf("Error:alpha_ldh should be between 0 and 1 \n");
		exit(0);
	};

	graph g(get_config());
	graph h(get_config());
	char cformat1='D',cformat2='D';
	if (bverbose) *gout<<"Data loading"<<std::endl;
	g.load_graph(get_param_s("graph_1"),'A',cformat1);
	h.load_graph(get_param_s("graph_2"),'A',cformat2);
	if (get_param_i("graph_dot_print")==1){ g.printdot("g.dot");h.printdot("h.dot");};
	// Matrix of local similarities between graph vertices
	std::string strfile=get_param_s("C_matrix");
	int N1 =g.get_adjmatrix()->size1;
	int N2 =h.get_adjmatrix()->size1;
 	gsl_matrix* gm_ldh=gsl_matrix_alloc(N1,N2);
	FILE *f=fopen(strfile.c_str(),"r");
	if (f!=NULL){
		gsl_set_error_handler_off();
		int ierror=gsl_matrix_fscanf(f,gm_ldh);
		fclose(f);
		gsl_set_error_handler(NULL);
		if (ierror!=0){ printf("Error: C_matrix is not correctly defined \n"); exit(0);};
		}
	else
	{
		if (bverbose) *gout<<"C matrix is set to constant"<<std::endl;
                dalpha_ldh=0;
		gsl_matrix_set_all(gm_ldh,1.0/(N1*N2));
	};
	//inverse the sign if C is a distance matrix
	if (get_param_i("C_matrix_dist")==1)
		gsl_matrix_scale(gm_ldh,-1);

	if (bverbose) *gout<<"Graph synhronization"<<std::endl;
	synchronize(g,h,&gm_ldh);

	//Cycle over all algoirthms
	for (int a=0;a<v_salgo_match.size();a++)
		{
			//initialization
			algorithm* algo_i=get_algorithm(v_salgo_init[a]);
			algo_i->read_config(get_config_string());
			algo_i->set_ldhmatrix(gm_ldh);
			match_result mres_i=algo_i->gmatch(g,h,NULL,NULL,dalpha_ldh);
			delete algo_i;
			//main algorithm
			algorithm* algo=get_algorithm(v_salgo_match[a]);
			algo->read_config(get_config_string());
			algo->set_ldhmatrix(gm_ldh);
			match_result mres_a=algo->gmatch(g,h,(mres_i.gm_P_exact!=NULL)?mres_i.gm_P_exact:mres_i.gm_P, NULL,dalpha_ldh);
			mres_a.vd_trace.clear();
			mres_a.salgo=v_salgo_match[a];
			v_mres.push_back(mres_a);
			delete algo;
		};
	gsl_matrix_free(gm_ldh);
	printout();
}
Example #11
0
int main(int argc, char **argv)
{
  gsl_matrix *Original;
  gsl_matrix *Mtr_Cov;


  FILE *input;
  int num_lineas;
  int i,j,k;
  double var;

/*---------------------------------------------------------
Verifica cuales archivos fueron usados como input y se asegura de que solo haya cargado uno.
-----------------------------------------------------------*/
  printf("Este programa se ejecutó con %d argumento(s):\n",argc-1);
  for(i=1;i<argc;i++){
    printf("%s\n", argv[i]);
  }
  if(argc!=2){
    printf("se necesita 1 argumento además del nombre del ejecutable!,\n  EXIT!\n");
    exit(1);
    }
    else{
      printf("El archivo a usar es %s\n", argv[1]);

    }
//--------------------------------------------------------
    input=fopen(argv[1],"r");
    if(!input){
      printf("surgió un problema abriendo el archivo\n");
      exit(1);
    }
/*---------------------------------------------------------
toma como archivo base el primer argumento y cuenta sus lineas
-----------------------------------------------------------*/
    num_lineas=0;
    while ((var = fgetc(input)) != EOF){
      if (var =='\n')
	++num_lineas;
    }

    //printf("Número de lineas del archivo:\n -->%d\n",num_lineas); 

/*---------------------------------------------------------
Data allocation
-----------------------------------------------------------*/ 
    rewind(input);
    Original=gsl_matrix_alloc((num_lineas),24);
    // printf("\nOriginal\n");
    gsl_matrix_fscanf(input,Original);
    //gsl_matrix_fprintf (stdout, Original, "%g");
    //CheckPoint
    //printf("matriz escaneada\n");
/*---------------------------------------------------------
Promedio
-----------------------------------------------------------*/ 
    float *prom;
    int y;
    
    prom=malloc((num_lineas) * sizeof(float));
    //printf("...\n");
    for(k=0;k<num_lineas;k++){
      float suma=0.0;
      for(y=0;y<24;y++){
	suma+=gsl_matrix_get(Original,k,y);
      }
      prom[k]=suma/24.0;
    }
    printf("Checkpoint... Promedios\n");

/*---------------------------------------------------------
Efectos en la matriz
-----------------------------------------------------------*/ 
    Mtr_Cov=gsl_matrix_alloc(num_lineas,num_lineas);
    //printf("...\n");
    int l,n,m;
    double sprite=0;
    for(l=0;l<num_lineas;l++){
      for(m=0;m<num_lineas;m++){
	for(n=1;n<24;n++){
	  double flan=gsl_matrix_get(Original,m,n);
	  double agua=prom[m];
	  double frutino=gsl_matrix_get(Original,l,n);
	  double tang=prom[l];
	  double jarra1=(flan-agua);
	  double jarra2=(frutino-tang);
	  //printf("%g  %g\n",jarra1,jarra2);
	  sprite=sprite+(jarra1*jarra2)/24;
	}
	
	gsl_matrix_set(Mtr_Cov,m,l,sprite);
	sprite=0;
      }}
    
/*---------------------------------------------------------
Matriz de covarianza creada;
sacar vectores y valores propios de la matriz.
-----------------------------------------------------------*/
/*
    int pa,po;
    double can;
    for(pa=0;pa<num_lineas;pa++){
      for(po=0;po<num_lineas;po++){
	can=gsl_matrix_get(Mtr_Cov,po,pa);
	printf("%f ",can);
      }
      printf("\n");
    }
*/




    gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (num_lineas);
    gsl_vector *eval = gsl_vector_alloc (num_lineas);
    gsl_matrix *evec = gsl_matrix_alloc (num_lineas,num_lineas);
  
    gsl_eigen_symmv (Mtr_Cov, eval, evec, w);   
    gsl_eigen_symmv_sort (eval, evec,GSL_EIGEN_SORT_ABS_ASC);
/*---------------------------------------------------------
PON ESA COSA HORROROSA AHI O VERAS!!!
-----------------------------------------------------------*/
    FILE *Out1;
    FILE *Out2;
    
    Out1 = fopen("JorgeHayek_eigenvalues.dat", "w");
    Out2 = fopen("JorgeHayek_eigenvectors.dat", "w");
    
    fprintf(Out1,"EigenValues:\n");
    fprintf(Out2,"EigenVectors:\n");



    for (i = 0; i < num_lineas; i++)
      {
	double eval_i  = gsl_vector_get (eval, i);

	fprintf (Out1,"%g\n", eval_i);
      }

     for (i = 0; i < 11; i++)
      {fprintf(Out2,"--------------------------------------------\nVector %d\n--------------------------------------------\n",i);
	gsl_vector_view evec_i 
	  = gsl_matrix_column (evec, i);

	gsl_vector_fprintf (Out2, 
			    &evec_i.vector, "%g");
	 
      }
     
  





/*---------------------------------------------------------
FIN
-----------------------------------------------------------*/  
    gsl_vector_free (eval);
    gsl_matrix_free (evec);
    gsl_matrix_free(Original);
    fclose(input);
    return 0;
}
Example #12
0
int main(){
    const int max_mu_size=601;
    const int zero_pad_size=pow(2,15);
    FILE *in;
    in= fopen("mean.chi", "r");
    gsl_matrix *e = gsl_matrix_alloc(max_mu_size, 4);
    gsl_vector * kvar=gsl_vector_alloc(max_mu_size);
    gsl_vector * muvar=gsl_vector_alloc(max_mu_size);
    gsl_vector * mu_0pad=gsl_vector_alloc(zero_pad_size);
    gsl_vector * r_0pad=gsl_vector_alloc(zero_pad_size/2); //half of lenght 
    gsl_vector * kvar_0pad=gsl_vector_alloc(zero_pad_size);

    gsl_matrix_fscanf(in, e);
    fclose(in);

    gsl_matrix_get_col(kvar,e,0);
    gsl_matrix_get_col(muvar,e,1);
    gsl_vector_set_zero(mu_0pad);
    gsl_matrix_free(e);


    double dk=gsl_vector_get (kvar, 1)-gsl_vector_get (kvar, 0);
    double dr=M_PI/float(zero_pad_size-1)/dk;

    for (int i = 0; i < zero_pad_size; i++)
    {
      gsl_vector_set (kvar_0pad, i, dk*i);
    }
    for (int i = 0; i < zero_pad_size/2; i++)
    {
      gsl_vector_set (r_0pad, i, dr*i);
    }
    for (int i = 0; i < max_mu_size; i++)
    {
      gsl_vector_set (mu_0pad, i, gsl_vector_get (muvar, i));
    }

    gsl_vector *mu_widowed=gsl_vector_alloc(zero_pad_size);
    gsl_vector_memcpy (mu_widowed, mu_0pad);
    double kmin=4.0, kmax=17.0, dwk=0.8;
    hanning(mu_widowed, kvar_0pad, kmin, kmax, dwk);


    //FFT transform
    double *data = (double *) malloc(zero_pad_size*sizeof(double)); 
    //new double [zero_pad_size] ;
    memcpy(data, mu_widowed->data, zero_pad_size*sizeof(double));
    gsl_fft_real_radix2_transform(data, 1, zero_pad_size);

    //Unpack complex vector
    gsl_vector_complex *fourier_data = gsl_vector_complex_alloc (zero_pad_size);
    gsl_fft_halfcomplex_radix2_unpack(data, fourier_data->data, 1, zero_pad_size);
    gsl_vector *fftR_real = gsl_vector_alloc(fourier_data->size/2);
    gsl_vector *fftR_imag = gsl_vector_alloc(fourier_data->size/2);
    gsl_vector *fftR_abs  = gsl_vector_alloc(fourier_data->size/2);
    complex_vector_parts(fourier_data, fftR_real, fftR_imag);
    complex_vector_abs(fftR_abs, fftR_real, fftR_imag);
    
    gsl_vector *first_shell=gsl_vector_alloc(fftR_abs->size);
    gsl_vector_memcpy (first_shell, fftR_abs);
    double rmin=0.2, rmax=3.0, dwr=0.1;
    hanning(first_shell, r_0pad, rmin, rmax, dwr);


    //feff0001.dat
    const int path_lines=68; 
    e = gsl_matrix_alloc(path_lines, 7); 
    gsl_vector * k_p  =gsl_vector_alloc(path_lines);
    gsl_vector * phc_p=gsl_vector_alloc(path_lines);
    gsl_vector * mag_p=gsl_vector_alloc(path_lines);
    gsl_vector * pha_p=gsl_vector_alloc(path_lines);
    gsl_vector * lam_p=gsl_vector_alloc(path_lines);
    
    in= fopen("feff0001.dat", "r");
    gsl_matrix_fscanf(in, e);
    fclose(in);
    
    gsl_matrix_get_col(k_p  ,e,0);
    gsl_matrix_get_col(phc_p,e,1);
    gsl_matrix_get_col(mag_p,e,2);
    gsl_matrix_get_col(pha_p,e,3);
    gsl_matrix_get_col(lam_p,e,5);
    gsl_matrix_free(e);

    gsl_interp_accel *acc = gsl_interp_accel_alloc ();
    gsl_spline *k_spline   = gsl_spline_alloc (gsl_interp_cspline, path_lines);
    gsl_spline *phc_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines);
    gsl_spline *mag_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines);
    gsl_spline *pha_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines);
    gsl_spline *lam_spline = gsl_spline_alloc (gsl_interp_cspline, path_lines);

    gsl_spline_init (k_spline  , k_p->data, k_p->data  , path_lines);
    gsl_spline_init (phc_spline, k_p->data, phc_p->data, path_lines);
    gsl_spline_init (mag_spline, k_p->data, mag_p->data, path_lines);
    gsl_spline_init (pha_spline, k_p->data, pha_p->data, path_lines);
    gsl_spline_init (lam_spline, k_p->data, lam_p->data, path_lines);


    gsl_vector * mu_p  =gsl_vector_alloc(path_lines);

    //struct fit_params { student_params t; double kshift; double S02; double N; inter_path splines; };
    //student_params t   = {2.45681867, 0.02776907, -21.28920008, 9.44741797, 0.0, 0.0, 0.0};

    splines.acc=acc; splines.phc_spline=phc_spline; splines.mag_spline=mag_spline;
    splines.pha_spline=pha_spline; splines.lam_spline=lam_spline;
    
    
    fit_params fp = { 2.45681867, 0.02776907, -21.28920008, 9.44741797, 1.0, 0.0};
    compute_itegral(k_p, &fp, mu_p);

    //mu_data_fit params = { k_p, mu_p};
    mu_data.k  = kvar_0pad;
    mu_data.mu = mu_0pad;
    mu_data.mu_ft = first_shell;
    mu_data.r = r_0pad;
    mu_data.kmin = kmin;
    mu_data.kmax = kmax;
    mu_data.rmin = rmin;
    mu_data.rmax = rmax;
    mu_data.dwk = dwk;
    mu_data.dwr = dwr;


    // initialize the solver
    size_t Nparams=6;
    gsl_vector *guess0 = gsl_vector_alloc(Nparams);

    gsl_vector_set(guess0, 0, 2.4307);
    gsl_vector_set(guess0, 1, 0.040969);
    gsl_vector_set(guess0, 2, 0.001314);
    gsl_vector_set(guess0, 3, 7835);
    gsl_vector_set(guess0, 4,  1.0);
    gsl_vector_set(guess0, 5,  0.0);


    gsl_vector *fit_r = gsl_vector_alloc(r_0pad->size);

    
    compute_itegral_r(&mu_data, fp, fit_r);
    gsl_matrix *plotting = gsl_matrix_calloc(r_0pad->size, 3);
    gsl_matrix_set_col (plotting, 0,  r_0pad);
    gsl_matrix_set_col (plotting, 1,  first_shell);
    gsl_matrix_set_col (plotting, 2,  fit_r);
    plot_matplotlib(plotting);
    gsl_matrix_free (plotting);
    


    gsl_multifit_function_fdf fit_mu_k;
    fit_mu_k.f = &resudial_itegral_r;
    fit_mu_k.n = MAX_FIT_POINTS;
    fit_mu_k.p = Nparams;
    fit_mu_k.params = &mu_data;
    fit_mu_k.df = NULL;
    fit_mu_k.fdf = NULL;




    gsl_multifit_fdfsolver *solver = gsl_multifit_fdfsolver_alloc(gsl_multifit_fdfsolver_lmsder, MAX_FIT_POINTS, Nparams);
    gsl_multifit_fdfsolver_set(solver, &fit_mu_k, guess0);

    size_t iter=0, status;
    do{
        iter++;
        //cout << solver->x->data[0] << " " << solver->x->data[1] <<endl;
        status = gsl_multifit_fdfsolver_iterate (solver);
        //printf("%12.4f %12.4f %12.4f\n", solver->J->data[0,0], solver->J->data[1,1], solver->J->data[2,2] );
        //gsl_multifit_fdfsolver_dif_df  (k_p, &fit_mu_k, mu_p, solver->J);
        //gsl_multifit_fdfsolver_dif_fdf (k_p, &fit_mu_k, mu_p, solver->J);
        for (int i =0; i< solver->x->size; i++){
            printf("%14.5f", gsl_vector_get (solver->x, i)) ;
        }
        printf("\n") ;

        if (status) break;
        status = gsl_multifit_test_delta (solver->dx, solver->x, 1e-4, 1e-4);
    }while (status == GSL_CONTINUE && iter < 100);

    gsl_vector * mu_fit  =gsl_vector_alloc(path_lines);
    fit_params fitp = { solver->x->data[0], solver->x->data[1],\
                        solver->x->data[2], solver->x->data[3],\
                        solver->x->data[4], solver->x->data[5]};
    compute_itegral(k_p, &fitp, mu_fit);



        fp.mu=gsl_vector_get (solver->x, 0);
        fp.sig=gsl_vector_get (solver->x, 1);
        fp.skew=gsl_vector_get (solver->x, 2);
        fp.nu=gsl_vector_get (solver->x, 3);
        fp.S02=gsl_vector_get (solver->x, 4);
        fp.kshift=gsl_vector_get (solver->x, 5);
        
        compute_itegral_r(&mu_data, fp, fit_r);
        //gsl_matrix *plotting = gsl_matrix_calloc(r_0pad->size, 3);
        gsl_matrix_set_col (plotting, 0,  r_0pad);
        gsl_matrix_set_col (plotting, 1,  first_shell);
        gsl_matrix_set_col (plotting, 2,  fit_r);
        int min_r=search_max(r_0pad, 0.);
        int max_r=search_max(r_0pad, 4.);
        gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_r, 0, max_r-min_r, plotting->size2);
        plot_matplotlib(&plotting_lim.matrix);
        gsl_matrix_free (plotting);


    //cout << gsl_spline_eval (k_spline, 1.333, acc) << endl;
    //cout << gsl_spline_eval (phc_spline, 1.333, acc) << endl;


    //cout << data[0] << "\t" << data[1] << "\t" << data[2] << "\t" << endl;
    //cout << fourier_data->data[0] << "\t" << fourier_data->data[1] << "\t" << fourier_data->data[2] << "\t" << endl;

   
    //Plotting
    /*
    gsl_matrix *plotting = gsl_matrix_calloc(zero_pad_size, 3);
    gsl_matrix_set_col (plotting, 0, kvar_0pad);
    gsl_matrix_set_col (plotting, 1, mu_0pad);
    gsl_matrix_set_col (plotting, 2, mu_widowed);
    int max_k=search_max(kvar_0pad, 35.);
    int min_k=search_max(kvar_0pad, 1.0);
    gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_k, 0, max_k-min_k, 3);
    plot_matplotlib(&plotting_lim.matrix);
    gsl_matrix_free (plotting);
    */

    /*
    gsl_matrix *plotting = gsl_matrix_calloc(zero_pad_size, 2);
    gsl_matrix_set_col (plotting, 0, r_0pad);
    gsl_matrix_set_col (plotting, 1, mu_0pad);
    int max_k=search_max(kvar_0pad, 35.);
    int min_k=search_max(kvar_0pad, 1.0);
    gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_k, 0, max_k-min_k, 3);
    plot_matplotlib(&plotting_lim.matrix);
    gsl_matrix_free (plotting);
    */
    /*  
    gsl_matrix *plotting = gsl_matrix_calloc(r_0pad->size, 5);
    gsl_matrix_set_col (plotting, 0,  r_0pad);
    gsl_matrix_set_col (plotting, 1,  fftR_abs);
    gsl_matrix_set_col (plotting, 2,  fftR_real);
    gsl_matrix_set_col (plotting, 3,  fftR_imag);
    gsl_matrix_set_col (plotting, 4,  first_shell);
    
    int min_r=search_max(r_0pad, 0.);
    int max_r=search_max(r_0pad, 5.);
    gsl_matrix_view plotting_lim = gsl_matrix_submatrix (plotting, min_r, 0, max_r-min_r, plotting->size2);
    plot_matplotlib(&plotting_lim.matrix);
    //plot_matplotlib(plotting);
    gsl_matrix_free (plotting);
    */






    //cout << "Done" << endl;
    //cout << data[1] <<"\t" << data[2] << endl;
    
    //for (int i = 0; i < kvar->size; i++)
    //{
    //    cout << gsl_vector_get (kvar, i) <<"\t" << gsl_vector_get (muvar, i) << endl;
    //}

}