首页 新闻 会员 周边

下列代码是如何读入一个稀疏矩阵的,

0
悬赏园豆:10 [已解决问题] 解决于 2011-11-25 08:39

int *readMatrix(char *fname, sparseStruct *mat, int &numEmptyCol, int formatType, char *scalingType)
// read in a sparse matrix from files into 'mat' and return #empty vectors and an array of empty vector IDs
{
  char filename[FILENAME_LENGTH];
  //clock_t start_clock, finish_clock;
  sprintf(filename, "%s%s", fname, "_dim");
 
  std::ifstream dimFile(filename);
  if (dimFile == 0)
    cout << "  !!! File open error: " << filename << " !!!" << endl << endl;
  sprintf(filename, "%s%s", fname, "_row_ccs");
  std::ifstream rowPtrFile(filename);
  if (rowPtrFile == 0)
    cout << "  !!! File open error: " << filename << " !!!" << endl << endl;
  sprintf(filename, "%s%s", fname, "_col_ccs");
  std::ifstream colIdxFile(filename);
  if (colIdxFile == 0)
    cout << "  !!! File open error: " << filename << " !!!" << endl << endl;
  sprintf(filename, "%s%s", fname, "_");
  if (formatType == 0)
    sprintf(filename, "%s%s", filename, TXX_SCALING);
  else
    sprintf(filename, "%s%s", filename, TFN_SCALING); 
  sprintf(filename, "%s%s", filename, "_nz");
  std::ifstream valFile(filename);
  if (valFile == 0)
      cout << "  !!! File open error: " << filename << " !!!" << endl;
  if (dimFile == 0 || rowPtrFile == 0 || colIdxFile == 0 || valFile == 0){
    cout << "  !!! Matrix file " << fname << "_* has missing file(s) !!!" << endl;
    exit(EXIT_FAILURE);
  }
  //data.width(MAX_DESC_STR_LENGTH);
  //data >> mat->descString;
  //data >> mat->numCol >> mat->numRow >> mat->numVal;
  //start_clock = clock();
  cout << "  Reading dimension file ... " << endl;
  dimFile >> mat->numRow >> mat->numCol >> mat->numVal;
  dimFile.close();
  mat->colPtr = new int[mat->numCol+1];
  mat->rowIdx = new int[mat->numVal];
  mat->value = new double[mat->numVal];
  //space used for storing the sparse matrix is as follows
  memoryUsed += (mat->numCol + 1 + mat->numVal) * sizeof(int) + mat->numVal * sizeof(double);
  //it is necessary to handle empty vectors separately
  int pre = -1;
  int *tempEmptyColId = new int[mat->numCol], *emptyColId;
  numEmptyCol = 0;
  cout << "  Reading column pointer file ... " << endl;
  for (int i = 0; i < mat->numCol+1; i++){
    colIdxFile >> (mat->colPtr)[i];
    if ((mat->colPtr)[i] == pre){
      tempEmptyColId[numEmptyCol] = i - 1;
      numEmptyCol++;
    }
    pre = (mat->colPtr)[i];
  }
  colIdxFile.close();
  emptyColId = new int[numEmptyCol+1];
  for(int i = 0; i < numEmptyCol; i++)
    emptyColId[i] = tempEmptyColId[i];
  emptyColId[numEmptyCol] = mat->numCol;
  delete [] tempEmptyColId;
  cout << "  Reading row index file ... " << endl;
  for (int i = 0; i < mat->numVal; i++)
    rowPtrFile >> (mat->rowIdx)[i];
  rowPtrFile.close();
  cout << "  Reading non-zero value file ... " << endl;
  for (int i = 0; i < mat->numVal; i++)
    valFile >> (mat->value)[i];
  valFile.close();
  cout << endl;
  //finish_clock = clock();
  //cout << "Reading file time: " << (finish_clock - start_clock) / 1e6 << " seconds." << endl;
  if (numEmptyCol == 0)
    cout << "  !!! No empty col found !!!" << endl;
  else {
    cout << "  !!! " << numEmptyCol << " empty col(s) found !!!" << endl;
    for(int i = 0; i < numEmptyCol; i++)
      cout << emptyColId[i] << " ";
    cout << endl;
  }
  return  emptyColId;
}

mat是一个结构变量,存储file中的稀疏矩阵,在Reading column pointer file 时遇到一些问题,比如一个稀疏矩阵

0  2  4  0  1

3  2  0  0  0

0  0  4  0  0

2  3  0  0  0

column pointer file的内容应该是什么?

5
笑翱江湖的主页 笑翱江湖 | 初学一级 | 园豆:126
提问于:2011-11-22 10:17
< >
分享
最佳答案
0

童靴,请把你要解决的问题描述清楚点。

你贴出的代码结构也不理想,叫人怎么读你的代码呢:)

比如函数说明吧

int *readMatrix(char *fname, sparseStruct *mat, int &numEmptyCol, int formatType, char *scalingType)

好歹要写点注释吧,哪些是输出信息,哪些是输入信息,返回值是什么,

sparseStruct 定义呢?

另外,初略估计了一下单这个函数大概有80到90行代码,函数体大了点,且函数体中缺少必要的注释和分段,建议关键的环节分段并添加必要的注释。


大家都很乐意帮你解答你的问题,但是可能有些大牛看到你的问题就退却了;)

建议重新组织一下你的问题,也许在组织问题的过程中你自己就把这个问题给解决了,这是很有可能的。

收获园豆:10
胡屯 | 小虾三级 |园豆:714 | 2011-11-22 11:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册