Storm 1.14.0.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));
62 return static_cast<int_fast64_t>(std::ceil(result));
63 default:
64 STORM_LOG_ASSERT(false, "All other operator types should have been handled before.");
65 return 0; // Warning suppression.
66 }
67 }
68}
69
71 STORM_LOG_THROW(this->hasNumericalType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as double.");
72
73 double result = this->getOperand()->evaluateAsDouble(valuation);
74 switch (this->getOperatorType()) {
76 result = -result;
77 break;
79 result = std::floor(result);
80 break;
82 result = std::ceil(result);
83 break;
85 result = std::cos(result);
86 break;
88 result = std::sin(result);
89 break;
90 }
91 return result;
92}
93
94std::shared_ptr<BaseExpression const> UnaryNumericalFunctionExpression::simplify() const {
95 std::shared_ptr<BaseExpression const> operandSimplified = this->getOperand()->simplify();
96
97 if (operandSimplified->isLiteral()) {
98 if (operandSimplified->hasIntegerType()) {
99 int_fast64_t intValue = operandSimplified->evaluateAsInt();
100 storm::RationalNumber rationalValue;
101 bool useInteger = true;
102 switch (this->getOperatorType()) {
104 intValue = -intValue;
105 break;
106 // Nothing to be done for the other cases:
109 break;
111 useInteger = false;
113 break;
115 useInteger = false;
117 break;
118 }
119 if (useInteger) {
120 return std::shared_ptr<BaseExpression>(new IntegerLiteralExpression(this->getManager(), intValue));
121 } else {
122 return std::shared_ptr<BaseExpression>(new RationalLiteralExpression(this->getManager(), rationalValue));
123 }
124 } else if (operandSimplified->hasRationalType()) {
125 storm::RationalNumber value = operandSimplified->evaluateAsRational();
126 bool convertToInteger = false;
127 switch (this->getOperatorType()) {
129 value = -value;
130 break;
132 value = storm::utility::floor(value);
133 convertToInteger = true;
134 break;
136 value = storm::utility::ceil(value);
137 convertToInteger = true;
138 break;
140 value = storm::utility::cos(value);
141 break;
143 value = storm::utility::sin(value);
144 break;
145 }
146 if (convertToInteger) {
147 return std::shared_ptr<BaseExpression>(new IntegerLiteralExpression(this->getManager(), storm::utility::convertNumber<int64_t>(value)));
148 } else {
149 return std::shared_ptr<BaseExpression>(new RationalLiteralExpression(this->getManager(), value));
150 }
151 }
152 }
153
154 if (operandSimplified.get() == this->getOperand().get()) {
155 return this->shared_from_this();
156 } else {
157 return std::shared_ptr<BaseExpression>(
158 new UnaryNumericalFunctionExpression(this->getManager(), this->getType(), operandSimplified, this->getOperatorType()));
159 }
160}
161
162boost::any UnaryNumericalFunctionExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
163 return visitor.visit(*this, data);
164}
165
169
170void UnaryNumericalFunctionExpression::printToStream(std::ostream& stream) const {
171 switch (this->getOperatorType()) {
173 stream << "-(";
174 break;
176 stream << "floor(";
177 break;
179 stream << "ceil(";
180 break;
182 stream << "cos(";
183 break;
185 stream << "sin(";
186 break;
187 }
188 stream << *this->getOperand() << ")";
189}
190} // namespace expressions
191} // 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
UnaryExpression(ExpressionManager const &manager, Type const &type, std::shared_ptr< BaseExpression const > const &operand)
Creates a unary expression with the given return type and operand.
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:15
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
ValueType sin(ValueType const &number)
ValueType floor(ValueType const &number)
ValueType ceil(ValueType const &number)
ValueType cos(ValueType const &number)
TargetType convertNumber(SourceType const &number)