1#ifndef STORM_UTILITY_VECTOR_H_
2#define STORM_UTILITY_VECTOR_H_
11#include <boost/optional.hpp>
24template<
typename ValueType>
26 size_t operator()(std::vector<ValueType>
const& vec)
const {
27 std::hash<ValueType> hasher;
29 for (ValueType
const& element : vec) {
30 seed ^= hasher(element) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
50 std::size_t position = std::find(vector.begin(), vector.end(), element) - vector.begin();
51 if (position == vector.size()) {
52 vector.emplace_back(std::move(element));
59 T
const& negativeValue = storm::utility::zero<T>()) {
61 vec.resize(positions.
size(), positiveValue);
63 while (index < positions.
size()) {
64 vec[index] = negativeValue;
68 vec.resize(positions.
size(), negativeValue);
69 for (uint64_t index : positions) {
70 vec[index] = positiveValue;
84 STORM_LOG_ASSERT(positions.
size() <= vector.size(),
"We cannot set positions that have not been initialized");
86 <<
") exceeds the size of the input vector ("
87 << values.size() <<
").");
88 uint_fast64_t oldPosition = 0;
89 for (
auto position : positions) {
90 vector[position] = values[oldPosition++];
104 STORM_LOG_ASSERT(positions.
size() <= vector.size(),
"We cannot set positions that have not been initialized");
105 for (
auto position : positions) {
106 vector[position] = value;
113 for (uint64_t i = 0; i < vec.size(); ++i) {
125template<
class OutputIterator,
class Size,
class Assignable>
126void iota_n(OutputIterator first, Size n, Assignable value) {
127 std::generate_n(first, n, [&value]() {
return value++; });
139 iota_n(std::back_inserter(v), diff,
min);
150 std::vector<uint_fast64_t> res = buildVectorForRange<uint_fast64_t>(0, v.size());
151 std::sort(res.begin(), res.end(), [&v](uint_fast64_t index1, uint_fast64_t index2) { return v[index1] > v[index2]; });
164 auto indexIt = sortedIndices.begin();
165 T
const* previous = &v[*indexIt];
166 for (++indexIt; indexIt != sortedIndices.end(); ++indexIt) {
167 T
const& current = v[*indexIt];
168 if (current == *previous) {
176template<
typename T,
typename Comparator>
177bool compareElementWise(std::vector<T>
const& left, std::vector<T>
const& right, Comparator comp = std::less<T>()) {
178 STORM_LOG_ASSERT(left.size() == right.size(),
"Expected that vectors for comparison have equal size");
179 return std::equal(left.begin(), left.end(), right.begin(), comp);
191 <<
") exceeds the size of the target vector ("
192 << vector.size() <<
").");
194 "Size mismatch of the positions vector (" << positions.
size() <<
") and the values vector (" << values.size() <<
").");
195 auto targetIt = vector.begin();
196 for (
auto position : positions) {
197 *targetIt = values[position];
212 std::vector<T>
const& values) {
213 auto targetIt = vector.begin();
214 for (
auto position : positions) {
215 for (uint_fast64_t i = rowGrouping[position]; i < rowGrouping[position + 1]; ++i, ++targetIt) {
216 *targetIt = values[i];
231void selectVectorValues(std::vector<T>& vector, std::vector<uint_fast64_t>
const& rowGroupToRowIndexMapping, std::vector<uint_fast64_t>
const& rowGrouping,
232 std::vector<T>
const& values) {
233 auto targetIt = vector.begin();
234 for (uint_fast64_t i = 0; i < vector.size(); ++i, ++targetIt) {
235 *targetIt = values[rowGrouping[i] + rowGroupToRowIndexMapping[i]];
247void selectVectorValues(std::vector<T>& vector, std::vector<uint_fast64_t>
const& indexSequence, std::vector<T>
const& values) {
249 "The number of selected positions (" << indexSequence.size() <<
") exceeds the size of the target vector (" << vector.size() <<
").");
251 for (uint_fast64_t vectorIndex = 0; vectorIndex < vector.size(); ++vectorIndex) {
252 vector[vectorIndex] = values[indexSequence[vectorIndex]];
267 std::vector<T>
const& values) {
268 auto targetIt = vector.begin();
269 for (
auto position : positions) {
270 for (uint_fast64_t i = rowGrouping[position]; i < rowGrouping[position + 1]; ++i, ++targetIt) {
271 *targetIt = values[position];
283 for (
auto& element : vector) {
284 element = storm::utility::one<T>() - element;
290 std::vector<uint_fast64_t>
const& rowGroupIndices) {
291 auto targetIt = target.begin();
292 for (
auto group :
filter) {
293 auto it = source.cbegin() + rowGroupIndices[group];
294 auto ite = source.cbegin() + rowGroupIndices[group + 1];
295 for (; it != ite; ++targetIt, ++it) {
310void addVectorToGroupedVector(std::vector<T>& target, std::vector<T>
const& source, std::vector<uint_fast64_t>
const& rowGroupIndices) {
311 auto targetIt = target.begin();
312 auto sourceIt = source.cbegin();
313 auto sourceIte = source.cend();
314 auto rowGroupIt = rowGroupIndices.cbegin();
316 for (; sourceIt != sourceIte; ++sourceIt) {
317 uint_fast64_t current = *rowGroupIt;
319 uint_fast64_t next = *rowGroupIt;
320 for (; current < next; ++targetIt, ++current) {
321 *targetIt += *sourceIt;
336 std::vector<uint_fast64_t>
const& rowGroupIndices) {
337 auto targetIt = target.begin();
338 for (
auto group :
filter) {
339 uint_fast64_t current = rowGroupIndices[group];
340 uint_fast64_t next = rowGroupIndices[group + 1];
341 for (; current < next; ++current, ++targetIt) {
342 *targetIt += source[group];
355template<
class InValueType1,
class InValueType2,
class OutValueType,
class Operation>
356void applyPointwiseTernary(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand, std::vector<OutValueType>& target,
357 Operation f = Operation()) {
358 auto firstIt = firstOperand.begin();
359 auto firstIte = firstOperand.end();
360 auto secondIt = secondOperand.begin();
361 auto targetIt = target.begin();
362 while (firstIt != firstIte) {
363 *targetIt = f(*firstIt, *secondIt, *targetIt);
370#ifdef STORM_HAVE_INTELTBB
371template<
class InValueType1,
class InValueType2,
class OutValueType,
class Operation>
372void applyPointwiseTernaryParallel(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand,
373 std::vector<OutValueType>& target, Operation f = Operation()) {
374 tbb::parallel_for(tbb::blocked_range<uint_fast64_t>(0, target.size()), [&](tbb::blocked_range<uint_fast64_t>
const& range) {
375 auto firstIt = firstOperand.begin() + range.begin();
376 auto firstIte = firstOperand.begin() + range.end();
377 auto secondIt = secondOperand.begin() + range.begin();
378 auto targetIt = target.begin() + range.begin();
379 while (firstIt != firstIte) {
380 *targetIt = f(*firstIt, *secondIt, *targetIt);
397template<
class InValueType1,
class InValueType2,
class OutValueType,
class Operation>
398void applyPointwise(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand, std::vector<OutValueType>& target,
399 Operation f = Operation()) {
400 std::transform(firstOperand.begin(), firstOperand.end(), secondOperand.begin(), target.begin(), f);
403#ifdef STORM_HAVE_INTELTBB
404template<
class InValueType1,
class InValueType2,
class OutValueType,
class Operation>
405void applyPointwiseParallel(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand, std::vector<OutValueType>& target,
406 Operation f = Operation()) {
407 tbb::parallel_for(tbb::blocked_range<uint_fast64_t>(0, target.size()), [&](tbb::blocked_range<uint_fast64_t>
const& range) {
408 std::transform(firstOperand.begin() + range.begin(), firstOperand.begin() + range.end(), secondOperand.begin() + range.begin(),
409 target.begin() + range.begin(), f);
421template<
class InValueType,
class OutValueType,
class Operation>
422void applyPointwise(std::vector<InValueType>
const& operand, std::vector<OutValueType>& target, Operation f = Operation()) {
423 std::transform(operand.begin(), operand.end(), target.begin(), f);
426#ifdef STORM_HAVE_INTELTBB
427template<
class InValueType,
class OutValueType,
class Operation>
428void applyPointwiseParallel(std::vector<InValueType>
const& operand, std::vector<OutValueType>& target, Operation f = Operation()) {
429 tbb::parallel_for(tbb::blocked_range<uint_fast64_t>(0, target.size()), [&](tbb::blocked_range<uint_fast64_t>
const& range) {
430 std::transform(operand.begin() + range.begin(), operand.begin() + range.end(), target.begin() + range.begin(), f);
442template<
class InValueType1,
class InValueType2,
class OutValueType>
443void addVectors(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand, std::vector<OutValueType>& target) {
444 applyPointwise<InValueType1, InValueType2, OutValueType, std::plus<>>(firstOperand, secondOperand, target);
454template<
class InValueType1,
class InValueType2,
class OutValueType>
455void subtractVectors(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand, std::vector<OutValueType>& target) {
456 applyPointwise<InValueType1, InValueType2, OutValueType, std::minus<>>(firstOperand, secondOperand, target);
466template<
class InValueType1,
class InValueType2,
class OutValueType>
468 std::vector<OutValueType>& target) {
469 applyPointwise<InValueType1, InValueType2, OutValueType, std::multiplies<>>(firstOperand, secondOperand, target);
479template<
class InValueType1,
class InValueType2,
class OutValueType>
480void divideVectorsPointwise(std::vector<InValueType1>
const& firstOperand, std::vector<InValueType2>
const& secondOperand, std::vector<OutValueType>& target) {
481 applyPointwise<InValueType1, InValueType2, OutValueType, std::divides<>>(firstOperand, secondOperand, target);
490template<
class ValueType1,
class ValueType2>
492 applyPointwise<ValueType1, ValueType1>(target, target, [&](ValueType1
const& argument) -> ValueType1 {
return argument * factor; });
503template<
class InValueType1,
class InValueType2,
class InValueType3>
504void addScaledVector(std::vector<InValueType1>& firstOperand, std::vector<InValueType2>
const& secondOperand, InValueType3
const& factor) {
505 applyPointwise<InValueType1, InValueType2, InValueType1>(
506 firstOperand, secondOperand, firstOperand, [&](InValueType1
const& val1, InValueType2
const& val2) -> InValueType1 {
return val1 + (factor * val2); });
517T
dotProduct(std::vector<T>
const& firstOperand, std::vector<T>
const& secondOperand) {
518 return std::inner_product(firstOperand.begin(), firstOperand.end(), secondOperand.begin(), storm::utility::zero<T>());
533 uint_fast64_t currentIndex = 0;
534 for (
auto const& value : values) {
535 if (function(value)) {
536 result.
set(currentIndex,
true);
553 return filter<T>(values, [](T
const& value) ->
bool {
return value > storm::utility::zero<T>(); });
564 return filter<T>(values, storm::utility::isZero<T>);
575 return filter<T>(values, storm::utility::isOne<T>);
586 return filter<T>(values, storm::utility::isInfinity<T>);
598 VT sum = storm::utility::zero<VT>();
619 VT current = values[*it];
622 for (; it != ite; ++it) {
623 current = std::max(values[*it], current);
642 VT current = values[*it];
645 for (; it != ite; ++it) {
646 current = std::min(values[*it], current);
651#ifdef STORM_HAVE_INTELTBB
652template<
class T,
class Filter>
653class TbbReduceVectorFunctor {
655 TbbReduceVectorFunctor(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping,
656 std::vector<uint_fast64_t>* choices, Filter
const& f)
657 : source(source), target(target), rowGrouping(rowGrouping), choices(choices), f(f) {
661 void operator()(tbb::blocked_range<uint64_t>
const& range)
const {
662 uint_fast64_t startRow = range.begin();
663 uint_fast64_t endRow = range.end();
665 typename std::vector<T>::iterator targetIt = target.begin() + startRow;
666 typename std::vector<T>::iterator targetIte = target.begin() + endRow;
667 typename std::vector<uint_fast64_t>::const_iterator rowGroupingIt = rowGrouping.begin() + startRow;
668 typename std::vector<T>::const_iterator sourceIt = source.begin() + *rowGroupingIt;
669 typename std::vector<T>::const_iterator sourceIte;
670 typename std::vector<uint_fast64_t>::iterator choiceIt;
672 choiceIt = choices->begin() + startRow;
676 T oldSelectedChoiceValue;
677 uint64_t selectedChoice;
679 uint64_t currentRow = 0;
680 for (; targetIt != targetIte; ++targetIt, ++rowGroupingIt, ++choiceIt) {
682 if (*rowGroupingIt != *(rowGroupingIt + 1)) {
683 *targetIt = *sourceIt;
687 if (*choiceIt == 0) {
688 oldSelectedChoiceValue = *targetIt;
695 for (sourceIte = source.begin() + *(rowGroupingIt + 1); sourceIt != sourceIte; ++sourceIt, ++currentRow) {
696 if (choices && *choiceIt + *rowGroupingIt == currentRow) {
697 oldSelectedChoiceValue = *sourceIt;
700 if (f(*sourceIt, *targetIt)) {
701 *targetIt = *sourceIt;
703 selectedChoice = std::distance(source.begin(), sourceIt) - *rowGroupingIt;
708 if (choices && f(*targetIt, oldSelectedChoiceValue)) {
709 *choiceIt = selectedChoice;
716 std::vector<T>
const& source;
717 std::vector<T>& target;
718 std::vector<uint_fast64_t>
const& rowGrouping;
719 std::vector<uint_fast64_t>* choices;
734template<
class T,
class Filter>
735void reduceVector(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping, std::vector<uint_fast64_t>* choices) {
737 typename std::vector<T>::iterator targetIt = target.begin();
738 typename std::vector<T>::iterator targetIte = target.end();
739 typename std::vector<uint_fast64_t>::const_iterator rowGroupingIt = rowGrouping.begin();
740 typename std::vector<T>::const_iterator sourceIt = source.begin();
741 typename std::vector<T>::const_iterator sourceIte;
742 typename std::vector<uint_fast64_t>::iterator choiceIt;
744 choiceIt = choices->begin();
748 T oldSelectedChoiceValue;
749 uint64_t selectedChoice;
751 uint64_t currentRow = 0;
752 for (; targetIt != targetIte; ++targetIt, ++rowGroupingIt, ++choiceIt) {
754 if (*rowGroupingIt != *(rowGroupingIt + 1)) {
755 *targetIt = *sourceIt;
759 if (*choiceIt == 0) {
760 oldSelectedChoiceValue = *targetIt;
767 for (sourceIte = source.begin() + *(rowGroupingIt + 1); sourceIt != sourceIte; ++sourceIt, ++currentRow) {
768 if (choices && *rowGroupingIt + *choiceIt == currentRow) {
769 oldSelectedChoiceValue = *sourceIt;
772 if (f(*sourceIt, *targetIt)) {
773 *targetIt = *sourceIt;
775 selectedChoice = std::distance(source.begin(), sourceIt) - *rowGroupingIt;
780 if (choices && f(*targetIt, oldSelectedChoiceValue)) {
781 *choiceIt = selectedChoice;
785 *targetIt = storm::utility::zero<T>();
790#ifdef STORM_HAVE_INTELTBB
791template<
class T,
class Filter>
792void reduceVectorParallel(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping,
793 std::vector<uint_fast64_t>* choices) {
794 tbb::parallel_for(tbb::blocked_range<uint64_t>(0, target.size()), TbbReduceVectorFunctor<T, Filter>(source, target, rowGrouping, choices, Filter()));
807void reduceVectorMin(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping,
808 std::vector<uint_fast64_t>* choices =
nullptr) {
809 reduceVector<T, storm::utility::ElementLess<T>>(source, target, rowGrouping, choices);
812#ifdef STORM_HAVE_INTELTBB
814void reduceVectorMinParallel(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping,
815 std::vector<uint_fast64_t>* choices =
nullptr) {
816 reduceVector<T, storm::utility::ElementLess<T>>(source, target, rowGrouping, choices);
829void reduceVectorMax(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping,
830 std::vector<uint_fast64_t>* choices =
nullptr) {
831 reduceVector<T, storm::utility::ElementGreater<T>>(source, target, rowGrouping, choices);
834#ifdef STORM_HAVE_INTELTBB
836void reduceVectorMaxParallel(std::vector<T>
const& source, std::vector<T>& target, std::vector<uint_fast64_t>
const& rowGrouping,
837 std::vector<uint_fast64_t>* choices =
nullptr) {
838 reduceVector<T, storm::utility::ElementGreater<T>>(source, target, rowGrouping, choices);
853 std::vector<uint_fast64_t>
const& rowGrouping, std::vector<uint_fast64_t>* choices =
nullptr) {
854 if (dir == storm::solver::OptimizationDirection::Minimize) {
861#ifdef STORM_HAVE_INTELTBB
864 std::vector<uint_fast64_t>
const& rowGrouping, std::vector<uint_fast64_t>* choices =
nullptr) {
865 if (dir == storm::solver::OptimizationDirection::Minimize) {
866 reduceVectorMinParallel(source, target, rowGrouping, choices);
868 reduceVectorMaxParallel(source, target, rowGrouping, choices);
886 if (storm::utility::isZero<T>(val1)) {
889 T relDiff = (val1 - val2) / val1;
894 T diff = val1 - val2;
904inline bool equalModuloPrecision(
double const& val1,
double const& val2,
double const& precision,
bool relativeError) {
909 double relDiff = (val1 - val2) / val1;
914 double diff = val1 - val2;
932bool equalModuloPrecision(std::vector<T>
const& vectorLeft, std::vector<T>
const& vectorRight, T
const& precision,
bool relativeError) {
933 STORM_LOG_ASSERT(vectorLeft.size() == vectorRight.size(),
"Lengths of vectors does not match.");
935 auto leftIt = vectorLeft.begin();
936 auto leftIte = vectorLeft.end();
937 auto rightIt = vectorRight.begin();
938 for (; leftIt != leftIte; ++leftIt, ++rightIt) {
960 bool relativeError) {
961 STORM_LOG_ASSERT(vectorLeft.size() == vectorRight.size(),
"Lengths of vectors does not match.");
963 for (
auto position : positions) {
964 if (!
equalModuloPrecision(vectorLeft[position], vectorRight[position], precision, relativeError)) {
984bool equalModuloPrecision(std::vector<T>
const& vectorLeft, std::vector<T>
const& vectorRight, std::vector<uint_fast64_t>
const& positions, T
const& precision,
985 bool relativeError) {
986 STORM_LOG_ASSERT(vectorLeft.size() == vectorRight.size(),
"Lengths of vectors does not match.");
988 for (uint_fast64_t position : positions) {
989 if (!
equalModuloPrecision(vectorLeft[position], vectorRight[position], precision, relativeError)) {
999 T res = storm::utility::zero<T>();
1000 for (
auto const& element : vector) {
1008 T maxDiff = storm::utility::zero<T>();
1009 auto leftIt = vectorLeft.begin();
1010 auto leftIte = vectorLeft.end();
1011 auto rightIt = vectorRight.begin();
1012 for (; leftIt != leftIte; ++leftIt, ++rightIt) {
1013 T diff = *leftIt - *rightIt;
1015 maxDiff = maxDiff < possDiff ? possDiff : maxDiff;
1024 T result = storm::utility::zero<T>();
1026 auto b1It = b1.begin();
1027 auto b1Ite = b1.end();
1028 auto b2It = b2.begin();
1030 for (; b1It != b1Ite; ++b1It, ++b2It) {
1031 result += storm::utility::pow<T>(*b1It - *b2It, 2);
1040template<
typename ValueType>
1041void clip(std::vector<ValueType>& x, boost::optional<ValueType>
const& lowerBound, boost::optional<ValueType>
const& upperBound) {
1042 for (
auto& entry : x) {
1043 if (lowerBound && entry < lowerBound.get()) {
1044 entry = lowerBound.get();
1045 }
else if (upperBound && entry > upperBound.get()) {
1046 entry = upperBound.get();
1054template<
typename ValueType>
1055void clip(std::vector<ValueType>& x, ValueType
const& bound,
bool boundFromBelow) {
1056 for (
auto& entry : x) {
1057 if (boundFromBelow && entry < bound) {
1059 }
else if (!boundFromBelow && entry > bound) {
1068template<
typename ValueType>
1069void clip(std::vector<ValueType>& x, std::vector<ValueType>
const& bounds,
bool boundFromBelow) {
1070 auto boundsIt = bounds.begin();
1071 for (
auto& entry : x) {
1072 if (boundFromBelow && entry < *boundsIt) {
1074 }
else if (!boundFromBelow && entry > *boundsIt) {
1093 uint_fast64_t currentRowCount = 0;
1094 uint_fast64_t currentIndexCount = 1;
1101 for (
auto index : constraint) {
1102 subVector[currentIndexCount] = currentRowCount + offsetVector[index + 1] - offsetVector[index];
1103 currentRowCount += offsetVector[index + 1] - offsetVector[index];
1104 ++currentIndexCount;
1118template<
typename TargetType,
typename SourceType>
1120 std::vector<TargetType> resultVector;
1121 resultVector.reserve(oldVector.size());
1122 for (
auto const& oldValue : oldVector) {
1123 resultVector.push_back(storm::utility::convertNumber<TargetType>(oldValue));
1125 return resultVector;
1137template<
typename TargetType,
typename SourceType>
1139 assert(inputVector.size() == targetVector.size());
1140 applyPointwise(inputVector, targetVector, [](SourceType
const& v) {
return storm::utility::convertNumber<TargetType>(v); });
1146template<
typename NewValueType,
typename ValueType>
1147std::vector<NewValueType>
toValueType(std::vector<ValueType>
const& oldVector) {
1148 std::vector<NewValueType> resultVector;
1149 resultVector.reserve(oldVector.size());
1150 for (
auto const& oldValue : oldVector) {
1151 resultVector.push_back(
static_cast<NewValueType
>(oldValue));
1153 return resultVector;
1156template<
typename ValueType,
typename TargetValueType>
1157typename std::enable_if<std::is_same<ValueType, storm::RationalNumber>::value, std::pair<std::vector<TargetValueType>, ValueType>>::type
toIntegralVector(
1158 std::vector<ValueType>
const& vec) {
1160 std::set<ValueType> occurringNonZeroNumbers;
1161 for (
auto const& v : vec) {
1163 occurringNonZeroNumbers.insert(v);
1169 if (occurringNonZeroNumbers.empty()) {
1170 factor = storm::utility::one<ValueType>();
1171 }
else if (occurringNonZeroNumbers.size() == 1) {
1172 factor = *occurringNonZeroNumbers.begin();
1176 auto numberIt = occurringNonZeroNumbers.begin();
1178 for (++numberIt; numberIt != occurringNonZeroNumbers.end(); ++numberIt) {
1182 numberIt = occurringNonZeroNumbers.begin();
1183 ValueType gcd = *numberIt * lcm;
1184 for (++numberIt; numberIt != occurringNonZeroNumbers.end(); ++numberIt) {
1185 gcd = carl::gcd(gcd,
static_cast<ValueType
>(*numberIt * lcm));
1192 std::vector<TargetValueType> result;
1193 result.reserve(vec.size());
1194 for (
auto const& v : vec) {
1195 ValueType vScaled = v / factor;
1197 result.push_back(storm::utility::convertNumber<TargetValueType, ValueType>(vScaled));
1199 return std::make_pair(std::move(result), std::move(factor));
1202template<
typename ValueType,
typename TargetValueType>
1203typename std::enable_if<!std::is_same<ValueType, storm::RationalNumber>::value, std::pair<std::vector<TargetValueType>, ValueType>>::type
toIntegralVector(
1204 std::vector<ValueType>
const& vec) {
1206 auto rationalNumberVec = convertNumericVector<storm::RationalNumber>(vec);
1207 auto rationalNumberResult = toIntegralVector<storm::RationalNumber, TargetValueType>(rationalNumberVec);
1209 return std::make_pair(std::move(rationalNumberResult.first), storm::utility::convertNumber<ValueType>(rationalNumberResult.second));
1212template<
typename Type>
1214 std::vector<Type> result;
1216 for (
auto index :
filter) {
1217 result.push_back(in[index]);
1223template<
typename Type>
1226 uint_fast64_t size = v.size();
1229 if (firstUnsetIndex < size) {
1230 auto vIt = v.begin() + firstUnsetIndex;
1232 *vIt = std::move(v[index]);
1235 v.resize(vIt - v.begin());
1243 return std::any_of(v.begin(), v.end(), [](T value) { return value < storm::utility::zero<T>(); });
1248 return std::any_of(v.begin(), v.end(), [](T value) { return value > storm::utility::zero<T>(); });
1253 return std::any_of(v.begin(), v.end(), [](T value) { return !storm::utility::isZero(value); });
1258 return std::any_of(v.begin(), v.end(), [](T value) { return storm::utility::isZero(value); });
1263 return std::any_of(v.begin(), v.end(), [](T value) { return storm::utility::isInfinity(value); });
1268 std::vector<T> result;
1269 result.reserve(source.size());
1270 for (uint64_t sourceIndex : inversePermutation) {
1271 result.push_back(source[sourceIndex]);
1278 std::vector<uint64_t>
const& rowGroupIndices) {
1279 STORM_LOG_ASSERT(inversePermutation.size() == rowGroupIndices.size() - 1,
"Inverse permutation and row group indices do not match.");
1280 std::vector<T> result;
1281 result.reserve(source.size());
1282 for (
auto sourceGroupIndex : inversePermutation) {
1283 for (uint64_t sourceIndex = rowGroupIndices[sourceGroupIndex]; sourceIndex < rowGroupIndices[sourceGroupIndex + 1]; ++sourceIndex) {
1284 result.push_back(source[sourceIndex]);
1287 STORM_LOG_ASSERT(result.size() == source.size(),
"result has unexpected length.");
1297template<
typename ValueType>
1298std::string
toString(std::vector<ValueType>
const& vector) {
1299 std::stringstream stream;
1300 stream <<
"vector (" << vector.size() <<
") [ ";
1301 if (!vector.empty()) {
1302 for (uint_fast64_t i = 0; i < vector.size() - 1; ++i) {
1303 stream << vector[i] <<
", ";
1305 stream << vector.back();
1308 return stream.str();
1311template<
typename PT1,
typename PT2>
1312std::string
toString(std::vector<std::pair<PT1, PT2>>
const& vector) {
1313 std::stringstream stream;
1314 stream <<
"vector (" << vector.size() <<
") [ ";
1315 if (!vector.empty()) {
1316 for (uint_fast64_t i = 0; i < vector.size() - 1; ++i) {
1317 stream <<
"{" << vector[i].first <<
"," << vector[i].second <<
"}, ";
1319 stream <<
"{" << vector.back().first <<
"," << vector.back().second <<
"}";
1322 return stream.str();
A bit vector that is internally represented as a vector of 64-bit values.
const_iterator end() const
Returns an iterator pointing at the element past the back of the bit vector.
uint_fast64_t getNextSetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
const_iterator begin() const
Returns an iterator to the indices of the set bits in the bit vector.
void set(uint_fast64_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.
uint_fast64_t getNextUnsetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to false in the bit vector.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
#define STORM_LOG_ASSERT(cond, message)
std::size_t findOrInsert(std::vector< T > &vector, T &&element)
Finds the given element in the given vector.
std::vector< TargetType > convertNumericVector(std::vector< SourceType > const &oldVector)
Converts the given vector to the given ValueType Assumes that both, TargetType and SourceType are num...
storm::storage::BitVector filter(std::vector< T > const &values, std::function< bool(T const &value)> const &function)
Retrieves a bit vector containing all the indices for which the value at this position makes the give...
void applyPointwiseTernary(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target, Operation f=Operation())
Applies the given operation pointwise on the two given vectors and writes the result to the third vec...
VT min_if(std::vector< VT > const &values, storm::storage::BitVector const &filter)
Computes the minimum of the entries from the values that are selected by the (non-empty) filter.
void addVectors(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target)
Adds the two given vectors and writes the result to the target vector.
void divideVectorsPointwise(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target)
Divides the two given vectors (pointwise) and writes the result to the target vector.
T dotProduct(std::vector< T > const &firstOperand, std::vector< T > const &secondOperand)
Computes the dot product (aka scalar product) and returns the result.
std::vector< NewValueType > toValueType(std::vector< ValueType > const &oldVector)
Converts the given vector to the given ValueType.
bool hasInfinityEntry(std::vector< T > const &v)
void addVectorToGroupedVector(std::vector< T > &target, std::vector< T > const &source, std::vector< uint_fast64_t > const &rowGroupIndices)
Adds the source vector to the target vector in a way such that the i-th entry is added to all element...
void setNonzeroIndices(std::vector< T > const &vec, storm::storage::BitVector &bv)
VT max_if(std::vector< VT > const &values, storm::storage::BitVector const &filter)
Computes the maximum of the entries from the values that are selected by the (non-empty) filter.
void setVectorValues(std::vector< T > &vector, storm::storage::BitVector const &positions, std::vector< T > const &values)
Sets the provided values at the provided positions in the given vector.
bool compareElementWise(std::vector< T > const &left, std::vector< T > const &right, Comparator comp=std::less< T >())
std::vector< T > buildVectorForRange(T min, T max)
Constructs a vector [min, min+1, ...., max-1].
void setAllValues(std::vector< T > &vec, storm::storage::BitVector const &positions, T const &positiveValue=storm::utility::one< T >(), T const &negativeValue=storm::utility::zero< T >())
void addFilteredVectorGroupsToGroupedVector(std::vector< T > &target, std::vector< T > const &source, storm::storage::BitVector const &filter, std::vector< uint_fast64_t > const &rowGroupIndices)
bool equalModuloPrecision(T const &val1, T const &val2, T const &precision, bool relativeError=true)
Compares the given elements and determines whether they are equal modulo the given precision.
void selectVectorValues(std::vector< T > &vector, storm::storage::BitVector const &positions, std::vector< T > const &values)
Selects the elements from a vector at the specified positions and writes them consecutively into anot...
T computeSquaredNorm2Difference(std::vector< T > const &b1, std::vector< T > const &b2)
bool isUnique(std::vector< T > const &v)
Returns true iff every element in the given vector is unique, i.e., there are no i,...
T maximumElementDiff(std::vector< T > const &vectorLeft, std::vector< T > const &vectorRight)
void reduceVectorMinOrMax(storm::solver::OptimizationDirection dir, std::vector< T > const &source, std::vector< T > &target, std::vector< uint_fast64_t > const &rowGrouping, std::vector< uint_fast64_t > *choices=nullptr)
Reduces the given source vector by selecting either the smallest or the largest out of each row group...
storm::storage::BitVector filterGreaterZero(std::vector< T > const &values)
Retrieves a bit vector containing all the indices for which the value at this position is greater tha...
void reduceVector(std::vector< T > const &source, std::vector< T > &target, std::vector< uint_fast64_t > const &rowGrouping, std::vector< uint_fast64_t > *choices)
Reduces the given source vector by selecting an element according to the given filter out of each row...
void addScaledVector(std::vector< InValueType1 > &firstOperand, std::vector< InValueType2 > const &secondOperand, InValueType3 const &factor)
Computes x:= x + a*y, i.e., adds each element of the first vector and (the corresponding element of t...
void multiplyVectorsPointwise(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target)
Multiplies the two given vectors (pointwise) and writes the result to the target vector.
bool hasPositiveEntry(std::vector< T > const &v)
storm::storage::BitVector filterOne(std::vector< T > const &values)
Retrieves a bit vector containing all the indices for which the value at this position is equal to on...
bool hasNegativeEntry(std::vector< T > const &v)
T maximumElementAbs(std::vector< T > const &vector)
void clip(std::vector< ValueType > &x, boost::optional< ValueType > const &lowerBound, boost::optional< ValueType > const &upperBound)
Takes the input vector and ensures that all entries conform to the bounds.
void addFilteredVectorToGroupedVector(std::vector< T > &target, std::vector< T > const &source, storm::storage::BitVector const &filter, std::vector< uint_fast64_t > const &rowGroupIndices)
Adds the source vector to the target vector in a way such that the i-th selected entry is added to al...
void selectVectorValuesRepeatedly(std::vector< T > &vector, storm::storage::BitVector const &positions, std::vector< uint_fast64_t > const &rowGrouping, std::vector< T > const &values)
Selects values from a vector at the specified positions and writes them into another vector as often ...
void applyPointwise(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target, Operation f=Operation())
Applies the given operation pointwise on the two given vectors and writes the result to the third vec...
void reduceVectorMin(std::vector< T > const &source, std::vector< T > &target, std::vector< uint_fast64_t > const &rowGrouping, std::vector< uint_fast64_t > *choices=nullptr)
Reduces the given source vector by selecting the smallest element out of each row group.
VT sum_if(std::vector< VT > const &values, storm::storage::BitVector const &filter)
Sum the entries from values that are set to one in the filter vector.
storm::storage::BitVector filterInfinity(std::vector< T > const &values)
Retrieves a bit vector containing all the indices for which the value at this position is equal to on...
std::vector< T > applyInversePermutation(std::vector< uint64_t > const &inversePermutation, std::vector< T > const &source)
void filterVectorInPlace(std::vector< Type > &v, storm::storage::BitVector const &filter)
void iota_n(OutputIterator first, Size n, Assignable value)
Iota function as a helper for efficient creating a range in a vector.
void subtractVectors(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target)
Subtracts the two given vectors and writes the result to the target vector.
std::vector< uint_fast64_t > getSortedIndices(std::vector< T > const &v)
Returns a list of indices such that the first index refers to the highest entry of the given vector,...
bool hasZeroEntry(std::vector< T > const &v)
storm::storage::BitVector filterZero(std::vector< T > const &values)
Retrieves a bit vector containing all the indices for which the value at this position is equal to ze...
void scaleVectorInPlace(std::vector< ValueType1 > &target, ValueType2 const &factor)
Multiplies each element of the given vector with the given factor and writes the result into the vect...
std::vector< T > applyInversePermutationToGroupedVector(std::vector< uint64_t > const &inversePermutation, std::vector< T > const &source, std::vector< uint64_t > const &rowGroupIndices)
std::vector< T > getConstrainedOffsetVector(std::vector< T > const &offsetVector, storm::storage::BitVector const &constraint)
Takes the given offset vector and applies the given contraint.
bool hasNonZeroEntry(std::vector< T > const &v)
void subtractFromConstantOneVector(std::vector< T > &vector)
Subtracts the given vector from the constant one-vector and writes the result to the input vector.
void reduceVectorMax(std::vector< T > const &source, std::vector< T > &target, std::vector< uint_fast64_t > const &rowGrouping, std::vector< uint_fast64_t > *choices=nullptr)
Reduces the given source vector by selecting the largest element out of each row group.
std::enable_if< std::is_same< ValueType, storm::RationalNumber >::value, std::pair< std::vector< TargetValueType >, ValueType > >::type toIntegralVector(std::vector< ValueType > const &vec)
std::vector< Type > filterVector(std::vector< Type > const &in, storm::storage::BitVector const &filter)
ValueType max(ValueType const &first, ValueType const &second)
ValueType min(ValueType const &first, ValueType const &second)
bool isAlmostZero(ValueType const &a)
std::pair< ValueType, ValueType > asFraction(ValueType const &number)
bool isZero(ValueType const &a)
bool isInteger(ValueType const &number)
ValueType abs(ValueType const &number)
std::string toString(Engine const &engine)
Returns a string representation of the given engine.
size_t operator()(std::vector< ValueType > const &vec) const