void Polynomial::operator-=(const Polynomial& p) { unsigned dp = p.get_degree(); unsigned dthis = get_degree(); auto p_coeffs = p.get_coeffs(); if(dthis<dp) coefficients.resize(p.get_degree()+1, 0.0f); std::transform(coefficients.cbegin(), coefficients.cbegin()+dp+1, p_coeffs.cbegin(), coefficients.begin(), std::minus<float>()); }
void Polynomial::operator/=(const Polynomial& p) { Polynomial Q = *this; while(Q.get_degree()>=p.get_degree()) { Q-=p*p(p.get_degree())*Q(Q.get_degree()); Q.remove_zeros(); } *this = Q; }
Polynomial Polynomial::operator =(const Polynomial &p) { if (this != &p) { degree = p.get_degree(); delete[] coefficients; coefficients = new ComplexNumber[degree + 1]; for (int i = 0; i < p.get_degree() + 1; i++) { coefficients[i] = p.get_coefficients()[i]; } } return *this; }
// Overloaded operator + Polynomial operator+(const Polynomial &polya, const int b) { Polynomial result(polya.get_degree()); for (int i = 0; i < polya.get_degree() + 1; i++) { if (i == 0) { result.set_coeff(i, polya.get_coeff(i) + b); } else { result.set_coeff(i, polya.get_coeff(i)); } } return result; }
// Overloaded operator * Polynomial operator*(const Polynomial &polya, const Polynomial &polyb) { int degree_a = polya.get_degree(); int degree_b = polyb.get_degree(); int max_degree = degree_a + degree_b; Polynomial result(max_degree); for (int i = 0; i <= degree_b; i++) { for (int j = 0; j <= degree_a; j++) { double nc = polyb.get_coeff(i) * polya.get_coeff(j) + result.get_coeff(i + j); result.set_coeff(i + j, nc); } } return result; }
// Overloaded operator - Polynomial operator-(const Polynomial &polya, const Polynomial &polyb) { int max_degree; if (polya.get_degree() > polyb.get_degree()) { max_degree = polya.get_degree(); } else { max_degree = polyb.get_degree(); } Polynomial result(max_degree); int i = 0; for (i = 0; i <= polya.get_degree() && i <= polyb.get_degree(); i++) { result.set_coeff(i, polya.get_coeff(i) - polyb.get_coeff(i)); } for (; i <= max_degree; i++) { if (max_degree == polya.get_degree()) { result.set_coeff(i, polya.get_coeff(i)); } else { result.set_coeff(i, -polyb.get_coeff(i)); } } return result; }
Polynomial::Polynomial(const Polynomial &poly) { // cout << "copy constructor\n"; degree = poly.get_degree(); coeff = new double[degree+1]; for (int i = 0; i <= degree; i++) { coeff[i] = poly.get_coeff(i); } }
bool Polynomial::operator==(const Polynomial& p) const { if(this->get_degree()!=p.get_degree()) return false; else { const std::vector<float>& coeffs1 = this->get_coeffs(); const std::vector<float>& coeffs2 = p.get_coeffs(); return std::equal(coeffs1.begin(), coeffs1.end(), coeffs2.begin()); } }
// Overloaded operator * Polynomial operator*(const int a, const Polynomial &polyb) { int degree_b = polyb.get_degree(); Polynomial result(degree_b); for (int i = 0; i <= degree_b; i++) { result.set_coeff(i, a * polyb.get_coeff(i)); } return result; }
// Overloaded operator * Polynomial operator*(const Polynomial &polya, const int b) { int degree_a = polya.get_degree(); Polynomial result(degree_a); for (int i = 0; i <= degree_a; i++) { result.set_coeff(i, b * polya.get_coeff(i)); } return result; }
bool Polynomial::operator ==(const Polynomial &z) const { if (degree != z.get_degree()) { return false; } for (int i = 0; i < degree + 1; i++) { if (coefficients[i] != z.get_coefficients()[i]) { return false; } } return true; }
// Overloaded operator + Polynomial operator+(const Polynomial &pola, const Polynomial &polb) { cout << "operator + \n"; int degA = pola.get_degree(); int degB = polb.get_degree(); int max_degr; if (degA > degB) { max_degr = degA; } else { max_degr = degB; } Polynomial result(max_degr); int i; for (i = 0; i <= degA && i <= degB; i++) { double nc = pola.get_coeff(i) + polb.get_coeff(i); result.set_coeff(i, nc); } // Finish the rest of the coefficients for (; i <= max_degr; i++) { if (degA != max_degr) { result.set_coeff(i, polb.get_coeff(i)); } else { result.set_coeff(i, pola.get_coeff(i)); } } return result; }
void Polynomial::operator*=(const Polynomial& p) { unsigned this_degree = this->get_degree(); unsigned p_degree = p.get_degree(); std::vector<float> new_coeffs(this_degree+p_degree+1, 0.0f); for(unsigned i=0;i<=this_degree;++i) { for(unsigned j=0;j<=p_degree;++j) { new_coeffs[i+j]+=p(j)*coefficients[i]; } } coefficients = new_coeffs; }
// Assignment operator void Polynomial::operator =(const Polynomial &poly) { // cout << "assignment operator overload\n"; if (this == &poly) { // Copy to itself. Nothing to be done. return; } degree = poly.get_degree(); delete [] coeff; coeff = new double[degree+1]; for (int i = 0; i <= degree; i++) { coeff[i] = poly.get_coeff(i); } return; }