Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- template<typename T>
- class Polynomial {
- private:
- std::vector<T> coefs_;
- public:
- Polynomial() = default;
- explicit Polynomial(const std::vector<T> &scalar) : coefs_(scalar) { simplify(); }
- explicit Polynomial(const T &alpha) : coefs_({alpha}) { simplify(); }
- template<typename Iter>
- Polynomial(Iter fst, Iter lst) {
- coefs_.reserve(std::distance(fst, lst));
- for (; fst != lst; ++fst)
- coefs_.emplace_back(*fst);
- simplify();
- }
- const std::vector<T> &GetCoefs() const {
- return coefs_;
- }
- void simplify() {
- for (; !coefs_.empty() && !coefs_.back();)
- coefs_.pop_back();
- }
- const typename std::vector<T>::const_iterator begin() const {
- return coefs_.begin();
- }
- const typename std::vector<T>::const_iterator end() const {
- return coefs_.end();
- }
- int Degree() const {
- if (coefs_.empty())
- return -1;
- return static_cast<int>(coefs_.size()) - 1;
- }
- const T operator[](int i) const {
- if (coefs_.empty() || i == -1 || i + 1 > static_cast<int>(coefs_.size()))
- return 0;
- return coefs_[i];
- }
- Polynomial &operator+=(const Polynomial &other) {
- Polynomial tmp = other;
- simplify();
- tmp.simplify();
- int deg_other = tmp.Degree(),
- deg_my = Degree();
- size_t sz = static_cast<size_t> (deg_other + 1);
- if (deg_other > deg_my)
- coefs_.resize(sz, 0);
- for (size_t i = 0; i < sz; ++i)
- coefs_[i] += tmp[i];
- simplify();
- return *this;
- }
- Polynomial &operator+=(T other) {
- return (*this) += Polynomial(other);
- }
- Polynomial &operator-=(const Polynomial &other) {
- Polynomial tmp = other;
- simplify();
- tmp.simplify();
- int deg_other = tmp.Degree(),
- deg_my = Degree();
- size_t sz = static_cast<size_t> (deg_other + 1);
- if (deg_other > deg_my)
- coefs_.resize(sz, 0);
- for (size_t i = 0; i < sz; ++i)
- coefs_[i] -= tmp[i];
- simplify();
- return *this;
- }
- Polynomial &operator-=(T other) {
- return (*this) -= Polynomial(other);
- }
- };
- template<typename T>
- bool operator==(const Polynomial<T> &one, const Polynomial<T> &two) {
- Polynomial tmp1 = one, tmp2 = two;
- tmp1.simplify();
- tmp2.simplify();
- return one.GetCoefs() == two.GetCoefs();
- }
- template<typename T>
- bool operator==(const Polynomial<T> &one, T two) {
- Polynomial nw(two);
- return one == nw;
- }
- template<typename T>
- bool operator==(T one, const Polynomial<T> &two) {
- Polynomial nw(one);
- return two == nw;
- }
- template<typename T>
- bool operator!=(const Polynomial<T> &one, const Polynomial<T> &two) {
- Polynomial tmp1 = one, tmp2 = two;
- tmp1.simplify();
- tmp2.simplify();
- return one.GetCoefs() != two.GetCoefs();
- }
- template<typename T>
- bool operator!=(const Polynomial<T> &one, T two) {
- Polynomial nw(two);
- return one != nw;
- }
- template<typename T>
- bool operator!=(T one, const Polynomial<T> &two) {
- Polynomial nw(one);
- return two != nw;
- }
- template<typename T>
- const Polynomial<T> operator+(const Polynomial<T> &one, const Polynomial<T> &two) {
- Polynomial tmp = one;
- return tmp += two;
- }
- template<typename T>
- const Polynomial<T> operator+(const T &one, const Polynomial<T> &two) {
- Polynomial tmp = Polynomial(one), other = two;
- return tmp += two;
- }
- template<typename T>
- const Polynomial<T> operator+(const Polynomial<T> &one, const T &two) {
- Polynomial tmp = one, other(two);
- return tmp += two;
- }
- template<typename T>
- const Polynomial<T> operator-(const Polynomial<T> &one, const T &two) {
- Polynomial tmp = one, other(two);
- return tmp -= two;
- }
- template<typename T>
- const Polynomial<T> operator-(const Polynomial<T> &one, const Polynomial<T> &two) {
- Polynomial tmp = one;
- return tmp -= two;
- }
- template<typename T>
- const Polynomial<T> operator-(const T &one, const Polynomial<T> &two) {
- Polynomial tmp = Polynomial(one);
- return tmp -= two;
- }
Advertisement
Add Comment
Please, Sign In to add comment