示例#1
0
文件: matio.cpp 项目: aliciaa/cs51501
int writeMatrix(char* filename, int m, int n, double* v)
{
	// try opening the file
	FILE *fp;
	if ((fp = fopen(filename, "w")) == NULL) {
		fprintf(stderr, "Error occurs while writing to file %s\n", filename);
		return 1;
	}

	// set the type
	MM_typecode type;
	mm_initialize_typecode(&type);
	mm_set_matrix(&type);
	mm_set_array(&type);
	mm_set_real(&type);
	mm_set_general(&type);

	// write banner and sizes
	mm_write_banner(fp, type);
	mm_write_mtx_array_size(fp, m, n);

	printf("writing %s:\n\ta %d x %d matrix...", filename, m, n);
	for (int j = 0; j < n; ++j) {
		for (int i = 0; i < m; ++i) { 
			fprintf(fp, "%lf\n", v[i * n + j]);
		}
	}
	printf("done\n");

	// close the file
	fclose(fp);

	return 0;
}
示例#2
0
/*** set to general ***/
static void
mm_real_set_general (mm_real *x)
{
	if (!mm_real_is_symmetric (x)) return;
	mm_set_general (&(x->typecode));
	x->symm = MM_REAL_GENERAL;
	return;
}
int CreateDenseMatrixGeneral(char *fileName, unsigned long int numRows, unsigned long int numCols, unsigned int seed, double min, double max) {
//Options: numRows numCols fileName seed

	FILE *output;
	//long nz;   
	unsigned long int i, j;
	MM_typecode outputmatcode;
	
	mm_initialize_typecode(&outputmatcode);
	mm_set_matrix(&outputmatcode);
	mm_set_coordinate(&outputmatcode);
	//mm_set_dense(&outputmatcode);
	mm_set_real(&outputmatcode);
	mm_set_general(&outputmatcode);
	
	if(strcmp(fileName,"stdout")==0){
		output = stdout;
	}
	else{
		if ((output = fopen(fileName, "w")) == NULL){
			fprintf(stderr,"[%s] Unable to open file for writing\n",__func__);
			return 0;
		}
	}
			
	
	double value = 0.0;
	
	srand (seed);

	mm_write_banner(output, outputmatcode);
	mm_write_mtx_crd_size(output, numRows, numCols, numRows*numCols);
	//ret_code = fprintf(output,"\%\%MatrixMarket matrix coordinate real symmetric\n");
	
	//unsigned long long int val1 = 0;
	//unsigned long long int val2 = 0;
	

	
	for(i = 0;i < numRows; i++){
	
		for(j = 0; j< numCols; j++){

            value = randfrom(min, max);
			fprintf(output, "%lu %lu %lg\n",i+1,j+1,value);

		}

	}
	

	fclose(output);

	return 1;
}
int CreateDenseMatrixGeneral(int argc, char *argv[]) {

	FILE *output;
	unsigned long int numRows, numCols, seed, i, j;
	char * outputpath;
	double value;
	MM_typecode outputmatcode;

	int verbose;
	struct st_option options[] = {
		FLAG_BOOL('v', "verbose", "Show more info when executing", &verbose, NULL),
		PARAM_INT("rows", "Number of rows of the matrix", &numRows, NULL),
		PARAM_INT("cols", "Number of columns of the matrix", &numCols, NULL),
		PARAM_STRING("outputfilename", "Filename to write the matrix into", &outputpath, NULL),
		PARAM_INT("seed", "Seed of the random number generator", &seed, "0"),
		OPTIONS_END,
	};

	if (options_parse(options, argc, argv)) {
		options_usage(options, "./MM-Suite CreateDenseMatrixGeneral");
		puts(option_err_msg);
		return 0;
	};

	if ((output = fopen(outputpath, "w")) == NULL){
		return 0;
	}


	mm_initialize_typecode(&outputmatcode);
	mm_set_matrix(&outputmatcode);
	mm_set_coordinate(&outputmatcode);
	mm_set_real(&outputmatcode);
	mm_set_general(&outputmatcode);

	srand(seed);

	mm_write_banner(output, outputmatcode);
	mm_write_mtx_crd_size(output, numRows, numCols, numRows*numCols);
	
	for (i = 0; i < numRows; i++) {
		for (j = 0; j < numCols; j++) {
			value = ((double) rand() / (double) RAND_MAX) / 100;
			fprintf(output, "%lu %lu %lg\n", i + 1, j + 1, value);
		}
	}
	
	fclose(output);

	return 1;
};
示例#5
0
文件: mmio.cpp 项目: DaniCF93/studia
int mm_read_banner(FILE *f, MM_typecode *matcode)
{
    char line[MM_MAX_LINE_LENGTH];
    char banner[MM_MAX_TOKEN_LENGTH];
    char mtx[MM_MAX_TOKEN_LENGTH]; 
    char crd[MM_MAX_TOKEN_LENGTH];
    char data_type[MM_MAX_TOKEN_LENGTH];
    char storage_scheme[MM_MAX_TOKEN_LENGTH];
    char *p;


    mm_clear_typecode(matcode);  

    if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) 
        return MM_PREMATURE_EOF;

    if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, 
        storage_scheme) != 5)
        return MM_PREMATURE_EOF;

    for (p=mtx; *p!='\0'; *p=tolower(*p),p++);  /* convert to lower case */
    for (p=crd; *p!='\0'; *p=tolower(*p),p++);  
    for (p=data_type; *p!='\0'; *p=tolower(*p),p++);
    for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++);

    /* check for banner */
    if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0)
        return MM_NO_HEADER;

    /* first field should be "mtx" */
    if (strcmp(mtx, MM_MTX_STR) != 0)
        return  MM_UNSUPPORTED_TYPE;
    mm_set_matrix(matcode);


    /* second field describes whether this is a sparse matrix (in coordinate
            storgae) or a dense array */


    if (strcmp(crd, MM_SPARSE_STR) == 0)
        mm_set_sparse(matcode);
    else
    if (strcmp(crd, MM_DENSE_STR) == 0)
            mm_set_dense(matcode);
    else
        return MM_UNSUPPORTED_TYPE;
    

    /* third field */

    if (strcmp(data_type, MM_REAL_STR) == 0)
        mm_set_real(matcode);
    else
    if (strcmp(data_type, MM_COMPLEX_STR) == 0)
        mm_set_complex(matcode);
    else
    if (strcmp(data_type, MM_PATTERN_STR) == 0)
        mm_set_pattern(matcode);
    else
    if (strcmp(data_type, MM_INT_STR) == 0)
        mm_set_integer(matcode);
    else
        return MM_UNSUPPORTED_TYPE;
    

    /* fourth field */

    if (strcmp(storage_scheme, MM_GENERAL_STR) == 0)
        mm_set_general(matcode);
    else
    if (strcmp(storage_scheme, MM_SYMM_STR) == 0)
        mm_set_symmetric(matcode);
    else
    if (strcmp(storage_scheme, MM_HERM_STR) == 0)
        mm_set_hermitian(matcode);
    else
    if (strcmp(storage_scheme, MM_SKEW_STR) == 0)
        mm_set_skew(matcode);
    else
        return MM_UNSUPPORTED_TYPE;
        

    return 0;
}
示例#6
0
void main(int argc, char *argv[])
{
    FILE *in_file;
    char Title[73], Key[9], Rhstype[4];
    char Type[4];
    char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
    int Ptrcrd, Indcrd, Valcrd, Rhscrd;
    int Indperline, Indwidth;
    int Valperline, Valwidth, Valprec;
    int Valflag;           /* Indicates 'E','D', or 'F' float format */
    int Nrow, Ncol, Nnzero;
    int Nrhs;
    char* ThisElement;
    char format[30];
    char rformat[30];
    int *colptr, *rowind;
    int *colcount;
    int i,j, repeat, count, col, ind, items, last;
    char line[BUFSIZ];
    MM_typecode matcode;

    if (argc != 2) {
      printf("Usage: %s HBfile\n\n", argv[0]);
      printf("   Sends Matrix Market formatted output to stdout\n");
      exit(-1);
    } 

    in_file = fopen( argv[1], "r");
    if (in_file == NULL)
    {
       fprintf(stderr,"Error: Cannot open file: %s\n",argv[1]);
       exit(1);
    }

    readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
                  Ptrfmt, Indfmt, Valfmt, Rhsfmt,
                  &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
    fclose(in_file);

    if (Type[0] == 'P' ) {
      fprintf(stderr,"This is a streaming translator for LARGE files with ");
      fprintf(stderr,"REAL or COMPLEX data.  Use 'hbmat2mtx' for PATTERN matrices.\n");
      exit(1);
    }
    in_file = readHB_newind(argv[1], &Nrow, &Ncol, &Nnzero, &colptr, &rowind);
    ParseIfmt(Indfmt,&Indperline,&Indwidth);
    sprintf(format,"%%%dd %%%dd ",Indwidth,Indwidth);
    ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
    sprintf(rformat,"%%%ds ",Valwidth);
    ThisElement = (char *) malloc(Valwidth+1);


/*  Skip to values in hb file: */

   mm_set_matrix(&matcode);
   mm_set_coordinate(&matcode);
   if ( Type[0] == 'R' )
    mm_set_real(&matcode);
   else if ( Type[0] == 'C' )
    mm_set_complex(&matcode);
   else if ( Type[0] == 'P' )
    mm_set_pattern(&matcode);
   else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",Type);
    exit(1);
   }
   if ( Type[1] == 'U' || Type[1] == 'R' )
    mm_set_general(&matcode);
   else if ( Type[1] == 'S' )
    mm_set_symmetric(&matcode);
   else if ( Type[1] == 'Z' )
    mm_set_skew(&matcode);
   else if ( Type[1] == 'H' )
    mm_set_hermitian(&matcode);
   else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",&Type[1]);
    exit(1);
   }
   if ( Type[2] != 'A' ){
    fprintf(stderr,"Unrecognized format in HB Type: %1s",&Type[2]);
    exit(1);
   }
    mm_write_banner(stdout, matcode);
    fprintf(stdout,"%% RBTitle: %s\n",Title);
    fprintf(stdout,"%% RBKey:   %s\n",Key);
    mm_write_mtx_crd_size(stdout, Nrow, Ncol, Nnzero);

    if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.\nhb2mtxstrm.c: Line 117 approx.");
    repeat=0;
    if ( Type[0] == 'C') repeat=1;
    count = 0;
    colcount = &colptr[1];
    items = 0; 
    for (i=0;i<Valcrd;i++)
    {
       fgets(line, BUFSIZ, in_file);
       if ( sscanf(line,"%*s") < 0 ) {
         fprintf(stderr,"iohb.c: Null (or blank) line in value data region of HB file.\n");
         exit(1);
       }
       if (Valflag == 'D') {
          while( strchr(line,'D') ) *strchr(line,'D') = 'E';
       }
       col =  0;
       for (ind = 0;ind<Valperline;ind++)
       {
          if (count == Nnzero) break;
          if (count == *colcount-1) colcount++;
          strncpy(ThisElement,line+col,Valwidth);
          *(ThisElement+Valwidth) = (char) NULL;
          if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) {
             /* insert a char prefix for exp */
             last = strlen(ThisElement);
             for (j=last+1;j>=0;j--) {
                ThisElement[j] = ThisElement[j-1];
                if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
                   ThisElement[j-1] = Valflag;
                   break;
                }
             }
          }
         items++;
         if (items==1) fprintf(stdout,format,rowind[count],colcount-colptr);
         if (items > repeat) { 
            fprintf(stdout,rformat,ThisElement);
            fprintf(stdout,"\n");
            count++; 
            items=0;
         } else {
            fprintf(stdout,rformat,ThisElement);
         }
         col += Valwidth;
       }
    }
       
}
示例#7
0
文件: hbmat2mtx.c 项目: nschloe/hbio
int main(int argc, char *argv[])
{
  FILE *in_file;
  char Title[73], Key[9], Rhstype[4];
  char Type[4];
  char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  int Ptrcrd, Indcrd, Valcrd;
  int Rhscrd = 0;
  int Indperline, Indwidth;
  int Valperline, Valwidth, Valprec;
  int Valflag;           /* Indicates 'E','D', or 'F' float format */
  int Nrow, Ncol, Nnzero;
  int Nrhs;
  char *valc = NULL;
  char *Valfmtc;
  char *tmp1;
  char *tmp2;
  char format[30];
  char rformat[30];
  char cformat[30];
  int *colptr, *rowind;
  int i,j, indxval, rowp1, colp1;
  MM_typecode matcode;

  if (argc != 2) {
    printf("Usage: %s HBfile \n", argv[0]);
    exit(-1);
  }

  in_file = fopen( argv[1], "r");
  if (in_file == NULL)
  {
    fprintf(stderr,"Error: Cannot open file: %s\n",argv[1]);
    exit(1);
  }

  readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
      Ptrfmt, Indfmt, Valfmt, Rhsfmt,
      &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  fclose(in_file);

  readHB_newmat_char(argv[1], &Nrow, &Ncol, &Nnzero, &colptr, &rowind, &valc,
      &Valfmtc);
  ParseIfmt(Indfmt,&Indperline,&Indwidth);
  ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  sprintf(format,"%%%dd %%%dd \n",Indwidth,Indwidth);
  sprintf(rformat,"%%%dd %%%dd %%%ds\n",Indwidth,Indwidth,Valwidth);
  sprintf(cformat,"%%%dd %%%dd %%%ds %%%ds\n",Indwidth,Indwidth,Valwidth,Valwidth);


  mm_set_matrix(&matcode);
  mm_set_coordinate(&matcode);
  if ( Type[0] == 'R' )
    mm_set_real(&matcode);
  else if ( Type[0] == 'C' )
    mm_set_complex(&matcode);
  else if ( Type[0] == 'P' )
    mm_set_pattern(&matcode);
  else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",Type);
    exit(1);
  }
  if ( Type[1] == 'U' || Type[1] == 'R' )
    mm_set_general(&matcode);
  else if ( Type[1] == 'S' )
    mm_set_symmetric(&matcode);
  else if ( Type[1] == 'Z' )
    mm_set_skew(&matcode);
  else if ( Type[1] == 'H' )
    mm_set_hermitian(&matcode);
  else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",&Type[1]);
    exit(1);
  }
  if ( Type[2] != 'A' ){
    fprintf(stderr,"Unrecognized format in HB Type: %1s",&Type[2]);
    exit(1);
  }
  mm_write_banner(stdout, matcode);
  fprintf(stdout,"%% RBTitle: %s\n",Title);
  fprintf(stdout,"%% RBKey:   %s\n",Key);
  mm_write_mtx_crd_size(stdout, Nrow, Ncol, Nnzero);

  if ( Type[0] == 'C' ) {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        indxval = 2*(i-1);
        rowp1 = rowind[i-1]+1-1;
        colp1 = j + 1;
        tmp1 = substr(valc,indxval*Valwidth,Valwidth);
        tmp2 = substr(valc,(indxval+1)*Valwidth,Valwidth);
        fprintf(stdout,cformat,rowp1,colp1,tmp1,tmp2);
      }
  } else if ( Type[0] == 'R' ) {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        rowp1 = rowind[i-1];
        colp1 = j + 1;
        tmp1 = substr(valc,(i-1)*Valwidth,Valwidth);
        fprintf(stdout,rformat,rowp1,colp1,tmp1);
      }
  } else {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        rowp1 = rowind[i-1];
        colp1 = j + 1;
        fprintf(stdout,format,rowp1,colp1);
      }
  }
  return 0;
}
示例#8
0
// MANDATORY
int mm_read_banner ( FILE *f, MM_typecode *matcode )
/******************************************************************************/
/*
  Purpose:
    MM_READ_BANNER reads the header line of an MM file.
  Modified:
    31 October 2008
    15 Oct 2012 to replace int with long unsigned int 
  Parameters:
    Input, FILE *F, a pointer to the input file.
    Output, MM_typecode *MATCODE, the header information.
*/
{
    char line[MM_MAX_LINE_LENGTH];
    char banner[MM_MAX_TOKEN_LENGTH];
    char mtx[MM_MAX_TOKEN_LENGTH]; 
    char crd[MM_MAX_TOKEN_LENGTH];
    char data_type[MM_MAX_TOKEN_LENGTH];
    char storage_scheme[MM_MAX_TOKEN_LENGTH];
    char *p;
    mm_clear_typecode(matcode);  
    if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) 
        return MM_PREMATURE_EOF;
    // banner: MatrixMarket
    // mtx: matrix or vector 
    // format: coordinate or dense array 
    // field : real or int of value 
    // symmetry: general, symmetric, skew-symmetric ... 
    if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, 
        storage_scheme) != 5)
        return MM_PREMATURE_EOF;
    for (p=mtx; *p!='\0'; *p=tolower(*p),p++);  /* convert to lower case */
    for (p=crd; *p!='\0'; *p=tolower(*p),p++);  
    for (p=data_type; *p!='\0'; *p=tolower(*p),p++);
    for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++);
