Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
UnaryNumericalFunctionExpression.cpp
Go to the documentation of this file.
2
3#include <cmath>
4
13
14namespace storm {
15namespace expressions {
17 std::shared_ptr<BaseExpression const> const& operand, OperatorType operatorType)
18 : UnaryExpression(manager, type, operand), operatorType(operatorType) {
19 // Intentionally left empty.
20}
21
25
47
48int_fast64_t UnaryNumericalFunctionExpression::evaluateAsInt(Valuation const* valuation) const {
49 STORM_LOG_THROW(this->hasIntegerType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as integer.");
50
51 if (this->getOperatorType() == OperatorType::Minus) {
52 STORM_LOG_THROW(this->getOperand()->hasIntegerType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as integer.");
53 int_fast64_t result = this->getOperand()->evaluateAsInt(valuation);
54 return -result;
55 } else {
56 // TODO: this should evaluate the operand as a rational.
57 double result = this->getOperand()->evaluateAsDouble(valuation);
58 switch (this->getOperatorType()) {
60 return static_cast<int_fast64_t>(std::floor(result));
61 break;
63 return static_cast<int_fast64_t>(std::ceil(result));
64 break;
65 default:
66 STORM_LOG_ASSERT(false, "All other operator types should have been handled before.");
67 return 0; // Warning suppression.
68 }
69 }
70}
71
73 STORM_LOG_THROW(this->hasNumericalType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as double.");
74
75 double result = this->getOperand()->evaluateAsDouble(valuation);
76 switch (this->getOperatorType()) {
78 result = -result;
79 break;
81 result = std::floor(result);
82 break;
84 result = std::ceil(result);
85 break;
87 result = std::cos(result);
88 break;
90 result = std::sin(result);
91 break;
92 }
93 return result;
94}
95
96std::shared_ptr<BaseExpression const> UnaryNumericalFunctionExpression::simplify() const {
97 std::shared_ptr<BaseExpression const> operandSimplified = this->getOperand()->simplify();
98
99 if (operandSimplified->isLiteral()) {
100 if (operandSimplified->hasIntegerType()) {
101 int_fast64_t intValue = operandSimplified->evaluateAsInt();
102 storm::RationalNumber rationalValue;
103 bool useInteger = true;
104 switch (this->getOperatorType()) {
106 intValue = -intValue;
107 break;
108 // Nothing to be done for the other cases:
111 break;
113 useInteger = false;
114 rationalValue = storm::utility::cos(storm::utility::convertNumber<storm::RationalNumber>(intValue));
115 break;
117 useInteger = false;
118 rationalValue = storm::utility::sin(storm::utility::convertNumber<storm::RationalNumber>(intValue));
119 break;
120 }
121 if (useInteger) {
122 return std::shared_ptr<BaseExpression>(new IntegerLiteralExpression(this->getManager(), intValue));
123 } else {
124 return std::shared_ptr<BaseExpression>(new RationalLiteralExpression(this->getManager(), rationalValue));
125 }
126 } else if (operandSimplified->hasRationalType()) {
127 storm::RationalNumber value = operandSimplified->evaluateAsRational();
128 bool convertToInteger = false;
129 switch (this->getOperatorType()) {
131 value = -value;
132 break;
134 value = storm::utility::floor(value);
135 convertToInteger = true;
136 break;
138 value = storm::utility::ceil(value);
139 convertToInteger = true;
140 break;
142 value = storm::utility::cos(value);
143 break;
145 value = storm::utility::sin(value);
146 break;
147 }
148 if (convertToInteger) {
149 return std::shared_ptr<BaseExpression>(new IntegerLiteralExpression(this->getManager(), storm::utility::convertNumber<int64_t>(value)));
150 } else {
151 return std::shared_ptr<BaseExpression>(new RationalLiteralExpression(this->getManager(), value));
152 }
153 }
154 }
155
156 if (operandSimplified.get() == this->getOperand().get()) {
157 return this->shared_from_this();
158 } else {
159 return std::shared_ptr<BaseExpression>(
160 new UnaryNumericalFunctionExpression(this->getManager(), this->getType(), operandSimplified, this->getOperatorType()));
161 }
162}
163
164boost::any UnaryNumericalFunctionExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
165 return visitor.visit(*this, data);
166}
167
171
172void UnaryNumericalFunctionExpression::printToStream(std::ostream& stream) const {
173 switch (this->getOperatorType()) {
175 stream << "-(";
176 break;
178 stream << "floor(";
179 break;
181 stream << "ceil(";
182 break;
184 stream << "cos(";
185 break;
187 stream << "sin(";
188 break;
189 }
190 stream << *this->getOperand() << ")";
191}
192} // namespace expressions
193} // namespace storm
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
bool hasIntegerType() const
Retrieves whether the expression has an integer type.
bool hasNumericalType() const
Retrieves whether the expression has a numerical type, i.e., integer or double.
Type const & getType() const
Retrieves the type of the expression.
This class is responsible for managing a set of typed variables and all expressions using these varia...
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data)=0
std::shared_ptr< BaseExpression const > const & getOperand() const
Retrieves the operand of the unary expression.
OperatorType
An enum type specifying the different functions applicable.
virtual int_fast64_t evaluateAsInt(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
virtual void printToStream(std::ostream &stream) const override
Prints the expression to the given stream.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
virtual storm::expressions::OperatorType getOperator() const override
Retrieves the operator of a function application.
virtual std::shared_ptr< BaseExpression const > simplify() const override
Simplifies the expression according to some simple rules.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const override
Accepts the given visitor by calling its visit method.
UnaryNumericalFunctionExpression(ExpressionManager const &manager, Type const &type, std::shared_ptr< BaseExpression const > const &operand, OperatorType operatorType)
Creates a unary numerical function expression with the given return type, operand and operator.
virtual double evaluateAsDouble(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
The base class of all valuations of variables.
Definition Valuation.h:16
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
ValueType sin(ValueType const &number)
ValueType floor(ValueType const &number)
ValueType ceil(ValueType const &number)
ValueType cos(ValueType const &number)