How to add like terms of an array after multiplying polynomials

Hi

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1720 Views - Last Post: 15 October 2009 - 12:27 AM Rate Topic: ----- 1 Votes

#1 mola123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 09

How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 12:36 AM

I am a novice i C and i am writting a program in C that multiplies two polynomials. I have managed to multiply the two polynomials term by term and have a result like this:
"9x^10+54x^7 + 3x^6+ 4x^7+2x^6" for example.
Now i want to add the like terms but i am not able to do that.
Can someone help me with this please.
Here is my code:
/* Multiplying two polynomials*/
#include <stdio.h>
#define MAX_TERMS 101

typedef struct {
	int coef;
	int exp;
} POLY_TERM;

/* multiplying two polynomials a and b, with output polynomial c.*/
void polyMult(POLY_TERM a[], POLY_TERM b[], POLY_TERM c[]){
	
	int aIndex=0, bIndex=0, cIndex=0; 
	
	for (aIndex=0; aIndex<MAX_TERMS; aIndex++){
	//while ((a[aIndex].exp>=0) && (b[bIndex].exp>=0)){
		if(a[aIndex].exp==-1)
			break;

		for (bIndex=0; bIndex<MAX_TERMS; bIndex++){
			
			if(b[bIndex].exp==-1)
				break;
			
			c[cIndex].coef =a[aIndex].coef * b[bIndex].coef;
			c[cIndex].exp = a[aIndex].exp + b[bIndex].exp;
			
			cIndex++;// bIndex++; //aIndex++;
			
		}

		
	}
	/*Adding like terms*/
	for (cIndex=0; cIndex<MAX_TERMS; cIndex++){
		if (c[cIndex].exp==c[cIndex+1].exp)
		c[cIndex].coef+=c[cIndex+1].coef;
		c[cIndex].exp= c[cIndex].exp;
		}
		
}

/* Print a polynomial. */
void polyPrint(POLY_TERM p[]){
	int i=0;
 	printf("%dx^%d", p[i].coef, p[i].exp);
	i++;
	while (p[i].exp>=0){
		printf(" + %dx^%d", p[i].coef, p[i].exp);
		i++;
	}
	printf("\n");
}

int main(int argc, char **argv){
	int i;
	POLY_TERM a[MAX_TERMS], b[MAX_TERMS], c[MAX_TERMS];
	
	/* Create polynomial a */
	for (i=0; i<MAX_TERMS; i++) a[i].exp=-1;
	a[0].coef=3; a[0].exp=5;
	a[1].coef=18; a[1].exp=2;
	a[2].coef=-3; a[2].exp=1;
	
	 
	/* Create polynomial b */
	for (i=0; i<MAX_TERMS; i++) b[i].exp=-1;
	b[0].coef=3; b[0].exp=5;
	b[1].coef=18; b[1].exp=2;
	b[2].coef=-3; b[2].exp=1;
	b[3].coef=2; b[3].exp=1;

	

	/*Do polynomial Multiplication*/
	polyPrint(a);
	polyPrint(b);
	for (i=0; i<MAX_TERMS; i++) c[i].exp=-1;
	polyMult(a, b, c);
	polyPrint(c);

	return(0);
	
}


Can someone help me check my code i see why am not able to do it. If there is a better way in C, i most welcome the code.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to add like terms of an array after multiplying polynomials

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 06:16 AM

View Postmola123, on 14 Oct, 2009 - 01:36 AM, said:

Here is my code:
	/*Adding like terms*/
	for (cIndex=0; cIndex<MAX_TERMS; cIndex++){
		if (c[cIndex].exp==c[cIndex+1].exp)
		c[cIndex].coef+=c[cIndex+1].coef;
		c[cIndex].exp= c[cIndex].exp;
		}



You are only checking its neighbouring element, and you are not drop/zeroing out the match.

for(i = 0; i < MAX_TERMS - 1; i++) /* Don't check last term in first loop, as it will be either 0 or != to any other */
        for(j = i + 1; i < MAX_TERMS; j++) { 
                if(c[i].exp==c[j].exp) {
                        /* Add to c[i] */
                        /* Zero out the c[j] */
                }
        }


Was This Post Helpful? 0
  • +
  • -

#3 Guest_Neumann*


Reputation:

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 08:05 AM

Add the like terms while you're performing multiplication. Multiplying kth term of first polynomial by pth term of the second polynomial should be added to the (k+p)th term of the answer. Simple as that.
Was This Post Helpful? 0

#4 mola123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 09

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 08:24 AM

View Postbrds, on 14 Oct, 2009 - 05:16 AM, said:

View Postmola123, on 14 Oct, 2009 - 01:36 AM, said:

Here is my code:
	/*Adding like terms*/
	for (cIndex=0; cIndex<MAX_TERMS; cIndex++){
		if (c[cIndex].exp==c[cIndex+1].exp)
		c[cIndex].coef+=c[cIndex+1].coef;
		c[cIndex].exp= c[cIndex].exp;
		}



