Browse Source

Added a test for reported bug by CDehnert: Sparse Matrix to Eigen conversion loses the diagonal entries

Fixed static_sparse_matrix.h
tempestpy_adaptions
PBerger 12 years ago
parent
commit
f5dce8d33a
  1. 4
      src/sparse/static_sparse_matrix.h
  2. 30
      test/sparse/static_sparse_matrix_test.cpp

4
src/sparse/static_sparse_matrix.h

@ -394,6 +394,10 @@ class StaticSparseMatrix {
for (uint_fast64_t row = 1; row <= row_count; ++row) { for (uint_fast64_t row = 1; row <= row_count; ++row) {
row_start = row_indications[row - 1]; row_start = row_indications[row - 1];
row_end = row_indications[row]; row_end = row_indications[row];
// insert the diagonal entry
mat->insert(row - 1, row - 1) = diagonal_storage[row];
while (row_start < row_end) { while (row_start < row_end) {
//tripletList.push_back(IntTriplet(row - 1, column_indications[row_start] - 1, value_storage[row_start])); //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]; mat->insert(row - 1, column_indications[row_start] - 1) = value_storage[row_start];

30
test/sparse/static_sparse_matrix_test.cpp

@ -282,3 +282,33 @@ TEST(StaticSparseMatrixTest, ConversionFromSparseEigen_RowMajor_SparseMatrixTest
ASSERT_EQ(target, coeff.value()); ASSERT_EQ(target, coeff.value());
} }
} }
TEST(StaticSparseMatrixTest, ConversionToSparseEigen_RowMajor_SparseMatrixTest) {
int values[100];
mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(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<int>::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<int>::MatrixStatus::Initialized);
ASSERT_NO_THROW(ssm->finalize());
ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
Eigen::SparseMatrix<int, Eigen::RowMajor, int_fast32_t>* 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));
}
}
}
Loading…
Cancel
Save