/* 
  check for banner 
*/
    if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0)
        return MM_NO_HEADER;
/* 
  first field should be "mtx" 
*/
    if (strcmp(mtx, MM_MTX_STR) != 0)
        return  MM_UNSUPPORTED_TYPE;
    mm_set_matrix(matcode);
/* 
  second field describes whether this is a sparse matrix (in coordinate
  storgae) or a dense array 
*/
    if (strcmp(crd, MM_SPARSE_STR) == 0)
        mm_set_sparse(matcode);
    else
    if (strcmp(crd, MM_DENSE_STR) == 0)
            mm_set_dense(matcode);
    else
        return MM_UNSUPPORTED_TYPE;   
/*
  third field 
*/
    if (strcmp(data_type, MM_REAL_STR) == 0)
        mm_set_real(matcode);
    else
    if (strcmp(data_type, MM_COMPLEX_STR) == 0)
        mm_set_complex(matcode);
    else
    if (strcmp(data_type, MM_PATTERN_STR) == 0)
        mm_set_pattern(matcode);
    else
    if (strcmp(data_type, MM_INT_STR) == 0)
        mm_set_integer(matcode);
    else
        return MM_UNSUPPORTED_TYPE;    
/*
  fourth field 
*/
    if (strcmp(storage_scheme, MM_GENERAL_STR) == 0)
        mm_set_general(matcode);
    else
    if (strcmp(storage_scheme, MM_SYMM_STR) == 0)
        mm_set_symmetric(matcode);
    else
    if (strcmp(storage_scheme, MM_HERM_STR) == 0)
        mm_set_hermitian(matcode);
    else
    if (strcmp(storage_scheme, MM_SKEW_STR) == 0)
        mm_set_skew(matcode);
    else
        return MM_UNSUPPORTED_TYPE;        
    return 0;
}