You are only checking its neighbouring element, and you are not drop/zeroing out the match.

for(i = 0; i < MAX_TERMS - 1; i++) /* Don't check last term in first loop, as it will be either 0 or != to any other */
        for(j = i + 1; i < MAX_TERMS; j++) { 
                if(c[i].exp==c[j].exp) {
                        /* Add to c[i] */
                        /* Zero out the c[j] */
                }
        }



maybe i don't get what you are saying here. I tried what you people suggest but it is not working. Maybe i am not doing it right. can someone modify the code i posted to do what you are trying to say so that i can see please/
Was This Post Helpful? 0
  • +
  • -

#5 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 09:07 AM

View Postmola123, on 14 Oct, 2009 - 09:24 AM, said:

View Postbrds, on 14 Oct, 2009 - 05:16 AM, said:

for(i = 0; i < MAX_TERMS - 1; i++) /* Don't check last term in first loop, as it will be either 0 or != to any other */
        for(j = i + 1; i < MAX_TERMS; j++) { 
                if(c[i].exp==c[j].exp) {
                        /* Add to c[i] */
                        /* Zero out the c[j] */
                }
        }



maybe i don't get what you are saying here. I tried what you people suggest but it is not working. Maybe i am not doing it right. can someone modify the code i posted to do what you are trying to say so that i can see please/


View Postmola123, on 14 Oct, 2009 - 01:36 AM, said:

Here is my code:
	/*Adding like terms*/
	for (cIndex=0; cIndex<MAX_TERMS; cIndex++){
		if (c[cIndex].exp==c[cIndex+1].exp)
		c[cIndex].coef+=c[cIndex+1].coef;
		c[cIndex].exp= c[cIndex].exp;
		}



1) You are only checking its neighbouring element, and 2) you are not drop/zeroing out the match.

1) Take the array arr[] = {0,1,2,3,4,5,6,7,8,9,1}, your just looking at it in pairs eg. 01 12 23 34 etc... so it will never find the matching term @ the end, arr[1] == arr[10].

2) After you have found the match, and added it to the original, it should be zeroed out. So that it dosn't get added in again.

9x^10+54x^7 + 3x^6+ 4x^7+2x^6 should reduce to 9x^10 + 58x^7 + 5x^6( + 0x^0 + 0x^0). Without zeroing out you get, 9x^10+58x^7+5x^6+4x^7+2x^6.
2x^2+2x^2+2x^2+3x^5 should reduce to 6x^2 (+ 0x^0 + 0x^0) + 3x^5. Without zeroing, 6x^2+4x^2+2x^2+3x^5.

The above (+ 0x^0 + 0x^0) is just to help you understand what is happening to the array.

@Edit : you should post your current code.

This post has been edited by brds: 14 October 2009 - 09:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 mola123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 09

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 09:37 AM

View Postbrds, on 14 Oct, 2009 - 08:07 AM, said:

View Postmola123, on 14 Oct, 2009 - 09:24 AM, said:

View Postbrds, on 14 Oct, 2009 - 05:16 AM, said:

for(i = 0; i < MAX_TERMS - 1; i++) /* Don't check last term in first loop, as it will be either 0 or != to any other */
        for(j = i + 1; i < MAX_TERMS; j++) { 
                if(c[i].exp==c[j].exp) {
                        /* Add to c[i] */
                        /* Zero out the c[j] */
                }
        }



maybe i don't get what you are saying here. I tried what you people suggest but it is not working. Maybe i am not doing it right. can someone modify the code i posted to do what you are trying to say so that i can see please/


View Postmola123, on 14 Oct, 2009 - 01:36 AM, said:

Here is my code:
	/*Adding like terms*/
	for (cIndex=0; cIndex<MAX_TERMS; cIndex++){
		if (c[cIndex].exp==c[cIndex+1].exp)
		c[cIndex].coef+=c[cIndex+1].coef;
		c[cIndex].exp= c[cIndex].exp;
		}



1) You are only checking its neighbouring element, and 2) you are not drop/zeroing out the match.

1) Take the array arr[] = {0,1,2,3,4,5,6,7,8,9,1}, your just looking at it in pairs eg. 01 12 23 34 etc... so it will never find the matching term @ the end, arr[1] == arr[10].

2) After you have found the match, and added it to the original, it should be zeroed out. So that it dosn't get added in again.

9x^10+54x^7 + 3x^6+ 4x^7+2x^6 should reduce to 9x^10 + 58x^7 + 5x^6( + 0x^0 + 0x^0). Without zeroing out you get, 9x^10+58x^7+5x^6+4x^7+2x^6.
2x^2+2x^2+2x^2+3x^5 should reduce to 6x^2 (+ 0x^0 + 0x^0) + 3x^5. Without zeroing, 6x^2+4x^2+2x^2+3x^5.

