You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
3.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // Created by Jip Spel on 24.09.18.
  3. //
  4. #include "RationalFunctionToExpression.h"
  5. #include "storm/utility/constants.h"
  6. namespace storm {
  7. namespace expressions {
  8. template <typename ValueType>
  9. RationalFunctionToExpression<ValueType>::RationalFunctionToExpression(std::shared_ptr<ExpressionManager> manager) : manager(manager) {
  10. // Intentionally left empty.
  11. }
  12. template <typename ValueType>
  13. std::shared_ptr<ExpressionManager> RationalFunctionToExpression<ValueType>::getManager() {
  14. return manager;
  15. }
  16. template <typename ValueType>
  17. Expression RationalFunctionToExpression<ValueType>::toExpression(ValueType function) {
  18. function.simplify();
  19. auto varsFunction = function.gatherVariables();
  20. for (auto var : varsFunction) {
  21. auto varsManager = manager->getVariables();
  22. bool found = find_if(varsManager.begin(), varsManager.end(),
  23. [&](auto val) -> bool {
  24. return val.getName() == var.name();
  25. }) != varsManager.end();
  26. if (!found) {
  27. manager->declareRationalVariable(var.name());
  28. }
  29. }
  30. auto denominator = function.denominator();
  31. if (!denominator.isConstant()) {
  32. STORM_LOG_DEBUG("Expecting the denominator to be constant");
  33. }
  34. storm::expressions::Expression result;
  35. if (function.isConstant()) {
  36. result = manager->rational(storm::utility::convertNumber<storm::RationalNumber, storm::RationalFunctionCoefficient>(function.constantPart()));
  37. } else {
  38. auto nominator = function.nominatorAsPolynomial().polynomialWithCoefficient();
  39. result = manager->rational(storm::utility::convertNumber<storm::RationalNumber, storm::RationalFunctionCoefficient>(nominator.constantPart()));
  40. for (auto itr = nominator.begin(); itr != nominator.end(); ++itr) {
  41. varsFunction.clear();
  42. itr->gatherVariables(varsFunction);
  43. storm::expressions::Expression nominatorPartExpr = manager->rational(storm::utility::convertNumber<storm::RationalNumber, storm::RationalFunctionCoefficient>(itr->coeff()));
  44. for (auto var : varsFunction) {
  45. nominatorPartExpr = nominatorPartExpr * storm::expressions::pow(manager->getVariable(var.name()), manager->rational(storm::utility::convertNumber<storm::RationalNumber, storm::RationalFunctionCoefficient>(itr->monomial()->exponentOfVariable(var))));
  46. }
  47. if (varsFunction.size() >= 1) {
  48. result = result + nominatorPartExpr;
  49. }
  50. }
  51. storm::expressions::Expression denominatorVal = manager->rational(storm::utility::convertNumber<storm::RationalNumber, storm::RationalFunctionCoefficient>(denominator.constantPart()));
  52. result = result / denominatorVal;
  53. }
  54. return result;
  55. }
  56. template class RationalFunctionToExpression<storm::RationalFunction>;
  57. }
  58. }