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.

184 lines
7.7 KiB

2 months ago
  1. import pycarl
  2. from pycarl._config import CARL_WITH_CLN as has_cln
  3. def convert_integer(integer):
  4. """
  5. Convert integer to gmp.
  6. :param integer: integer.
  7. :return: gmp interger.
  8. """
  9. if has_cln and isinstance(integer, pycarl.cln.Integer):
  10. return pycarl.gmp.Integer(integer)
  11. elif isinstance(integer, pycarl.gmp.Integer):
  12. return integer
  13. elif isinstance(integer, int):
  14. return pycarl.gmp.Integer(integer)
  15. else:
  16. raise TypeError("Integer of type {} cannot be convert to gmp".format(type(integer)))
  17. def convert_rational(rational):
  18. """
  19. Convert rational number to gmp.
  20. :param rational: rational number.
  21. :return: gmp rational.
  22. """
  23. if has_cln and isinstance(rational, pycarl.cln.Rational):
  24. return pycarl.gmp.Rational(rational)
  25. elif isinstance(rational, pycarl.gmp.Rational):
  26. return rational
  27. elif isinstance(rational, float):
  28. return pycarl.gmp.Rational(rational)
  29. else:
  30. raise TypeError("Rational of type {} cannot be convert to gmp".format(type(rational)))
  31. def convert_term(term):
  32. """
  33. Convert term to gmp.
  34. :param term: term.
  35. :return: gmp term.
  36. """
  37. if has_cln and isinstance(term, pycarl.cln.Term):
  38. coeff = convert_rational(term.coeff)
  39. return pycarl.gmp.Term(coeff, term.monomial)
  40. elif isinstance(term, pycarl.gmp.Term):
  41. return term
  42. else:
  43. raise TypeError("Term of type {} cannot be convert to gmp".format(type(term)))
  44. def convert_polynomial(polynomial):
  45. """
  46. Convert polynomial to gmp.
  47. :param polynomial: polynomial.
  48. :return: gmp polynomial.
  49. """
  50. if has_cln and isinstance(polynomial, pycarl.cln.Polynomial):
  51. terms = []
  52. for term in polynomial:
  53. terms.append(convert_term(term))
  54. return pycarl.gmp.Polynomial(terms)
  55. elif isinstance(polynomial, pycarl.gmp.Polynomial):
  56. return polynomial
  57. else:
  58. raise TypeError("Polynomial of type {} cannot be convert to gmp".format(type(polynomial)))
  59. def convert_rational_function(ratfunc):
  60. """
  61. Convert rational function to gmp.
  62. :param ratfunc: rational function.
  63. :return: gmp rational function.
  64. """
  65. if has_cln and isinstance(ratfunc, pycarl.cln.RationalFunction):
  66. numerator = convert_polynomial(ratfunc.numerator)
  67. denominator = convert_polynomial(ratfunc.denominator)
  68. return pycarl.gmp.RationalFunction(numerator, denominator)
  69. elif isinstance(ratfunc, pycarl.gmp.RationalFunction):
  70. return ratfunc
  71. else:
  72. raise TypeError("Rational function of type {} cannot be convert to gmp".format(type(ratfunc)))
  73. def convert_factorized_polynomial(polynomial):
  74. """
  75. Convert factorized polynomial to gmp.
  76. :param polynomial: factorized polynomial.
  77. :return: gmp factorized polynomial.
  78. """
  79. if has_cln and isinstance(polynomial, pycarl.cln.FactorizedPolynomial):
  80. coefficient = convert_rational(polynomial.coefficient)
  81. converted = pycarl.gmp.FactorizedPolynomial(coefficient)
  82. for (factor, exponent) in polynomial.factorization():
  83. pol = convert_polynomial(factor.polynomial())
  84. factorized = pycarl.gmp.create_factorized_polynomial(pol)
  85. converted *= factorized ** exponent
  86. return converted
  87. elif isinstance(polynomial, pycarl.gmp.FactorizedPolynomial):
  88. return polynomial
  89. else:
  90. raise TypeError("Factorized polynomial of type {} cannot be convert to gmp".format(type(polynomial)))
  91. def convert_factorized_rational_function(ratfunc):
  92. """
  93. Convert factorized rational function to gmp.
  94. :param ratfunc: factorized rational function.
  95. :return: gmp factorized rational function.
  96. """
  97. if has_cln and isinstance(ratfunc, pycarl.cln.FactorizedRationalFunction):
  98. numerator = convert_factorized_polynomial(ratfunc.numerator)
  99. denominator = convert_factorized_polynomial(ratfunc.denominator)
  100. return pycarl.gmp.FactorizedRationalFunction(numerator, denominator)
  101. elif isinstance(ratfunc, pycarl.gmp.FactorizedRationalFunction):
  102. return ratfunc
  103. else:
  104. raise TypeError("Factorized rational function of type {} cannot be convert to gmp".format(type(ratfunc)))
  105. def convert_constraint(constraint):
  106. """
  107. Convert constraint to gmp.
  108. :param constraint: constraint.
  109. :return: gmp constraint.
  110. """
  111. if has_cln and isinstance(constraint, pycarl.cln.formula.Constraint):
  112. lhs = convert_polynomial(constraint.lhs)
  113. return pycarl.gmp.formula.Constraint(lhs, constraint.relation)
  114. elif isinstance(constraint, pycarl.gmp.formula.Constraint):
  115. return constraint
  116. else:
  117. raise TypeError("Constraint of type {} cannot be convert to gmp".format(type(constraint)))
  118. def convert_formula(formula):
  119. if isinstance(formula, pycarl.gmp.formula.Formula):
  120. return formula
  121. if formula.type == pycarl.formula.FormulaType.TRUE:
  122. return pycarl.gmp.formula.Formula(pycarl.gmp.formula.Constraint(True))
  123. if formula.type == pycarl.formula.FormulaType.FALSE:
  124. return pycarl.gmp.formula.Formula(pycarl.gmp.formula.Constraint(False))
  125. if formula.type == pycarl.formula.FormulaType.CONSTRAINT:
  126. return convert_constraint(formula.get_constraint())
  127. if has_cln and isinstance(formula, pycarl.cln.formula.Formula):
  128. csubformulae = [pycarl.gmp.formula.Formula(convert(subf)) for subf in formula.get_subformulas()]
  129. return pycarl.gmp.formula.Formula(formula.type, csubformulae)
  130. else:
  131. raise TypeError("Formula of type {} cannot be convert to gmp".format(type(formula)))
  132. def convert(data):
  133. """
  134. Convert arbitrary data type to gmp.
  135. :param data: data structure.
  136. :return: gmp data structure.
  137. """
  138. if (has_cln and isinstance(data, pycarl.cln.Integer)) or isinstance(data, pycarl.gmp.Integer) or isinstance(data,
  139. int):
  140. return convert_integer(data)
  141. elif (has_cln and isinstance(data, pycarl.cln.Rational)) or isinstance(data, pycarl.gmp.Rational) or isinstance(
  142. data, float):
  143. return convert_rational(data)
  144. elif (has_cln and isinstance(data, pycarl.cln.Term)) or isinstance(data, pycarl.gmp.Term):
  145. return convert_term(data)
  146. elif (has_cln and isinstance(data, pycarl.cln.Polynomial)) or isinstance(data, pycarl.gmp.Polynomial):
  147. return convert_polynomial(data)
  148. elif (has_cln and isinstance(data, pycarl.cln.RationalFunction)) or isinstance(data, pycarl.gmp.RationalFunction):
  149. return convert_rational_function(data)
  150. elif (has_cln and isinstance(data, pycarl.cln.FactorizedPolynomial)) or isinstance(data,
  151. pycarl.gmp.FactorizedPolynomial):
  152. return convert_factorized_polynomial(data)
  153. elif (has_cln and isinstance(data, pycarl.cln.FactorizedRationalFunction)) or isinstance(data,
  154. pycarl.gmp.FactorizedRationalFunction):
  155. return convert_factorized_rational_function(data)
  156. elif (has_cln and isinstance(data, pycarl.cln.formula.Constraint)) or isinstance(data,
  157. pycarl.gmp.formula.Constraint):
  158. return convert_constraint(data)
  159. elif (has_cln and isinstance(data, pycarl.cln.formula.Formula)) or isinstance(data,
  160. pycarl.gmp.formula.Formula):
  161. return convert_formula(data)
  162. else:
  163. raise TypeError("Unknown type {} for conversion to gmp".format(type(data)))