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