⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparsematrix.cc

📁 一种聚类算法,名字是cocluster
💻 CC
📖 第 1 页 / 共 3 页
字号:
double SparseMatrix::computeColDistance(int colId, int colCluster, int *rowCL, double **cM, double colQuality4Compressed){  double temp = 0;  for (int k = colPtr[colId]; k < colPtr[colId+1]; k++)    temp += value[k] * cM[rowCL[rowIdx[k]]][colCluster];  return (-2 * temp + colQuality4Compressed);}double SparseMatrix::computeRowDistance(int rowId, int rowCluster, int *colCL, double **cM, double rowQuality4Compressed, bool *isReversed){  double temp = 0;  if (isReversed[rowId])    for (int k = colPtr[rowId]; k < colPtr[rowId+1]; k++)      temp += (0 - value[k]) * cM[rowCluster][colCL[rowIdx[k]]];  else    for (int k = colPtr[rowId]; k < colPtr[rowId+1]; k++)      temp += value[k] * cM[rowCluster][colCL[rowIdx[k]]];  return (-2 * temp + rowQuality4Compressed);}double SparseMatrix::computeColDistance(int colId, int colCluster, int *rowCL, double **cM, double colQuality4Compressed, bool *isReversed){  double temp = 0;  for (int k = colPtr[colId]; k < colPtr[colId+1]; k++){    int rowId = rowIdx[k];    if (isReversed[rowId])      temp += (0 - value[k]) * cM[rowCL[rowId]][colCluster];    else      temp += value[k] * cM[rowCL[rowId]][colCluster];  }  return (-2 * temp + colQuality4Compressed);}// MSSRIICCvoid SparseMatrix::computeRowCentroid(int numRowCluster, int *rowCL, double **rowCentroid){  for (int rc = 0; rc < numRowCluster; rc++)    for (int c = 0; c < numRow; c++)		// numRow (in CRS) equals to numCol (in CCS).      rowCentroid[rc][c] = 0;  for (int r = 0; r < numCol; r++)		// numCol (in CRS) equals to numRow (in CCS).    for (int k = colPtr[r]; k < colPtr[r+1]; k++)      rowCentroid[rowCL[r]][rowIdx[k]] += value[k];}void SparseMatrix::computeRowCentroid(int numRowCluster, int *rowCL, double **rowCentroid, bool *isReversed){  for (int rc = 0; rc < numRowCluster; rc++)    for (int c = 0; c < numRow; c++)		// numRow (in CRS) equals to numCol (in CCS).      rowCentroid[rc][c] = 0;  for (int r = 0; r < numCol; r++)		// numCol (in CRS) equals to numRow (in CCS).    if (isReversed[r])      for (int k = colPtr[r]; k < colPtr[r+1]; k++)        rowCentroid[rowCL[r]][rowIdx[k]] -= value[k];    else      for (int k = colPtr[r]; k < colPtr[r+1]; k++)        rowCentroid[rowCL[r]][rowIdx[k]] += value[k];}void SparseMatrix::computeColCentroid(int numColCluster, int *colCL, double **colCentroid){  for (int cc = 0; cc < numColCluster; cc++)    for (int r = 0; r < numRow; r++)      colCentroid[cc][r] = 0;  for (int c = 0; c < numCol; c++)    for (int k = colPtr[c]; k < colPtr[c+1]; k++)      colCentroid[colCL[c]][rowIdx[k]] += value[k];}void SparseMatrix::computeColCentroid(int numColCluster, int *colCL, double **colCentroid, bool *isReversed){  for (int cc = 0; cc < numColCluster; cc++)    for (int r = 0; r < numRow; r++)      colCentroid[cc][r] = 0;  for (int c = 0; c < numCol; c++)    for (int k = colPtr[c]; k < colPtr[c+1]; k++){      int tempRowId = rowIdx[k];      if (isReversed[tempRowId])        colCentroid[colCL[c]][tempRowId] -= value[k];       else        colCentroid[colCL[c]][tempRowId] += value[k];    }}/*double SparseMatrix::computeRowDistance(int rowId, int *colCL, double *, double *mAR, double rowQuality4Compressed) {  double temp = 0;  for (int j = colPtr[rowId]; j < colPtr[rowId+1]; j++){    int tempColId = rowIdx[j];    temp += (value[j] - colCentroid[colCL[tempColId]]) * mAR[tempColId];  }  return (-2 * temp + rowQuality4Compressed);}double SparseMatrix::computeColDistance(int colId, int *rowCL, double *rowCentroid, double *nAC, double colQuality4Compressed){  double temp = 0;  for (int j = colPtr[colId]; j < colPtr[colId+1]; j++){    int tempRowId = rowIdx[j];    temp += (value[j] - rowCentroid[tempRowId]) * nAC[tempRowId];  }  return (-2 * temp + colQuality4Compressed);}*/double SparseMatrix::computeRowDistance(int rowId, int rowCluster, int *rowCL, int *colCL, double **cM){  double temp = 0;  double *tempDistance = new double[numRow];		// numRow (in CRS) equals to numCol (in CCS).  for (int c = 0; c < numRow; c++)    tempDistance[c] = 0;  for (int k = colPtr[rowId]; k < colPtr[rowId+1]; k++)    tempDistance[rowIdx[k]] = value[k];  for (int c = 0; c < numRow; c++){    double tempValue = tempDistance[c] - cM[rowCluster][colCL[c]];    temp += tempValue * tempValue;  }    delete [] tempDistance;  return temp;}double SparseMatrix::computeColDistance(int colId, int colCluster, int *rowCL, int *colCL, double **cM){  double temp = 0;  double *tempDistance = new double[numRow];  for (int r = 0; r < numRow; r++)    tempDistance[r] = 0;  for (int k = colPtr[colId]; k < colPtr[colId+1]; k++)    tempDistance[rowIdx[k]] = value[k];  for (int r = 0; r < numRow; r++){    double tempValue = tempDistance[r] - cM[rowCL[r]][colCluster];    temp += tempValue * tempValue;  }  delete [] tempDistance;  return temp;}void SparseMatrix::computeRowAP(int rowId, double **colCentroid, int *colCL, double *rowAP){  for (int c = 0; c < numRow; c++)		// numRow (in CRS) equals to numCol (in CCS).    rowAP[c] = 0;  for (int k = colPtr[rowId]; k < colPtr[rowId+1]; k++)    rowAP[rowIdx[k]] = value[k];  for (int c = 0; c < numRow; c++)		// numRow (in CRS) equals to numCol (in CCS).    rowAP[c] -= colCentroid[colCL[c]][rowId];}void SparseMatrix::computeRowAP(int rowId, double **colCentroid, int *colCL, double *rowAP, bool *isReversed){  for (int c = 0; c < numRow; c++)		// numRow (in CRS) equals to numCol (in CCS).    rowAP[c] = 0;  if (isReversed[rowId])    for (int k = colPtr[rowId]; k < colPtr[rowId+1]; k++)      rowAP[rowIdx[k]] -= value[k];  else    for (int k = colPtr[rowId]; k < colPtr[rowId+1]; k++)      rowAP[rowIdx[k]] = value[k];  for (int c = 0; c < numRow; c++)		// numRow (in CRS) equals to numCol (in CCS).    rowAP[c] -= colCentroid[colCL[c]][rowId];}void SparseMatrix::computeColAP(int colId, double **rowCentroid, int *rowCL, double *colAP){  for (int r = 0; r < numRow; r++)    colAP[r] = 0;  for (int k = colPtr[colId]; k < colPtr[colId+1]; k++)    colAP[rowIdx[k]] = value[k];  for (int r = 0; r < numRow; r++)    colAP[r] -= rowCentroid[rowCL[r]][colId];}void SparseMatrix::computeColAP(int colId, double **rowCentroid, int *rowCL, double *colAP, bool *isReversed){  for (int r = 0; r < numRow; r++)    colAP[r] = 0;  for (int k = colPtr[colId]; k < colPtr[colId+1]; k++){    int rowId = rowIdx[k];    if (isReversed[rowId])      colAP[rowId] -= value[k];    else      colAP[rowId] = value[k];  }  for (int r = 0; r < numRow; r++)    colAP[r] -= rowCentroid[rowCL[r]][colId];}void SparseMatrix::subtractRow(double *x, int r){  for (int k = colPtr[r]; k < colPtr[r+1]; k++)    x[rowIdx[k]] -= value[k];}void SparseMatrix::subtractCol(double *x, int c){  for (int k = colPtr[c]; k < colPtr[c+1]; k++)    x[rowIdx[k]] -= value[k];}void SparseMatrix::addRow(double *x, int r){  for (int k = colPtr[r]; k < colPtr[r+1]; k++)    x[rowIdx[k]] += value[k];}void SparseMatrix::addCol(double *x, int c){  for (int k = colPtr[c]; k < colPtr[c+1]; k++)    x[rowIdx[k]] += value[k];}// for reversed rowsvoid SparseMatrix::subtractRow(double *x, int r, bool *isReversed){  if (isReversed[r])    for (int k = colPtr[r]; k < colPtr[r+1]; k++)      x[rowIdx[k]] += value[k];   else    for (int k = colPtr[r]; k < colPtr[r+1]; k++)      x[rowIdx[k]] -= value[k]; }void SparseMatrix::subtractCol(double *x, int c, bool *isReversed){  for (int k = colPtr[c]; k < colPtr[c+1]; k++){    int rowId = rowIdx[k];    if (isReversed[rowId])      x[rowId] += value[k];    else      x[rowId] -= value[k];  }  }void SparseMatrix::addRow(double *x, int r, bool *isReversed){  if (isReversed[r])    for (int k = colPtr[r]; k < colPtr[r+1]; k++)      x[rowIdx[k]] -= value[k];  else    for (int k = colPtr[r]; k < colPtr[r+1]; k++)      x[rowIdx[k]] += value[k];}void SparseMatrix::addCol(double *x, int c, bool *isReversed){  for (int k = colPtr[c]; k < colPtr[c+1]; k++){    int rowId = rowIdx[k];    if (isReversed[rowId])      x[rowId] -= value[k];    else      x[rowId] += value[k];  }}void SparseMatrix::subtractCol(double **x, int col, int i, int *rowCL, bool *isReversed){  for (int j = colPtr[i]; j < colPtr[i+1]; j++){    int rowId = rowIdx[j];    if (isReversed[rowId])      x[rowCL[rowId]][col] += value[j];    else      x[rowCL[rowId]][col] -= value[j];  }}void SparseMatrix::subtractCol(double *x, int i, int *rowCL, bool *isReversed){  for (int j = colPtr[i]; j < colPtr[i+1]; j++){    int rowId = rowIdx[j];    if (isReversed[rowId])      x[rowCL[rowId]] += value[j];    else      x[rowCL[rowId]] -= value[j];  }}void SparseMatrix::addCol(double **x, int col, int i, int *rowCL, bool *isReversed){  for (int j = colPtr[i]; j < colPtr[i+1]; j++){    int rowId = rowIdx[j];    if (isReversed[rowId])      x[rowCL[rowId]][col] -= value[j];    else      x[rowCL[rowId]][col] += value[j];  }}void SparseMatrix::addCol(double *x, int i, int *rowCL, bool *isReversed){  for (int j = colPtr[i]; j < colPtr[i+1]; j++){    int rowId = rowIdx[j];    if (isReversed[rowId])      x[rowCL[rowId]] -= value[j];    else      x[rowCL[rowId]] += value[j];  }}double SparseMatrix::computeObjectiveFunctionValue(int *rowCL, int *colCL, double **cM){  double temp = 0;  double *tempDifference = new double[numRow];  for (int c = 0; c < numCol; c++){    for (int r = 0; r < numRow; r++)      tempDifference[r] = 0;    for (int k = colPtr[c]; k < colPtr[c+1]; k++)      tempDifference[rowIdx[k]] = value[k];    for (int r = 0; r < numRow; r++){      double tempValue = tempDifference[r] - cM[rowCL[r]][colCL[c]];      temp += tempValue * tempValue;    }  }  delete [] tempDifference;  return temp;}  double SparseMatrix::computeObjectiveFunctionValue(int *rowCL, int *colCL, double **cM, bool *isReversed){  double temp = 0;  double *tempDifference = new double[numRow];  for (int c = 0; c < numCol; c++){    for (int r = 0; r < numRow; r++)      tempDifference[r] = 0;    for (int k = colPtr[c]; k < colPtr[c+1]; k++){      int tempRowId = rowIdx[k];      if (isReversed[tempRowId])        tempDifference[tempRowId] = (0 - value[k]);      else        tempDifference[tempRowId] = value[k];    }    for (int r = 0; r < numRow; r++){      double tempValue = tempDifference[r] - cM[rowCL[r]][colCL[c]];      temp += tempValue * tempValue;    }  }  delete [] tempDifference;  return temp;}  double SparseMatrix::computeObjectiveFunctionValue(int *rowCL, int *colCL, double **cM, double **rowCentroid, double **colCentroid){  double temp = 0;  double *tempDifference = new double[numRow];  for (int c = 0; c < numCol; c++){    for (int r = 0; r < numRow; r++)      tempDifference[r] = 0;    for (int k = colPtr[c]; k < colPtr[c+1]; k++)      tempDifference[rowIdx[k]] = value[k];    for (int r = 0; r < numRow; r++){      double tempValue = tempDifference[r] - rowCentroid[rowCL[r]][c] - colCentroid[colCL[c]][r] + cM[rowCL[r]][colCL[c]];      temp += tempValue * tempValue;    }  }  delete [] tempDifference;  return temp;}  double SparseMatrix::computeObjectiveFunctionValue(int *rowCL, int *colCL, double **cM, double **rowCentroid, double **colCentroid, bool *isReversed){  double temp = 0;  double *tempDifference = new double[numRow];  for (int c = 0; c < numCol; c++){    for (int r = 0; r < numRow; r++)      tempDifference[r] = 0;    for (int k = colPtr[c]; k < colPtr[c+1]; k++){      int tempRowId = rowIdx[k];      if (isReversed[tempRowId])        tempDifference[tempRowId] = (0 - value[k]);      else        tempDifference[tempRowId] = value[k];    }    for (int r = 0; r < numRow; r++){      double tempValue = tempDifference[r] - rowCentroid[rowCL[r]][c] - colCentroid[colCL[c]][r] + cM[rowCL[r]][colCL[c]];      temp += tempValue * tempValue;    }  }  delete [] tempDifference;  return temp;}  double SparseMatrix::computeObjectiveFunctionValue4RowCluster(int *rowCL, double **rowCentroid){  double temp = 0, tempValue = 0;  double *tempRow = new double[numRow];  for (int r = 0; r < numCol; r++){	// numRow (in CRS) equals to numCol (in CCS).    for (int c = 0; c < numRow; c++)      tempRow[c] = 0;    for (int k = colPtr[r]; k < colPtr[r+1]; k++)      tempRow[rowIdx[k]] = value[k];    for (int c = 0; c < numRow; c++){      tempValue = tempRow[c] - rowCentroid[rowCL[r]][c];      temp += tempValue * tempValue;    }  }  delete [] tempRow;  return temp;}double SparseMatrix::computeObjectiveFunctionValue4ColCluster(int *colCL, double **colCentroid){  double temp = 0, tempValue = 0;  double *tempCol = new double[numRow];  for (int c = 0; c < numCol; c++){    for (int r = 0; r < numRow; r++)      tempCol[r] = 0;    for (int k = colPtr[c]; k < colPtr[c+1]; k++)      tempCol[rowIdx[k]] = value[k];    for (int r = 0; r < numRow; r++){      tempValue = tempCol[r] - colCentroid[colCL[c]][r];      temp += tempValue * tempValue;    }  }  delete [] tempCol;  return temp;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -