Exemple #1
0
int init_matrix_from_file(struct Matrix* m, const char* f) {
    if (0 != get_matrix_size(m, f)) {
        return -1;
    }

    m->m = (double**)malloc(sizeof(double*) * m->row);
    int ii = 0;
    for (ii = 0; ii < m->row; ++ii) {
        m->m[ii] = malloc(sizeof(double) * m->col);
    }

    FILE* fp = fopen(f, "r");
    if (NULL == fp) {
        printf("can't open file!\n");
        return -1;
    }
    char* line = NULL;
    size_t linecap = 0;
    ssize_t linelen = 0;
    int i = 0;
    int j = 0;
    while ((linelen = getline(&line, &linecap, fp)) > 0) {
        char* tmp = line;
        j = 0;
        while ((tmp = strtok(tmp, " ")) != NULL)
        {
            m->m[i][j] = atof(tmp); 
            tmp = NULL;
            j++;
        }
        i++;
    }
    fclose(fp);
    return 0;
}
Exemple #2
0
void DiscreteProblem::create_matrix()
{
  // remove any previous matrix
  free_matrix_indices();
  free_matrix_values();

  // calculate the total number of DOFs
  ndofs = 0;
  for (int i = 0; i < neq; i++)
    ndofs += spaces[i]->get_num_dofs();
  if (!quiet) verbose("Ndofs: %d", ndofs);
  if (!ndofs) return;

  // get row and column indices of nonzero matrix elements
  Page** pages = new Page*[ndofs];
  memset(pages, 0, sizeof(Page*) * ndofs);
  if (!quiet) { verbose("Calculating matrix sparse structure..."); begin_time(); }
  precalculate_sparse_structure(pages);

  // initialize the arrays Ap and Ai
  Ap = (int*) malloc(sizeof(int) * (ndofs+1));
  int aisize = get_num_indices(pages, ndofs);
  Ai = (int*) malloc(sizeof(int) * aisize);
  if (Ai == NULL) error("Out of memory. Could not allocate the array Ai.");

  // sort the indices and remove duplicities, insert into Ai
  int i, pos = 0, num;
  for (i = 0; i < ndofs; i++)
  {
    Ap[i] = pos;
    pos += sort_and_store_indices(pages[i], Ai + pos, Ai + aisize);
  }
  Ap[i] = pos;
  if (!quiet) verbose("  Nonzeros: %d\n  Total matrix size: %0.1lf MB\n  (time: %g sec)",
                         pos, (double) get_matrix_size() / (1024*1024), end_time());
  delete [] pages;

  // shrink Ai to the actual size
  int* oldAi = Ai;
  Ai = (int*) realloc(Ai, sizeof(int) * pos);
  if (oldAi != Ai) warn("Realloc moved Ai when shrinking."); // this should not happen

  // UMFPACK: perform symbolic analysis of the matrix
  if (!quiet) { verbose("Performing UMFPACK symbolic analysis..."); begin_time(); }
  int status = umfpack_symbolic(ndofs, ndofs, Ap, Ai, NULL, &Symbolic, NULL, NULL);
  if (status != UMFPACK_OK) umfpack_status(status);
  if (!quiet) verbose("  (time: %g sec)", end_time());

  equi = (double*) malloc(sizeof(double) * ndofs);
  if (equi == NULL) error("Out of memory. Error allocating the equilibration vector.");
  for (int i = 0; i < ndofs; i++)
    equi[i] = 1.0;
  is_equi = false;
}
Exemple #3
0
int main(int argc, char* argv[])
{
    /* Start the timer */
    uglyTime(NULL);
    printf("\nQUBIC %.1f: greedy biclustering (compiled "__DATE__" "__TIME__")\n\n", VER);
    rows = cols = 0;

    /* get the program options defined in get_options.c */
    get_options(argc, argv);

    /*get the size of input expression matrix*/
    get_matrix_size(po->FP);
    progress("File %s contains %d genes by %d conditions", po -> FN, rows, cols);
    if (rows < 3 || cols < 3)
    {
        /*neither rows number nor cols number can be too small*/
        errAbort("Not enough genes or conditions to make inference");
    }
    genes = alloc2c(rows, LABEL_LEN);
    conds = alloc2c(cols, LABEL_LEN);

    /* Read in the gene names and condition names */
    read_labels(po -> FP);

    /* Read in the expression data */
    if (po->IS_DISCRETE)
        read_discrete(po -> FP);
    else
    {
        read_continuous(po -> FP);

        /* formatting rules */
        discretize(addSuffix(po->FN, ".rules"));
    }
    fclose(po->FP);

    /*we can do expansion by activate po->IS_SWITCH*/
    if (po->IS_SWITCH)
    {
        read_and_solve_blocks(po->FB, addSuffix(po->BN, ".expansion"));
    }
    else
    {
        /* formatted file */
        write_imported(addSuffix(po->FN, ".chars"));

        /* the file that stores all blocks */
        make_graph(addSuffix(po->FN, ".blocks"));
    }/* end of main else */
    free(po);
    return 0;
}
Exemple #4
0
void main(int argc, char **argv) {

	FILE *file, *fout, *mask_file; // arquivos de entrada e saída

	char line[MAX], header_type[MAX]; // strings

	int matrix_width, matrix_height, grayscale, weight, soma, mask_size; // colunas, linhas, escala de cinza, weight da máscara, soma dos elementos

	int i, j, w, z, x, y; // contadores e auxiliares

	int **array, **output, **mask; // matriz da imagem original, matriz da imagem de saída, matriz da mascara

	int c, r = 0; // colunas, linhas

	// verifica se os argumentos foram passados corretamente
	if (argc != 5 && argc != 4) {
		printf(
				"Os argumentos foram passados incorretamente. Por favor, consulte a documentação.");
		exit(EXIT_FAILURE);
	} else {
		if (!(file = fopen(argv[1], "r"))) {
			printf("Não foi possível abrir a imagem.");
			exit(EXIT_FAILURE);
		}

		if (!(fout = fopen(argv[argc - 1], "w+"))) {
			printf("Não foi possível abrir a imagem.");
			exit(EXIT_FAILURE);
		}
	}

	// se for utilizado o filtro de média, abre a imagem
	if (argc == 4) {
		if (!(mask_file = fopen(argv[2], "r"))) {
			printf("Não foi possível abrir a máscara.");
			exit(EXIT_FAILURE);
		}
	}

	// tipo da imagem: P5 ou P2
	get_image_type(file, &header_type);

	// tamanho da matrix: col lin
	get_matrix_size(file, &matrix_width, &matrix_height);

	// maior valor da escala de cinza
	get_grayscale(file, &grayscale);

	// verifica se o nível de cinza é o permitido
	if (grayscale > MAX_GRAYSCALE) {
		printf("O valor da escala de cinza é maior do que o permitido.");
		exit(EXIT_FAILURE);
	}

	// escreve o cabeçalho da imagem no arquivo
	write_image_header(fout, header_type, matrix_width, matrix_height,
			grayscale);

	// aloca memória para a imagem de saída
	output = (int **) (mallocc(sizeof(int *) * matrix_height));
	for (i = 0; i < matrix_height; i++) {
		output[i] = (int *) (mallocc(sizeof(int) * matrix_width));
	}

	// copia o corpo da imagem para a matriz
	array = read_matrix_elements(file, matrix_width, matrix_height);
	output = read_matrix_elements(file, matrix_width, matrix_height);

	// filtro da média
	mask_size = get_mask_size(mask_file);
	if(argc == 4){
		// aloca memória para a imagem de saída
		mask = (int **) (mallocc(sizeof(int *) * mask_size));
		for (i = 0; i < mask_size; i++) {
			mask[i] = (int *) (mallocc(sizeof(int) * mask_size));
		}

		// passa os elementos do arquivo para uma array
		mask = read_matrix_elements(mask_file, mask_size, mask_size);

		// verifica se o tamanho da mascara está correto
		if ((mask_size % 2 == 0) || mask_size < 3) {
			printf("Tamanho da máscara incorreto.");
			exit(EXIT_FAILURE);
		}

		// calcula o weight da máscara
		for (i = 0; i < mask_size; i++) {
			for (j = 0; j < mask_size; j++) {
				weight = weight + mask[i][j];
			}
		}

		// aplica o filtro
		media( matrix_height, matrix_width, mask_size, weight, array, output );
	} else
		mediana(matrix_height, matrix_width, mask_size, array, output);

	// grava imagem
	write_image_body(fout, matrix_width, matrix_height, output);

	// libera a memória alocada
	free(array);
	free(output);

	// fecha o buffer dos arquivos
	fclose(file);
	fclose(fout);

	return;
}