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.

263 lines
7.6 KiB

2 months ago
  1. %module MonomialT
  2. %{
  3. #include <carl/core/Monomial.h>
  4. #include <carl/core/Term.h>
  5. #include <carl/core/MultivariatePolynomial.h>
  6. #include <carl/core/RationalFunction.h>
  7. #include "gmp.h"
  8. #include "gmpxx.h"
  9. typedef mpq_class Rational;
  10. typedef unsigned int uint;
  11. typedef std::pair<carl::Variable,uint> VarIntPair;
  12. typedef carl::Term<Rational> Term;
  13. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  14. typedef carl::RationalFunction<Polynomial> RationalFunction;
  15. %}
  16. %include "std_string.i"
  17. //TODO: for 32 bit support this has to be changed..
  18. //%import <stddef> //for size_t maybe?
  19. typedef long unsigned int size_t; //this should be okay for 64 bits at least
  20. typedef unsigned int uint;
  21. %include "std_pair.i"
  22. %include "std_vector.i"
  23. %import "variable.i"
  24. %import "term.i"
  25. %import "polynomial.i"
  26. %import "rationalfunction.i"
  27. %include "std_shared_ptr.i"
  28. %shared_ptr(carl::Monomial);
  29. typedef std::pair<carl::Variable,uint> VarIntPair;
  30. namespace std {
  31. %template(VarIntPair) pair<carl::Variable,uint>;
  32. %template(VarIntPairVector) vector<VarIntPair>;
  33. }
  34. typedef carl::Term<Rational> Term;
  35. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  36. typedef carl::RationalFunction<Polynomial> RationalFunction;
  37. %nodefaultctor carl::Monomial;
  38. namespace carl {
  39. typedef unsigned int exponent;
  40. class Monomial {
  41. public:
  42. typedef std::shared_ptr<const carl::Monomial> Arg;
  43. typedef std::vector<VarIntPair> Content;
  44. /*
  45. explicit Monomial(Variable::Arg v, carl::exponent e = 1) :
  46. mExponents(1, std::make_pair(v,e)),
  47. mTotalDegree(e)
  48. {
  49. }
  50. */
  51. carl::exponent tdeg() const;
  52. const carl::Monomial::Content& exponents() const;
  53. std::string toString(bool infix = true, bool friendlyVarNames = true) const;
  54. size_t nrVariables() const;
  55. %extend {
  56. static carl::Monomial::Arg createMonomial(Variable::Arg v, carl::exponent e = 1) {
  57. return carl::MonomialPool::getInstance().create(v, e);
  58. }
  59. Polynomial add(const Polynomial& rhs) {
  60. carl::Monomial::Content exp($self->exponents());
  61. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  62. return ptr+rhs;
  63. }
  64. Polynomial add(const Term& rhs) {
  65. carl::Monomial::Content exp($self->exponents());
  66. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  67. return ptr+rhs;
  68. }
  69. Polynomial add(const Monomial::Arg& rhs) {
  70. carl::Monomial::Content exp($self->exponents());
  71. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  72. return carl::operator+<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(ptr,rhs);
  73. }
  74. Polynomial add(carl::Variable::Arg rhs) {
  75. carl::Monomial::Content exp($self->exponents());
  76. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  77. return carl::operator+<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(ptr,rhs);
  78. }
  79. Polynomial add(const Rational& rhs) {
  80. carl::Monomial::Content exp($self->exponents());
  81. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  82. return ptr+rhs;
  83. }
  84. Polynomial sub(const Polynomial& rhs) {
  85. carl::Monomial::Content exp($self->exponents());
  86. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  87. return ptr-rhs;
  88. }
  89. Polynomial sub(const Term& rhs) {
  90. carl::Monomial::Content exp($self->exponents());
  91. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  92. return ptr-rhs;
  93. }
  94. Polynomial sub(const Monomial::Arg& rhs) {
  95. carl::Monomial::Content exp($self->exponents());
  96. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  97. return carl::operator-<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(ptr,rhs);
  98. }
  99. Polynomial sub(carl::Variable::Arg rhs) {
  100. carl::Monomial::Content exp($self->exponents());
  101. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  102. return carl::operator-<Rational,carl::GrLexOrdering,carl::StdMultivariatePolynomialPolicies<>>(ptr,rhs);
  103. }
  104. Polynomial sub(const Rational& rhs) {
  105. carl::Monomial::Content exp($self->exponents());
  106. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  107. return ptr-rhs;
  108. }
  109. Polynomial mul(const Polynomial& rhs) {
  110. carl::Monomial::Content exp($self->exponents());
  111. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  112. return rhs*ptr;
  113. }
  114. Term mul(const Term& rhs) {
  115. carl::Monomial::Content exp($self->exponents());
  116. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  117. return ptr*rhs;
  118. }
  119. Polynomial mul(const Monomial::Arg& rhs) {
  120. carl::Monomial::Content exp($self->exponents());
  121. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  122. //const std::shared_ptr<const carl::Monomial> ptr2(rhs);
  123. return carl::operator*(ptr,Polynomial(rhs));
  124. }
  125. Polynomial mul(carl::Variable::Arg rhs) {
  126. carl::Monomial::Content exp($self->exponents());
  127. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  128. return carl::operator*(ptr,Polynomial(rhs));
  129. }
  130. Term mul(const Rational& rhs) {
  131. carl::Monomial::Content exp($self->exponents());
  132. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  133. return ptr*rhs;
  134. }
  135. RationalFunction div(const RationalFunction& rhs) {
  136. carl::Monomial::Content exp($self->exponents());
  137. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  138. return RationalFunction(Polynomial(ptr)) / rhs;
  139. }
  140. RationalFunction div(const Polynomial& rhs) {
  141. carl::Monomial::Content exp($self->exponents());
  142. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  143. return RationalFunction(Polynomial(ptr)) / rhs;
  144. }
  145. RationalFunction div(const Term& rhs) {
  146. carl::Monomial::Content exp($self->exponents());
  147. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  148. return RationalFunction(Polynomial(ptr)) / rhs;
  149. }
  150. RationalFunction div(const Monomial::Arg& rhs) {
  151. carl::Monomial::Content exp($self->exponents());
  152. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  153. return RationalFunction(Polynomial(ptr)) / rhs;
  154. }
  155. RationalFunction div(carl::Variable::Arg rhs) {
  156. carl::Monomial::Content exp($self->exponents());
  157. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  158. return RationalFunction(Polynomial(ptr)) / rhs;
  159. }
  160. Term div(const Rational& rhs) {
  161. carl::Monomial::Content exp($self->exponents());
  162. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  163. return Term(ptr) / rhs;
  164. }
  165. Term neg() {
  166. carl::Monomial::Content exp($self->exponents());
  167. const std::shared_ptr<const carl::Monomial> ptr = std::make_shared<const carl::Monomial>(std::move(exp));
  168. return ptr*Rational(-1);
  169. }
  170. carl::Monomial::Arg pow(uint exp) {
  171. return $self->pow(exp);
  172. }
  173. VarIntPair getItem(std::size_t index) {
  174. return *($self->begin()+index);
  175. }
  176. }
  177. };
  178. }