Example #1
0
int main(int argc, char **argv)
{
	int i, j;
	int row, col;
	double **a, **b;
	FILE *fin;

	if(argc != 2) {
		fprintf(stderr, "\n[使用法] : transmat <file>\n");
		exit(1);
	}

	/* 次元数を確認 */
	get_size(argv[1], &row, &col);

	/* 配列を確保 */
	a = new_double_matrix(col, row);

	/* ファイルの有無を確認 */
	if((fin = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "\n[エラー] : ファイル %s が開けません\n", argv[1]);
		exit(2);
	}

	for(i = 0; i < col; i++)
		for(j = 0; j < row; j++)
			fscanf(fin, "%lf", &a[i][j]);

	fclose(fin);

	/* 転置した行列を出力 */
	b = trans_mat(a, row, col);
	
	for(i = 0; i < row; i++){
		for(j = 0; j < col; j++){
			fprintf(stdout, "%.6f", b[i][j]);

			if(j != row-1)
				printf(" ");
		}
		fprintf(stdout, "\n");
	}

	/* 配列を確保 */
	free_double_matrix(a);
	free_double_matrix(b);

	exit(0);
}
Example #2
0
int
mat_rank(int dim, double **mat)
{
	int i, j;
	int count = 0;
	double *dr = NULL, *di = NULL;
	double **tmp;
	
	dr = new_double_vector(dim);
	di = new_double_vector(dim);
	tmp = new_double_matrix(dim, dim);
	
	/* 計算途中で値が上書きされるから元の行列のコピーを使う */
	for(i = 0; i < dim; i++)
		for(j = 0; j < dim; j++)
			tmp[i][j] = mat[i][j];
	
	/* 固有値の計算 */
	hes(tmp, dim);
	hqr(tmp, dr, di, dim);

	/* 固有値を調べる */
	/* drが実数成分、diが虚数成分 */
	for(i = 0; i < dim; i++)
		if(dr[i] > 0.0 && (!(di[i] > 0.0 || di[i] < 0.0)))
			count++;
	
	/* 配列を解放 */
	free_double_vector(dr);
	free_double_vector(di);
	free_double_matrix(tmp);

	return count;
}
Example #3
0
void
free_param(Spec *spc, Data *dat, KNN *knn)
{
	/* 配列の解放 */
	free_int_vector(spc->tr_sample);
	free_int_vector(spc->te_sample);

	free_double_matrix(dat->x_tr);
	free_double_matrix(dat->x_te);
	free_int_vector(dat->label_tr);
	free_int_vector(dat->label_te);

	free_double_vector(knn->dist_queue);
	free_int_vector(knn->index);

	free_int_matrix(knn->count);
}
Example #4
0
/* 行列×ベクトルの計算を行う */
double *matxvec(double **mat, int nrow, int ncol, double *v, int size)
{
	int    i;
	double *vec = NULL;
	double **tmp1 = NULL, **tmp2 = NULL;
 
	/* ベクトルを1×sizeの2次元の配列にする */
	tmp1 = new_double_matrix(size, 1);
	for(i = 0; i < size; i++)
		tmp1[i][0] = v[i];

	/* 行列同士の掛け算を行う */
	tmp2 = mult_mat(mat, nrow, ncol, tmp1, size, 1);

	/* 計算結果を代入する */
	vec = new_double_vector(nrow);
	for(i = 0; i < nrow; i++)
		vec[i] = tmp2[i][0];

	free_double_matrix(tmp1);
	free_double_matrix(tmp2);

	return vec;
}
Example #5
0
/* ベクトル×行列の計算を行う */
double *vecxmat(double *v, int size, double **mat, int nrow, int ncol)
{
	int    i;
	double *vec = NULL;
	double **tmp1 = NULL, **tmp2 = NULL;
 
	/* ベクトルを1×sizeの2次元の配列にする */
	tmp1 = new_double_matrix(1, size);
	for(i = 0; i < size; i++)
		tmp1[0][i] = v[i];

	/* 行列同士の掛け算を行う */
	tmp2 = mult_mat(tmp1, 1, size, mat, nrow, ncol);

	/* 計算結果を代入する */
	vec = new_double_vector(ncol);
	for(i = 0; i < ncol; i++)
		vec[i] = tmp2[0][i];

	free_double_matrix(tmp1);
	free_double_matrix(tmp2);

	return vec;
}
Example #6
0
/*
  多変量正規分布 N(Μ,Σ)の X に対する値を計算する。
  単変量の正規分布は上の関数を使うこと。
*/
double
get_multi_norm(double *mean, double **covmat, double *X, int dim)
{
  double *tmp1 = NULL, *tmp2 = NULL;
  double det, **invmat = NULL;
	double val;

  tmp1 = del_vec(X, mean, dim);
  tmp2 = tmp1;
  invmat  = matinv(dim, covmat, &det);
  tmp1 = vecxmat(tmp1, dim, invmat, dim, dim);
  val = dvid(safe_exp(-0.5 * innerprod(dim, tmp1, tmp2)),
						 (pow(2.0 * M_PI, dim / 2.0) * sqrt(det)));

	free_double_matrix(invmat);
	free_double_vector(tmp1);
	free_double_vector(tmp2);

	return val;
}
void Test_approximate_block_solver(CuTest *tc)
{
    double_vector *p = alloc_double_vector(2);
    p->values[0] = 0.5;
    p->values[1] = 1.0;
    double_matrix *A = alloc_double_matrix(3, 2);
    A->values[0 * 2 + 0] = 1.0;
    A->values[1 * 2 + 0] = 2.0;
    A->values[2 * 2 + 0] = 3.0;
    
    A->values[0 * 2 + 1] = 2.0;
    A->values[1 * 2 + 1] = 2.0;
    A->values[2 * 2 + 1] = 3.0;
    
    double_vector *opt = approximate_block_solver(A, p, 5, 0.001);
    
    CuAssertDblEquals(tc, 0.0, opt->values[0], 0.01);
    CuAssertDblEquals(tc, 0.0, opt->values[1], 0.01);
    CuAssertDblEquals(tc, 5.0, opt->values[2], 0.01);
    
    free_double_vector(p);
    free_double_matrix(A);
    free_double_vector(opt);
}
Example #8
0
int main(int argc, char **argv)
{
	int      i, j;
	int      flag = 0;
	int      dim, size;
	double **data;
	double  *mean;
	double **sigma;
	double **corr;
	FILE    *fp;

	if(argc < 2){
		fprintf(stderr, "\n[使用法] : corrmat <file> (-s)\n");
		exit(1);
	}

	/* 出力フラグを調べる */
	if(is_opt(argc, argv, "-s"))
		flag = 1;
	
	/* 次元数とサンプル数を調べる */
	get_size(argv[1], &dim, &size);
	data = new_double_matrix(size, dim);

	/* データを読み込む */
	if((fp = fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "\n[エラー] : ファイル %s が開けません\n", argv[1]);
		exit(1);
	}
	
	for(i = 0; i < size; i++)
		for(j = 0; j < dim; j++)
			fscanf(fp, "%lf", &data[i][j]);

	fclose(fp);

	/* 平均を計算する */
	mean = calc_mean(data, size, dim);

	/* 共分散行列を計算する */
	sigma = calc_cov_mat(data, mean, size, dim);

	/* 相関係数行列を計算する */
	corr = calc_corr_mat(sigma, dim);

	/* 結果を出力 */
	if(!flag)
		printf("Correlation Matrix:\n");

	for(i = 0; i < dim; i++){
		for(j = 0; j < dim; j++){
			printf("%#+.2f", corr[i][j]);
			if(j < dim - 1)
				printf(" ");
		}
		printf("\n");
		fflush(stdout);
	}

	/* 終了処理 */
	free_double_matrix(data);
	free_double_vector(mean);
	free_double_matrix(sigma);
	free_double_matrix(corr);

	exit(0);
}
Example #9
0
void run_viterbi(ModelParams *model_params, Observation *observations, int *state_indices, double *state_probabilities, double *log_prob)
{
    double **log_delta;
    int **psi;
    double *log_pi;
    double **log_a;
    int i, j;
    long t;
    double max_logprob, this_logprob;
    int max_index, index;
    double **bprob, **eprob, **alpha, **beta, **gamma, mll;

    /* Find probabilities by solving for gammas: */
    fprintf(stderr, "Starting viterbi\n");

    bprob = alloc_double_matrix(model_params->T, model_params->N);
    eprob = alloc_double_matrix(model_params->T, model_params->N);
    alpha = alloc_double_matrix(model_params->T, model_params->N);
    beta = alloc_double_matrix(model_params->T, model_params->N);
    gamma = alloc_double_matrix(model_params->T, model_params->N);

    calc_bprobs(model_params, observations, bprob);
    calc_eprobs(model_params, observations, eprob);
    calc_alphas(model_params, observations, alpha, bprob, eprob, &mll);
    calc_betas(model_params, observations, beta, bprob, eprob);
    calc_gammas(model_params, alpha, beta, gamma);

    /* Calculate logs of parameters for easier manipulation. */

    log_pi = alloc_double_vector(model_params->N);
    for (i = 0; i < model_params->N; i++) {
        log_pi[i] = log((model_params->pi)[i]);
    } 

    log_a = alloc_double_matrix(model_params->N, model_params->N);
    for (i = 0; i < model_params->N; i++) {
        for (j = 0; j < model_params->N; j++) {
            log_a[i][j] = log(model_params->a[i][j]);
        }
    }

    log_delta = alloc_double_matrix(model_params->T, model_params->N);

    psi = alloc_int_matrix(model_params->T, model_params->N);

    /* Initialization */
    for (i = 0; i < model_params->N; i++) {
        log_delta[0][i] = log_pi[i] + log(bprob[0][i]) + log(eprob[0][i]);
        psi[0][i] = 0;
    }

    /* Recursion */
    for (t = 1; t < model_params->T; t++) {
        for (i = 0; i < model_params->N; i++) {
            max_logprob = -99999999.0;
            for (j = 0; j < model_params->N; j++) {
                this_logprob = log_delta[t-1][j] + log_a[j][i]; 
                if (this_logprob > max_logprob) {
                    max_logprob = this_logprob;
                    max_index = j;
                }
            }
            log_delta[t][i] = max_logprob + log(bprob[t][i]) + log(eprob[t][i]);
            psi[t][i] = max_index;
        }
    }

    /* Termination */
    *log_prob = -99999999.0;
    state_indices[model_params->T - 1] = 1;
    for (i = 0; i < model_params->N; i++) {
        if (log_delta[model_params->T - 1][i] > *log_prob) {
            *log_prob = log_delta[model_params->T - 1][i];
            state_indices[model_params->T - 1] = i;
        }
    }

    /* Traceback */
    for (t = model_params->T - 2; t >= 0; t--) {
        state_indices[t] = psi[t + 1][state_indices[t + 1]];
    }

    /* free memory */
    free_double_vector(log_pi);
    free_double_matrix(log_a, model_params->N);
    free_double_matrix(log_delta, model_params->T);
    free_int_matrix(psi, model_params->N);

    fprintf(stderr, "Done with viterbi\n");


    for (t = 0; t < model_params->T; t++) {
        index = state_indices[t];
        state_probabilities[t] = gamma[t][index];
        /* fprintf(stderr, "time %ld probability %lf for state %d\n", t, state_probabilities[t], index); */
    }

    free_double_matrix(eprob, model_params->T);
    free_double_matrix(alpha, model_params->T);
    free_double_matrix(beta, model_params->T);
    free_double_matrix(gamma, model_params->T);
}
Example #10
0
int main()
{
    printf("Programa para realizar figuras de polos generalizadas utilizando el analisis de Williamson-Hall\n");
    printf("Opciones:\n");
    printf("Modelos 1 y 2 (opcion 9):  (H * cos(\\theta) / \\lambda - \\delta * wc) = sqrt(\\alpha * \\ro) * [K * sqrt(C)]\n");
    printf("\\alpha = 0.5 * \\pi * M^2 * b^2\nK = 2 * sin(\\theta) / \\lambda\nC == contrast factor\n");
    printf("\\delta * wc = stacking fault broadening correction\n\\ro = dislocation density\n");
    printf("H = FWHM o BREADTH (opcion 8)\nModelos pares (opcion 7) trabajan con los anchos corregidos por ancho instrumental\n");
    printf("Para correr escribir:\n./idea-wh.exe");
    printf("IMPORTANTE\nIMPORTANTE\nIMPORTANTE\n");
    printf("IMPORTANTE\NVerificar que el archivo para_WH.dat se encuentre en el mismo directorio que idea-wh.exe");
    printf("IMPORTANTE\nIMPORTANTE\nIMPORTANTE\n");

    //variables del programa
    FILE *fp_in, *fp_out;
    char name[500];
    int i, nlines = 13320, nparam = 7;
    double m = 0, h = 0, **cov = matrix_double_alloc(2, 2), chisq = 0, R = 0;
    file_data * fdata = (file_data *) malloc(sizeof(file_data));
    crystal_data * cdata = (crystal_data *) malloc(sizeof(crystal_data));
    aux_data * adata = (aux_data *) malloc(sizeof(aux_data));
    angles_grad * angles = (angles_grad *) malloc(sizeof(angles_grad));
    linear_fit * fit_data = (linear_fit *) malloc(sizeof(linear_fit));
    shape_params * widths = (shape_params *) malloc(sizeof(shape_params));
    best_values * out_values = (best_values *) malloc(sizeof(best_values));

    //cargo los datos basicos
    if((fp_in = fopen("para_WH.dat", "r")) == NULL)
    {
        fprintf(stderr, "\nError opening para_WH.dat.\n");
        exit(1);
    }
    read_input(fp_in, fdata, cdata, adata);
    fclose(fp_in);
    //datos de la estructura cristalina
    double *h02 = vector_double_alloc(cdata->npeaks);
    double *wc = vector_double_alloc(cdata->npeaks);
    for(i = 0; i < cdata->npeaks; i++)
    {
        h02[i] = H2(cdata->indices[i]);
        wc[i] = warren_constants(cdata->type, cdata->indices[i]);
    }
    cdata -> H2 = h02;
    cdata -> warrenc = wc;
    //flags de control
    printf("\n----------------\nDatos de entrada\n----------------\n");
    printf_filedata(fdata);
    printf_crystaldata(cdata);
    printf_auxdata(adata);
    printf("----------------\nDatos de entrada\n----------------\n\n");
    //datos del difractograma
    double **dostheta = matrix_double_alloc(cdata->npeaks, nlines), **theta = matrix_double_alloc(cdata->npeaks, nlines);
    double **alpha = matrix_double_alloc(cdata->npeaks, nlines), **beta = matrix_double_alloc(cdata->npeaks, nlines);
    double **FWHM = matrix_double_alloc(cdata->npeaks, nlines), **FWHM_err = matrix_double_alloc(cdata->npeaks, nlines);
    double **breadth = matrix_double_alloc(cdata->npeaks, nlines), **breadth_err = matrix_double_alloc(cdata->npeaks, nlines);
    double **FWHM_corr = matrix_double_alloc(cdata->npeaks, nlines), **FWHM_corr_err = matrix_double_alloc(cdata->npeaks, nlines);
    double **breadth_corr = matrix_double_alloc(cdata->npeaks, nlines), **breadth_corr_err = matrix_double_alloc(cdata->npeaks, nlines);
    double *x = vector_double_alloc(cdata->npeaks), *y = vector_double_alloc(cdata->npeaks), *y_err = vector_double_alloc(cdata->npeaks);
    
    //generacion de las estructuras
    //estructura que contiene las coordenadas angulares (en grados)
    angles->theta_grad = theta;
    angles->dostheta_grad = dostheta;
    angles->alpha_grad = alpha;
    angles->beta_grad = beta;
    
    //datos del ajuste lineal
    fit_data->n_out_params = nparam;
    fit_data->m = m;
    fit_data->h = h;
    fit_data->x = x;
    fit_data->y = y;
    fit_data->y_err = y_err;
    fit_data->R = R;
    fit_data->chisq = chisq;
    fit_data->covar = cov;

    //datos con los parametros de ensanchamiento del pico
    widths->FWHM = FWHM;
    widths->FWHM_err = FWHM_err;
    widths->breadth = breadth;
    widths->breadth_err = breadth_err;
    widths->FWHM_corr = FWHM_corr;
    widths->FWHM_corr_err = FWHM_corr_err;
    widths->breadth_corr = breadth_corr;
    widths->breadth_corr_err = breadth_corr_err;

    printf("Inicio lectura figuras de polos\n");
    nlines = read_pole_figures(fdata, angles, widths);

    //datos con los mejores resultados del ajuste de williamson hall
    double ** best_R_val = matrix_double_alloc(fit_data->n_out_params, nlines);
    double ** best_chi_val = matrix_double_alloc(fit_data->n_out_params, nlines);
    out_values->R_max = 0;
    out_values->best_R_values = best_R_val;
    out_values->chisq_min = 1000;
    out_values->best_chisq_values = best_chi_val;

    printf("Iniciando el ajuste de Williamson-Hall\n");
    if(strcmp(fdata->is_H, "FWHM") == 0)
        if(strcmp(fdata->is_corr, "N") == 0)
            if(fdata->model == 1)
                williamson_hall_plot(nlines, adata, cdata, widths->FWHM, widths->FWHM_err, angles, fit_data, out_values);
            else
            {
                printf("Modelo no aceptado o modelo no compatible con lo ingresado en las opciones 7 y 8\n");
                exit(1);
            }
        else if(strcmp(fdata->is_corr, "Y") == 0)
            if(fdata->model == 2)
                williamson_hall_plot(nlines, adata, cdata, widths->FWHM_corr, widths->FWHM_corr_err, angles, fit_data, out_values);
            else
            {
                printf("Modelo no aceptado o modelo no compatible con lo ingresado en las opciones 7 y 8\n");
                exit(1);
            }
        else
        {
            printf("El texto ingresado en la opción 7 no es válido. Ingrese un caracter valido (Y o N)\n");
            exit(1);
        }
    else if(strcmp(fdata->is_H, "BREADTH") == 0)
        if(strcmp(fdata->is_corr, "N") == 0)
            if(fdata->model == 1)
                williamson_hall_plot(nlines, adata, cdata, widths->breadth, widths->breadth_err, angles, fit_data, out_values);
            else
            {
                printf("Modelo no aceptado o modelo no compatible con lo ingresado en las opciones 7 y 8\n");
                exit(1);
            }
        else if(strcmp(fdata->is_corr, "Y") == 0)
            if(fdata->model == 2)
                williamson_hall_plot(nlines, adata, cdata, widths->breadth_corr, widths->breadth_corr_err, angles, fit_data, out_values);
            else
            {
                printf("Modelo no aceptado o modelo no compatible con lo ingresado en las opciones 7 y 8\n");
                exit(1);
            }
        else
        {
            printf("El texto ingresado en la opción 7 no es válido. Ingrese un caracter valido (y o n)\n");
            exit(1);
        }
    else
    {
        printf("La opcion 8 seleccionada no es valida. Ingrese una opcion valida (FWHM o BREADTH)\n");
        exit(1);
    }

    printf("\nImprimiendo los mejores resultados segun R\n");
    sprintf(name, "%s%s%s_%d_WH_R.dat", fdata->outPath, fdata->filename, fdata->is_H, fdata->model);
    fp_out = fopen(name, "w");
    print_results(fdata, fp_out, out_values->best_R_values, fit_data, nlines, angles, cdata);
    fclose(fp_out);
    /*
    printf("Imprimiendo los mejores resultados segun chi^2\n");
    sprintf(name, "%s%s%s_%d_WH_chi2.dat", fdata->outPath, fdata->filename, fdata->is_H, fdata->model);
    fp_out = fopen(name, "w");
    print_results(fdata, fp_out, out_values->best_chisq_values, fit_data, nlines, angles, cdata);
    fclose(fp_out);
    */
    printf("Liberando memoria\n");
    free_double_matrix(out_values->best_R_values, fit_data->n_out_params);
    free_double_matrix(out_values->best_chisq_values, fit_data->n_out_params);
    free_double_matrix(cov, 2);
    free_double_matrix(dostheta, cdata->npeaks); free_double_matrix(theta, cdata->npeaks);
    free_double_matrix(alpha, cdata->npeaks); free_double_matrix(beta, cdata->npeaks);
    free_double_matrix(FWHM, cdata->npeaks);
    free_double_matrix(breadth, cdata->npeaks);
    free(x); free(y); free(y_err); free(FWHM_err), free(breadth_err);
    free(angles); free(fit_data); free(widths); free(out_values);
    free(fdata); free(cdata); free(adata);
    printf("done!\n");
    return 0;
}
Example #11
0
int main(int argc, char **argv)
{
	int    l, i, j;
	int    dim, num;
	int    step;
	int    level;
	char   name[NAMELEN];
	double min, max, shift, threshold;
	double **data;
	FILE   *fp;

	if(argc != 4){
		fprintf(stderr, "\n[使用法] : contour <file> <step> <level>\n");
		exit(1);
	}

	get_size(argv[1], &dim, &num);

	if(dim != DIM + 1){
		fprintf(stderr, "\n[エラー] : このプログラムは2次元データのみ対応\n");
		exit(1);
	}

	step = atoi(argv[2]);
	level = atoi(argv[3]);
	data = new_double_matrix(num, dim);

	if((fp = fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "\n[エラー] : %s が開けません\n", argv[1]);
		exit(1);
	}
	
	for(i = 0; i < num; i++)
		for(j = 0; j < dim; j++)
			fscanf(fp, "%lf", &data[i][j]);
	
	fclose(fp);
	
	min = DBL_MAX;
	max = -DBL_MAX;
	for(i = 0; i < num; i++){
		if(min > data[i][2])
			min = data[i][2];
		if(max < data[i][2])
			max = data[i][2];
	}

	shift = (max - min) / ((double)level + 1.0);
	threshold = min + shift;
	
	for(l = 1; l <= level; l++){
	
		sprintf(name, "level%d.dat", l);
		if((fp = fopen(name, "w")) == NULL){
			fprintf(stderr, "\n[エラー] : %sが用意できませんでした\n", name);
			exit(1);
		}

		for(i = 0; i < num; i++){

			if(((i+1) % step) != 1){

				/* 指定された等高線かどうか判定 */
				if((data[i][2] - threshold) * (data[i-1][2] - threshold) < 0.0){

					for(j = 0; j < 2; j++)
						fprintf(fp, "%f ", (data[i][j] + data[i-1][j]) / 2.0);
					fprintf(fp, "\n");

				}

			}

		}

		threshold += shift;
		fclose(fp);
	}

	free_double_matrix(data);

	exit(0);
}
Example #12
0
int main(int argc, char **argv)
{
	int      i, j;
	int      flag = 0;
	int      dim, size;
	double **data;
	double  *mean;
	double **sigma;
	FILE    *fp;

	if(argc < 2){
		fprintf(stderr, "\n[使用法] : meancov <file> (-s)\n");
		exit(1);
	}

	/* 出力フラグを調べる */
	if(is_opt(argc, argv, "-s"))
		flag = 1;
	
	/* 次元数とサンプル数を調べる */
	get_size(argv[1], &dim, &size);
	data = new_double_matrix(size, dim);

	/* データを読み込む */
	if((fp = fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "\n[エラー] : ファイル %s が開けません\n", argv[1]);
		exit(1);
	}
	
	for(i = 0; i < size; i++)
		for(j = 0; j < dim; j++)
			fscanf(fp, "%lf", &data[i][j]);

	fclose(fp);

	/* 平均を計算する */
	mean = calc_mean(data, size, dim);

	/* 共分散行列を計算する */
	sigma = calc_cov_mat(data, mean, size, dim);

	/* 結果を出力 */
	if(!flag)
		fprintf(stdout, "Mean :\n");

	for(i = 0; i < dim; i++){
		fprintf(stdout, "%#+.6g", mean[i]);
		if(i != dim - 1)
			fprintf(stdout, " ");
	}
	
	if(!flag)
		fprintf(stdout, "\n\n");
	else
		fprintf(stdout, "\n");

	if(!flag)
		fprintf(stdout, "Covariance :\n");

	for(i = 0; i < dim; i++){
		for(j = 0; j < dim; j++){
			fprintf(stdout, "%#+.6g", sigma[i][j]);
			if(j != dim - 1)
				fprintf(stdout, " ");
		}
		fprintf(stdout, "\n");
	}

	/* 終了処理 */
	free_double_matrix(data);
	free_double_vector(mean);
	free_double_matrix(sigma);

	exit(0);
}
Example #13
0
int main(int argc, char* argv[])
{
  //Init lock
  pthread_mutex_init(&lock,NULL);

  /* Get size of current state as input */
  int i, j;
  printf("Number of processes: ");
  scanf("%d", &m);
  printf("Number of resources: ");
  scanf("%d", &n);

  s = (State *) malloc(sizeof(State));
  s->resource = (int *) malloc(n * sizeof(int));
  s->available = (int *) malloc(n * sizeof(int));
  s->max = allocate_double_matrix(m,n);
  s->allocation = allocate_double_matrix(m,n);
  s->need = allocate_double_matrix(m,n);

  if (s == NULL) { printf("\nYou need to allocate memory for the state!\n"); exit(0); };

  /* Get current state as input */
    printf("Resource vector: ");
  for(i = 0; i < n; i++)
    scanf("%d", &s->resource[i]);
  printf("Enter max matrix: ");
  for(i = 0;i < m; i++)
    for(j = 0;j < n; j++)
      scanf("%d", &s->max[i][j]);
  printf("Enter allocation matrix: ");
  for(i = 0; i < m; i++)
    for(j = 0; j < n; j++) {
      scanf("%d", &s->allocation[i][j]);
    }
  printf("\n");

  /* Calcuate the need matrix */
  for(i = 0; i < m; i++)
    for(j = 0; j < n; j++)
      s->need[i][j] = s->max[i][j]-s->allocation[i][j];

  /* Calcuate the availability vector */
  for(j = 0; j < n; j++) {
    int sum = 0;
    for(i = 0; i < m; i++)
      sum += s->allocation[i][j];
    s->available[j] = s->resource[j] - sum;
  }

  /* Output need matrix and availability vector */
  printf("Need matrix:\n");
  for(i = 0; i < n; i++)
    printf("R%d ", i+1);
  printf("\n");
  for(i = 0; i < m; i++) {
    for(j = 0; j < n; j++)
      printf("%d  ",s->need[i][j]);
    printf("\n");
  }
  printf("Availability vector:\n");
  for(i = 0; i < n; i++)
    printf("R%d ", i+1);
  printf("\n");
  for(j = 0; j < n; j++)
    printf("%d  ",s->available[j]);
  printf("\n");

  /* If initial state is unsafe then terminate with error */
  if (safe() == 0) {
    printf("An error occurred. The state is not safe\n");
    exit(0);
  }

  /* Seed the random number generator */
  struct timeval tv;
  gettimeofday(&tv, NULL);
  srand(tv.tv_usec);

  /* Create m threads */
  pthread_t *tid = malloc(m*sizeof(pthread_t));
  for (i = 0; i < m; i++)
    pthread_create(&tid[i], NULL, process_thread, (void *) (long) i);

  /* Wait for threads to finish */
  pthread_exit(0);
  free(tid);

  free(s->available);
  free(s->resource);
  free_double_matrix(s->max);
  free_double_matrix(s->allocation);
  free_double_matrix(s->need);
  free(s);

  printf("done");
}
Example #14
0
void test_likelihood(t_setting* setting, const t_corpus* corpus, const std::vector<t_cat> tree_structure)
{
	FILE* 	fileptr_lowerbound_result;
	FILE* 	fileptr_lowerbound_summary;
	FILE* 	fileptr_document_completion_result;
	FILE* 	fileptr_document_completion_summary;
	char 	filename[MAX_BUF];

	sprintf(filename, "%s_tilda", setting->model_path);
	t_tilda_model* trained_tilda_model = load_tilda_model(filename);

	sprintf(filename, "%s_var", setting->model_path);
	t_tilda_var_model* trained_var_model = load_var_model(filename, corpus);

	double**	rho = NULL;
	double* 	old_rho = NULL;
	double* 	nu = NULL;
	double*		dirichlet_prior = NULL;
	double*		expected_theta = NULL;
	double**	expected_beta = NULL;


	const int& K = trained_tilda_model->num_topics;
	setting->num_topics = K;

	oneoverk = 1 / (double) K;

	double 	document_completion_sum_ll = 0.0;
	int		document_completion_sum_num_words = 0;

	double	lowerbound_sum_likelihood = 0;
	int  	lowerbound_sum_num_words = 0;

	nu = zero_init_double_array(K);
	rho = zero_init_double_matrix(corpus->max_length, K);
	old_rho = zero_init_double_array(K);
	dirichlet_prior = zero_init_double_array(K);
	expected_theta = zero_init_double_array(K);
	expected_beta = zero_init_double_matrix(K, corpus->num_terms);

	digamma_nu = zero_init_double_matrix(corpus->num_docs, K);
	digamma_nu_sum = zero_init_double_array(corpus->num_docs);

	digamma_lambda = zero_init_double_matrix(K, corpus->num_terms);
	digamma_lambda_sum = zero_init_double_array(K);

	compute_lambda_statistics(trained_var_model, expected_beta);

	sprintf(filename, "%s_lowerbound_result", setting->output_path);
	fileptr_lowerbound_result = fopen(filename, "w");

	sprintf(filename, "%s_document_completion_result", setting->output_path);
	fileptr_document_completion_result = fopen(filename, "w");

	for (int i = 0; i < tree_structure.size(); ++i) {
		const double&	alpha_t = trained_tilda_model->alpha[i];
		const double*	kappa_t = trained_var_model->kappa[i];
		const double&	tau_t = trained_var_model->tau[i];

		for (int j = 0; j < K; ++j) {
			dirichlet_prior[j] = alpha_t * kappa_t[j];
		}

		for (int d = 0; d < tree_structure[i].docids.size(); ++d) {
			const int& docid = tree_structure[i].docids[d];

			// evaluation using variational bound
			double this_doc_lowerbound = doc_e_step(&(corpus->docs[docid]), dirichlet_prior, nu,
													digamma_lambda, digamma_lambda_sum, setting,
													docid, rho, old_rho);

			assert(!std::isnan(this_doc_lowerbound));

			this_doc_lowerbound += lgamma(alpha_t);
			this_doc_lowerbound -= (K - alpha_t) * digamma(tau_t);
			this_doc_lowerbound -= alpha_t * (K - 1) / tau_t;
			for (int j = 0; j < K; ++j) {
				this_doc_lowerbound -= lgamma(alpha_t * kappa_t[j]) +
										(1 - alpha_t * kappa_t[j]) * (log(kappa_t[j]) - digamma(tau_t * kappa_t[j]));
			}

			for (int j = 0; j < K; ++j) {
				this_doc_lowerbound += dirichlet_prior[j] * (digamma_nu[docid][j] - digamma_nu_sum[docid]);
			}

			assert(!std::isnan(this_doc_lowerbound));

			fprintf(fileptr_lowerbound_result, "docid %d\tlower_bound %5.5f\tnum_words %d\n", docid, this_doc_lowerbound, corpus->docs[docid].total);

			lowerbound_sum_likelihood += this_doc_lowerbound;
			lowerbound_sum_num_words += corpus->docs[docid].total;

			// evaluation using document completion
			t_document*	inference_doc = NULL;
			t_document*	test_doc = NULL;
			split_document(inference_doc, test_doc, &(corpus->docs[docid]));
			double half_doc_lowerbound = doc_e_step(inference_doc, dirichlet_prior, nu,
													digamma_lambda, digamma_lambda_sum, setting,
													docid, rho, old_rho);

			assert(!std::isnan(half_doc_lowerbound));

			half_doc_lowerbound += lgamma(alpha_t);
			half_doc_lowerbound -= (K - alpha_t) * digamma(tau_t);
			half_doc_lowerbound -= alpha_t * (K - 1) / tau_t;
			for (int j = 0; j < K; ++j) {
				half_doc_lowerbound -= lgamma(alpha_t * kappa_t[j]) +
										(1 - alpha_t * kappa_t[j]) * (log(kappa_t[j]) - digamma(tau_t * kappa_t[j]));
			}

			for (int j = 0; j < K; ++j) {
				half_doc_lowerbound += dirichlet_prior[j] * (digamma_nu[docid][j] - digamma_nu_sum[docid]);
			}

			assert(!std::isnan(half_doc_lowerbound));

			double document_completion_log_likelihood = 0.0;
			double nu_sum = 0.0;
			for (int j = 0; j < K; ++j) {
				nu_sum += nu[j];
			}
			for (int j = 0; j < K; ++j) {
				expected_theta[j] = nu[j] / nu_sum;
			}

			for (int n = 0; n < test_doc->length; n++) {
				double this_word_likelihood = 0.0;
				for (int j = 0; j < K; ++j) {
					this_word_likelihood += expected_theta[j] * expected_beta[j][test_doc->words[n]];
				}
				document_completion_log_likelihood += log(this_word_likelihood + 1e-100) * test_doc->counts[n];
			}

			fprintf(fileptr_document_completion_result, "docid %d\thalf_lower_bound %5.5f\tscore %5.5f\ttest_num_words %d\n",
					docid, half_doc_lowerbound, document_completion_log_likelihood, test_doc->total);

			document_completion_sum_ll += document_completion_log_likelihood;
			document_completion_sum_num_words += test_doc->total;

			free_document(inference_doc);
			free_document(test_doc);
		}
	}

	fclose(fileptr_lowerbound_result);
	fclose(fileptr_document_completion_result);

	double perplexity = exp(-lowerbound_sum_likelihood / (double) lowerbound_sum_num_words);
	sprintf(filename, "%s_lowerbound_summary", setting->output_path);
	fileptr_lowerbound_summary = fopen(filename, "w");
	fprintf(fileptr_lowerbound_summary, "sum_lowerbound %5.5f\n", lowerbound_sum_likelihood);
	fprintf(fileptr_lowerbound_summary, "sum_num_words %d\n", lowerbound_sum_num_words);
	fprintf(fileptr_lowerbound_summary, "perplexity %5.5f\n", perplexity);
	fclose(fileptr_lowerbound_summary);

	double per_word_ll = document_completion_sum_ll / (double) document_completion_sum_num_words;
	sprintf(filename, "%s_document_completion_summary", setting->output_path);
	fileptr_document_completion_summary = fopen(filename, "w");
	fprintf(fileptr_document_completion_summary, "sum_num_words %d\n", document_completion_sum_num_words);
	fprintf(fileptr_document_completion_summary, "per_word_ll %5.5f\n", per_word_ll);
	fprintf(fileptr_document_completion_summary, "perplexity %5.5f\n", exp(-per_word_ll));
	fclose(fileptr_document_completion_summary);

	free_double_matrix(digamma_lambda);
	free(digamma_lambda_sum);

	free_double_matrix(digamma_nu);
	free(digamma_nu_sum);

	free_double_matrix(expected_beta);
	free(expected_theta);
	free(dirichlet_prior);
	free(nu);
	free_double_matrix(rho);
	free(old_rho);
	free_var_model(trained_var_model);
	free_tilda_model(trained_tilda_model);

}
Example #15
0
void pv_fitting(char basename[1024], int exists, exp_data * sync_data, peak_data * difra, double * intens, double ** seeds)
{
    //printf("pv_fitting start\n");
    //auxiliary variables
    char filename[500];
    FILE *fp;
    int i, j, n, bad_fit, zero_peak_index[difra->numrings];
    // Remove peaks with intensity below the treshold
    int n_peaks = check_for_null_peaks(difra->treshold, difra->numrings, zero_peak_index, intens);
    double t0[n_peaks], I0[n_peaks], shift_H[n_peaks], shift_eta[n_peaks];
    int seeds_size = 4 * n_peaks + 2, all_seeds_size = 4 * difra->numrings + 2;
    int ptrn_start = theta2bin(difra->bg[0][0], sync_data->pixel, sync_data->dist);
    int ptrn_end = theta2bin(difra->bg[0][difra->n_bg - 1], sync_data->pixel, sync_data->dist);
    int net_size = ptrn_end - ptrn_start;
    // Set the vector with the seeds for the fitting
    double ** peak_seeds = matrix_double_alloc(2, seeds_size), *fit_errors = vector_double_alloc(seeds_size);
    memset(fit_errors, 0, seeds_size * sizeof(double));
    set_seeds(all_seeds_size, zero_peak_index, exists, seeds, peak_seeds);
    // Number of parameters to fit
    int n_param[4] = {2 * n_peaks + 1 + difra->n_bg, 3 * n_peaks + difra->n_bg, 2 * n_peaks + 2 + difra->n_bg, 4 * n_peaks + difra->n_bg};
    // Fixed parameters
    gsl_vector * ttheta = gsl_vector_alloc(net_size); // 2theta
    gsl_vector * y = gsl_vector_alloc(net_size); // counts of the pattern
    gsl_vector * sigma = gsl_vector_alloc(net_size); // count error
    gsl_vector * bg_pos = gsl_vector_alloc(difra->n_bg); // background points

    //printf("Getting data\n");
    j = 0;
    for(i = ptrn_start; i < ptrn_end; i++){        
        gsl_vector_set(ttheta, j, bin2theta(i, sync_data->pixel, sync_data->dist));// convert from bin to 2theta
        gsl_vector_set(y, j, difra->intensity[i]); // set inensities
        gsl_vector_set(sigma, j, sqrt(difra->intensity[i])); // the error of the intensities is the square root
        j++;
    }
    for(i = 0; i < difra->n_bg; i++)
        gsl_vector_set(bg_pos, i, difra->bg[0][i]);
    struct data d = {net_size, n_peaks, difra->n_bg, ttheta, y, sigma, bg_pos}; // structure with the experimental data
 
    // Logfile with the input and output seeds
    char fitlogname[1024];
    sprintf(fitlogname, "%sfit_results.log", sync_data->root_name);
    FILE * fp_logfile = fopen(fitlogname, "a");
    fprintf(fp_logfile, "spr: %d gamma: %d (p%d)\nStarting values\n", difra->spr, difra->gamma, difra->npattern);
    print_seeds2file(fp_logfile, peak_seeds[exists], fit_errors, seeds_size, difra->bg, difra->n_bg);
    //printf("Start interations\n");
    //printf("Step 1\n");
    //print_seeds(peak_seeds[exists], seeds_size, difra->bg, difra->n_bg);
    pv_step1(exists, peak_seeds, seeds_size, difra->bg, &d, n_param[0]);
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    check(y, peak_seeds, seeds_size, n_peaks, difra->bg, difra->n_bg);
    //printf("Step 2\n");
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    pv_step2(exists, peak_seeds, seeds_size, difra->bg, &d, n_param[1]);
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    check(y, peak_seeds, seeds_size, n_peaks, difra->bg, difra->n_bg);
    //printf("Step 3\n");
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    pv_step3(exists, peak_seeds, fit_errors, seeds_size, difra->bg, &d, n_param[2]);
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    check(y, peak_seeds, seeds_size, n_peaks, difra->bg, difra->n_bg);
    //printf("Step 4\n");
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    pv_step4(exists, peak_seeds, fit_errors, seeds_size, difra->bg, &d, n_param[3]);
    //print_seeds(peak_seeds[1], seeds_size, difra->bg, difra->n_bg);
    //printf("End of iterations\n");

    //Print the result of the fitting in fp_logfile
    fprintf(fp_logfile, "Final values\n");
    print_seeds2file(fp_logfile, peak_seeds[1], fit_errors, seeds_size, difra->bg, difra->n_bg);
    fflush(fp_logfile);
    fclose(fp_logfile);
  
    //printf("Correction and output of the results\n");
    sprintf(filename, "%s%sspr_%d_pattern_%d.dat", sync_data->path_out, sync_data->root_name, difra->spr, difra->gamma);
    if((fp = fopen(filename, "w")) == NULL)
    {
        fprintf(stderr, "Error opening file %s\n", filename);
        exit(1);
    }
    for(i = 0; i < net_size; i++)
        if(gsl_vector_get(y, i))
            fprintf(fp, "%.5lf %.5lf\n", gsl_vector_get(ttheta, i), gsl_vector_get(y, i));
        else
            fprintf(fp, "%.5lf %.5lf\n", gsl_vector_get(ttheta, i), 1.0);

    fflush(fp);
    fclose(fp);
    //imprimo las posiciones de los picos y sus intensidades
    sprintf(filename, "%s%sspr_%d_pattern_%d.peak-index.dat", sync_data->path_out, sync_data->root_name, difra->spr, difra->gamma);
    if((fp = fopen(filename, "w")) == NULL)
    {
        fprintf(stderr, "Error opening file %s\n", filename);
        exit(1);
    }
    n = 0;
    for(i = 2; i < seeds_size; i += 4)
    {
      t0[n] = peak_seeds[1][i]; 
      I0[n] = peak_seeds[1][i + 1];
      shift_H[n] = peak_seeds[1][i + 2];
      shift_eta[n] = peak_seeds[1][i + 3];
      n++;
    }
    n = 2;
    for(i = 0; i < difra->numrings; i++)
    {
        if(zero_peak_index[i] == 0)
        {
            double theta = peak_seeds[1][n], H = peak_seeds[1][0], eta = peak_seeds[1][1];
            double I = pseudo_voigt(theta, n_peaks, I0, t0, H, eta, shift_H, shift_eta, difra->n_bg, bg_pos, difra->bg[1]);
            fprintf(fp, "%.5lf %.5lf %d %d\n", peak_seeds[1][n], I, difra->hkl[i], difra->ph[i]);
            n += 4;
        }
    }    
    fflush(fp);
    fclose(fp);
    //imprimo las posiciones y las intensidades de los puntos de background
    sprintf(filename, "%s%sspr_%d_pattern_%d.bg-spline.dat", sync_data->path_out, sync_data->root_name, difra->spr, difra->gamma);
    if((fp = fopen(filename, "w")) == NULL)
    {
        fprintf(stderr, "Error opening file %s\n", filename);
        exit(1);
    }
    for(i = 0; i < difra->n_bg; i++)
      fprintf(fp, "%.5lf %.5lf\n", difra->bg[0][i], difra->bg[1][i]);
    fflush(fp);
    fclose(fp);
    //se puede analizar la posibilidad de usar una cubic spline en vez de una lineal para este programa
    //ya que el propio cmwp va a usar una cubic spline
    bad_fit = fit_result(all_seeds_size, peak_seeds, fit_errors, zero_peak_index, sync_data, difra);
    if(bad_fit) check(y, peak_seeds, seeds_size, n_peaks, difra->bg, difra->n_bg);
    set_seeds_back(all_seeds_size, zero_peak_index, exists, seeds, peak_seeds);
    
    //liberacion de memoria allocada
    gsl_vector_free(ttheta);
    gsl_vector_free(y);
    gsl_vector_free(sigma);
    gsl_vector_free(bg_pos);
    free_double_matrix(peak_seeds, 2);
    free(fit_errors);
//    printf("Fin pv_fitting\n");
}