diff --git a/MathLib/LinAlg/Sparse/CRSMatrix.h b/MathLib/LinAlg/Sparse/CRSMatrix.h index beeb3a4407cb10f9d43823d78cb21d67f4b7fa00..4ec9bb1033d4ff016612cc25aba82e03c27509a8 100644 --- a/MathLib/LinAlg/Sparse/CRSMatrix.h +++ b/MathLib/LinAlg/Sparse/CRSMatrix.h @@ -194,19 +194,17 @@ public: /** * erase rows and columns from sparse matrix * @param n_rows_cols number of rows / columns to remove - * @param rows sorted list of rows that should be removed - * @param cols sorted list of columns that should be removed + * @param rows_cols sorted list of rows/columns that should be removed */ - void eraseEntries (IDX_TYPE n_rows_cols, - IDX_TYPE const*const rows, IDX_TYPE const*const cols) + void eraseEntries(IDX_TYPE n_rows_cols, IDX_TYPE const* const rows_cols) { IDX_TYPE n_cols(MatrixBase::_n_rows); //*** remove the rows - removeRows(n_rows_cols, rows); + removeRows(n_rows_cols, rows_cols); //*** transpose transpose(n_cols); //*** remove columns in original means removing rows in the transposed - removeRows(n_rows_cols, cols); + removeRows(n_rows_cols, rows_cols); //*** transpose again transpose(MatrixBase::_n_rows); } @@ -241,11 +239,16 @@ protected: row_ptr_new[0] = 0; IDX_TYPE row_cnt (1), erase_row_cnt(0); for (unsigned k(0); k<MatrixBase::_n_rows; k++) { - if (k != rows[erase_row_cnt]) { + if (erase_row_cnt < n_rows_cols) { + if (k != rows[erase_row_cnt]) { + row_ptr_new[row_cnt] = _row_ptr[k+1] - _row_ptr[k]; + row_cnt++; + } else { + erase_row_cnt++; + } + } else { row_ptr_new[row_cnt] = _row_ptr[k+1] - _row_ptr[k]; row_cnt++; - } else { - erase_row_cnt++; } } @@ -269,7 +272,20 @@ protected: row_cnt = 0; // copy column index and data entries for (IDX_TYPE k(0); k<MatrixBase::_n_rows; k++) { - if (k != rows[erase_row_cnt]) { + if (erase_row_cnt < n_rows_cols) { + if (k != rows[erase_row_cnt]) { + const IDX_TYPE end (_row_ptr[k+1]); + // walk through row + for (IDX_TYPE j(_row_ptr[k]); j<end; j++) { + col_idx_new[row_ptr_new_tmp[row_cnt]] = _col_idx[j]; + data_new[row_ptr_new_tmp[row_cnt]] = _data[j]; + row_ptr_new_tmp[row_cnt]++; + } + row_cnt++; + } else { + erase_row_cnt++; + } + } else { const IDX_TYPE end (_row_ptr[k+1]); // walk through row for (IDX_TYPE j(_row_ptr[k]); j<end; j++) { @@ -278,8 +294,6 @@ protected: row_ptr_new_tmp[row_cnt]++; } row_cnt++; - } else { - erase_row_cnt++; } } @@ -288,6 +302,7 @@ protected: BASELIB::swap (col_idx_new, _col_idx); BASELIB::swap (data_new, _data); + delete [] row_ptr_new_tmp; delete [] row_ptr_new; delete [] col_idx_new; delete [] data_new; diff --git a/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp b/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp index f671342885f82b71bcd85d3a6f0312a59ded5719..0526308f936ee34ff8168f40a9592a8d7c795498 100644 --- a/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp +++ b/SimpleTests/MatrixTests/MatTestRemoveRowsCols.cpp @@ -51,33 +51,27 @@ int main(int argc, char *argv[]) MathLib::CRSMatrix<double, unsigned> *mat (new MathLib::CRSMatrix<double, unsigned>(n, iA, jA, A)); - const double val0 ((*mat)(1,1)); - std::cout << val0 << std::endl; - -// double val ((*(const_cast<MathLib::CRSMatrix<double, unsigned> const*>(mat)))(1,1)); -// std::cout << val << std::endl; - - MathLib::CRSMatrix<double, unsigned> const& ref_mat(*mat); - double val1 (ref_mat(1,1)); - std::cout << val1 << std::endl; - - const unsigned n_rows_cols_to_erase(3); - unsigned *rows_to_erase(new unsigned[n_rows_cols_to_erase]); - unsigned *cols_to_erase(new unsigned[n_rows_cols_to_erase]); + const unsigned n_rows_cols_to_erase(300); + unsigned *rows_cols_to_erase(new unsigned[n_rows_cols_to_erase]); for (unsigned k(0); k<n_rows_cols_to_erase; k++) { - rows_to_erase[k] = (k+1)*2; - cols_to_erase[k] = (k+1)*2; + rows_cols_to_erase[k] = (k+1)*2; } - mat->eraseEntries(n_rows_cols_to_erase, rows_to_erase, cols_to_erase); - delete[] rows_to_erase; - delete[] cols_to_erase; + RunTimeTimer timer; + std::cout << "erasing " << n_rows_cols_to_erase << " rows and columns ... " << std::flush; + timer.start(); + mat->eraseEntries(n_rows_cols_to_erase, rows_cols_to_erase); + timer.stop(); + std::cout << "ok, " << timer.elapsed() << " s" << std::endl; + delete[] rows_cols_to_erase; fname_mat = argv[2]; std::ofstream out (fname_mat.c_str(), std::ios::binary); CS_write (out, mat->getNRows(), mat->getRowPtrArray(), mat->getColIdxArray(), mat->getEntryArray()); out.close(); + std::cout << "wrote " << fname_mat << " with " << mat->getNRows() << " rows and " << mat->getRowPtrArray()[mat->getNRows()] << " entries" << std::endl; + delete mat; }