char *mm_typecode_to_str(MM_typecode matcode) { char buffer[MM_MAX_LINE_LENGTH]; char *types[4]; int error =0; int i; /* check for MTX type */ if (mm_is_matrix(matcode)) types[0] = MM_MTX_STR; else error=1; /* check for CRD or ARR matrix */ if (mm_is_sparserow(matcode)) types[1] = MM_SPARSEROW_STR; else if (mm_is_coordinate(matcode)) types[1] = MM_COORDINATE_STR; else if (mm_is_dense(matcode)) types[1] = MM_DENSE_STR; else return NULL; /* check for element data type */ if (mm_is_real(matcode)) types[2] = MM_REAL_STR; else if (mm_is_complex(matcode)) types[2] = MM_COMPLEX_STR; else if (mm_is_pattern(matcode)) types[2] = MM_PATTERN_STR; else if (mm_is_integer(matcode)) types[2] = MM_INT_STR; else return NULL; /* check for symmetry type */ if (mm_is_general(matcode)) types[3] = MM_GENERAL_STR; else if (mm_is_symmetric(matcode)) types[3] = MM_SYMM_STR; else if (mm_is_hermitian(matcode)) types[3] = MM_HERM_STR; else if (mm_is_skew(matcode)) types[3] = MM_SKEW_STR; else return NULL; sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]); return strdup(buffer); }
int mm_is_valid ( MM_typecode matcode ) /******************************************************************************/ /* Purpose: MM_IS_VALID checks whether the MM header information is valid. Modified: 31 October 2008 Parameters: Input, MM_typecode MATCODE, the header information. Output, int MM_IS_VALID, is TRUE if the matrix code is valid. */ { if ( !mm_is_matrix ( matcode ) ) { return 0; } if ( mm_is_dense ( matcode ) && mm_is_pattern ( matcode ) ) { return 0; } if ( mm_is_real ( matcode ) && mm_is_hermitian ( matcode ) ) { return 0; } if ( mm_is_pattern ( matcode ) && ( mm_is_hermitian ( matcode ) || mm_is_skew ( matcode ) ) ) { return 0; } return 1; }
int mm_is_valid(MM_typecode matcode) { if (!mm_is_matrix(matcode)) return 0; if (mm_is_dense(matcode) && mm_is_pattern(matcode)) return 0; if (mm_is_real(matcode) && mm_is_hermitian(matcode)) return 0; if (mm_is_pattern(matcode) && (mm_is_hermitian(matcode) || mm_is_skew(matcode))) return 0; return 1; }
void mm_typecode_to_str(MM_typecode matcode, char * buffer) { char type0[20]; char type1[20]; char type2[20]; char type3[20]; int error =0; /* check for MTX type */ if (mm_is_matrix(matcode)) strcpy(type0, MM_MTX_STR); else error=1; /* check for CRD or ARR matrix */ if (mm_is_sparse(matcode)) strcpy(type1, MM_SPARSE_STR); else if (mm_is_dense(matcode)) strcpy(type1, MM_DENSE_STR); else return; /* check for element data type */ if (mm_is_real(matcode)) strcpy(type2, MM_REAL_STR); else if (mm_is_complex(matcode)) strcpy(type2, MM_COMPLEX_STR); else if (mm_is_pattern(matcode)) strcpy(type2, MM_PATTERN_STR); else if (mm_is_integer(matcode)) strcpy(type2, MM_INT_STR); else return; /* check for symmetry type */ if (mm_is_general(matcode)) strcpy(type3, MM_GENERAL_STR); else if (mm_is_symmetric(matcode)) strcpy(type3, MM_SYMM_STR); else if (mm_is_hermitian(matcode)) strcpy(type3, MM_HERM_STR); else if (mm_is_skew(matcode)) strcpy(type3, MM_SKEW_STR); else return; sprintf(buffer,"%s %s %s %s", type0, type1, type2, type3); return; }
int DenseMatrix_mm_read_strassen(DenseMatrix *m, char *file_name, int *nr_rows, int *nr_cols) { int res; MM_typecode matcode; FILE *file; file = fopen(file_name, "r"); CHECK_NULL_RETURN(file); res = mm_read_banner(file, &matcode); CHECK_ZERO_ERROR_RETURN(res, "Failed to read matrix code"); CHECK_ERROR_RETURN(!mm_is_matrix(matcode), "File is not a matrix", 1); if (mm_is_sparse(matcode)) { int nr_sparse_elements; res = mm_read_mtx_crd_size(file, nr_rows, nr_cols, &nr_sparse_elements); CHECK_ZERO_ERROR_RETURN(res, "Failed to read sparse mm dimensions"); int dim = pow2dim(*nr_rows, *nr_cols); // Initialzie matrix to zero to fill in with sparse elements res = DenseMatrix_init_zero(m, dim, dim); CHECK_ZERO_ERROR_RETURN(res, "Failed to allocate memory for mm matrix"); res = DenseMatrix_parse_mm_sparse(m, file, nr_sparse_elements); } else if (mm_is_dense(matcode)) { res = mm_read_mtx_array_size(file, nr_rows, nr_cols); CHECK_ZERO_ERROR_RETURN(res, "Failed to read dense mm dimensions"); int dim = pow2dim(*nr_rows, *nr_cols); res = DenseMatrix_init_zero(m, dim, dim); CHECK_ZERO_ERROR_RETURN(res, "Failed to allocate memory for mm matrix"); res = DenseMatrix_parse_mm_dense(m, file); } else { ERROR("mm matrix code is not supported. Only supports dense and sparse matrices"); } CHECK_ZERO_ERROR_RETURN(res, "Failed to parse mm file"); return 0; }
static void read_mtx_and_return_csr( int argc, char **argv, int *mm, int *nn, int **ia, int **ja, double **aa) { int ret_code; MM_typecode matcode; FILE *f; int m, n, nz; int i, *I, *J; double *val; if (argc < 2) { fprintf(stderr, "Usage: %s [martix-market-filename]\n", argv[0]); exit(1); } else { printf("\n===================================\n"); printf("mtx file = %s\n", argv[1]); if ((f = fopen(argv[1], "r")) == NULL) { printf("Could not open %s\n", argv[1]); exit(1); } } if (mm_read_banner(f, &matcode) != 0) { printf("Could not process Matrix Market banner.\n"); exit(1); } /* This is how one can screen matrix types if their application */ /* only supports a subset of the Matrix Market data types. */ if ( mm_is_pattern(matcode) || mm_is_dense(matcode) || mm_is_array(matcode) ) { printf("Sorry, this application does not support "); printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode)); exit(1); } if (mm_is_complex(matcode) && mm_is_matrix(matcode) && mm_is_sparse(matcode) ) { printf("Sorry, this application does not support "); printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode)); exit(1); } /* find out size of sparse matrix .... */ if ((ret_code = mm_read_mtx_crd_size(f, &m, &n, &nz)) !=0) exit(1); /* reseve memory for matrices */ I = (int *) malloc(nz * sizeof(int)); J = (int *) malloc(nz * sizeof(int)); val = (double *) malloc(nz * sizeof(double)); /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */ /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */ /* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */ for (i=0; i<nz; i++) { fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]); I[i]--; /* adjust from 1-based to 0-based */ J[i]--; } if (f !=stdin) fclose(f); if (m != n) exit(1); *mm = m; *nn = n; *ia = (int*) malloc (sizeof(int) * (n+1)); *ja = (int*) malloc (sizeof(int) * nz); *aa = (double*) malloc (sizeof(double) * nz); coo2csr(n, nz, val, I, J, *aa, *ja, *ia); free (I); free (J); free (val); }
char *mm_typecode_to_str(MM_typecode matcode) { char buffer[MM_MAX_LINE_LENGTH]; const char *types[4]; char *mm_strdup(const char *); int error =0; /* check for MTX type */ if (mm_is_matrix(matcode)) types[0] = MM_MTX_STR; else { types[0] = NULL; error=1; } /* check for CRD or ARR matrix */ if (mm_is_sparse(matcode)) types[1] = MM_SPARSE_STR; else if (mm_is_dense(matcode)) types[1] = MM_DENSE_STR; else return NULL; /* check for element data type */ if (mm_is_real(matcode)) types[2] = MM_REAL_STR; else if (mm_is_complex(matcode)) types[2] = MM_COMPLEX_STR; else if (mm_is_pattern(matcode)) types[2] = MM_PATTERN_STR; else if (mm_is_integer(matcode)) types[2] = MM_INT_STR; else return NULL; /* check for symmetry type */ if (mm_is_general(matcode)) types[3] = MM_GENERAL_STR; else if (mm_is_symmetric(matcode)) types[3] = MM_SYMM_STR; else if (mm_is_hermitian(matcode)) types[3] = MM_HERM_STR; else if (mm_is_skew(matcode)) types[3] = MM_SKEW_STR; else return NULL; if( error == 1 ) sprintf(buffer,"Object to write is not a matrix; this is unsupported by the current mmio code."); else sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]); return mm_strdup(buffer); }
char *mm_typecode_to_str ( MM_typecode matcode ) /******************************************************************************/ /* Purpose: MM_TYPECODE_TO_STR converts the internal typecode to an MM header string. Modified: 31 October 2008 */ { char buffer[MM_MAX_LINE_LENGTH]; char *types[4]; char *mm_strdup(const char *); //int error =0; /* check for MTX type */ if (mm_is_matrix(matcode)) types[0] = MM_MTX_STR; // else // error=1; /* check for CRD or ARR matrix */ if (mm_is_sparse(matcode)) types[1] = MM_SPARSE_STR; else if (mm_is_dense(matcode)) types[1] = MM_DENSE_STR; else return NULL; /* check for element data type */ if (mm_is_real(matcode)) types[2] = MM_REAL_STR; else if (mm_is_complex(matcode)) types[2] = MM_COMPLEX_STR; else if (mm_is_pattern(matcode)) types[2] = MM_PATTERN_STR; else if (mm_is_integer(matcode)) types[2] = MM_INT_STR; else return NULL; /* check for symmetry type */ if (mm_is_general(matcode)) types[3] = MM_GENERAL_STR; else if (mm_is_symmetric(matcode)) types[3] = MM_SYMM_STR; else if (mm_is_hermitian(matcode)) types[3] = MM_HERM_STR; else if (mm_is_skew(matcode)) types[3] = MM_SKEW_STR; else return NULL; sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]); return mm_strdup(buffer); }