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.

238 lines
3.8 KiB

2 months ago
  1. %module Rational
  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 <carl/numbers/numbers.h>
  8. #include "gmp.h"
  9. #include "gmpxx.h"
  10. typedef mpq_class Rational;
  11. typedef carl::Term<Rational> Term;
  12. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  13. typedef carl::RationalFunction<Polynomial> RationalFunction;
  14. %}
  15. %include "std_string.i"
  16. %import "monomial.i"
  17. %import "variable.i"
  18. %import "term.i"
  19. %import "polynomial.i"
  20. %import "rationalfunction.i"
  21. namespace carl {
  22. typedef long sint;
  23. }
  24. %rename(Rational) mpq_class;
  25. class mpq_class {
  26. //NOTE: maybe change this to getDouble or whatever..
  27. %rename(toDouble) get_d;
  28. public:
  29. mpq_class(signed int i) { mpq_init(mp); mpq_set_si(mp, i, 1); }
  30. mpq_class(double d) { mpq_init(mp); mpq_set_d(mp, d); }
  31. explicit mpq_class(const std::string &s, int base = 0)
  32. {
  33. mpq_init(mp);
  34. if (mpq_set_str (mp, s.c_str(), base) != 0)
  35. {
  36. mpq_clear (mp);
  37. throw std::invalid_argument ("mpq_set_str");
  38. }
  39. }
  40. double get_d() const;
  41. %extend{
  42. std::string toString() {
  43. return carl::toString(*$self,true);
  44. }
  45. int nominator() {
  46. return carl::toInt<carl::sint>(carl::getNum(*$self));
  47. }
  48. int numerator() {
  49. return carl::toInt<carl::sint>(carl::getNum(*$self));
  50. }
  51. int denominator() {
  52. return carl::toInt<carl::sint>(carl::getDenom(*$self));
  53. }
  54. carl::sint toInt() {
  55. double d = carl::toDouble(*$self);
  56. return static_cast<carl::sint>(d);
  57. }
  58. Polynomial add(const Polynomial & rhs) {
  59. return *$self+rhs;
  60. }
  61. Polynomial add(const Term& rhs) {
  62. return *$self+rhs;
  63. }
  64. Polynomial add(const carl::Monomial::Arg& rhs) {
  65. return *$self+rhs;
  66. }
  67. Polynomial add(carl::Variable::Arg rhs) {
  68. return *$self+rhs;
  69. }
  70. Rational add(const Rational& rhs) {
  71. return *$self+rhs;
  72. }
  73. Polynomial sub(const Polynomial & rhs) {
  74. return *$self-rhs;
  75. }
  76. Polynomial sub(const Term& rhs) {
  77. return *$self-rhs;
  78. }
  79. Polynomial sub(const carl::Monomial::Arg& rhs) {
  80. return *$self-rhs;
  81. }
  82. Polynomial sub(carl::Variable::Arg rhs) {
  83. return *$self-rhs;
  84. }
  85. Rational sub(const Rational& rhs) {
  86. return *$self-rhs;
  87. }
  88. Polynomial mul(const Polynomial & rhs) {
  89. return *$self*rhs;
  90. }
  91. Term mul(const Term& rhs) {
  92. return *$self*rhs;
  93. }
  94. Term mul(const carl::Monomial::Arg& rhs) {
  95. return *$self*rhs;
  96. }
  97. Term mul(carl::Variable::Arg rhs) {
  98. return *$self*rhs;
  99. }
  100. Rational mul(const Rational& rhs) {
  101. return *$self*rhs;
  102. }
  103. RationalFunction div(const RationalFunction& rhs) {
  104. return RationalFunction(*$self) / rhs;
  105. }
  106. RationalFunction div(const Polynomial& rhs) {
  107. return RationalFunction(*$self) / rhs;
  108. }
  109. RationalFunction div(const Term& rhs) {
  110. return RationalFunction(*$self) / rhs;
  111. }
  112. RationalFunction div(const carl::Monomial::Arg& rhs) {
  113. return RationalFunction(*$self) / rhs;
  114. }
  115. RationalFunction div(carl::Variable::Arg rhs) {
  116. return RationalFunction(*$self) / rhs;
  117. }
  118. Rational div(const Rational& rhs) {
  119. if (carl::isZero(rhs)) throw std::runtime_error("Div by zero");
  120. return *$self / rhs;
  121. }
  122. Rational pow(std::size_t exp) {
  123. return pow($self->get_d(),exp);
  124. }
  125. Rational neg() {
  126. return -(*$self);
  127. }
  128. Rational abs() {
  129. return abs(*$self);
  130. }
  131. bool equals(const Rational& rhs) {
  132. return *$self==rhs;
  133. }
  134. bool notEquals(const Rational& rhs) {
  135. return *$self!=rhs;
  136. }
  137. bool lessEquals(const Rational& rhs) {
  138. return *$self<=rhs;
  139. }
  140. bool greaterEquals(const Rational& rhs) {
  141. return *$self>=rhs;
  142. }
  143. bool less(const Rational& rhs) {
  144. return *$self<rhs;
  145. }
  146. bool greater(const Rational& rhs) {
  147. return *$self>rhs;
  148. }
  149. }
  150. };
  151. typedef mpq_class Rational;
  152. /*
  153. typedef carl::Term<Rational> Term;
  154. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  155. typedef carl::RationalFunction<Polynomial> RationalFunction; */