in_chainz

Untitled

Feb 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<typename T>
  5. class Polynomial {
  6. private:
  7.     std::vector<T> coefs_;
  8. public:
  9.     explicit Polynomial(const std::vector<T> &scalar) : coefs_(scalar) { simplify(); }
  10.  
  11.     explicit Polynomial(const T& alpha) : coefs_({alpha}) {}
  12.  
  13.     template<typename Iter>
  14.     Polynomial(Iter fst, Iter lst) {
  15.         coefs_.reserve(std::distance(fst, lst));
  16.         for (; fst != lst; ++fst)
  17.             coefs_.emplace_back(*fst);
  18.         simplify();
  19.     }
  20.  
  21.  
  22.     const std::vector<T> &GetCoefs() const {
  23.         return coefs_;
  24.     }
  25.  
  26.     void simplify() {
  27.         for (; !coefs_.empty() && !coefs_.back();)
  28.             coefs_.pop_back();
  29.     }
  30.  
  31.     const typename std::vector<T>::const_iterator begin() {
  32.         simplify();
  33.         return coefs_.begin();
  34.     }
  35.  
  36.     const typename std::vector<T>::const_iterator end() {
  37.         simplify();
  38.         return coefs_.end();
  39.     }
  40.  
  41.     int Degree() {
  42.         simplify();
  43.         if (coefs_.empty())
  44.             return -1;
  45.         return static_cast<int>(coefs_.size()) - 1;
  46.     }
  47.  
  48.     const T &operator[](int i) {
  49.         simplify();
  50.         if (coefs_.empty() || i == -1 || i + 1 > coefs_.size())
  51.             return 0;
  52.         return coefs_[i];
  53.  
  54.     }
  55.  
  56.     Polynomial &operator+=(const Polynomial &other) {
  57.         Polynomial tmp = other;
  58.         simplify();
  59.         tmp.simplify();
  60.         int deg_other = tmp.Degree(),
  61.                 deg_my = Degree();
  62.         if (deg_other > deg_my)
  63.             coefs_.resize(deg_other + 1, 0);
  64.  
  65.         for (size_t i = 0; i < deg_other + 1; ++i)
  66.             (*this)[i] += tmp[i];
  67.  
  68.         return *this;
  69.     }
  70.  
  71.     Polynomial &operator+=(const std::vector<T> &other) {
  72.         return (*this) += Polynomial(other);
  73.     }
  74.  
  75.     Polynomial &operator-=(const Polynomial &other) {
  76.         Polynomial tmp = other;
  77.         simplify();
  78.         tmp.simplify();
  79.         int deg_other = tmp.Degree(),
  80.                 deg_my = Degree();
  81.         if (deg_other > deg_my)
  82.             coefs_.resize(deg_other + 1, 0);
  83.  
  84.         for (size_t i = 0; i < deg_other + 1; ++i)
  85.             (*this)[i] -= tmp[i];
  86.  
  87.         return *this;
  88.     }
  89.  
  90.     Polynomial &operator-=(const std::vector<T> &other) {
  91.         return (*this) -= Polynomial(other);
  92.     }
  93. };
  94.  
  95. template<typename T>
  96. bool operator==(const Polynomial<T> &one, const Polynomial<T> &two) {
  97.     one.simplify();
  98.     two.simplify();
  99.     return one.GetCoefs() == two.GetCoefs();
  100. }
  101.  
  102. template<typename T>
  103. bool operator==(const Polynomial<T> &one, const std::vector<T> &two) {
  104.     one.simplify();
  105.     return one.GetCoefs() == two;
  106. }
  107.  
  108. template<typename T>
  109. bool operator==(const std::vector<T> &one, const Polynomial<T> &two) {
  110.     two.simplify();
  111.     return one == two.GetCoefs();
  112. }
  113.  
  114. template<typename T>
  115. bool operator!=(const Polynomial<T> &one, const Polynomial<T> &two) {
  116.     one.simplify();
  117.     two.simplify();
  118.     return one.GetCoefs() != two.GetCoefs();
  119. }
  120.  
  121. template<typename T>
  122. bool operator!=(const Polynomial<T> &one, const std::vector<T> &two) {
  123.     one.simplify();
  124.     return one.GetCoefs() != two;
  125. }
  126.  
  127. template<typename T>
  128. bool operator!=(const std::vector<T> &one, const Polynomial<T> &two) {
  129.     two.simplify();
  130.     return one != two.GetCoefs();
  131. }
  132.  
  133. template<typename T>
  134. const Polynomial<T> &operator+(const Polynomial<T> &one, const Polynomial<T> &two) {
  135.     Polynomial tmp = one;
  136.     return tmp += two;
  137. }
  138.  
  139. template<typename T>
  140. const Polynomial<T> &operator+(const Polynomial<T> &one, const std::vector<T> &two) {
  141.     Polynomial tmp = one, other(two);
  142.     return tmp += other;
  143. }
  144.  
  145. template<typename T>
  146. const Polynomial<T> &operator+(const std::vector<T> &one, const Polynomial<T> &two) {
  147.     Polynomial tmp = Polynomial(one);
  148.     return tmp += two;
  149. }
  150.  
  151. template<typename T>
  152. const Polynomial<T> &operator-(const Polynomial<T> &one, const Polynomial<T> &two) {
  153.     Polynomial tmp = one;
  154.     return tmp -= two;
  155. }
  156.  
  157. template<typename T>
  158. const Polynomial<T> &operator-(const Polynomial<T> &one, const std::vector<T> &two) {
  159.     Polynomial tmp = one, other(two);
  160.     return tmp -= other;
  161. }
  162.  
  163. template<typename T>
  164. const Polynomial<T> &operator-(const std::vector<T> &one, const Polynomial<T> &two) {
  165.     Polynomial tmp = Polynomial(one);
  166.     return tmp -= two;
  167. }
  168.  
  169.  
  170. int main() {
  171.  
  172.     std::vector<int> a = {4, 3, 2, 1}, b = {5, 6, 8, 7};
  173.  
  174.     Polynomial p1(a);
  175.     Polynomial p2(b.begin(), b.end());
  176.  
  177.     std::cout << p1.Degree() << ' ' << p2.Degree() << std::endl;
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment