Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. //author: teluguboy
  2. //date: 23-02-20
  3.  
  4. #include "polynomial.hpp"
  5.  
  6. using namespace std;
  7.  
  8. monomial operator * (const monomial& lhs, const monomial& rhs) {
  9. monomial res(lhs.coeff * rhs.coeff, lhs.vars_exps);
  10. for (const pair<const string, int>& vars : rhs.vars_exps) {
  11. if (res.vars_exps.count(vars.first)) {
  12. res.vars_exps[vars.first] += vars.second;
  13. }
  14. else {
  15. res.vars_exps[vars.first] = vars.second;
  16. }
  17. }
  18. return res;
  19. }
  20.  
  21. monomial operator ^ (const monomial& base, int pow) {
  22. monomial res;
  23. res.coeff = 1;
  24. for (int i = 0; i < pow; i++) {
  25. res = res * res;
  26. }
  27. return res;
  28. }
  29.  
  30. bool operator == (const monomial& lhs, const monomial& rhs) {
  31. if (lhs.vars_exps == rhs.vars_exps && lhs.coeff == rhs.coeff) {
  32. return true;
  33. }
  34. else {
  35. return false;
  36. }
  37. }
  38.  
  39. bool operator != (const monomial& lhs, const monomial& rhs) {
  40. return !(lhs == rhs);
  41. }
  42.  
  43. polynomial operator + (const monomial& lhs, const monomial& rhs)
  44. {
  45. polynomial res;
  46. res.expression.push_back(lhs);
  47. res.expression.push_back(rhs);
  48. res.simplify();
  49. return res;
  50. }
  51.  
  52. polynomial operator + (const monomial& lhs, const polynomial& rhs) {
  53. polynomial res(rhs);
  54. res.expression.push_back(lhs);
  55. res.simplify();
  56. return res;
  57. }
  58.  
  59. polynomial operator + (const polynomial& lhs, const monomial& rhs) {
  60. return rhs + lhs;
  61. }
  62.  
  63. polynomial operator + (const polynomial& lhs, const polynomial& rhs)
  64. {
  65. polynomial res(lhs);
  66. for (const monomial& mono : rhs.expression) {
  67. res.expression.push_back(mono);
  68. }
  69. res.simplify();
  70. return res;
  71. }
  72.  
  73. polynomial operator - (const polynomial&lhs, const polynomial&rhs) {
  74. polynomial res(lhs);
  75. vector<monomial> tmp = rhs.expression;
  76. for (monomial& mono : tmp) {
  77. mono.coeff *= -1;
  78. res.expression.push_back(mono);
  79. mono.coeff *= -1;
  80. }
  81. res.simplify();
  82. return res;
  83. }
  84.  
  85. polynomial operator - (const monomial& lhs, const polynomial& rhs) {
  86. return rhs - vector<monomial>({ lhs });
  87. }
  88.  
  89. polynomial operator - (const polynomial& lhs, const monomial& rhs) {
  90. return rhs - lhs;
  91. }
  92.  
  93. polynomial operator * (const monomial& lhs, const polynomial& rhs) {
  94. polynomial res(rhs);
  95. for (monomial& mono_ : res.expression) {
  96. mono_ = mono_ * lhs;
  97. }
  98. res.simplify();
  99. return res;
  100. }
  101.  
  102. polynomial operator * (const polynomial& lhs, const monomial& rhs) {
  103. return rhs * lhs;
  104. }
  105.  
  106. polynomial operator * (const polynomial& lhs, const polynomial& rhs)
  107. {
  108. polynomial res;
  109. for (const monomial& mono1 : lhs.expression) {
  110. for (const monomial& mono2 : rhs.expression) {
  111. res.expression.push_back(mono1 * mono2);
  112. }
  113. }
  114. res.simplify();
  115. if (res.str() == "0") {
  116. return polynomial();
  117. }
  118. return res;
  119. }
  120.  
  121. polynomial operator ^ (const polynomial& poly, int pow) {
  122. polynomial res("1");
  123. for (int i = 0; i < pow; i++) {
  124. res = res * poly;
  125. res.simplify();
  126. }
  127. return res;
  128. }
  129.  
  130. bool operator == (const polynomial& lhs, const polynomial& rhs) {
  131. if (lhs.expression.size() != rhs.expression.size()) {
  132. return false;
  133. }
  134. for (size_t i = 0; i < lhs.expression.size(); i++) {
  135. if (lhs.expression[i] != rhs.expression[i]) {
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141.  
  142. bool operator != (const polynomial& lhs, const polynomial& rhs) {
  143. return !(lhs == rhs);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement