From f5dce8d33ae384cd5b4ae74fa65c5b4133b966f9 Mon Sep 17 00:00:00 2001 From: PBerger Date: Tue, 13 Nov 2012 22:21:40 +0100 Subject: [PATCH] Added a test for reported bug by CDehnert: Sparse Matrix to Eigen conversion loses the diagonal entries Fixed static_sparse_matrix.h --- src/sparse/static_sparse_matrix.h | 4 +++ test/sparse/static_sparse_matrix_test.cpp | 30 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/sparse/static_sparse_matrix.h b/src/sparse/static_sparse_matrix.h index e919ccde0..7efdebb68 100644 --- a/src/sparse/static_sparse_matrix.h +++ b/src/sparse/static_sparse_matrix.h @@ -394,6 +394,10 @@ class StaticSparseMatrix { for (uint_fast64_t row = 1; row <= row_count; ++row) { row_start = row_indications[row - 1]; row_end = row_indications[row]; + + // insert the diagonal entry + mat->insert(row - 1, row - 1) = diagonal_storage[row]; + while (row_start < row_end) { //tripletList.push_back(IntTriplet(row - 1, column_indications[row_start] - 1, value_storage[row_start])); mat->insert(row - 1, column_indications[row_start] - 1) = value_storage[row_start]; diff --git a/test/sparse/static_sparse_matrix_test.cpp b/test/sparse/static_sparse_matrix_test.cpp index 76e155261..167f221f5 100644 --- a/test/sparse/static_sparse_matrix_test.cpp +++ b/test/sparse/static_sparse_matrix_test.cpp @@ -281,4 +281,34 @@ TEST(StaticSparseMatrixTest, ConversionFromSparseEigen_RowMajor_SparseMatrixTest ASSERT_TRUE(ssm->getValue(coeff.row() + 1, coeff.col() + 1, &target)); ASSERT_EQ(target, coeff.value()); } +} + +TEST(StaticSparseMatrixTest, ConversionToSparseEigen_RowMajor_SparseMatrixTest) { + int values[100]; + mrmc::sparse::StaticSparseMatrix *ssm = new mrmc::sparse::StaticSparseMatrix(10); + + for (uint_fast32_t i = 0; i < 100; ++i) { + values[i] = i; + } + + ASSERT_NO_THROW(ssm->initialize(100 - 10)); + ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix::MatrixStatus::Initialized); + + for (uint_fast32_t row = 1; row <= 10; ++row) { + for (uint_fast32_t col = 1; col <= 10; ++col) { + ASSERT_NO_THROW(ssm->addNextValue(row, col, values[(row - 1) * 10 + (col - 1)])); + } + } + ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix::MatrixStatus::Initialized); + + ASSERT_NO_THROW(ssm->finalize()); + ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix::MatrixStatus::ReadReady); + + Eigen::SparseMatrix* esm = ssm->toEigenSparseMatrix(); + + for (uint_fast32_t row = 0; row < 10; ++row) { + for (uint_fast32_t col = 0; col < 10; ++col) { + ASSERT_EQ(values[row * 10 + col], esm->coeff(row, col)); + } + } } \ No newline at end of file