Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.04 KB | None | 0 0
  1. #include <cstddef>
  2. #include <map>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. template<typename T>
  7. T power(T x, int pw) {
  8.     T res(T(1));
  9.     while (pw > 0) {
  10.         if (pw % 2 == 1) {
  11.             res *= x;
  12.         }
  13.         pw /= 2;
  14.         x *= x;
  15.     }
  16.     return res;
  17. }
  18.  
  19. template <typename T>
  20. class Polynomial {
  21. private:
  22.     std::map<int, T> coef;
  23.  
  24.     void Compress() {
  25.         std::vector<int> toErase;
  26.         for (const auto& i : coef) {
  27.             if (i.second == T(0)) {
  28.                 toErase.push_back(i.first);
  29.             }
  30.         }
  31.         for (const auto& i : toErase) {
  32.             coef.erase(i);
  33.         }
  34.     }
  35.  
  36. public:
  37.     Polynomial(const std::vector<T>& arr) {
  38.         for (size_t i = 0; i < arr.size(); ++i) {
  39.             coef[i] = arr[i];
  40.         }
  41.         Compress();
  42.     }
  43.  
  44.     Polynomial(const std::map<int, T>& arr) : coef(arr) {
  45.         Compress();
  46.     }
  47.  
  48.     Polynomial(T el = T(0)) {
  49.         coef[0] = el;
  50.         Compress();
  51.     }
  52.  
  53.     template<typename K>
  54.     Polynomial(K beginIt, const K& endIt) {
  55.         int i = 0;
  56.         while (beginIt != endIt) {
  57.             coef[i++] = *(beginIt++);
  58.         }
  59.         Compress();
  60.     }
  61.  
  62.     int Degree() const {
  63.         if (coef.size() == 0) {
  64.             return -1;
  65.         } else {
  66.             return coef.rbegin()->first;
  67.         }
  68.     }
  69.  
  70.     T operator[](const size_t& n) const {
  71.         if (coef.count(n)) {
  72.             return coef.at(n);
  73.         } else {
  74.             return T(0);
  75.         }
  76.     }
  77.  
  78.     typename std::map<int, T>::const_iterator begin() const {
  79.         return coef.begin();
  80.     }
  81.  
  82.     typename std::map<int, T>::const_iterator end() const {
  83.         return coef.end();
  84.     }
  85.  
  86.     Polynomial& operator += (const std::map<int, T>& other) {
  87.         for (const auto& i : other) {
  88.             coef[i.first] += i.second;
  89.         }
  90.         Compress();
  91.         return *this;
  92.     }
  93.  
  94.     Polynomial& operator += (const Polynomial& other) {
  95.         return *this += other.coef;
  96.     }
  97.  
  98.     Polynomial& operator += (const T& other) {
  99.         return *this += Polynomial(other);
  100.     }
  101.  
  102.     Polynomial operator + (const std::map<int, T>& other) const {
  103.         return Polynomial(*this) += other;
  104.     }
  105.  
  106.     Polynomial operator + (const Polynomial& other) const {
  107.         return Polynomial(*this) += other;
  108.     }
  109.  
  110.     Polynomial operator + (const T& other) const {
  111.         return Polynomial(*this) += other;
  112.     }
  113.  
  114.     Polynomial& operator -= (const std::map<int, T>& other) {
  115.         for (const auto& i : other) {
  116.             coef[i.first] -= i.second;
  117.         }
  118.         Compress();
  119.         return *this;
  120.     }
  121.  
  122.     Polynomial& operator -= (const Polynomial& other) {
  123.         return *this -= other.coef;
  124.     }
  125.  
  126.     Polynomial& operator -= (const T& other) {
  127.         return *this -= Polynomial(other);
  128.     }
  129.  
  130.     Polynomial operator - (const std::map<int, T>& other) const {
  131.         return Polynomial(*this) -= other;
  132.     }
  133.  
  134.     Polynomial operator - (const Polynomial& other) const {
  135.         return Polynomial(*this) -= other;
  136.     }
  137.  
  138.     Polynomial operator - (const T& other) const {
  139.         return Polynomial(*this) -= other;
  140.     }
  141.  
  142.     bool operator == (const std::map<int, T>& other) const {
  143.         return coef == other;
  144.     }
  145.  
  146.     bool operator == (const Polynomial& other) const {
  147.         return *this == other.coef;
  148.     }
  149.  
  150.     bool operator == (const T& other) const {
  151.         return *this == Polynomial(other);
  152.     }
  153.  
  154.     bool operator != (const std::map<int, T>& other) const {
  155.         return !(*this == other);
  156.     }
  157.  
  158.     bool operator != (const Polynomial& other) const {
  159.         return !(*this == other);
  160.     }
  161.  
  162.     bool operator != (const T& other) const {
  163.         return !(*this == other);
  164.     }
  165.  
  166.     Polynomial& operator *= (const std::map<int, T>& other) {
  167.         if (Degree() == -1 || other.size() == 0) {
  168.             return *this = Polynomial();
  169.         }
  170.         std::map<int, T> res;
  171.         for (const auto& i : *this) {
  172.             for (const auto& j : other) {
  173.                 res[i.first + j.first] += i.second * j.second;
  174.             }
  175.         }
  176.         return *this = Polynomial(res);
  177.     }
  178.  
  179.     Polynomial& operator *= (const Polynomial& other) {
  180.         return *this *= other.coef;
  181.     }
  182.  
  183.     Polynomial& operator *= (const T& other) {
  184.         return *this *= Polynomial(other);
  185.     }
  186.  
  187.     Polynomial operator * (const std::map<int, T>& other) const {
  188.         return Polynomial(*this) *= other;
  189.     }
  190.  
  191.     Polynomial operator * (const Polynomial& other) const {
  192.         return Polynomial(*this) *= other;
  193.     }
  194.  
  195.     Polynomial operator * (const T& other) const {
  196.         return Polynomial(*this) *= other;
  197.     }
  198.  
  199.     T operator () (const T& x) const {
  200.         T res = T(0);
  201.         for (const auto& i : *this) {
  202.             res += power<T>(x, i.first) * i.second;
  203.         }
  204.         return res;
  205.     }
  206.  
  207.     Polynomial operator & (const Polynomial& other) const {
  208.         Polynomial res(T(0));
  209.         for (const auto& i : *this) {
  210.             res += power(other, i.first) * i.second;
  211.         }
  212.         return res;
  213.     }
  214.  
  215.     Polynomial operator / (const Polynomial& other) const {
  216.         Polynomial res(T(0)), residue(*this);
  217.         while (residue.Degree() >= other.Degree()) {
  218.             Polynomial temp = power(Polynomial(std::vector<T>{T(0), T(1)}),
  219.             residue.Degree() - other.Degree())
  220.             * (residue[residue.Degree()] / other[other.Degree()]);
  221.             res += temp;
  222.             residue -= temp * other;
  223.         }
  224.         return res;
  225.     }
  226.  
  227.     Polynomial operator % (const Polynomial& other) const {
  228.         return *this - ((*this) / other) * other;
  229.     }
  230.  
  231.     Polynomial operator , (const Polynomial& other) const {
  232.         Polynomial a(*this), b(other);
  233.         while ((a * b) != Polynomial(T(0))) {
  234.             Polynomial temp = a % b;
  235.             a = b;
  236.             b = temp;
  237.         }
  238.         if (a == Polynomial(T(0))) {
  239.             return b / Polynomial(b[b.Degree()]);
  240.         } else {
  241.             return a / Polynomial(a[a.Degree()]);
  242.         }
  243.     }
  244. };
  245.  
  246. template<typename T>
  247. std::ostream& operator << (std::ostream& out, const Polynomial<T>& p) {
  248.     if (p.Degree() == -1) {
  249.         out << T(0);
  250.         return out;
  251.     }
  252.     for (int i = p.Degree(); i >= 0; --i) {
  253.         if (p[i] == T(0)) {
  254.             continue;
  255.         }
  256.         if (i != p.Degree() && p[i] > T(0)) {
  257.             out << '+';
  258.         }
  259.         if (i == 0) {
  260.             out << p[i];
  261.             continue;
  262.         }
  263.         if (p[i] == T(-1)) {
  264.             out << '-';
  265.         } else if (p[i] != T(1)) {
  266.             out << p[i] << '*';
  267.         }
  268.         if (i > 1) {
  269.             out << "x^" << i;
  270.         } else {
  271.             out << 'x';
  272.         }
  273.     }
  274.     return out;
  275. }
  276.  
  277. template<typename T>
  278. Polynomial<T> power(Polynomial<T> f, int pw) {
  279.     Polynomial<T> res(T(1));
  280.     while (pw > 0) {
  281.         if (pw % 2 == 1) {
  282.             res *= f;
  283.         }
  284.         f *= f;
  285.         pw /= 2;
  286.     }
  287.     return res;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement