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.

207 lines
4.0 KiB

3 months ago
  1. %module PolynomialT
  2. %{
  3. #include <carl/core/MultivariatePolynomial.h>
  4. #include <carl/core/RationalFunction.h>
  5. #include "gmp.h"
  6. #include "gmpxx.h"
  7. typedef mpq_class Rational;
  8. typedef carl::Term<Rational> Term;
  9. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  10. typedef carl::RationalFunction<Polynomial> RationalFunction;
  11. typedef unsigned int uint;
  12. typedef std::pair<carl::Variable,uint> VarIntPair;
  13. %}
  14. %include "std_string.i"
  15. //TODO: for 32 bit support this has to be changed..
  16. //%import <stddef> //for size_t maybe?
  17. typedef long unsigned int size_t; //this should be okay for 64 bits at least
  18. %import "rational.i"
  19. %import "variable.i"
  20. %import "term.i"
  21. %import "monomial.i"
  22. %import "rationalfunction.i"
  23. %include "std_vector.i"
  24. typedef mpq_class Rational;
  25. typedef carl::RationalFunction<Polynomial> RationalFunction;
  26. namespace std {
  27. %template(VarVector) vector<carl::Variable>;
  28. }
  29. namespace carl {
  30. template<typename Coeff>
  31. class MultivariatePolynomial
  32. {
  33. public:
  34. typedef Coeff CoeffType;
  35. typedef Coeff NumberType; //ATTENTION: This is only correct if polynomials are never instantiated with a type that's not a number
  36. explicit MultivariatePolynomial(const carl::Term<Coeff>& t);
  37. explicit MultivariatePolynomial(const std::shared_ptr<const carl::Monomial>& m);
  38. explicit MultivariatePolynomial(Variable::Arg v);
  39. explicit MultivariatePolynomial(const Coeff& c);
  40. const Coeff& constantPart() const;
  41. template<typename SubstitutionType = Coeff>
  42. SubstitutionType evaluate(const std::map<Variable, SubstitutionType>& substitutions) const;
  43. std::size_t totalDegree() const;
  44. std::size_t degree(Variable::Arg var) const;
  45. size_t nrTerms() const;
  46. std::string toString(bool infix=true, bool friendlyVarNames=true) const;
  47. size_t size() const;
  48. %extend{
  49. bool equals(const MultivariatePolynomial<Coeff>& other) {
  50. return *$self == other;
  51. }
  52. bool notEquals(const MultivariatePolynomial<Coeff>& other) {
  53. return *$self != other;
  54. }
  55. std::vector<carl::Variable> gatherVariables() const {
  56. std::set<carl::Variable> asSet = $self->gatherVariables();
  57. return std::vector<carl::Variable>(asSet.begin(),asSet.end());
  58. }
  59. Polynomial add(const Polynomial& rhs) {
  60. return *$self+rhs;
  61. }
  62. Polynomial add(const Term& rhs) {
  63. return *$self+rhs;
  64. }
  65. Polynomial add(const Monomial::Arg& rhs) {
  66. return *$self+rhs;
  67. }
  68. Polynomial add(carl::Variable::Arg rhs) {
  69. return *$self+rhs;
  70. }
  71. Polynomial add(const Rational& rhs) {
  72. return *$self+rhs;
  73. }
  74. Polynomial sub(const Polynomial& rhs) {
  75. return *$self-rhs;
  76. }
  77. Polynomial sub(const Term& rhs) {
  78. return *$self-rhs;
  79. }
  80. Polynomial sub(const Monomial::Arg& rhs) {
  81. return *$self-rhs;
  82. }
  83. Polynomial sub(carl::Variable::Arg rhs) {
  84. return *$self-rhs;
  85. }
  86. Polynomial sub(const Rational& rhs) {
  87. return *$self-rhs;
  88. }
  89. Polynomial mul(const Polynomial& rhs) {
  90. return *$self*rhs;
  91. }
  92. Polynomial mul(const Term& rhs) {
  93. return *$self*rhs;
  94. }
  95. Polynomial mul(const Monomial::Arg& rhs) {
  96. return *$self*rhs;
  97. }
  98. Polynomial mul(carl::Variable::Arg rhs) {
  99. return *$self*rhs;
  100. }
  101. Polynomial mul(const Rational& rhs) {
  102. return *$self*rhs;
  103. }
  104. RationalFunction div(const RationalFunction& rhs) {
  105. return RationalFunction(*$self) / rhs;
  106. }
  107. RationalFunction div(const Polynomial& rhs) {
  108. return RationalFunction(*$self) / rhs;
  109. }
  110. RationalFunction div(const Term& rhs) {
  111. return RationalFunction(*$self) / rhs;
  112. }
  113. RationalFunction div(const Monomial::Arg& rhs) {
  114. return RationalFunction(*$self) / rhs;
  115. }
  116. RationalFunction div(carl::Variable::Arg rhs) {
  117. return RationalFunction(*$self) / rhs;
  118. }
  119. Polynomial div(const Rational& rhs) {
  120. return *$self / rhs;
  121. }
  122. Polynomial pow(uint exp) {
  123. return $self->pow(exp);
  124. }
  125. Polynomial neg() {
  126. return *$self*Rational(-1);
  127. }
  128. Term getItem(std::size_t index) {
  129. return *($self->begin()+index);
  130. }
  131. }
  132. };
  133. }
  134. %include "std_map.i"
  135. namespace std {
  136. %template(VarRationalMap) map<carl::Variable,Rational>;
  137. }
  138. %template(evaluate) carl::MultivariatePolynomial::evaluate<Rational>;
  139. %template(Polynomial) carl::MultivariatePolynomial<Rational>;