16template<
typename ValueType>
18 : variableToCoefficientMapping(), constantPart(
std::move(constantPart)) {
22template<
typename ValueType>
23void LinearCoefficientVisitor<ValueType>::VariableCoefficients::removeZeroCoefficients() {
24 for (
auto it = variableToCoefficientMapping.begin(); it != variableToCoefficientMapping.end();) {
29template<
typename ValueType>
32 for (
auto const& otherVariableCoefficientPair : other.variableToCoefficientMapping) {
33 auto [it, inserted] = this->variableToCoefficientMapping.try_emplace(otherVariableCoefficientPair.first, otherVariableCoefficientPair.second);
35 it->second += otherVariableCoefficientPair.second;
38 constantPart += other.constantPart;
39 removeZeroCoefficients();
43template<
typename ValueType>
47 return *
this += std::move(other);
50template<
typename ValueType>
53 STORM_LOG_THROW(variableToCoefficientMapping.size() == 0 || other.variableToCoefficientMapping.size() == 0, storm::exceptions::InvalidArgumentException,
54 "Expression is non-linear.");
55 if (other.variableToCoefficientMapping.size() > 0) {
56 variableToCoefficientMapping = std::move(other.variableToCoefficientMapping);
57 std::swap(constantPart, other.constantPart);
59 for (
auto& variableCoefficientPair : this->variableToCoefficientMapping) {
60 variableCoefficientPair.second *= other.constantPart;
62 constantPart *= other.constantPart;
63 removeZeroCoefficients();
67template<
typename ValueType>
71 "Expression is non-linear or divides by zero.");
72 for (
auto& variableCoefficientPair : this->variableToCoefficientMapping) {
73 variableCoefficientPair.second /= other.constantPart;
75 constantPart /= other.constantPart;
79template<
typename ValueType>
81 for (
auto& variableCoefficientPair : variableToCoefficientMapping) {
82 variableCoefficientPair.second = -variableCoefficientPair.second;
84 constantPart = -constantPart;
87template<
typename ValueType>
89 variableToCoefficientMapping.insert_or_assign(variable, coefficient);
92template<
typename ValueType>
94 auto it = variableToCoefficientMapping.find(variable);
98template<
typename ValueType>
100 return variableToCoefficientMapping.size();
103template<
typename ValueType>
105 return this->constantPart;
108template<
typename ValueType>
110 for (
auto const& rhsVariableCoefficientPair : rhs.variableToCoefficientMapping) {
111 auto [it, inserted] = this->variableToCoefficientMapping.try_emplace(rhsVariableCoefficientPair.first, -rhsVariableCoefficientPair.second);
113 it->second -= rhsVariableCoefficientPair.second;
116 removeZeroCoefficients();
117 rhs.variableToCoefficientMapping.clear();
118 rhs.constantPart -= this->constantPart;
121template<
typename ValueType>
123 return this->variableToCoefficientMapping.begin();
126template<
typename ValueType>
128 return this->variableToCoefficientMapping.end();
131template<
typename ValueType>
137template<
typename ValueType>
140 STORM_LOG_THROW(result.has_value(), storm::exceptions::InvalidArgumentException,
"Expression is non-linear.");
141 return std::move(result.value());
144template<
typename ValueType>
149template<
typename ValueType>
154template<
typename ValueType>
156 Result left = boost::any_cast<Result>(expression.
getFirstOperand()->accept(*
this, data));
160 Result right = boost::any_cast<Result>(expression.
getSecondOperand()->accept(*
this, data));
167 *left += std::move(*right);
170 *left -= std::move(*right);
173 if (left->size() > 0 && right->size() > 0) {
176 *left *= std::move(*right);
182 *left /= std::move(*right);
190template<
typename ValueType>
195template<
typename ValueType>
202 return Result{std::move(coefficients)};
205template<
typename ValueType>
210template<
typename ValueType>
212 Result child = boost::any_cast<Result>(expression.
getOperand()->accept(*
this, data));
220template<
typename ValueType>
225template<
typename ValueType>
230template<
typename ValueType>
238#if (defined(STORM_USE_CLN_EA) && !defined(STORM_USE_CLN_RF)) || (!defined(STORM_USE_CLN_EA) && defined(STORM_USE_CLN_RF))
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const =0
Accepts the given visitor by calling its visit method.
Type const & getType() const
Retrieves the type of the expression.
std::shared_ptr< BaseExpression const > const & getSecondOperand() const
Retrieves the second operand of the expression.
std::shared_ptr< BaseExpression const > const & getFirstOperand() const
Retrieves the first operand of the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
BaseExpression const & getBaseExpression() const
Retrieves the base expression underlying this expression object.
int_fast64_t getValue() const
Retrieves the value of the integer literal.
Computes the coefficients of a linear expression.
std::optional< VariableCoefficients > tryGetLinearCoefficients(Expression const &expression)
As getLinearCoefficients, but returns std::nullopt instead of throwing if the expression is not linea...
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data) override
VariableCoefficients getLinearCoefficients(Expression const &expression)
Computes the coefficients of all identifiers appearing in the expression if the expression was rewrit...
storm::RationalNumber getValue() const
Retrieves the value of the double literal.
bool isNumericalType() const
Checks whether this type is a numerical type.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
Variable const & getVariable() const
Retrieves the variable associated with this expression.
#define STORM_LOG_THROW(cond, exception, message)
bool isZero(ValueType const &a)
TargetType convertNumber(SourceType const &number)
VariableCoefficients & operator-=(VariableCoefficients &&other)
std::map< storm::expressions::Variable, ValueType >::const_iterator begin() const
VariableCoefficients & operator*=(VariableCoefficients &&other)
ValueType getCoefficient(storm::expressions::Variable const &variable)
VariableCoefficients(ValueType constantPart=ValueType(0))
std::map< storm::expressions::Variable, ValueType >::const_iterator end() const
VariableCoefficients & operator+=(VariableCoefficients &&other)
ValueType const & getConstantPart() const
void separateVariablesFromConstantPart(VariableCoefficients &rhs)
Brings all variables of the right-hand side coefficients to the left-hand side by negating them and m...
VariableCoefficients & operator/=(VariableCoefficients &&other)
void setCoefficient(storm::expressions::Variable const &variable, ValueType const &coefficient)