Adding polynomials in an ArrayList?

I have an ArrayList terms with various polynomial terms inside. This is what is inside:

Quote:

Values of this: 8.00x^2+1.00+4.00x^2+2.00

So there are 4 elements inside the array. Each one has their own coefficient and exponent.

I want to do a loop that adds the terms with like exponents and store them in another List P3. I want P3 to have this:

Quote:

Values of P3: 12.00x^2+3.00

I have tried a for loop that compares the first element of the array with the second, third... and so on. If the exponents match, then I add the coefficients and add a new term with the new coefficient and the exponent to P3.

I also put another condition that if the term does not match any other term, then simply add that term into P3.

However, the output I get adds each term over and over. This is the result I get:

Quote:

Values of P3: 8.00x^2+12.00x^2+8.00x^2+3.00+4.00x^2

This is the full code of the loop I have done so far. Could anyone tell me what I'm doing wrong?

Code :

for(int i = 0; i <= this.terms.size() - 1; i++)
{
for(int j = i + 1; j <= this.terms.size() - 1; j++)
{
if(this.terms.get(i).getExponent() == this.terms.get(j).getExponent())
{
double newCoeff = this.terms.get(i).getCoefficient() + this.terms.get(j).getCoefficient();
P3.addTerm(new TermImp(newCoeff, this.terms.get(i).getExponent()));
}
else if(this.terms.get(i).getExponent() > this.terms.get(j).getExponent())
{
double newCoeff = this.terms.get(i).getCoefficient();
int newExp = this.terms.get(i).getExponent();
P3.addTerm(new TermImp(newCoeff,newExp));
}
}
}

--- Update ---

Now I see that it is comparing each and everyone if the terms and doing an action depending on the comparison. For example, at the start it compares 8x^2 with 1x^0 and doing this line of code:

Code :

else if(this.terms.get(i).getExponent() > this.terms.get(j).getExponent())
{
double newCoeff = this.terms.get(i).getCoefficient();
int newExp = this.terms.get(i).getExponent();
P3.addTerm(new TermImp(newCoeff,newExp));
}

How can I make it so that only the ones that have like terms add and the other ones are added only once?

Re: Adding polynomials in an ArrayList?

Given:
Quote:

Values of this: 8.00x^2 + 1.00 + 4.00x^2 + 2.00

Quote:

This is the result I get:

Values of P3: 8.00x^2 + 12.00x^2 + 8.00x^2 + 3.00 + 4.00x^2

And this is what you want:

Quote:

Values of P3: 12.00x^2 + 3.00

Can you post a small program that compiles, executes and shows the problem?

Re: Adding polynomials in an ArrayList?

Quote:

Originally Posted by

**jean28**
...

How can I make it so that only the ones that have like terms add and the other ones are added only once?

Well, you have to keep track of that somehow.

The following pseudo-code might indicate one way to do the deed, assuming that all of the exponents are greater than or equal to zero:

Code :

// Initially, assume that all Exponent values are >= 0
Repeat the following loop as i goes from zero to the size of the polynomial
BEGIN LOOP // Outer loop
IF the Exponent of term i is greater than or equal to zero THEN
Add term i to the output list.
Repeat the following loop as j goes from i+1 to the size of p:
BEGIN LOOP // Inner loop
IF the Exponent of term j is equal to the Exponent of term i THEN
Add the Coefficient of term j to the Coefficient of term i.
Set the Exponent of term j equal to -1.
END IF
END LOOP // End of inner loop
END IF
END LOOP // End of outer loop

See? Once a term has been added, it will be disqualified from further consideration. Would this work? Are there any disadvantages to this method?

