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.

94 lines
4.5 KiB

2 months ago
  1. import pycarl
  2. class TestGmpConvert():
  3. def test_convert_int(self):
  4. original = 43
  5. assert isinstance(original, int)
  6. converted = pycarl.convert.convert_to_gmp(original)
  7. assert isinstance(converted, pycarl.gmp.Integer)
  8. def test_convert_integer(self):
  9. original = pycarl.gmp.Integer(23)
  10. assert isinstance(original, pycarl.gmp.Integer)
  11. converted = pycarl.convert.convert_to_gmp(original)
  12. assert isinstance(converted, pycarl.gmp.Integer)
  13. def test_convert_float(self):
  14. original = -3.4
  15. assert isinstance(original, float)
  16. converted = pycarl.convert.convert_to_gmp(original)
  17. assert isinstance(converted, pycarl.gmp.Rational)
  18. def test_convert_rational(self):
  19. original = pycarl.gmp.Rational(pycarl.gmp.Integer(12), pycarl.gmp.Integer(-4))
  20. assert isinstance(original, pycarl.gmp.Rational)
  21. converted = pycarl.convert.convert_to_gmp(original)
  22. assert isinstance(converted, pycarl.gmp.Rational)
  23. def test_convert_term(self):
  24. pycarl.clear_pools()
  25. var = pycarl.Variable("n")
  26. rational = pycarl.gmp.Integer(-1) / pycarl.gmp.Integer(6)
  27. monomial = pycarl.create_monomial(var, 3)
  28. original = pycarl.gmp.Term(rational, monomial)
  29. assert isinstance(original, pycarl.gmp.Term)
  30. converted = pycarl.convert.convert_to_gmp(original)
  31. assert isinstance(converted, pycarl.gmp.Term)
  32. def test_convert_polynomial(self):
  33. pycarl.clear_pools()
  34. var1 = pycarl.Variable("n")
  35. var2 = pycarl.Variable("o")
  36. original = pycarl.gmp.Polynomial(5) * var2 * var1 + var2 * var2 + pycarl.gmp.Integer(3)
  37. assert isinstance(original, pycarl.gmp.Polynomial)
  38. converted = pycarl.convert.convert_to_gmp(original)
  39. assert isinstance(converted, pycarl.gmp.Polynomial)
  40. def test_convert_rational_function(self):
  41. pycarl.clear_pools()
  42. var1 = pycarl.Variable("x")
  43. var2 = pycarl.Variable("y")
  44. pol1 = var1 * var2 + pycarl.gmp.Integer(42)
  45. pol2 = var1 + pycarl.gmp.Integer(-1)
  46. original = pycarl.gmp.RationalFunction(pol1, pol2)
  47. assert isinstance(original, pycarl.gmp.RationalFunction)
  48. converted = pycarl.convert.convert_to_gmp(original)
  49. assert isinstance(converted, pycarl.gmp.RationalFunction)
  50. def test_convert_factorized_polynomial(self):
  51. pycarl.clear_pools()
  52. var1 = pycarl.Variable("a")
  53. var2 = pycarl.Variable("b")
  54. pol1 = pycarl.gmp.create_factorized_polynomial(pycarl.gmp.Polynomial(4) * (var2 + pycarl.gmp.Integer(-2)))
  55. pol2 = pycarl.gmp.create_factorized_polynomial(pycarl.gmp.Polynomial(var1) - 3)
  56. pol3 = pycarl.gmp.create_factorized_polynomial(pycarl.gmp.Polynomial(2) * var2)
  57. original = pol1 * pol2 * pol3
  58. assert isinstance(original, pycarl.gmp.FactorizedPolynomial)
  59. converted = pycarl.convert.convert_to_gmp(original)
  60. assert isinstance(converted, pycarl.gmp.FactorizedPolynomial)
  61. assert len(converted.factorization()) == len(original.factorization())
  62. def test_convert_factorized_rational_function(self):
  63. pycarl.clear_pools()
  64. var1 = pycarl.Variable("a")
  65. var2 = pycarl.Variable("b")
  66. pol1 = pycarl.gmp.create_factorized_polynomial(pycarl.gmp.Polynomial(55) * (var1 + pycarl.gmp.Integer(20)))
  67. pol2 = pycarl.gmp.create_factorized_polynomial(pycarl.gmp.Polynomial(var2) - 2)
  68. pol3 = pycarl.gmp.create_factorized_polynomial(pycarl.gmp.Polynomial(562) * var2)
  69. original = pycarl.gmp.FactorizedRationalFunction(pol1 * pol2, pol2 * pol3)
  70. assert isinstance(original, pycarl.gmp.FactorizedRationalFunction)
  71. converted = pycarl.convert.convert_to_gmp(original)
  72. assert isinstance(converted, pycarl.gmp.FactorizedRationalFunction)
  73. assert len(converted.numerator.factorization()) == len(original.numerator.factorization())
  74. assert len(converted.denominator.factorization()) == len(original.denominator.factorization())
  75. def test_convert_constraint(self):
  76. pycarl.clear_pools()
  77. var1 = pycarl.Variable("a")
  78. pol1 = pycarl.gmp.Polynomial(2) * var1 * var1 + var1 + pycarl.gmp.Integer(4)
  79. original = pycarl.gmp.formula.Constraint(pol1, pycarl.formula.Relation.GREATER)
  80. assert isinstance(original, pycarl.gmp.formula.Constraint)
  81. converted = pycarl.convert.convert_to_gmp(original)
  82. assert isinstance(converted, pycarl.gmp.formula.Constraint)
  83. assert converted.relation == original.relation