The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

96 lines
4.5 KiB

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