Maybe a simpler way: Once the coefficient of term *j* has been added to that of term *i* in the output list, just remove term *j* from the input list. (you wouldn't need the "IF" statement inside the inner loop). Would that work? If not, why not? If so, would it be "better" than the first method?

Other ways???

Cheers!

Z

Re: Adding polynomials in an ArrayList?

Quote:

Originally Posted by

**Zaphod_b**
Well, you have to keep track of that somehow.

The following pseudo-code might indicate one way to do the deed, assuming that all of the exponents are greater than or equal to zero:

Code :

// Initially, assume that all Exponent values are >= 0
Repeat the following loop as i goes from zero to the size of the polynomial
BEGIN LOOP // Outer loop
IF the Exponent of term i is greater than or equal to zero THEN
Add term i to the output list.
Repeat the following loop as j goes from i+1 to the size of p:
BEGIN LOOP // Inner loop
IF the Exponent of term j is equal to the Exponent of term i THEN
Add the Coefficient of term j to the Coefficient of term i.
Set the Exponent of term j equal to -1.
END IF
END LOOP // End of inner loop
END IF
END LOOP // End of outer loop

See? Once a term has been added, it will be disqualified from further consideration. Would this work? Are there any disadvantages to this method?

Maybe a simpler way: Once the coefficient of term

*j* has been added to that of term

*i* in the output list, just remove term

*j* from the input list. (you wouldn't need the "IF" statement inside the inner loop). Would that work? If not, why not? If so, would it be "better" than the first method?

Other ways???

Cheers!

Z

I did that loop that you wrote here:

Code :

for(int i = 0; i <= this.terms.size() - 1; i++)
{
if(this.terms.get(i).getExponent() >= 0)
{
P3.addTerm(new TermImp(this.terms.get(i).getCoefficient(), this.terms.get(i).getExponent()));
for(int j = i + 1; j <= this.terms.size() - 1 ; j++)
{
if(this.terms.get(j).getExponent() == this.terms.get(i).getExponent())
{
P3.addTerm(new TermImp(this.terms.get(i).getCoefficient() + this.terms.get(j).getCoefficient(), this.terms.get(i).getExponent()));
}
}
}
}

Unfortunately, the output is not correct:

Quote:

Values of this: 8.00x^2+1.00+4.00x^2+2.00

Values of P3: 8.00x^2+12.00x^2+1.00+3.00+4.00x^2+2.00

So the 8x^2 , 1, and 2 are surprlus.

I thould try to remove them as you said before.

Re: Adding polynomials in an ArrayList?

Quote:

Originally Posted by

**jean28**
I did that loop that you wrote here:

...

Unfortunately, the output is not correct:

...

I did not want to take it on myself to solve your problem, I merely wanted to suggest that there are a number of ways to approach the problem. If one of my ways doesn't suit your sensibilities, try something else.

My pseudo-code was intended to suggest a way of thinking about it. Rather than implementing it, I was kind of hoping that you would *think* about it and see that the correct step inside the inner loop would be to add the new coefficient to the term already in the output list that had the same exponent. Then mark that term in the original list so that it won't be added again. (I didn't mean to mislead you, and I'm sorry if you thought that my pseudo-code was actually supposed to be a valid solution.)

I mean, if there were only one way to solve a problem, life is easy: Figure it out (discover it or have someone tell you about it) and then apply it. Problem solved---on to the next.

The main thing is that once you have broken out of the box in which you were trapped (no solution) to thinking about ways of solving, then, there are lots of things to try.

Cheers!

Z

Re: Adding polynomials in an ArrayList?

Quote:

Originally Posted by

**Zaphod_b**
...

I did not want to take it on myself to solve your problem, I merely wanted to suggest that there are a number of ways to approach the problem. If one of my ways doesn't suit your sensibilities, try something else.

My pseudo-code was intended to suggest a way of thinking about it. Rather than implementing it, I was kind of hoping that you would *think* about it and see that the correct step inside the inner loop would be to add the new coefficient to the term already in the output list that had the same exponent. Then mark that term in the original list so that it won't be added again. (I didn't mean to mislead you, and I'm sorry if you thought that my pseudo-code was actually supposed to be a valid solution.)

I mean, if there were only one way to solve a problem, life is easy: Figure it out (discover it or have someone tell you about it) and then apply it. Problem solved---on to the next.

The main thing is that once you have broken out of the box in which you were trapped (no solution) to thinking about ways of solving, then, there are lots of things to try.

Cheers!

Z

It is okay. I already got it :)

Code :

@Override
public Polynomial add(Polynomial P2)
{
PolynomialImp P3 = new PolynomialImp();
for(Term t: P2)
{
this.addTerm(t);
}
for(int i = this.degree(); i >= 0; i--)
{
ArrayList<Term> temp = new ArrayList<Term>(5);
for(int j = 0; j <= this.terms.size() - 1; j++)
{
if(this.terms.get(j).getExponent() == i)
{
temp.add(this.terms.get(j));
}
}
if(temp.size() == 1)
{
P3.addTerm(temp.get(0));
}
else if(temp.size() > 1)
{
double coeff = 0;
//temp.get(0).getCoefficient() + temp.get(1).getCoefficient();
int expo = temp.get(0).getExponent();
for(int j = 0; j <= temp.size() - 1; j++)
{
coeff += temp.get(j).getCoefficient();
}
if(coeff == 0)
{
}
else
{
P3.addTerm(new TermImp(coeff,expo));
}
}
}
return P3;
}