Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
PolynomialToExpression.h
Go to the documentation of this file.
1#pragma once
2
3#include <set>
4
10
11namespace storm {
12namespace expressions {
13
27template<typename PolynomialType>
28Expression polynomialToExpression(PolynomialType const& polynomial, std::shared_ptr<ExpressionManager> const& manager) {
29 std::set<storm::RationalFunctionVariable> polynomialVariables;
30 polynomial.gatherVariables(polynomialVariables);
31 for (auto const& variable : polynomialVariables) {
32 if (!manager->hasVariable(variable.name())) {
33 manager->declareRationalVariable(variable.name());
34 }
35 }
36
37 // Put the terms of the polynomial into carl's canonical order, so that the resulting expression does not
38 // depend on the order in which the polynomial happens to store them.
39 polynomial.makeOrdered();
40
43
44 Expression result;
45 bool isFirstTerm = true;
46 for (auto const& term : polynomial) {
47 // The constant term has no monomial, in which case this set stays empty and the monomial is never
48 // dereferenced.
49 std::set<storm::RationalFunctionVariable> termVariables;
50 term.gatherVariables(termVariables);
51
53 bool const isNegative = coefficient < zero;
54 auto absoluteCoefficient = isNegative ? -coefficient : coefficient;
55
56 Expression variablesPart;
57 for (auto const& variable : termVariables) {
58 auto exponent = static_cast<int64_t>(term.monomial()->exponentOfVariable(variable));
59 auto variableExpression = manager->getVariable(variable.name());
60 auto factor = exponent == 1 ? variableExpression : pow(variableExpression, manager->integer(exponent));
61 variablesPart = variablesPart.isInitialized() ? variablesPart * factor : factor;
62 }
63
64 // Terms without variables are plain constants and keep their sign, since there is nothing to subtract
65 // them from.
66 bool const isConstantTerm = !variablesPart.isInitialized();
67 Expression termExpression = isConstantTerm ? manager->rational(coefficient)
68 : (absoluteCoefficient == one ? variablesPart : manager->rational(absoluteCoefficient) * variablesPart);
69
70 // Emit negative non-constant terms as a subtraction, so that the result reads naturally.
71 if (isFirstTerm) {
72 result = isNegative && !isConstantTerm ? -termExpression : termExpression;
73 } else {
74 result = isNegative && !isConstantTerm ? result - termExpression : result + termExpression;
75 }
76 isFirstTerm = false;
77 }
78
79 // The polynomial has no terms at all, so it represents zero.
80 if (isFirstTerm) {
81 result = manager->rational(zero);
82 }
83 return result;
84}
85
86} // namespace expressions
87} // namespace storm
void gatherVariables(std::set< storm::expressions::Variable > &variables) const
Retrieves the set of all variables that appear in the expression.
bool isInitialized() const
Checks whether the object encapsulates a base-expression.
Expression pow(Expression const &base, Expression const &exponent, bool allowIntegerType)
The type of the resulting expression is.
Expression polynomialToExpression(PolynomialType const &polynomial, std::shared_ptr< ExpressionManager > const &manager)
Converts the given polynomial into a Storm expression.
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
TargetType convertNumber(SourceType const &number)