From bba72e452b69042868eaa70490870c5a492cb317 Mon Sep 17 00:00:00 2001 From: dehnert Date: Fri, 7 Jun 2013 18:15:22 +0200 Subject: [PATCH] Fixed off-by-one for our matrix-vector multiplication. --- src/storage/SparseMatrix.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/storage/SparseMatrix.h b/src/storage/SparseMatrix.h index 96d56424e..4bc369a57 100644 --- a/src/storage/SparseMatrix.h +++ b/src/storage/SparseMatrix.h @@ -955,21 +955,25 @@ public: */ void multiplyWithVector(std::vector const& vector, std::vector& result) const { // Initialize two iterators that - ConstRowsIterator elementIt(*this); - ConstRowsIterator elementIte(*this, 1); + ConstRowsIterator matrixElementIt(*this); + ConstRowsIterator matrixElementIte(*this); // Iterate over all positions of the result vector and compute its value as the scalar // product of the corresponding row with the input vector. // Note that only the end iterator has to be moved by one row, because the other iterator // is automatically moved forward one row by the inner loop. - for (auto it = result.begin(), ite = result.end(); it != ite; ++it, elementIte.moveToNextRow()) { - *it = storm::utility::constGetZero(); + for (auto& element : result) { + // Put the past-the-end iterator to the correct position. + matrixElementIte.moveToNextRow(); + + // Initialize the result to be 0. + element = storm::utility::constGetZero(); // Perform the scalar product. - for (; elementIt != elementIte; ++elementIt) { - *it += elementIt.value() * vector[elementIt.column()]; + for (; matrixElementIt != matrixElementIte; ++matrixElementIt) { + element += matrixElementIt.value() * vector[matrixElementIt.column()]; } - } + } } /*!