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.

177 lines
7.1 KiB

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