in_chainz

Untitled

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