28template<
typename IndexType,
typename ValueType>
33template<
typename IndexType,
typename ValueType>
38template<
typename IndexType,
typename ValueType>
40 return this->entry.first;
43template<
typename IndexType,
typename ValueType>
45 this->entry.first = column;
48template<
typename IndexType,
typename ValueType>
50 return this->entry.second;
53template<
typename IndexType,
typename ValueType>
55 this->entry.second = value;
58template<
typename IndexType,
typename ValueType>
63template<
typename IndexType,
typename ValueType>
68template<
typename IndexType,
typename ValueType>
70 return this->entry.first == other.entry.first && this->entry.second == other.entry.second;
73template<
typename IndexType,
typename ValueType>
75 return !(*
this == other);
78template<
typename IndexTypePrime,
typename ValueTypePrime>
80 out <<
"(" << entry.getColumn() <<
", " << entry.getValue() <<
")";
84template<
typename ValueType>
87 : initialRowCountSet(rows != 0),
88 initialRowCount(rows),
89 initialColumnCountSet(columns != 0),
90 initialColumnCount(columns),
91 initialEntryCountSet(entries != 0),
92 initialEntryCount(entries),
93 forceInitialDimensions(forceDimensions),
94 hasCustomRowGrouping(hasCustomRowGrouping),
95 initialRowGroupCountSet(rowGroups != 0),
96 initialRowGroupCount(rowGroups),
100 currentEntryCount(0),
104 currentRowGroupCount(0) {
106 if (initialRowCountSet) {
107 rowIndications.reserve(initialRowCount + 1);
109 if (initialEntryCountSet) {
110 columnsAndValues.reserve(initialEntryCount);
112 if (hasCustomRowGrouping) {
113 rowGroupIndices = std::vector<index_type>();
115 if (initialRowGroupCountSet && hasCustomRowGrouping) {
116 rowGroupIndices.get().reserve(initialRowGroupCount + 1);
118 rowIndications.push_back(0);
121template<
typename ValueType>
123 : initialRowCountSet(false),
125 initialColumnCountSet(false),
126 initialColumnCount(0),
127 initialEntryCountSet(false),
128 initialEntryCount(0),
129 forceInitialDimensions(false),
130 hasCustomRowGrouping(!matrix.trivialRowGrouping),
131 initialRowGroupCountSet(false),
132 initialRowGroupCount(0),
134 columnsAndValues(
std::move(matrix.columnsAndValues)),
135 rowIndications(
std::move(matrix.rowIndications)),
136 currentEntryCount(matrix.entryCount),
137 currentRowGroupCount() {
138 lastRow = matrix.rowCount == 0 ? 0 : matrix.rowCount - 1;
139 lastColumn = columnsAndValues.empty() ? 0 : columnsAndValues.back().getColumn();
140 highestColumn = matrix.getColumnCount() == 0 ? 0 : matrix.getColumnCount() - 1;
143 if (hasCustomRowGrouping) {
144 rowGroupIndices = std::move(matrix.rowGroupIndices);
145 if (!rowGroupIndices->empty()) {
146 rowGroupIndices.get().pop_back();
148 currentRowGroupCount = rowGroupIndices->empty() ? 0 : rowGroupIndices.get().size() - 1;
152 if (!rowIndications.empty()) {
153 rowIndications.pop_back();
156 if (rowIndications.empty()) {
157 rowIndications.push_back(0);
167template<
typename ValueType>
172 case AccumulationMode::Sum:
173 return existingValue + newValue;
174 case AccumulationMode::ExpectSame:
175 STORM_LOG_THROW(existingValue == newValue, storm::exceptions::InvalidArgumentException,
176 "Multiple different values (" << existingValue <<
" and " << newValue <<
") added to matrix entry in row " << row <<
" and column "
177 << column <<
", but all values for the same entry are expected to be equal.");
178 return existingValue;
179 case AccumulationMode::ExpectNone:
181 "Multiple values added to matrix entry in row " << row <<
" and column " << column <<
", but no duplicate entries are expected.");
184 return existingValue;
188template<
typename ValueType>
190 accumulationMode = mode;
193template<
typename ValueType>
195 return accumulationMode;
198template<
typename ValueType>
201 STORM_LOG_THROW(row >= lastRow, storm::exceptions::InvalidArgumentException,
202 "Adding an element in row " << row <<
", but an element in row " << lastRow <<
" has already been added.");
203 STORM_LOG_ASSERT(columnsAndValues.size() == currentEntryCount,
"Unexpected size of columnsAndValues vector.");
206 if (pendingDiagonalEntry) {
207 index_type diagColumn = hasCustomRowGrouping ? currentRowGroupCount - 1 : lastRow;
208 if (row > lastRow || column >= diagColumn) {
209 ValueType diagValue = std::move(pendingDiagonalEntry.get());
210 pendingDiagonalEntry = boost::none;
218 if (forceInitialDimensions) {
219 STORM_LOG_THROW(!initialRowCountSet || row < initialRowCount, storm::exceptions::OutOfRangeException,
220 "Cannot insert value at illegal row " << row <<
".");
221 STORM_LOG_THROW(!initialColumnCountSet || column < initialColumnCount, storm::exceptions::OutOfRangeException,
222 "Cannot insert value at illegal column " << column <<
".");
226 if (row != lastRow) {
227 STORM_LOG_ASSERT(rowIndications.size() == lastRow + 1,
"Row indications size mismatch.");
228 rowIndications.resize(row + 1, currentEntryCount);
233 bool const rowHasEntries = rowIndications.back() < currentEntryCount;
234 if (!rowHasEntries || columnsAndValues.back().getColumn() < column) {
236 columnsAndValues.emplace_back(column, value);
238 }
else if (columnsAndValues.back().getColumn() == column) {
240 columnsAndValues.back().setValue(accumulate(accumulationMode, row, column, columnsAndValues.back().getValue(), value));
243 auto const rowBegin = columnsAndValues.begin() + rowIndications.back();
245 std::lower_bound(rowBegin, columnsAndValues.end(), column,
247 if (position != columnsAndValues.end() && position->getColumn() == column) {
248 position->setValue(accumulate(accumulationMode, row, column, position->getValue(), value));
250 STORM_LOG_TRACE(
"Column " << column <<
" is added out-of-order to row " << row <<
".");
251 columnsAndValues.emplace(position, column, value);
256 lastColumn = columnsAndValues.back().getColumn();
257 highestColumn = std::max(highestColumn, column);
259 if (forceInitialDimensions) {
260 STORM_LOG_THROW(!initialEntryCountSet || currentEntryCount <= initialEntryCount, storm::exceptions::OutOfRangeException,
261 "Too many entries in matrix, expected only " << initialEntryCount <<
".");
265template<
typename ValueType>
267 STORM_LOG_THROW(hasCustomRowGrouping, storm::exceptions::InvalidStateException,
"Matrix was not created to have a custom row grouping.");
268 STORM_LOG_THROW(startingRow >= lastRow, storm::exceptions::InvalidStateException,
"Illegal row group with negative size.");
271 if (pendingDiagonalEntry) {
272 STORM_LOG_ASSERT(currentRowGroupCount > 0,
"Diagonal entry was set before opening the first row group.");
273 index_type diagColumn = currentRowGroupCount - 1;
274 ValueType diagValue = std::move(pendingDiagonalEntry.get());
275 pendingDiagonalEntry = boost::none;
279 rowGroupIndices.get().push_back(startingRow);
280 ++currentRowGroupCount;
283 if (lastRow + 1 < startingRow) {
285 STORM_LOG_ASSERT(rowIndications.size() == lastRow + 1,
"Row indications size mismatch.");
286 rowIndications.resize(startingRow, currentEntryCount);
288 lastRow = startingRow - 1;
293template<
typename ValueType>
297 if (pendingDiagonalEntry) {
298 index_type diagColumn = hasCustomRowGrouping ? currentRowGroupCount - 1 : lastRow;
299 ValueType diagValue = std::move(pendingDiagonalEntry.get());
300 pendingDiagonalEntry = boost::none;
304 bool hasEntries = currentEntryCount != 0;
306 uint_fast64_t rowCount = hasEntries ? lastRow + 1 : 0;
309 if (hasCustomRowGrouping) {
310 if (lastRow < rowGroupIndices->back()) {
315 if (overriddenRowCount != 0) {
316 STORM_LOG_THROW(rowCount <= overriddenRowCount, storm::exceptions::InvalidStateException,
317 "Expected not more than " << overriddenRowCount <<
" rows, but got " << rowCount <<
".");
319 if (initialRowCountSet && forceInitialDimensions) {
320 STORM_LOG_THROW(rowCount <= initialRowCount, storm::exceptions::InvalidStateException,
321 "Expected not more than " << initialRowCount <<
" rows, but got " << rowCount <<
".");
322 rowCount = std::max(rowCount, initialRowCount);
324 rowCount = std::max(rowCount, overriddenRowCount);
328 rowIndications.push_back(currentEntryCount);
335 rowIndications.push_back(currentEntryCount);
338 "Wrong sizes of row indications vector: (Rowcount) " << rowCount <<
" != " << (rowIndications.size() - 1) <<
" (Vector).");
339 uint_fast64_t columnCount = hasEntries ? highestColumn + 1 : 0;
340 if (overriddenColumnCount != 0) {
341 STORM_LOG_THROW(columnCount <= overriddenColumnCount, storm::exceptions::InvalidStateException,
342 "Expected not more than " << overriddenColumnCount <<
" columns, but got " << columnCount <<
".");
344 if (initialColumnCountSet && forceInitialDimensions) {
345 STORM_LOG_THROW(columnCount <= initialColumnCount, storm::exceptions::InvalidStateException,
346 "Expected not more than " << initialColumnCount <<
" columns, but got " << columnCount <<
".");
347 columnCount = std::max(columnCount, initialColumnCount);
349 columnCount = std::max(columnCount, overriddenColumnCount);
351 uint_fast64_t entryCount = currentEntryCount;
352 if (initialEntryCountSet && forceInitialDimensions) {
353 STORM_LOG_THROW(entryCount == initialEntryCount, storm::exceptions::InvalidStateException,
354 "Expected " << initialEntryCount <<
" entries, but got " << entryCount <<
".");
358 if (hasCustomRowGrouping) {
359 uint_fast64_t rowGroupCount = currentRowGroupCount;
360 if (overriddenRowGroupCount != 0) {
361 STORM_LOG_THROW(rowGroupCount <= overriddenRowGroupCount, storm::exceptions::InvalidStateException,
362 "Expected not more than " << overriddenRowGroupCount <<
" row groups, but got " << rowGroupCount <<
".");
364 if (initialRowGroupCountSet && forceInitialDimensions) {
365 STORM_LOG_THROW(rowGroupCount <= initialRowGroupCount, storm::exceptions::InvalidStateException,
366 "Expected not more than " << initialRowGroupCount <<
" row groups, but got " << rowGroupCount <<
".");
367 rowGroupCount = std::max(rowGroupCount, initialRowGroupCount);
369 rowGroupCount = std::max(rowGroupCount, overriddenRowGroupCount);
371 for (
index_type i = currentRowGroupCount;
i <= rowGroupCount; ++
i) {
372 rowGroupIndices.get().push_back(rowCount);
376 return SparseMatrix<ValueType>(columnCount, std::move(rowIndications), std::move(columnsAndValues), std::move(rowGroupIndices));
379template<
typename ValueType>
384template<
typename ValueType>
386 if (this->hasCustomRowGrouping) {
387 return currentRowGroupCount;
393template<
typename ValueType>
399template<
typename ValueType>
407 std::cout <<
"\t---- group " << group <<
"/" << (rowGroupIndices.size() - 1) <<
" ---- \n";
408 endGroups = group < rowGroupIndices.size() - 1 ? rowGroupIndices[group + 1] : rowIndications.size();
411 endRows =
i < rowIndications.size() - 1 ? rowIndications[
i + 1] : columnsAndValues.size();
413 std::cout <<
"Row " <<
i <<
" (" << rowIndications[
i] <<
" - " << endRows <<
")"
416 std::cout <<
"(" << columnsAndValues[pos].getColumn() <<
": " << columnsAndValues[pos].getValue() <<
") ";
423template<
typename ValueType>
427 for (
index_type row = 0; row < rowIndications.size(); ++row) {
428 bool changed =
false;
429 auto startRow = std::next(columnsAndValues.begin(), rowIndications[row]);
430 auto endRow = row < rowIndications.size() - 1 ? std::next(columnsAndValues.begin(), rowIndications[row + 1]) : columnsAndValues.end();
431 for (
auto entry = startRow; entry != endRow; ++entry) {
432 if (entry->getColumn() >= offset) {
434 entry->setColumn(replacements[entry->getColumn() - offset]);
437 maxColumn = std::max(maxColumn, entry->getColumn());
441 std::sort(startRow, endRow,
448 "Columns not sorted.");
452 highestColumn = maxColumn;
453 lastColumn = columnsAndValues.empty() ? 0 : columnsAndValues.back().getColumn();
456template<
typename ValueType>
458 STORM_LOG_THROW(row >= lastRow, storm::exceptions::InvalidArgumentException,
459 "Adding a diagonal element in row " << row <<
", but an element in row " << lastRow <<
" has already been added.");
460 if (pendingDiagonalEntry) {
461 if (row == lastRow) {
463 index_type column = hasCustomRowGrouping ? currentRowGroupCount - 1 : lastRow;
464 pendingDiagonalEntry = accumulate(accumulationMode, row, column, pendingDiagonalEntry.get(), value);
468 index_type column = hasCustomRowGrouping ? currentRowGroupCount - 1 : lastRow;
469 ValueType diagValue = std::move(pendingDiagonalEntry.get());
470 pendingDiagonalEntry = boost::none;
474 pendingDiagonalEntry = value;
475 if (lastRow != row) {
476 STORM_LOG_ASSERT(rowIndications.size() == lastRow + 1,
"Row indications size mismatch.");
477 rowIndications.resize(row + 1, currentEntryCount);
483template<
typename ValueType>
488template<
typename ValueType>
490 return beginIterator;
493template<
typename ValueType>
495 return beginIterator + entryCount;
498template<
typename ValueType>
500 return this->entryCount;
503template<
typename ValueType>
508template<
typename ValueType>
510 return beginIterator;
513template<
typename ValueType>
515 return beginIterator + entryCount;
518template<
typename ValueType>
520 return this->entryCount;
523template<
typename ValueType>
525 : rowCount(0), columnCount(0), entryCount(0), nonzeroEntryCount(0), columnsAndValues(), rowIndications(), rowGroupIndices() {
529template<
typename ValueType>
531 : rowCount(other.rowCount),
532 columnCount(other.columnCount),
533 entryCount(other.entryCount),
534 nonzeroEntryCount(other.nonzeroEntryCount),
535 columnsAndValues(other.columnsAndValues),
536 rowIndications(other.rowIndications),
537 trivialRowGrouping(other.trivialRowGrouping),
538 rowGroupIndices(other.rowGroupIndices) {
542template<
typename ValueType>
546 *
this = other.
getSubmatrix(
false, rowConstraint, columnConstraint, insertDiagonalElements);
549template<
typename ValueType>
551 : rowCount(other.rowCount),
552 columnCount(other.columnCount),
553 entryCount(other.entryCount),
554 nonzeroEntryCount(other.nonzeroEntryCount),
555 columnsAndValues(
std::move(other.columnsAndValues)),
556 rowIndications(
std::move(other.rowIndications)),
557 trivialRowGrouping(other.trivialRowGrouping),
558 rowGroupIndices(
std::move(other.rowGroupIndices)) {
561 other.columnCount = 0;
562 other.entryCount = 0;
565template<
typename ValueType>
568 boost::optional<std::vector<index_type>>
const& rowGroupIndices)
569 : rowCount(rowIndications.size() - 1),
570 columnCount(columnCount),
571 entryCount(columnsAndValues.size()),
572 nonzeroEntryCount(0),
573 columnsAndValues(columnsAndValues),
574 rowIndications(rowIndications),
575 trivialRowGrouping(!rowGroupIndices),
576 rowGroupIndices(rowGroupIndices) {
580template<
typename ValueType>
583 boost::optional<std::vector<index_type>>&& rowGroupIndices)
584 : columnCount(columnCount),
585 nonzeroEntryCount(0),
586 columnsAndValues(
std::move(columnsAndValues)),
587 rowIndications(
std::move(rowIndications)),
588 rowGroupIndices(
std::move(rowGroupIndices)) {
591 this->rowCount = this->rowIndications.size() - 1;
592 this->entryCount = this->columnsAndValues.size();
593 this->trivialRowGrouping = !this->rowGroupIndices;
597template<
typename ValueType>
600 if (
this != &other) {
601 rowCount = other.rowCount;
602 columnCount = other.columnCount;
603 entryCount = other.entryCount;
604 nonzeroEntryCount = other.nonzeroEntryCount;
606 columnsAndValues = other.columnsAndValues;
607 rowIndications = other.rowIndications;
608 rowGroupIndices = other.rowGroupIndices;
609 trivialRowGrouping = other.trivialRowGrouping;
614template<
typename ValueType>
617 if (
this != &other) {
618 rowCount = other.rowCount;
619 columnCount = other.columnCount;
620 entryCount = other.entryCount;
621 nonzeroEntryCount = other.nonzeroEntryCount;
623 columnsAndValues = std::move(other.columnsAndValues);
624 rowIndications = std::move(other.rowIndications);
625 rowGroupIndices = std::move(other.rowGroupIndices);
626 trivialRowGrouping = other.trivialRowGrouping;
631template<
typename ValueType>
633 if (
this == &other) {
637 bool equalityResult =
true;
640 if (!equalityResult) {
643 equalityResult &= this->getColumnCount() == other.
getColumnCount();
644 if (!equalityResult) {
652 if (!equalityResult) {
658 for (index_type row = 0; row < this->getRowCount(); ++row) {
659 const_iterator it1 = this->begin(row), ite1 = this->end(row), it2 = other.
begin(row), ite2 = other.
end(row);
668 if (it1 == ite1 || it2 == ite2) {
670 if (it1 != ite1 || it2 != ite2) {
675 if (it1->getColumn() != it2->getColumn() || it1->getValue() != it2->getValue()) {
683 return equalityResult;
686template<
typename ValueType>
691template<
typename ValueType>
696template<
typename ValueType>
701template<
typename ValueType>
710 return getRowEntryCount(group);
714template<
typename ValueType>
716 return (this->rowIndications[row + 1] - this->rowIndications[row]);
719template<
typename ValueType>
721 return nonzeroEntryCount;
724template<
typename ValueType>
726 this->nonzeroEntryCount = 0;
727 for (
auto const& element : *
this) {
729 ++this->nonzeroEntryCount;
734template<
typename ValueType>
736 this->nonzeroEntryCount += difference;
739template<
typename ValueType>
741 this->nonzeroEntryCount = 0;
742 this->columnCount = 0;
743 for (
auto const& element : *
this) {
745 ++this->nonzeroEntryCount;
746 this->columnCount = std::max(element.getColumn() + 1, this->columnCount);
751template<
typename ValueType>
754 return rowGroupIndices.get().size() - 1;
760template<
typename ValueType>
765template<
typename ValueType>
772 for (
auto const&
i : rowGroupIndices.get()) {
773 res = std::max(res,
i - previousGroupStart);
774 previousGroupStart =
i;
779template<
typename ValueType>
791 numRows +=
end - start;
797template<
typename ValueType>
800 if (!this->rowGroupIndices) {
801 STORM_LOG_ASSERT(trivialRowGrouping,
"Only trivial row-groupings can be constructed on-the-fly.");
804 return rowGroupIndices.get();
807template<
typename ValueType>
809 return rowIndications;
812template<
typename ValueType>
815 "Invalid row group index:" << group <<
". Only " << this->
getRowGroupCount() <<
" row groups available.");
816 if (this->rowGroupIndices) {
817 return boost::irange(rowGroupIndices.get()[group], rowGroupIndices.get()[group + 1]);
819 return boost::irange(group, group + 1);
823template<
typename ValueType>
825 std::vector<index_type> result;
826 if (this->rowGroupIndices) {
827 result = std::move(rowGroupIndices.get());
828 rowGroupIndices = std::move(newRowGrouping);
833template<
typename ValueType>
835 trivialRowGrouping =
false;
836 rowGroupIndices = newRowGroupIndices;
839template<
typename ValueType>
841 return trivialRowGrouping;
844template<
typename ValueType>
846 if (trivialRowGrouping) {
849 "Row grouping is supposed to be trivial but actually it is not.");
851 trivialRowGrouping =
true;
852 rowGroupIndices = boost::none;
856template<
typename ValueType>
859 for (uint64_t group : groupConstraint) {
865template<
typename ValueType>
869 for (uint64_t group : groupConstraint) {
871 bool choiceSatisfiesColumnConstraint =
true;
872 for (
auto const& entry : this->
getRow(row)) {
873 if (!columnConstraint.
get(entry.getColumn())) {
874 choiceSatisfiesColumnConstraint =
false;
878 if (choiceSatisfiesColumnConstraint) {
879 result.
set(row,
true);
886template<
typename ValueType>
891 if (setIfForAllRowsInGroup) {
893 if (rowConstraint.
getNextUnsetIndex(groupIndices[group]) >= groupIndices[group + 1]) {
895 result.
set(group,
true);
899 for (uint64_t group = 0; group < this->getRowGroupCount(); ++group) {
900 if (rowConstraint.
getNextSetIndex(groupIndices[group]) < groupIndices[group + 1]) {
902 result.
set(group,
true);
909template<
typename ValueType>
913 for (uint64_t row :
rows) {
917 this->dropZeroEntries();
921template<
typename ValueType>
926 for (uint64_t rowGroup : rowGroupConstraint) {
932 for (uint64_t rowGroup : rowGroupConstraint) {
937 this->dropZeroEntries();
941template<
typename ValueType>
948 STORM_LOG_THROW(columnValuePtr < columnValuePtrEnd, storm::exceptions::InvalidStateException,
949 "Illegal call to SparseMatrix::makeRowDirac: cannot make row " << row <<
" absorbing, because there is no entry in this row.");
955 while (columnValuePtr->getColumn() < column && columnValuePtr != lastColumnValuePtr) {
957 --this->nonzeroEntryCount;
964 ++this->nonzeroEntryCount;
967 columnValuePtr->setColumn(column);
968 for (++columnValuePtr; columnValuePtr != columnValuePtrEnd; ++columnValuePtr) {
970 --this->nonzeroEntryCount;
974 if (dropZeroEntries) {
975 this->dropZeroEntries();
979template<
typename ValueType>
985 for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
990 if (it1 == end1 && it2 == end2) {
996template<
typename ValueType>
999 for (
size_t rowgroup = 0; rowgroup < this->getRowGroupCount(); ++rowgroup) {
1001 for (
size_t row2 = row1; row2 < this->
getRowGroupIndices().at(rowgroup + 1); ++row2) {
1011template<
typename ValueType>
1018 index_type largerRow =
getRow(row1).getNumberOfEntries() >
getRow(row2).getNumberOfEntries() ? row1 : row2;
1019 index_type smallerRow = largerRow == row1 ? row2 : row1;
1020 index_type rowSizeDifference =
getRow(largerRow).getNumberOfEntries() -
getRow(smallerRow).getNumberOfEntries();
1023 auto copyRow =
getRow(largerRow);
1024 std::vector<MatrixEntry<index_type, value_type>> largerRowContents(copyRow.begin(), copyRow.end());
1026 if (largerRow < smallerRow) {
1027 auto writeIt =
getRows(largerRow, smallerRow + 1).begin();
1030 for (
auto& smallerRowEntry :
getRow(smallerRow)) {
1031 *writeIt = std::move(smallerRowEntry);
1037 for (
auto& intermediateRowEntry : getRows(largerRow + 1, smallerRow)) {
1038 *writeIt = std::move(intermediateRowEntry);
1043 writeIt = getRow(smallerRow).begin();
1047 for (
auto& largerRowEntry : largerRowContents) {
1048 *writeIt = std::move(largerRowEntry);
1052 STORM_LOG_ASSERT(writeIt == getRow(smallerRow).end(),
"Unexpected position of write iterator.");
1056 for (index_type row = largerRow + 1; row <= smallerRow; ++row) {
1057 rowIndications[row] -= rowSizeDifference;
1061 auto writeIt = getRows(smallerRow, largerRow + 1).end() - 1;
1064 auto copyRow = getRow(smallerRow);
1065 for (
auto smallerRowEntryIt = copyRow.end() - 1; smallerRowEntryIt != copyRow.begin() - 1; --smallerRowEntryIt) {
1066 *writeIt = std::move(*smallerRowEntryIt);
1072 for (
auto intermediateRowEntryIt = getRows(smallerRow + 1, largerRow).end() - 1;
1073 intermediateRowEntryIt !=
getRows(smallerRow + 1, largerRow).begin() - 1; --intermediateRowEntryIt) {
1074 *writeIt = std::move(*intermediateRowEntryIt);
1079 writeIt =
getRow(smallerRow).end() - 1;
1083 for (
auto largerRowEntryIt = largerRowContents.rbegin(); largerRowEntryIt != largerRowContents.rend(); ++largerRowEntryIt) {
1084 *writeIt = std::move(*largerRowEntryIt);
1093 for (
index_type row = smallerRow + 1; row <= largerRow; ++row) {
1094 rowIndications[row] += rowSizeDifference;
1100template<
typename ValueType>
1102 std::vector<ValueType> result(this->
getRowCount());
1105 for (
auto resultIt = result.begin(), resultIte = result.end(); resultIt != resultIte; ++resultIt, ++row) {
1112template<
typename ValueType>
1116 if (constraint.
get(it->getColumn())) {
1117 result += it->getValue();
1123template<
typename ValueType>
1128 for (uint64_t row : rowConstraint) {
1134template<
typename ValueType>
1137 std::vector<ValueType> result;
1140 for (uint64_t rowGroup : rowGroupConstraint) {
1146 for (uint64_t rowGroup : rowGroupConstraint) {
1147 result.push_back(getConstrainedRowSum(rowGroup, columnConstraint));
1153template<
typename ValueType>
1159 if (makeZeroColumns.
size() > 0) {
1160 makeZeroColumnsRef.
reset(makeZeroColumns);
1163 return submatrixBuilder.
getByRowGroupConstraint(rowConstraint, columnConstraint, insertDiagonalElements, makeZeroColumnsRef);
1165 return submatrixBuilder.
getByRowConstraint(rowConstraint, columnConstraint, insertDiagonalElements, makeZeroColumnsRef);
1169template<
typename ValueType>
1175 for (uint64_t row : rowsToKeep) {
1176 entryCount += this->
getRow(row).getNumberOfEntries();
1180 index_type firstTrailingEmptyRowGroup = this->getRowGroupCount();
1185 --firstTrailingEmptyRowGroup;
1187 STORM_LOG_THROW(allowEmptyRowGroups || firstTrailingEmptyRowGroup == this->getRowGroupCount(), storm::exceptions::InvalidArgumentException,
1188 "Empty rows are not allowed, but row group " << firstTrailingEmptyRowGroup <<
" is empty.");
1193 for (
index_type rowGroup = 0; rowGroup < firstTrailingEmptyRowGroup; ++rowGroup) {
1196 bool rowGroupEmpty =
true;
1199 rowGroupEmpty =
false;
1200 for (
auto const& entry : this->
getRow(row)) {
1201 builder.addNextValue(newRow, entry.getColumn(), entry.getValue());
1205 STORM_LOG_THROW(allowEmptyRowGroups || !rowGroupEmpty, storm::exceptions::InvalidArgumentException,
1206 "Empty rows are not allowed, but row group " << rowGroup <<
" is empty.");
1210 SparseMatrix<ValueType> res =
builder.build();
1214template<
typename ValueType>
1218 for (uint64_t row : rowFilter) {
1219 entryCount +=
getRow(row).getNumberOfEntries();
1224 for (uint64_t row : rowFilter) {
1225 for (
auto const& entry :
getRow(row)) {
1226 builder.addNextValue(row, entry.getColumn(), entry.getValue());
1238template<
typename ValueType>
1244 for (
auto const& entry :
getRow(row)) {
1246 builder.addNextValue(row, entry.getColumn(), entry.getValue());
1250 SparseMatrix<ValueType> result = builder.
build();
1252 if (!hasTrivialRowGrouping()) {
1253 result.setRowGroupIndices(getRowGroupIndices());
1255 *
this = std::move(result);
1259template<
typename ValueType>
1261 bool insertDiagonalEntries)
const {
1265 for (
index_type rowGroupIndex = 0, rowGroupIndexEnd = rowGroupToRowIndexMapping.size(); rowGroupIndex < rowGroupIndexEnd; ++rowGroupIndex) {
1268 "Cannot point to row offset " << rowGroupToRowIndexMapping[rowGroupIndex] <<
" for rowGroup " << rowGroupIndex <<
" which starts at "
1274 bool foundDiagonalElement =
false;
1276 if (it->getColumn() == rowGroupIndex) {
1277 foundDiagonalElement =
true;
1281 if (insertDiagonalEntries && !foundDiagonalElement) {
1290 for (
index_type rowGroupIndex = 0, rowGroupIndexEnd = rowGroupToRowIndexMapping.size(); rowGroupIndex < rowGroupIndexEnd; ++rowGroupIndex) {
1296 bool insertedDiagonalElement =
false;
1298 if (it->getColumn() == rowGroupIndex) {
1299 insertedDiagonalElement =
true;
1300 }
else if (insertDiagonalEntries && !insertedDiagonalElement && it->getColumn() > rowGroupIndex) {
1302 insertedDiagonalElement =
true;
1304 matrixBuilder.
addNextValue(rowGroupIndex, it->getColumn(), it->getValue());
1306 if (insertDiagonalEntries && !insertedDiagonalElement) {
1312 return matrixBuilder.
build();
1315template<
typename ValueType>
1317 bool insertDiagonalEntries)
const {
1321 for (
index_type row = 0, rowEnd = rowIndexSequence.size(); row < rowEnd; ++row) {
1322 bool foundDiagonalElement =
false;
1323 for (
const_iterator it = this->
begin(rowIndexSequence[row]), ite = this->
end(rowIndexSequence[row]); it != ite; ++it) {
1324 if (it->getColumn() == row) {
1325 foundDiagonalElement =
true;
1329 if (insertDiagonalEntries && !foundDiagonalElement) {
1338 for (
index_type row = 0, rowEnd = rowIndexSequence.size(); row < rowEnd; ++row) {
1339 bool insertedDiagonalElement =
false;
1340 for (
const_iterator it = this->
begin(rowIndexSequence[row]), ite = this->
end(rowIndexSequence[row]); it != ite; ++it) {
1341 if (it->getColumn() == row) {
1342 insertedDiagonalElement =
true;
1343 }
else if (insertDiagonalEntries && !insertedDiagonalElement && it->getColumn() > row) {
1345 insertedDiagonalElement =
true;
1347 matrixBuilder.
addNextValue(row, it->getColumn(), it->getValue());
1349 if (insertDiagonalEntries && !insertedDiagonalElement) {
1355 return matrixBuilder.
build();
1358template<
typename ValueType>
1366 for (
index_type writeTo = 0; writeTo < inversePermutation.size(); ++writeTo) {
1367 index_type const& readFrom = inversePermutation[writeTo];
1368 auto row = this->
getRow(readFrom);
1369 for (
auto const& entry : row) {
1370 matrixBuilder.
addNextValue(writeTo, entry.getColumn(), entry.getValue());
1374 auto result = matrixBuilder.
build();
1375 if (this->rowGroupIndices) {
1376 result.setRowGroupIndices(this->rowGroupIndices.get());
1381template<
typename ValueType>
1383 std::vector<index_type>
const& columnPermutation)
const {
1388 auto oldGroupIt = inverseRowGroupPermutation.cbegin();
1390 while (newRowIndex < rowCount) {
1395 for (
auto const& oldEntry :
getRow(oldRowIndex)) {
1396 matrixBuilder.
addNextValue(newRowIndex, columnPermutation[oldEntry.getColumn()], oldEntry.getValue());
1402 return matrixBuilder.
build();
1405template<
typename ValueType>
1417 std::vector<index_type> rowIndications(rowCount + 1);
1418 std::vector<MatrixEntry<index_type, ValueType>> columnsAndValues(entryCount);
1421 for (
index_type group = 0; group < columnCount; ++group) {
1422 for (
auto const& transition : joinGroups ? this->
getRowGroup(group) : this->
getRow(group)) {
1424 ++rowIndications[transition.getColumn() + 1];
1431 rowIndications[
i] = rowIndications[
i - 1] + rowIndications[
i];
1437 std::vector<index_type> nextIndices = rowIndications;
1440 for (
index_type group = 0; group < columnCount; ++group) {
1441 for (
auto const& transition : joinGroups ? this->
getRowGroup(group) : this->
getRow(group)) {
1443 columnsAndValues[nextIndices[transition.getColumn()]] = std::make_pair(group, transition.getValue());
1444 nextIndices[transition.getColumn()]++;
1451 return transposedMatrix;
1454template<
typename ValueType>
1457 index_type columnCount = this->getRowGroupCount();
1461 std::vector<index_type> rowIndications(columnCount + 1);
1462 auto rowGroupChoiceIt = rowGroupChoices.begin();
1463 for (
index_type rowGroup = 0; rowGroup < columnCount; ++rowGroup, ++rowGroupChoiceIt) {
1464 for (
auto const& entry : this->
getRow(rowGroup, *rowGroupChoiceIt)) {
1467 ++rowIndications[entry.getColumn() + 1];
1474 rowIndications[
i] = rowIndications[
i - 1] + rowIndications[
i];
1477 std::vector<MatrixEntry<index_type, ValueType>> columnsAndValues(entryCount);
1482 std::vector<index_type> nextIndices = rowIndications;
1485 rowGroupChoiceIt = rowGroupChoices.begin();
1486 for (
index_type rowGroup = 0; rowGroup < columnCount; ++rowGroup, ++rowGroupChoiceIt) {
1487 for (
auto const& entry : this->
getRow(rowGroup, *rowGroupChoiceIt)) {
1489 columnsAndValues[nextIndices[entry.getColumn()]] = std::make_pair(rowGroup, entry.getValue());
1490 ++nextIndices[entry.getColumn()];
1498template<
typename ValueType>
1504template<
typename ValueType>
1510 bool foundDiagonalElement =
false;
1511 for (
index_type group = 0; group < this->getRowGroupCount(); ++group) {
1513 if (entry.getColumn() == group) {
1514 if (entry.getValue() == one) {
1515 --this->nonzeroEntryCount;
1516 entry.setValue(zero);
1517 }
else if (entry.getValue() == zero) {
1518 ++this->nonzeroEntryCount;
1519 entry.setValue(one);
1521 entry.setValue(one - entry.getValue());
1523 foundDiagonalElement =
true;
1528 STORM_LOG_THROW(foundDiagonalElement, storm::exceptions::InvalidArgumentException,
1529 "Illegal call to SparseMatrix::invertDiagonal: matrix is missing diagonal entries.");
1533template<
typename ValueType>
1536 for (
index_type group = 0; group < this->getRowGroupCount(); ++group) {
1538 if (entry.getColumn() != group) {
1539 entry.setValue(-entry.getValue());
1545template<
typename ValueType>
1548 for (
index_type group = 0; group < this->getRowGroupCount(); ++group) {
1550 if (entry.getColumn() == group) {
1551 --this->nonzeroEntryCount;
1557 this->dropZeroEntries();
1561template<
typename ValueType>
1564 "Canno compute Jacobi decomposition of non-square matrix.");
1568 std::vector<ValueType> invertedDiagonal(rowCount);
1571 for (
index_type rowNumber = 0; rowNumber < rowCount; ++rowNumber) {
1573 if (it->getColumn() == rowNumber) {
1576 luBuilder.
addNextValue(rowNumber, it->getColumn(), it->getValue());
1581 return std::make_pair(luBuilder.
build(), std::move(invertedDiagonal));
1586 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"This operation is not supported.");
1592 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"This operation is not supported.");
1595template<
typename ValueType>
1596template<
typename OtherValueType,
typename ResultValueType>
1605 for (; it1 != ite1 && it2 != ite2; ++it1) {
1606 if (it1->getColumn() < it2->getColumn()) {
1612 STORM_LOG_ASSERT(it1->getColumn() == it2->getColumn(),
"The given matrix is not a submatrix of this one.");
1613 result += it2->getValue() * OtherValueType(it1->getValue());
1620template<
typename ValueType>
1621template<
typename OtherValueType,
typename ResultValueType>
1623 std::vector<ResultValueType> result;
1624 result.reserve(rowCount);
1631template<
typename ValueType>
1633 std::vector<value_type>
const* summand)
const {
1635 std::vector<ValueType>* target;
1636 std::vector<ValueType> temporary;
1637 if (&vector == &result) {
1638 STORM_LOG_WARN(
"Vectors are aliased. Using temporary, which is potentially slow.");
1639 temporary = std::vector<ValueType>(vector.size());
1640 target = &temporary;
1647 if (target == &temporary) {
1648 std::swap(result, *target);
1652template<
typename ValueType>
1654 std::vector<value_type>
const* summand)
const {
1657 std::vector<index_type>::const_iterator rowIterator = rowIndications.begin();
1658 typename std::vector<ValueType>::iterator resultIterator = result.begin();
1659 typename std::vector<ValueType>::iterator resultIteratorEnd = result.end();
1660 typename std::vector<ValueType>::const_iterator summandIterator;
1662 summandIterator = summand->begin();
1665 for (; resultIterator != resultIteratorEnd; ++rowIterator, ++resultIterator, ++summandIterator) {
1668 newValue = *summandIterator;
1673 for (ite = this->
begin() + *(rowIterator + 1); it != ite; ++it) {
1674 newValue += it->getValue() * vector[it->getColumn()];
1677 *resultIterator = newValue;
1681template<
typename ValueType>
1683 std::vector<value_type>
const* summand)
const {
1686 std::vector<index_type>::const_iterator rowIterator = rowIndications.end() - 2;
1687 typename std::vector<ValueType>::iterator resultIterator = result.end() - 1;
1688 typename std::vector<ValueType>::iterator resultIteratorEnd = result.begin() - 1;
1689 typename std::vector<ValueType>::const_iterator summandIterator;
1691 summandIterator = summand->end() - 1;
1694 for (; resultIterator != resultIteratorEnd; --rowIterator, --resultIterator, --summandIterator) {
1697 newValue = *summandIterator;
1702 for (ite = this->
begin() + *rowIterator - 1; it != ite; --it) {
1703 newValue += (it->getValue() * vector[it->getColumn()]);
1706 *resultIterator = newValue;
1710template<
typename ValueType>
1712 ValueType result = std::move(offset);
1713 for (
auto const& entry : this->
getRow(row)) {
1717 result += entry.getValue() * vector[entry.getColumn()];
1722template<
typename ValueType>
1727 for (
auto const& entry : this->
getRow(row)) {
1731 auto const& vectorValue = vector[entry.getColumn()];
1733 if (result.
isFinite() && vectorValue.isFinite()) {
1734 result.
getFinite() += entry.getValue() * vectorValue.getFinite();
1736 result += entry.getValue() * vectorValue;
1742template<
typename ValueType>
1746 std::vector<index_type>::const_iterator rowIterator = rowIndications.end() - 2;
1747 typename std::vector<ValueType>::const_iterator bIt = b.end() - 1;
1748 typename std::vector<ValueType>::iterator resultIterator = x.end() - 1;
1749 typename std::vector<ValueType>::iterator resultIteratorEnd = x.begin() - 1;
1752 for (; resultIterator != resultIteratorEnd; --rowIterator, --resultIterator, --bIt) {
1757 for (ite = this->
begin() + *rowIterator - 1; it != ite; --it) {
1758 if (it->getColumn() != currentRow) {
1759 tmpValue += it->getValue() * x[it->getColumn()];
1761 diagonalElement += it->getValue();
1771 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"This operation is not supported.");
1774template<
typename ValueType>
1776 std::vector<ValueType>
const& ax, std::vector<ValueType>& result)
const {
1779 std::vector<index_type>::const_iterator rowIterator = rowIndications.begin();
1783 for (
auto& entry : result) {
1787 for (
index_type row = 0; row < rowCount; ++row, ++rowIterator) {
1788 for (ite = this->
begin() + *(rowIterator + 1); it != ite; ++it) {
1789 result[it->getColumn()] += it->getValue() * (b[row] / ax[row]);
1793 auto xIterator = x.begin();
1794 auto sumsIterator = columnSums.begin();
1795 for (
auto& entry : result) {
1796 entry *= *xIterator / *sumsIterator;
1804 std::vector<Interval>
const& ax, std::vector<Interval>& result)
const {
1805 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"This operation is not supported.");
1808template<
typename ValueType>
1810 std::vector<ValueType>
const& vector, std::vector<ValueType>
const* summand,
1811 std::vector<ValueType>& result, std::vector<uint64_t>* choices)
const {
1812 if (dir == OptimizationDirection::Minimize) {
1819template<
typename ValueType>
1820template<
typename Compare>
1822 std::vector<ValueType>
const* summand, std::vector<ValueType>& result,
1823 std::vector<uint64_t>* choices)
const {
1825 auto elementIt = this->
begin();
1826 auto rowGroupIt = rowGroupIndices.begin();
1827 auto rowIt = rowIndications.begin();
1828 typename std::vector<ValueType>::const_iterator summandIt;
1830 summandIt = summand->begin();
1832 typename std::vector<uint64_t>::iterator choiceIt;
1834 choiceIt = choices->begin();
1838 ValueType oldSelectedChoiceValue;
1839 uint64_t selectedChoice;
1841 uint64_t currentRow = 0;
1842 for (
auto resultIt = result.begin(), resultIte = result.end(); resultIt != resultIte; ++resultIt, ++choiceIt, ++rowGroupIt) {
1846 if (*rowGroupIt < *(rowGroupIt + 1)) {
1848 currentValue = *summandIt;
1852 for (
auto elementIte = this->
begin() + *(rowIt + 1); elementIt != elementIte; ++elementIt) {
1853 currentValue += elementIt->getValue() * vector[elementIt->getColumn()];
1858 if (*choiceIt == 0) {
1859 oldSelectedChoiceValue = currentValue;
1866 for (; currentRow < *(rowGroupIt + 1); ++rowIt, ++currentRow) {
1868 for (
auto elementIte = this->
begin() + *(rowIt + 1); elementIt != elementIte; ++elementIt) {
1869 newValue += elementIt->getValue() * vector[elementIt->getColumn()];
1872 if (choices && currentRow == *choiceIt + *rowGroupIt) {
1873 oldSelectedChoiceValue = newValue;
1876 if (compare(newValue, currentValue)) {
1877 currentValue = newValue;
1879 selectedChoice = currentRow - *rowGroupIt;
1888 *resultIt = currentValue;
1889 if (choices && compare(currentValue, oldSelectedChoiceValue)) {
1890 *choiceIt = selectedChoice;
1898 std::vector<storm::RationalFunction>
const& vector,
1899 std::vector<storm::RationalFunction>
const* b,
1900 std::vector<storm::RationalFunction>& result, std::vector<uint64_t>* choices)
const {
1901 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"This operation is not supported.");
1904template<
typename ValueType>
1906 std::vector<ValueType>
const& vector, std::vector<ValueType>
const* summand,
1907 std::vector<ValueType>& result, std::vector<uint64_t>* choices)
const {
1908 if (dir == storm::OptimizationDirection::Minimize) {
1915template<
typename ValueType>
1916template<
typename Compare>
1918 std::vector<ValueType>
const* summand, std::vector<ValueType>& result,
1919 std::vector<uint64_t>* choices)
const {
1921 auto elementIt = this->
end() - 1;
1922 auto rowGroupIt = rowGroupIndices.end() - 2;
1923 auto rowIt = rowIndications.end() - 2;
1924 typename std::vector<ValueType>::const_iterator summandIt;
1926 summandIt = summand->end() - 1;
1928 typename std::vector<uint64_t>::iterator choiceIt;
1930 choiceIt = choices->end() - 1;
1934 ValueType oldSelectedChoiceValue;
1935 uint64_t selectedChoice;
1938 for (
auto resultIt = result.end() - 1, resultIte = result.begin() - 1; resultIt != resultIte; --resultIt, --choiceIt, --rowGroupIt) {
1942 if (*rowGroupIt < *(rowGroupIt + 1)) {
1944 currentValue = *summandIt;
1948 for (
auto elementIte = this->
begin() + *rowIt - 1; elementIt != elementIte; --elementIt) {
1949 currentValue += elementIt->getValue() * vector[elementIt->getColumn()];
1952 selectedChoice = currentRow - *rowGroupIt;
1953 if (*choiceIt == selectedChoice) {
1954 oldSelectedChoiceValue = currentValue;
1960 for (uint64_t
i = *rowGroupIt + 1,
end = *(rowGroupIt + 1);
i <
end; --rowIt, --currentRow, ++
i, --summandIt) {
1962 for (
auto elementIte = this->
begin() + *rowIt - 1; elementIt != elementIte; --elementIt) {
1963 newValue += elementIt->getValue() * vector[elementIt->getColumn()];
1966 if (choices && currentRow == *choiceIt + *rowGroupIt) {
1967 oldSelectedChoiceValue = newValue;
1970 if (compare(newValue, currentValue)) {
1971 currentValue = newValue;
1973 selectedChoice = currentRow - *rowGroupIt;
1979 *resultIt = currentValue;
1980 if (choices && compare(currentValue, oldSelectedChoiceValue)) {
1981 *choiceIt = selectedChoice;
1989 std::vector<storm::RationalFunction>
const& vector,
1990 std::vector<storm::RationalFunction>
const* b,
1991 std::vector<storm::RationalFunction>& result, std::vector<uint64_t>* choices)
const {
1992 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"This operation is not supported.");
1995template<
typename ValueType>
1997 std::vector<ValueType>
const& vector, std::vector<ValueType>
const* summand, std::vector<ValueType>& result,
1998 std::vector<uint64_t>* choices)
const {
2000 std::vector<ValueType>* target;
2001 std::vector<ValueType> temporary;
2002 if (&vector == &result) {
2003 STORM_LOG_WARN(
"Vectors are aliased but are not allowed to be. Using temporary, which is potentially slow.");
2004 temporary = std::vector<ValueType>(vector.size());
2005 target = &temporary;
2012 if (target == &temporary) {
2013 std::swap(temporary, result);
2017template<
typename ValueType>
2021 std::vector<index_type>::const_iterator rowIterator = rowIndications.begin();
2022 std::vector<index_type>::const_iterator rowIteratorEnd = rowIndications.end();
2025 for (; rowIterator != rowIteratorEnd - 1; ++rowIterator) {
2026 for (ite = this->
begin() + *(rowIterator + 1); it != ite; ++it) {
2027 result[it->getColumn()] += it->getValue() * vector[currentRow];
2033template<
typename ValueType>
2035 STORM_LOG_ASSERT(factors.size() == this->getRowCount(),
"Can not scale rows: Number of rows and number of scaling factors do not match.");
2037 for (
auto const& factor : factors) {
2038 for (
auto& entry :
getRow(row)) {
2039 entry.setValue(entry.getValue() * factor);
2045template<
typename ValueType>
2047 STORM_LOG_ASSERT(divisors.size() == this->getRowCount(),
"Can not divide rows: Number of rows and number of divisors do not match.");
2049 for (
auto const& divisor : divisors) {
2051 for (
auto& entry :
getRow(row)) {
2052 entry.setValue(entry.getValue() / divisor);
2060 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"This operation is not supported.");
2063template<
typename ValueType>
2065 return const_rows(this->columnsAndValues.begin() + this->rowIndications[startRow], this->rowIndications[endRow] - this->rowIndications[startRow]);
2068template<
typename ValueType>
2070 return rows(this->columnsAndValues.begin() + this->rowIndications[startRow], this->rowIndications[endRow] - this->rowIndications[startRow]);
2073template<
typename ValueType>
2078template<
typename ValueType>
2083template<
typename ValueType>
2091 return getRow(rowGroup + offset);
2095template<
typename ValueType>
2103 return getRow(rowGroup + offset);
2107template<
typename ValueType>
2113 return getRows(rowGroup, rowGroup + 1);
2117template<
typename ValueType>
2123 return getRows(rowGroup, rowGroup + 1);
2127template<
typename ValueType>
2130 return this->columnsAndValues.begin() + this->rowIndications[row];
2133template<
typename ValueType>
2136 return this->columnsAndValues.begin() + this->rowIndications[row];
2139template<
typename ValueType>
2141 return this->columnsAndValues.begin();
2144template<
typename ValueType>
2146 return this->columnsAndValues.begin();
2149template<
typename ValueType>
2152 return this->columnsAndValues.begin() + this->rowIndications[row + 1];
2155template<
typename ValueType>
2158 return this->columnsAndValues.begin() + this->rowIndications[row + 1];
2161template<
typename ValueType>
2163 return this->columnsAndValues.begin() + this->rowIndications[rowCount];
2166template<
typename ValueType>
2168 return this->columnsAndValues.begin() + this->rowIndications[rowCount];
2171template<
typename ValueType>
2175 sum += it->getValue();
2180template<
typename ValueType>
2183 for (
auto const& entry : *
this) {
2188 return nonConstEntries;
2191template<
typename ValueType>
2194 for (
index_type rowGroup = 0; rowGroup < this->getRowGroupCount(); ++rowGroup) {
2195 for (
auto const& entry : this->
getRowGroup(rowGroup)) {
2197 ++nonConstRowGroups;
2202 return nonConstRowGroups;
2205template<
typename ValueType>
2212 auto toBaseType = [](ValueType
const& value) {
2213 if constexpr (std::is_same_v<ValueType, BaseType>) {
2224 auto isContained = [&toBaseType](ValueType
const& value, BaseType
const& lower, BaseType
const& upper) {
2229 return value.lower() <= upper && value.upper() >= lower;
2230 }
else if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
2233 auto const constValue = toBaseType(value);
2234 return constValue <= upper && constValue >= lower;
2240 return value <= upper && value >= lower;
2244 auto toString = [](ValueType
const& value) {
2245 std::stringstream s;
2250 for (
index_type row = 0; row < this->rowCount; ++row) {
2252 for (
auto const& entry :
getRow(row)) {
2253 if (!isContained(entry.getValue(), zeroMinusTolerance, onePlusTolerance)) {
2255 *reason =
"Entry in row " + std::to_string(row) +
" is not a probability: " +
toString(entry.getValue());
2259 rowSum += entry.getValue();
2261 if (!isContained(rowSum, oneMinusTolerance, onePlusTolerance)) {
2272template<
typename ValueType>
2274 for (
auto const& entry : *
this) {
2282template<
typename ValueType>
2288 for (
auto const& entry : this->
getRow(row)) {
2289 sumOfUpperBounds += entry.getValue().upper();
2291 for (
auto const& entry : this->
getRow(row)) {
2293 BaseType
const tightenedLower =
2306template<
typename ValueType>
2307template<
typename OtherValueType>
2312 (!this->hasTrivialRowGrouping() && this->getRowGroupIndices() != matrix.
getRowGroupIndices())) {
2318 auto it2 = matrix.
begin(row);
2319 auto ite2 = matrix.
end(row);
2322 while (it2 != ite2 && it2->getColumn() < it1->getColumn()) {
2325 if (it2 == ite2 || it1->getColumn() != it2->getColumn()) {
2333template<
typename ValueType>
2341 for (uint64_t row = 0; row < this->
getRowCount(); ++row) {
2342 bool rowHasEntry =
false;
2343 for (
auto const& entry : this->
getRow(row)) {
2344 if (entry.getColumn() == row) {
2362template<
typename ValueType>
2364 std::string result =
2373template<
typename ValueType>
2384 out <<
"\t---- group " << group <<
"/" << (matrix.
getRowGroupCount() - 1) <<
" ---- \n";
2392 out <<
i <<
"\t(\t";
2394 while (currentRealIndex < matrix.columnCount) {
2395 if (nextIndex < matrix.rowIndications[
i + 1] && currentRealIndex == matrix.columnsAndValues[nextIndex].getColumn()) {
2396 out << matrix.columnsAndValues[nextIndex].getValue() <<
"\t";
2403 out <<
"\t)\t" <<
i <<
'\n';
2417template<
typename ValueType>
2428 while (currentRealIndex < this->columnCount) {
2429 if (nextIndex < this->rowIndications[
i + 1] && currentRealIndex == this->columnsAndValues[nextIndex].getColumn()) {
2430 out << this->columnsAndValues[nextIndex].getValue() <<
" ";
2442template<
typename ValueType>
2444 std::size_t result = 0;
2449 boost::hash_combine(result, boost::hash_range(columnsAndValues.begin(), columnsAndValues.end()));
2450 boost::hash_combine(result, boost::hash_range(rowIndications.begin(), rowIndications.end()));
2452 boost::hash_combine(result, boost::hash_range(rowGroupIndices.get().begin(), rowGroupIndices.get().end()));
Helper class that optionally holds a reference to an object of type T.
void reset()
Unsets the reference.
A bit vector that is internally represented as a vector of 64-bit values.
void setMultiple(uint64_t bitIndex, uint64_t nrOfBits, bool newValue=true)
Sets multiple bits to the given value.
uint64_t getNextSetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
uint64_t getNextUnsetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to false in the bit vector.
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
size_t size() const
Retrieves the number of bits this bit vector can store.
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
MatrixEntry(index_type column, value_type value)
Constructs a matrix entry with the given column and value.
MatrixEntry operator*(value_type factor) const
Multiplies the entry with the given factor and returns the result.
value_type const & getValue() const
Retrieves the value of the matrix entry.
std::pair< index_type, value_type > const & getColumnValuePair() const
Retrieves a pair of column and value that characterizes this entry.
void setColumn(index_type const &column)
Sets the column of the current entry.
index_type const & getColumn() const
Retrieves the column of the matrix entry.
bool operator!=(MatrixEntry const &other) const
void setValue(value_type const &value)
Sets the value of the entry in the matrix.
bool operator==(MatrixEntry const &other) const
This class represents a number of consecutive rows of the matrix.
const_rows(const_iterator begin, index_type entryCount)
Constructs an object that represents the rows defined by the value of the first entry,...
const_iterator end() const
Retrieves an iterator that points past the last entry of the rows.
index_type getNumberOfEntries() const
Retrieves the number of entries in the rows.
const_iterator begin() const
Retrieves an iterator that points to the beginning of the rows.
This class represents a number of consecutive rows of the matrix.
iterator end()
Retrieves an iterator that points past the last entry of the rows.
iterator begin()
Retrieves an iterator that points to the beginning of the rows.
index_type getNumberOfEntries() const
Retrieves the number of entries in the rows.
rows(iterator begin, index_type entryCount)
Constructs an object that represents the rows defined by the value of the first entry,...
A class that can be used to build a sparse matrix by adding value by value.
index_type getCurrentRowGroupCount() const
Retrieves the current row group count.
AccumulationMode
Specifies how values that are added for the same row and column are combined.
index_type getLastRow() const
Retrieves the most recently used row.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
void replaceColumns(std::vector< index_type > const &replacements, index_type offset)
Replaces all columns with id > offset according to replacements.
SparseMatrixIndexType index_type
SparseMatrixBuilder(index_type rows=0, index_type columns=0, index_type entries=0, bool forceDimensions=true, bool hasCustomRowGrouping=false, index_type rowGroups=0)
Constructs a sparse matrix builder producing a matrix with the given number of rows,...
void newRowGroup(index_type startingRow)
Starts a new row group in the matrix.
index_type getLastColumn() const
Retrieves the most recently used row.
void addDiagonalEntry(index_type row, ValueType const &value)
Makes sure that a diagonal entry will be inserted at the given row.
void setAccumulationMode(AccumulationMode mode)
Sets how values that are added for the same row and column are combined.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
AccumulationMode getAccumulationMode() const
Retrieves how values that are added for the same row and column are combined.
A class that holds a possibly non-square matrix in the compressed row storage format.
void divideRowsInPlace(std::vector< value_type > const &divisors)
Divides each row of the matrix, i.e., divides each element in row i with divisors[i].
ResultValueType getPointwiseProductRowSum(storm::storage::SparseMatrix< OtherValueType > const &otherMatrix, index_type const &row) const
Performs a pointwise multiplication of the entries in the given row of this matrix and the entries of...
void convertToEquationSystem()
Transforms the matrix into an equation system.
SparseMatrix()
Constructs an empty sparse matrix.
void swapRows(index_type const &row1, index_type const &row2)
Swaps the two rows.
bool operator==(SparseMatrix< value_type > const &other) const
Determines whether the current and the given matrix are semantically equal.
const_rows getRow(index_type row) const
Returns an object representing the given row.
index_type getSizeOfLargestRowGroup() const
Returns the size of the largest row group of the matrix.
SparseMatrix selectRowsFromRowGroups(std::vector< index_type > const &rowGroupToRowIndexMapping, bool insertDiagonalEntries=true) const
Selects exactly one row from each row group of this matrix and returns the resulting matrix.
void multiplyWithVectorForward(std::vector< value_type > const &vector, std::vector< value_type > &result, std::vector< value_type > const *summand=nullptr) const
index_type getEntryCount() const
Returns the number of entries in the matrix.
bool isProbabilistic(ValueType const &tolerance, storm::OptionalRef< std::string > reason={}) const
Checks for each row whether (i) each entry is between zero and one and (ii) all entries sum to one.
const_rows getRows(index_type startRow, index_type endRow) const
index_type getNonconstantEntryCount() const
Returns the number of non-constant entries.
void multiplyVectorWithMatrix(std::vector< value_type > const &vector, std::vector< value_type > &result) const
Multiplies the vector to the matrix from the left and writes the result to the given result vector.
void multiplyAndReduceBackward(storm::solver::OptimizationDirection const &dir, std::vector< uint64_t > const &rowGroupIndices, std::vector< ValueType > const &vector, std::vector< ValueType > const *b, std::vector< ValueType > &result, std::vector< uint64_t > *choices) const
index_type getNumRowsInRowGroups(storm::storage::BitVector const &groupConstraint) const
Returns the total number of rows that are in one of the specified row groups.
void makeRowsAbsorbing(storm::storage::BitVector const &rows, bool dropZeroEntries=false)
This function makes the given rows absorbing.
ValueType multiplyRowWithVector(index_type row, std::vector< ValueType > const &vector, ValueType offset=storm::utility::zero< ValueType >()) const
Multiplies a single row of the matrix with the given vector and adds the given offset to the result.
void multiplyWithVector(std::vector< value_type > const &vector, std::vector< value_type > &result, std::vector< value_type > const *summand=nullptr) const
Multiplies the matrix with the given vector and writes the result to the given result vector.
void updateNonzeroEntryCount() const
void multiplyWithVectorBackward(std::vector< value_type > const &vector, std::vector< value_type > &result, std::vector< value_type > const *summand=nullptr) const
SparseMatrix getSubmatrix(bool useGroups, storm::storage::BitVector const &rowConstraint, storm::storage::BitVector const &columnConstraint, bool insertDiagonalEntries=false, storm::storage::BitVector const &makeZeroColumns=storm::storage::BitVector()) const
Creates a submatrix of the current matrix by dropping all rows and columns whose bits are not set to ...
void performWalkerChaeStep(std::vector< ValueType > const &x, std::vector< ValueType > const &columnSums, std::vector< ValueType > const &b, std::vector< ValueType > const &ax, std::vector< ValueType > &result) const
Performs one step of the Walker-Chae technique.
void updateDimensions() const
Recomputes the number of columns and the number of non-zero entries.
const_iterator begin(index_type row) const
Retrieves an iterator that points to the beginning of the given row.
void printAsMatlabMatrix(std::ostream &out) const
Prints the matrix in a dense format, as also used by e.g.
void performSuccessiveOverRelaxationStep(ValueType omega, std::vector< ValueType > &x, std::vector< ValueType > const &b) const
Performs one step of the successive over-relaxation technique.
std::vector< value_type > getConstrainedRowSumVector(storm::storage::BitVector const &rowConstraint, storm::storage::BitVector const &columnConstraint) const
Computes a vector whose i-th entry is the sum of the entries in the i-th selected row where only thos...
BitVector duplicateRowsInRowgroups() const
Finds duplicate rows in a rowgroup.
bool compareRows(index_type i1, index_type i2) const
Compares two rows.
const_rows getRowGroup(index_type rowGroup) const
Returns an object representing the given row group.
SparseMatrix permuteRows(std::vector< index_type > const &inversePermutation) const
Permute rows of the matrix according to the vector.
void setRowGroupIndices(std::vector< index_type > const &newRowGroupIndices)
Sets the row grouping to the given one.
void negateAllNonDiagonalEntries()
Negates (w.r.t.
const_iterator begin() const
Retrieves an iterator that points to the beginning of the first row of the matrix.
std::vector< index_type > swapRowGroupIndices(std::vector< index_type > &&newRowGrouping)
Swaps the grouping of rows of this matrix.
SparseMatrix restrictRows(storm::storage::BitVector const &rowsToKeep, bool allowEmptyRowGroups=false) const
Restrict rows in grouped rows matrix.
std::vector< ValueType > getRowSumVector() const
Sums the entries in all rows.
SparseMatrix< ValueType > transposeSelectedRowsFromRowGroups(std::vector< uint64_t > const &rowGroupChoices, bool keepZeros=false) const
Transposes the matrix w.r.t.
std::vector< index_type > const & getRowIndices() const
Returns the entry indices within the given row.
index_type getRowEntryCount(index_type const row) const
void multiplyAndReduceForward(storm::solver::OptimizationDirection const &dir, std::vector< uint64_t > const &rowGroupIndices, std::vector< ValueType > const &vector, std::vector< ValueType > const *b, std::vector< ValueType > &result, std::vector< uint64_t > *choices) const
value_type getRowSum(index_type row) const
const_iterator end(index_type row) const
Retrieves an iterator that points past the end of the given row.
SparseMatrix permuteRowGroupsAndColumns(std::vector< index_type > const &inverseRowGroupPermutation, std::vector< index_type > const &columnPermutation) const
Permutes row groups and columns of the matrix according to the given permutations.
void multiplyAndReduce(storm::solver::OptimizationDirection const &dir, std::vector< uint64_t > const &rowGroupIndices, std::vector< ValueType > const &vector, std::vector< ValueType > const *summand, std::vector< ValueType > &result, std::vector< uint64_t > *choices) const
Multiplies the matrix with the given vector, reduces it according to the given direction and and writ...
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
void dropZeroEntries()
Removes all zero entries from this.
bool isSubmatrixOf(SparseMatrix< OtherValueType > const &matrix) const
Checks if the current matrix is a submatrix of the given matrix, where a matrix A is called a submatr...
SparseMatrix< value_type > & operator=(SparseMatrix< value_type > const &other)
Assigns the contents of the given matrix to the current one by deep-copying its contents.
void makeRowGroupsAbsorbing(storm::storage::BitVector const &rowGroupConstraint, bool dropZeroEntries=false)
This function makes the groups of rows given by the bit vector absorbing.
std::string getDimensionsAsString() const
Returns a string describing the dimensions of the matrix.
index_type getColumnCount() const
Returns the number of columns of the matrix.
std::pair< storm::storage::SparseMatrix< value_type >, std::vector< value_type > > getJacobiDecomposition() const
Calculates the Jacobi decomposition of this sparse matrix.
void makeRowDirac(index_type row, index_type column, bool dropZeroEntries=false)
This function makes the given row Dirac.
std::vector< MatrixEntry< index_type, value_type > >::const_iterator const_iterator
value_type getConstrainedRowSum(index_type row, storm::storage::BitVector const &columns) const
Sums the entries in the given row and columns.
std::vector< ResultValueType > getPointwiseProductRowSumVector(storm::storage::SparseMatrix< OtherValueType > const &otherMatrix) const
Performs a pointwise matrix multiplication of the matrix with the given matrix and returns a vector c...
void deleteDiagonalEntries(bool dropZeroEntries=false)
Sets all diagonal elements to zero.
bool hasTrivialRowGrouping() const
void invertDiagonal()
Inverts all entries on the diagonal, i.e.
storm::storage::BitVector getRowGroupFilter(storm::storage::BitVector const &rowConstraint, bool setIfForAllRowsInGroup) const
Returns the indices of all row groups selected by the row constraints.
void makeRowGroupingTrivial()
Makes the row grouping of this matrix trivial.
SparseMatrix selectRowsFromRowIndexSequence(std::vector< index_type > const &rowIndexSequence, bool insertDiagonalEntries=true) const
Selects the rows that are given by the sequence of row indices, allowing to select rows arbitrarily o...
bool isProbabilisticGraphPreserving() const
Checks whether the support graph of the matrix is preserved by every way of resolving the uncertainty...
std::size_t hash() const
Calculates a hash value over all values contained in the matrix.
std::vector< index_type > const & getRowGroupIndices() const
std::vector< MatrixEntry< index_type, value_type > >::iterator iterator
bool isIdentityMatrix() const
index_type getRowGroupSize(index_type group) const
Returns the size of the given row group.
std::vector< value_type > getConstrainedRowGroupSumVector(storm::storage::BitVector const &rowGroupConstraint, storm::storage::BitVector const &columnConstraint) const
Computes a vector whose entries represent the sums of selected columns for all rows in selected row g...
storm::storage::SparseMatrix< value_type > transpose(bool joinGroups=false, bool keepZeros=false) const
Transposes the matrix.
bool hasOnlyPositiveEntries() const
Checks whether each present entry is strictly positive (omitted entries are not considered).
index_type getRowCount() const
Returns the number of rows of the matrix.
SparseMatrixIndexType index_type
index_type getNonzeroEntryCount() const
Returns the cached number of nonzero entries in the matrix.
const_iterator end() const
Retrieves an iterator that points past the end of the last row of the matrix.
index_type getNonconstantRowGroupCount() const
Returns the number of rowGroups that contain a non-constant value.
void scaleRowsInPlace(std::vector< value_type > const &factors)
Scales each row of the matrix, i.e., multiplies each element in row i with factors[i].
index_type getRowGroupEntryCount(index_type const group) const
Returns the number of entries in the given row group of the matrix.
storm::storage::BitVector getRowFilter(storm::storage::BitVector const &groupConstraint) const
Returns a bitvector representing the set of rows, with all indices set that correspond to one of the ...
SparseMatrix filterEntries(storm::storage::BitVector const &rowFilter) const
Returns a copy of this matrix that only considers entries in the selected rows.
Helper class to extract submatrices of a fixed sparse matrix.
SparseMatrix< ValueType > getByRowGroupConstraint(RowConstraintType const &rowGroupConstraint, ColumnConstraintType const &columnConstraint, bool const insertDiagonalElement, storm::OptionalRef< storm::storage::BitVector const > makeZeroColumns=storm::NullRef) const
Creates the submatrix that keeps only the given row groups and columns The row group structure of the...
SparseMatrix< ValueType > getByRowConstraint(RowConstraintType const &rowConstraint, ColumnConstraintType const &columnConstraint, bool const insertDiagonalElement, storm::OptionalRef< storm::storage::BitVector const > makeZeroColumns=storm::NullRef) const
Creates the submatrix that keeps only the given rows and columns If the matrix has a non-trivial row ...
A value of ValueType extended with the two infinities -infinity and +infinity.
ValueType const & getFinite() const
#define STORM_LOG_WARN(message)
#define STORM_LOG_TRACE(message)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
std::string toString(PomdpMemoryPattern const &pattern)
void print(std::vector< typename SparseMatrix< ValueType >::index_type > const &rowGroupIndices, std::vector< MatrixEntry< typename SparseMatrix< ValueType >::index_type, typename SparseMatrix< ValueType >::value_type > > const &columnsAndValues, std::vector< typename SparseMatrix< ValueType >::index_type > const &rowIndications)
std::ostream & operator<<(std::ostream &out, ParameterRegion< ParametricType > const ®ion)
bool isValidPermutation(std::vector< index_type > const &permutation)
Returns true if the given vector is a permutation of the numbers 0, 1, ..., n-1 for n = permutation....
std::vector< T > buildVectorForRange(T min, T max)
Constructs a vector [min, min+1, ...., max-1].
bool isPositive(ValueType const &a)
bool isOne(ValueType const &a)
bool isConstant(ValueType const &)
bool isZero(ValueType const &a)
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
carl::Interval< storm::RationalNumber > RationalInterval
carl::Interval< double > Interval
Interval type.
solver::OptimizationDirection OptimizationDirection
constexpr bool IsIntervalType
Helper to check if a type is an interval.
carl::RationalFunction< Polynomial, true > RationalFunction
typename detail::IntervalMetaProgrammingHelper< ValueType >::BaseType IntervalBaseType
Helper to access the type in which interval boundaries are stored.
static const bool IsExact