The above (+ 0x^0 + 0x^0) is just to help you understand what is happening to the array.

@Edit : you should post your current code.

/*Adding like terms*/
	for(i = 0; i < MAX_TERMS - 1; i++) /* Don't check last term in first loop, as it will be either 0 or != to any other */		
		for(j = i + 1; j < MAX_TERMS; j++) {				 
			if(c[i].exp==c[j].exp) {						
				c[i].coef= c[i].coef + c[j].coef;  /* Add to c[i] */
				c[i].exp= c[j].exp;
				c[j].coef= 0;   /* Zero out the c[j] */ 
				c[j].exp= 0; 
			}		
		}

i have don't want you said. The above is my code. But i want the part the is being zero out not to show as 0x^0 instead to dissapear( not shown on the equation). How can i achieve this. Cane you modify mt code to let me do that. OR show me a code that can help me do that.
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:06 AM

for(i = 0; i < MAX_TERMS - 1; i++) /* If the last element is zero, you don't care */
        if(c[i] == 0) { /* Look for zero */
                for(j = i + 1; j < MAX_TERMS; j++) { /* If the last element is != 0, we care */
                        if(c[j] != 0) { /* Look for non-zero */
                                /* Copy c[j] to c[i] */
                                /* Zero out c[j] */
                                 break; /* Exit j loop, find next zero */
                        }
                }
                if(j == MAX_TERMS) /* Then we are done, j will equal MAX_TERMS if no more non-zero values are found */
                        break; /* Exit i loop */
        }



@Edit: additional comments.

This post has been edited by brds: 14 October 2009 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Neumann*


Reputation:

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:12 AM

View Postmola123, on 14 Oct, 2009 - 08:37 AM, said:

i have don't want you said.

Was This Post Helpful? 0

#9 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:16 AM

That was bad enough the first time. I didn't need to see it again.
Was This Post Helpful? 0
  • +
  • -

#10 mola123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 09

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:24 AM

View Postbrds, on 14 Oct, 2009 - 09:16 AM, said:

That was bad enough the first time. I didn't need to see it again.


Thanks for the help, but i still have a problem of making the zero terms dissappear. That is instead of having 0x^0 in the equation, i want it dissapear, not printed. ie if we have 2x^3+0x^0+2x^2 we should have 2x^3+2x^2 printed on the screen. How can i do this please?
Thanks
Was This Post Helpful? 0
  • +
  • -

#11 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:31 AM

Look @ the post following your first inquiry into that subject.
Was This Post Helpful? 0
  • +
  • -

#12 mola123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 09

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:39 AM

View Postmola123, on 14 Oct, 2009 - 09:24 AM, said:

View Postbrds, on 14 Oct, 2009 - 09:16 AM, said:

That was bad enough the first time. I didn't need to see it again.


Thanks for the help, but i still have a problem of making the zero terms dissappear. That is instead of having 0x^0 in the equation, i want it dissapear, not printed. ie if we have 2x^3+0x^0+2x^2 we should have 2x^3+2x^2 printed on the screen. How can i do this please?
Thanks


Yea, i have zero out the terms not needed, but how to make them disapear is my problem. can you kindly help please?
Was This Post Helpful? 0
  • +
  • -

#13 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:47 AM


Click here!


@edit: stupid c&p.

This post has been edited by brds: 14 October 2009 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#14 mola123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 09

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 10:52 AM

View Postbrds, on 14 Oct, 2009 - 09:47 AM, said:


Click here!


@edit: stupid c&p.

that link to me to Fireworks Vs. Photoshop topic. I quess it is a wrong link. Can you just tell me how to do it Please.
Thanks

Is this not what you are talking about?

Quote

You are only checking its neighbouring element, and you are not drop/zeroing out the match.


I did not know how to make the zeroed terms dissappear. I need help on that. Right now i have zeroed the terms not needed. But i need them not shown in the polynomial terms.

This post has been edited by mola123: 14 October 2009 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#15 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to add like terms of an array after multiplying polynomials

Posted 14 October 2009 - 11:21 AM

No. I'm talking about post #7 in this thread.

View Postbrds, on 14 Oct, 2009 - 11:06 AM, said:

for(i = 0; i < MAX_TERMS - 1; i++) /* If the last element is zero, you don't care */
        if(c[i] == 0) { /* Look for zero */
                for(j = i + 1; j < MAX_TERMS; j++) { /* If the last element is != 0, we care */
                        if(c[j] != 0) { /* Look for non-zero */
                                /* Copy c[j] to c[i] */
                                /* Zero out c[j] */
                                 break; /* Exit j loop, find next zero */
                        }
                }
                if(j == MAX_TERMS) /* Then we are done, j will equal MAX_TERMS if no more non-zero values are found */
                        break; /* Exit i loop */
        }



@Edit: additional comments.

This post has been edited by brds: 14 October 2009 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2