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:
- explicit Polynomial(const std::vector<T> &scalar) : coefs_(scalar) { simplify(); }
- explicit Polynomial(const T& alpha) : coefs_({alpha}) {}
- 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() {
- simplify();
- return coefs_.begin();
- }
- const typename std::vector<T>::const_iterator end() {
- simplify();
- return coefs_.end();
- }
- int Degree() {
- simplify();
- if (coefs_.empty())
- return -1;
- return static_cast<int>(coefs_.size()) - 1;
- }
- const T &operator[](int i) {
- simplify();
- if (coefs_.empty() || i == -1 || i + 1 > 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();
- if (deg_other > deg_my)
- coefs_.resize(deg_other + 1, 0);
- for (size_t i = 0; i < deg_other + 1; ++i)
- (*this)[i] += tmp[i];
- return *this;
- }
- Polynomial &operator+=(const std::vector<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();
- if (deg_other > deg_my)
- coefs_.resize(deg_other + 1, 0);
- for (size_t i = 0; i < deg_other + 1; ++i)
- (*this)[i] -= tmp[i];
- return *this;
- }
- Polynomial &operator-=(const std::vector<T> &other) {
- return (*this) -= Polynomial(other);
- }
- };
- template<typename T>
- bool operator==(const Polynomial<T> &one, const Polynomial<T> &two) {
- one.simplify();
- two.simplify();
- return one.GetCoefs() == two.GetCoefs();
- }
- template<typename T>
- bool operator==(const Polynomial<T> &one, const std::vector<T> &two) {
- one.simplify();
- return one.GetCoefs() == two;
- }
- template<typename T>
- bool operator==(const std::vector<T> &one, const Polynomial<T> &two) {
- two.simplify();
- return one == two.GetCoefs();
- }
- template<typename T>
- bool operator!=(const Polynomial<T> &one, const Polynomial<T> &two) {
- one.simplify();
- two.simplify();
- return one.GetCoefs() != two.GetCoefs();
- }
- template<typename T>
- bool operator!=(const Polynomial<T> &one, const std::vector<T> &two) {
- one.simplify();
- return one.GetCoefs() != two;
- }
- template<typename T>
- bool operator!=(const std::vector<T> &one, const Polynomial<T> &two) {
- two.simplify();
- return one != two.GetCoefs();
- }
- 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 Polynomial<T> &one, const std::vector<T> &two) {
- Polynomial tmp = one, other(two);
- return tmp += other;
- }
- template<typename T>
- const Polynomial<T> &operator+(const std::vector<T> &one, const Polynomial<T> &two) {
- Polynomial tmp = Polynomial(one);
- 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 Polynomial<T> &one, const std::vector<T> &two) {
- Polynomial tmp = one, other(two);
- return tmp -= other;
- }
- template<typename T>
- const Polynomial<T> &operator-(const std::vector<T> &one, const Polynomial<T> &two) {
- Polynomial tmp = Polynomial(one);
- return tmp -= two;
- }
- int main() {
- std::vector<int> a = {4, 3, 2, 1}, b = {5, 6, 8, 7};
- Polynomial p1(a);
- Polynomial p2(b.begin(), b.end());
- std::cout << p1.Degree() << ' ' << p2.Degree() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment