function - C++ Polynomial Multplication -


i'm trying multiply 2 polynomials in c++. i'd keep overal structure of code as-is if possible. understand there may "faster way." here code polynomial addition:

    polynomial add(polynomial &poly1, polynomial &poly2)     {       vector<int> temp1;       if( poly1.degree() > poly2.degree() )       {         for( int = 0 ; i<poly2.degree() ; i++ )         {           temp1[i]=poly1.coefficient(i)+poly2.coefficient(i);         }         polynomial temp0(temp1);         return temp0;       }       else if ( poly1.degree() < poly2.degree() )       {         for( int = 0 ; i<poly1.degree() ; i++ )         {           temp1[i]=poly1.coefficient(i)+poly2.coefficient(i);         }         polynomial temp0(temp1);         return temp0;       }     } 

here degree() member function definition:

      int polynomial::degree() const     {        for(int = 0; < coefficient.size(); i++)        {         int last=0;         if(coefficient[i] != 0)         {           last = i;         }         return last;       }     } 

here polynomial class declaration:

    class polynomial {     public:       polynomial();       polynomial(vector<int> &coeffs);       int degree() const;       int coefficient(int k) const;       void print() const;       void constantmultiply(int x);       void transform();       int nonzero() const;     private:       vector<int> coefficient;     }; 

now, i'm trying multiply 2 input polynomials using addition function, , feel use below literature cohn's classic algebra trick sufficient thought.

cohn's classic algebra reference

i think should you're looking for. if poly1 has coefficient degree iterate on poly2's coeffecients. each coefficient of poly2 multiplied , result degree i+j, x^1*x^2=x^(1+2)=x^3.

polynomial mul(polynomial &poly1, polynomial &poly2) {   vector<int> temp1;   for( int = 0; i<poly1.degree() ; i++ ){     if(poly1.coefficient[i] != 0){       for( int j = 0; j<poly2.degree() ; j++ ){         if(poly2.coefficient[j] != 0){           temp1[i+j] = poly1.coefficient(i)*poly2.coefficient(j);         }       }     }   }   polynomial temp0(temp1);   return temp0; } 

please let me know if i'm missing question, or if doesn't solve you!


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -