Let P1 and P2 are two polynominals..

where

p1 = 1 + 4X^2 + x^4

and

p2 = 2x^2 + x^4

program is to multiply p1 with p2

p3 = p1 * p2

Like


line 1: ( 1 + 4x^2 + x^4) (2x^2 + x^4)

line 2: 1*(2x^2 + x^4) + 4x^2 * (2x^2 + x^4) + x^4 * (2x^2 + x^4)

line 3: 2x^2 + x^4 + 8x^4 + 4x^6 + 2x^6 + x^8

line 4: 2x^2 + 9x^4 + 6x^6 + x^8

I am stucked in obtaining line 4..


here is my Polynominal

typedef struct termType
{
   int coeff,expo;
}termType;

typedef struct poly
{
   termType terms[20];
   int noOfTerms;
}poly;

and my multiplication function

void mul(poly p1,poly p2,poly *p3)
{
   poly p4;
   int i,j,k=0;
   for(i=0;i<p1.noOfTerms;i++)
   {
      for(j=0;j<p2.noOfTerms;j++,k++)
      {
	  p4.terms[k].coeff = p1.terms[i].coeff * p2.terms[j].coeff;
	  p4.terms[k].expo  = p1.terms[i].expo  + p2.terms[j].expo;
      }
   }
   p4.noOfTerms = k;

 // line 4 code............

}

Any Help Will be Appriciated

Thanks

Vinit Mittal

B4 posting it want to say im student and if any mistake im sorry

May be like manually how we do ...
Assign first term of p4 to p5 and then go on checking if same exponent term exists,
If exists add thr coefficinents
and if not than add that term to p5, it will be p5.term[1]
and again go on checking again if same exponent in p4 exists if not add next term and so on ..

May be like

poly p5;

p5.term[0] = p4->term[0];
p5.noOfTerms = 1;

while( i <= k)
{
    for(i=0; i <= p5.noOfTerms; i++)
     {
        if(p5.term[i].expo == p4->term[i]->expo)
            p5.term[i].coeff += p4->term[i]->coeff ;
     }
   /* If same exponent term in p5 and p4 not found
      add that term to p5 
      and p5.noOfTerms++ */
}

okk i got it...

void mul(poly p1,poly p2,poly *p3)
{
   poly p4,p5;

   int i,j,k=0,maxd=0, flag;
   
   for(i=0;i<20;i++)
      p5.terms[i].coeff=0;
   
   for(i=0;i<p1.noOfTerms;i++)
   {
      for(j=0;j<p2.noOfTerms;j++,k++)
      {
	  p4.terms[k].coeff = p1.terms[i].coeff * p2.terms[j].coeff;
	  p4.terms[k].expo  = p1.terms[i].expo  + p2.terms[j].expo;
      }
   }
   p4.noOfTerms = k;
   
   for(i=0;i<p4.noOfTerms;i++)
     if(p4.terms[i].expo > maxd)
	 maxd = p4.terms[i].expo;
   k=0;

   for(i=0;i<=maxd;i++)
   {
       flag=0;
       for(j=0;j<p4.noOfTerms;j++)
       {
	   if(p4.terms[j].expo == i)
	   {
		p5.terms[k].coeff  = p5.terms[k].coeff + (p4.terms[j].coeff);
		p5.terms[k].expo   = p4.terms[j].expo;
		flag=1;
	   }
      }

      if (flag == 1)
	 k++;

  }
  p5.noOfTerms = k;
  *p3 = p5;
}

it's working now...
please suggest to make it better...

@ shankye

Thanks for replying buddy...
okk.. let me implement what you have suggest then i will reply back..

ok thanks :)

How about going 4 5 years back in schooling time and doing how we used to do in school

p1 = 3x^2 + 2x^1 + x^0
p2 = 1x^2 + 1x^1 + 2x^0
----------------
multiplying term by term

p4 = 6x^2 + 4x^1 + 2x^0
p5 = 3x^3 + 2x^2 + 1x^1
p6 = 3x^4 + 2x^3 + 1x^2
-------------------------------------

Adding above will give our answer ..


I think this approach will be easier ..

mul(poly p1, poly p2, poly *p3)
{
poly p[p1.noOfterms];

	for(int i=0; i<p1.noOfTerms; i++)
	{
	   for(int j=0,int k=0; j<p2.noOfTerms; j++,k++)
            {
		p[i].terms[k].coeff = p1.terms[i].coeff * p2.terms[j].coeff;
	        p[i].terms[k].expo  = p1.terms[i].expo  + p2.terms[j].expo;
	    }
	}

   // Using a loop Comapare exponent and if equal add their coeff

}
commented: Like ur suggestion :) +1

ok thanks

How about going 4 5 years back in schooling time and doing how we used to do in school

p1 = 3x^2 + 2x^1 + x^0
p2 = 1x^2 + 1x^1 + 2x^0
----------------
multiplying term by term

p4 = 0x^4 + 0x^3 + 6x^2 + 4x^1 + 2x^0
p5 = 0x^4 + 3x^3 + 2x^2 + 1x^1 + 0x^0
p6 = 3x^4 + 2x^3 + 1x^2 + 0x^1 + 0x^0
-------------------------------------

Adding above will give our answer ..


I think this approach will be easier ..

mul(poly p1, poly p2, poly *p3)
{
poly p[p1.noOfterms];

	for(int i=0; i<p1.noOfTerms; i++)
	{
	   for(int j=0,int k=0; j<p2.noOfTerms; j++,k++)
            {
		p[i].terms[k].coeff = p1.terms[i].coeff * p2.terms[j].coeff;
	        p[i].terms[k].expo  = p1.terms[i].expo  + p2.terms[j].expo;
	    }
	}

   // Comapare exponent and if equal add their coeff
}

okk.. what about adding process...

Oopss no idea

may be consider one poly use two loops Check term by term with all remaining poly

considering p4 (in program p[0])

for(i=0; i<MAX_EXP; i++)
{
 for(j=0; j<k; j++)
 {
   for(int m=0; m<k; k++)
    {
     if(p[0].term[j].expo = p[i].term[m].expo)
        p[0].term[j].coeff += p[i].term[m].coeff;
    }
  }
}

But if exponent are in decreasing order and dont think this type of comparisn may be needed ..

Hope it helped

well... I was waiting for experts advice, Anyways my problem is already solved so there is no neeed to continue this thread anymore.

@ Shankye Thanks for your suggestions buddy..:)

Pleasure :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.