当前位置: 首页>>代码示例>>C++>>正文


C++ PackedVector::makeSparse方法代码示例

本文整理汇总了C++中PackedVector::makeSparse方法的典型用法代码示例。如果您正苦于以下问题:C++ PackedVector::makeSparse方法的具体用法?C++ PackedVector::makeSparse怎么用?C++ PackedVector::makeSparse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PackedVector的用法示例。


在下文中一共展示了PackedVector::makeSparse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: solveInPlace

void BaseSpline::solveInPlace(PackedMatrix& mat, PackedVector& res) {
  res = res.makeSparse();
  // Current implementation requires a quadratic mat
  int nCol = mat.numCols();
  PackedVector nonEmpty;
  int col, row, rowPos;
  for (col = 0; col < nCol; col++) {
    //int stop = 1;
    //if(col==stop-1){
//      cout << "*********************"<<endl;
//      cout << col << endl;
//      cout << "*********************"<<endl;
//      mat.displayMatrix();
//      cout << "RES: " <<res.values<<endl;
    //}

    // find the non-null elements in this column (below row "col")
    nonEmpty = PackedVector();
    int pivotPos(-1);
    for (row = col; row < mat.numRows(); row++) {
      int rowEntries = mat[row].numberEntries();
      for (rowPos = rowEntries; rowPos--;) {
        int entryIndex = mat[row].index(rowPos);
        if (entryIndex == col) {
          nonEmpty.packedAddElement(row, mat[row][rowPos]);
          if (row == col) {
            pivotPos = nonEmpty.numberEntries()-1;
          }
        }
      }
    }
//    if(col==stop-1){
//      cout<<"nonEmpty " << nonEmpty.values<< endl;
//      cout<<"pivot " << pivotPos<< endl<<endl;
//    }

    //find most significant row
    double maxVal(0.0);
    int maxRow(-1), maxRowPos(-1);
    for (rowPos = nonEmpty.numberEntries(); rowPos--;) {
      double val = nonEmpty[rowPos];
      assert(isfinite(val));
      if (fabs(val) > fabs(maxVal)) {
        maxVal = val;
        maxRow = nonEmpty.index(rowPos);
        maxRowPos = rowPos;
      }
    }
    //int imaxRow = maxRow;
    //int imaxRowPos = maxRowPos;
//    if(col==stop-1){
//      cout << "imaxRow " << imaxRow << endl;
//      cout << "imaxRowPos " << imaxRowPos << endl<<endl;
//    }

    // Put the most significant row at row "col"
    if (maxVal != 0.0) {
      if (maxRow != col) {
        swap(mat[col], mat[maxRow]);
        res.swapElements(col, maxRow);

        if (pivotPos >= 0) {
          nonEmpty.swapElements(maxRowPos, pivotPos);
        }
      }
//      if(col==stop-1){
//        cout << "SWAP\n";
//        mat.displayMatrix();
//      }

      // Divide the row with maxVal
      mat[col].packedDiv(maxVal);
      double value = res[col] / maxVal;
      res.packedReplace(col,value);
//      if(col==stop-1){
//        cout << "\nDIVIDE ROW\n";
//        mat.displayMatrix();
//        cout << "res " << res.values<<endl;
//        cout << "nonEmpty " << nonEmpty.values<<endl;
//        cout << "\n\n";
//      }
    }
    // subtract the row from other rows
    for (rowPos = nonEmpty.numberEntries(); rowPos--;) {
      row = nonEmpty.index(rowPos);
      if (row == col) {
        continue;
      }
      // If the pivotRow was empty (prior to swap) at col=row do not process this row
      if (pivotPos < 0 && row == maxRow) {
        continue;
      }
      double val = nonEmpty[rowPos];
      PackedVector prodVector = mat[col];
      mat[row] = mat[row].packedSubtract(prodVector.packedProd(val));
      double value = res[row] - (val * res[col]);
      res.packedReplace(row, value);

//      if(col==stop-1){
//        cout << "SUBTRACT ROW\n";
//.........这里部分代码省略.........
开发者ID:fu,项目名称:percolator,代码行数:101,代码来源:BaseSpline.cpp


注:本文中的PackedVector::makeSparse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。