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.

240 lines
5.1 KiB

3 months ago
  1. %module VariableT
  2. %{
  3. #include <carl/core/Variable.h>
  4. #include <carl/core/Monomial.h>
  5. #include <carl/core/Term.h>
  6. #include <carl/core/MultivariatePolynomial.h>
  7. #include <carl/core/RationalFunction.h>
  8. typedef mpq_class Rational;
  9. typedef carl::Term<Rational> Term;
  10. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  11. typedef carl::RationalFunction<Polynomial> RationalFunction;
  12. %}
  13. %include "std_string.i"
  14. %import "monomial.i"
  15. %import "term.i"
  16. %import "polynomial.i"
  17. %import "rationalfunction.i"
  18. //TODO: for 32 bit support this has to be changed..
  19. //%import <stddef> //for size_t maybe?
  20. typedef long unsigned int size_t; //this should be okay for 64 bits at least
  21. typedef mpq_class Rational;
  22. typedef carl::Term<Rational> Term;
  23. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  24. //typedef carl::RationalFunction<Polynomial> RationalFunction;
  25. namespace carl {
  26. %include "enums.swg"
  27. %javaconst(1);
  28. //maybe also define the toString for this enum, if needed
  29. //enum VariableType { VT_BOOL = 0, VT_REAL = 1, VT_INT = 2, VT_UNINTERPRETED = 3, VT_BITVECTOR = 4, MIN_TYPE = VT_BOOL, MAX_TYPE = VT_BITVECTOR};
  30. enum class VariableType { VT_BOOL = 0, VT_REAL = 1, VT_INT = 2, VT_UNINTERPRETED = 3, VT_BITVECTOR = 4, MIN_TYPE = VT_BOOL, MAX_TYPE = VT_BITVECTOR, TYPE_SIZE = MAX_TYPE - MIN_TYPE + 1 };
  31. class Variable {
  32. public:
  33. #ifdef VARIABLE_PASS_BY_VALUE
  34. /// Argument type for variables being function arguments.
  35. typedef Variable Arg;
  36. #else
  37. /// Argument type for variables being function arguments.
  38. typedef const Variable& Arg;
  39. #endif
  40. %extend {
  41. Variable(std::string name, carl::VariableType type) {
  42. //try and generate new variable from pool
  43. carl::Variable newVar;
  44. auto& pool = carl::VariablePool::getInstance();
  45. carl::Variable res = pool.findVariableWithName(name);
  46. if (res != carl::Variable::NO_VARIABLE) {
  47. newVar = res;
  48. } else {
  49. newVar = freshVariable(name, type);
  50. }
  51. return new carl::Variable(newVar);
  52. }
  53. Variable(carl::VariableType type) {
  54. carl::Variable newVar = freshVariable(type);
  55. return new carl::Variable(newVar);
  56. }
  57. std::string toString() {
  58. std::stringstream ss;
  59. ss << *$self;
  60. return ss.str();
  61. }
  62. }
  63. std::size_t getId() const;
  64. VariableType getType() const;
  65. std::string getName() const;
  66. std::size_t getRank();
  67. %extend {
  68. bool equals(carl::Variable::Arg other) {
  69. return *$self == other;
  70. }
  71. bool notEquals(carl::Variable::Arg other) {
  72. return *$self != other;
  73. }
  74. bool less(carl::Variable::Arg other) {
  75. return *$self < other;
  76. }
  77. bool lessEquals(carl::Variable::Arg other) {
  78. return *$self <= other;
  79. }
  80. bool greater(carl::Variable::Arg other) {
  81. return *$self > other;
  82. }
  83. bool greaterEquals(carl::Variable::Arg other) {
  84. return *$self >= other;
  85. }
  86. Polynomial add(const Polynomial& rhs) {
  87. return *($self)+rhs;
  88. }
  89. Polynomial add(const Term& rhs) {
  90. return *($self)+rhs;
  91. }
  92. Polynomial add(const carl::Monomial::Arg& rhs) {
  93. return carl::operator+<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(*($self),rhs);
  94. }
  95. Polynomial add(carl::Variable::Arg rhs) {
  96. return carl::operator+<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(*($self),rhs);
  97. }
  98. Polynomial add(const Rational& rhs) {
  99. return *($self)+Term(rhs);
  100. }
  101. Polynomial sub(const Polynomial& rhs) {
  102. return *($self)-rhs;
  103. }
  104. Polynomial sub(const Term& rhs) {
  105. return *($self)-rhs;
  106. }
  107. Polynomial sub(const carl::Monomial::Arg& rhs) {
  108. return carl::operator-<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(*($self),rhs);
  109. }
  110. Polynomial sub(carl::Variable::Arg rhs) {
  111. return carl::operator-<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(*($self),rhs);
  112. }
  113. Polynomial sub(const Rational& rhs) {
  114. return *($self)-Term(rhs);
  115. }
  116. Polynomial mul(const Polynomial& rhs) {
  117. return *($self)*rhs;
  118. }
  119. Term mul(const Term& rhs) {
  120. return *($self)*rhs;
  121. }
  122. Polynomial mul(const Monomial::Arg& rhs) {
  123. //const std::shared_ptr<const carl::Monomial> ptr(rhs);
  124. return carl::operator*(*($self),Polynomial(rhs));
  125. }
  126. Polynomial mul(carl::Variable::Arg rhs) {
  127. return carl::operator*(*($self),Polynomial(rhs));
  128. }
  129. Term mul(const Rational& rhs) {
  130. return *($self)*Term(rhs);
  131. }
  132. RationalFunction div(const carl::RationalFunction<Polynomial>& rhs) {
  133. return RationalFunction(*($self)) / rhs;
  134. }
  135. RationalFunction div(const Polynomial& rhs) {
  136. return RationalFunction(*($self)) / rhs;
  137. }
  138. RationalFunction div(const Term& rhs) {
  139. return RationalFunction(*($self)) / rhs;
  140. }
  141. RationalFunction div(const carl::Monomial::Arg& rhs) {
  142. return RationalFunction(*($self)) / rhs;
  143. }
  144. RationalFunction div(carl::Variable::Arg rhs) {
  145. return RationalFunction(*($self)) / rhs;
  146. }
  147. Polynomial div(const Rational& rhs) {
  148. if (carl::isZero(rhs)) throw std::runtime_error("Div by zero");
  149. return Polynomial(*($self)) / rhs;
  150. }
  151. Term neg() {
  152. return *$self*Rational(-1);
  153. }
  154. carl::Monomial::Arg pow(uint exp) {
  155. carl::Monomial m(*$self);
  156. return m.pow(exp);
  157. }
  158. }
  159. };
  160. }