Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
KwekMehlhorn.cpp
Go to the documentation of this file.
2
4
7
9
10namespace storm {
11namespace utility {
12namespace kwek_mehlhorn {
13
14template<typename IntegerType>
15std::pair<IntegerType, IntegerType> findRational(IntegerType const& alpha, IntegerType const& beta, IntegerType const& gamma, IntegerType const& delta) {
16 std::pair<IntegerType, IntegerType> alphaDivBetaPair = storm::utility::divide(alpha, beta);
17 std::pair<IntegerType, IntegerType> gammaDivDeltaPair = storm::utility::divide(gamma, delta);
18
19 if (alphaDivBetaPair.first == gammaDivDeltaPair.first && !storm::utility::isZero(alphaDivBetaPair.second)) {
20 std::pair<IntegerType, IntegerType> subresult = findRational(delta, gammaDivDeltaPair.second, beta, alphaDivBetaPair.second);
21 std::pair<IntegerType, IntegerType> result(alphaDivBetaPair.first * subresult.first + subresult.second, subresult.first);
22
23 return result;
24 } else {
25 std::pair<IntegerType, IntegerType> result(
26 storm::utility::isZero(alphaDivBetaPair.second) ? alphaDivBetaPair.first : alphaDivBetaPair.first + storm::utility::one<IntegerType>(),
27 storm::utility::one<IntegerType>());
28 return result;
29 }
30}
31
32template<typename RationalType, typename ImpreciseType>
33std::pair<typename NumberTraits<RationalType>::IntegerType, typename NumberTraits<RationalType>::IntegerType> truncateToRational(ImpreciseType const& value,
34 uint64_t precision) {
35 typedef typename NumberTraits<RationalType>::IntegerType IntegerType;
36
37 IntegerType powerOfTen = storm::utility::pow(storm::utility::convertNumber<IntegerType>(static_cast<uint64_t>(10)), precision);
38 IntegerType truncated = storm::utility::trunc<RationalType>(value * powerOfTen);
39 return std::make_pair(truncated, powerOfTen);
40}
41
42template<typename RationalType>
43std::pair<typename NumberTraits<RationalType>::IntegerType, typename NumberTraits<RationalType>::IntegerType> truncateToRational(double const& value,
44 uint64_t precision) {
45 if (precision > std::numeric_limits<double>::max_digits10) {
46 throw storm::exceptions::PrecisionExceededException() << "Exceeded precision of double, consider switching to rational numbers.";
47 }
48
49 double powerOfTen = std::pow(10, precision);
50 auto truncated = storm::utility::trunc<double>(value * powerOfTen);
51 return std::make_pair(storm::utility::convertNumber<typename NumberTraits<RationalType>::IntegerType>(truncated),
53}
54
55template<typename RationalType, typename ImpreciseType>
56RationalType findRational(uint64_t precision, ImpreciseType const& value) {
57 typedef typename NumberTraits<RationalType>::IntegerType IntegerType;
58
59 std::pair<IntegerType, IntegerType> truncatedFraction = truncateToRational<RationalType>(value, precision);
60 std::pair<IntegerType, IntegerType> result = findRational<IntegerType>(
61 truncatedFraction.first, truncatedFraction.second, truncatedFraction.first + storm::utility::one<IntegerType>(), truncatedFraction.second);
62
63 // Convert one of the arguments to a rational type to not get integer division.
64 return storm::utility::convertNumber<RationalType>(result.first) / result.second;
65}
66
67template<typename RationalType, typename ImpreciseType>
68RationalType sharpen(uint64_t precision, ImpreciseType const& value) {
69 ImpreciseType integer = storm::utility::floor(value);
70 ImpreciseType fraction = value - integer;
71 auto rational = findRational<RationalType>(precision, fraction);
72 return storm::utility::convertNumber<RationalType>(integer) + rational;
73}
74
75template<typename RationalType, typename ImpreciseType>
76void sharpen(uint64_t precision, std::vector<ImpreciseType> const& input, std::vector<RationalType>& output) {
77 for (uint64_t index = 0; index < input.size(); ++index) {
78 output[index] = sharpen<RationalType, ImpreciseType>(precision, input[index]);
79 }
80}
81
82template storm::RationalNumber sharpen(uint64_t precision, double const& input);
83template storm::RationalNumber sharpen(uint64_t precision, storm::RationalNumber const& input);
84
85template void sharpen(uint64_t precision, std::vector<double> const& input, std::vector<storm::RationalNumber>& output);
86template void sharpen(uint64_t precision, std::vector<storm::RationalNumber> const& input, std::vector<storm::RationalNumber>& output);
87
88} // namespace kwek_mehlhorn
89} // namespace utility
90} // namespace storm
std::pair< typename NumberTraits< RationalType >::IntegerType, typename NumberTraits< RationalType >::IntegerType > truncateToRational(ImpreciseType const &value, uint64_t precision)
std::pair< IntegerType, IntegerType > findRational(IntegerType const &alpha, IntegerType const &beta, IntegerType const &gamma, IntegerType const &delta)
RationalType sharpen(uint64_t precision, ImpreciseType const &value)
ValueType floor(ValueType const &number)
bool isZero(ValueType const &a)
Definition constants.cpp:41
std::pair< IntegerType, IntegerType > divide(IntegerType const &dividend, IntegerType const &divisor)
(Integer-)Divides the dividend by the divisor and returns the result plus the remainder.
ValueType pow(ValueType const &value, int_fast64_t exponent)
TargetType convertNumber(SourceType const &number)
Definition constants.cpp:98
LabParser.cpp.
Definition cli.cpp:18