// // Created by Jip Spel on 24.09.18. // #include "RationalFunctionToExpression.h" #include "storm/utility/constants.h" namespace storm { namespace expressions { template RationalFunctionToExpression::RationalFunctionToExpression(std::shared_ptr manager) : manager(manager) { // Intentionally left empty. } template std::shared_ptr RationalFunctionToExpression::getManager() { return manager; } template Expression RationalFunctionToExpression::toExpression(ValueType function) { function.simplify(); auto varsFunction = function.gatherVariables(); for (auto var : varsFunction) { auto varsManager = manager->getVariables(); bool found = find_if(varsManager.begin(), varsManager.end(), [&](auto val) -> bool { return val.getName() == var.name(); }) != varsManager.end(); if (!found) { manager->declareRationalVariable(var.name()); } } auto denominator = function.denominator(); if (!denominator.isConstant()) { STORM_LOG_DEBUG("Expecting the denominator to be constant"); } storm::expressions::Expression result; if (function.isConstant()) { result = manager->rational(storm::utility::convertNumber(function.constantPart())); } else { auto nominator = function.nominatorAsPolynomial().polynomialWithCoefficient(); result = manager->rational(storm::utility::convertNumber(nominator.constantPart())); for (auto itr = nominator.begin(); itr != nominator.end(); ++itr) { varsFunction.clear(); itr->gatherVariables(varsFunction); storm::expressions::Expression nominatorPartExpr = manager->rational(storm::utility::convertNumber(itr->coeff())); for (auto var : varsFunction) { nominatorPartExpr = nominatorPartExpr * storm::expressions::pow(manager->getVariable(var.name()), manager->rational(storm::utility::convertNumber(itr->monomial()->exponentOfVariable(var)))); } if (varsFunction.size() >= 1) { result = result + nominatorPartExpr; } } storm::expressions::Expression denominatorVal = manager->rational(storm::utility::convertNumber(denominator.constantPart())); result = result / denominatorVal; } return result; } template class RationalFunctionToExpression; } }