diff --git a/src/storm/storage/SparseMatrix.cpp b/src/storm/storage/SparseMatrix.cpp index 9d92d5e35..83ff4f3af 100644 --- a/src/storm/storage/SparseMatrix.cpp +++ b/src/storm/storage/SparseMatrix.cpp @@ -2015,6 +2015,35 @@ namespace storm { return true; } + template<typename ValueType> + bool SparseMatrix<ValueType>::isIdentityMatrix() const { + if (this->getRowCount() != this->getColumnCount()) { + return false; + } + if (this->getNonzeroEntryCount() != this->getRowCount()) { + return false; + } + for (uint64_t row = 0; row < this->getRowCount(); ++row) { + bool rowHasEntry = false; + for (auto const& entry : this->getRow(row)) { + if (entry.getColumn() == row) { + if (!storm::utility::isOne(entry.getValue())) { + return false; + } + rowHasEntry = true; + } else { + if (!storm::utility::isZero(entry.getValue())) { + return false; + } + } + } + if (!rowHasEntry) { + return false; + } + } + return true; + } + template<typename ValueType> std::ostream& operator<<(std::ostream& out, SparseMatrix<ValueType> const& matrix) { // Print column numbers in header. diff --git a/src/storm/storage/SparseMatrix.h b/src/storm/storage/SparseMatrix.h index 92cb31b00..08a23ab2e 100644 --- a/src/storm/storage/SparseMatrix.h +++ b/src/storm/storage/SparseMatrix.h @@ -929,6 +929,9 @@ namespace storm { template<typename OtherValueType> bool isSubmatrixOf(SparseMatrix<OtherValueType> const& matrix) const; + // Returns true if the matrix is the identity matrix + bool isIdentityMatrix() const; + template<typename TPrime> friend std::ostream& operator<<(std::ostream& out, SparseMatrix<TPrime> const& matrix);