Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Adding polynomials in an ArrayList?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Adding polynomials in an ArrayList?

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

    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:

    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:

    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?

    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:

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding polynomials in an ArrayList?

    Given:
    Values of this: 8.00x^2 + 1.00 + 4.00x^2 + 2.00
    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:
    Values of P3: 12.00x^2 + 3.00
    Can you post a small program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Adding polynomials in an ArrayList?

    Quote Originally Posted by jean28 View Post
    ...
    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:

    // 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

  4. #4
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Adding polynomials in an ArrayList?

    Quote Originally Posted by Zaphod_b View Post
    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:

    // 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:

    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:

    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.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Adding polynomials in an ArrayList?

    Quote Originally Posted by jean28 View Post
    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

  6. #6
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Adding polynomials in an ArrayList?

    Quote Originally Posted by Zaphod_b View Post
    ...

    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

    @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;
    	}

Similar Threads

  1. Adding Object to ArrayList
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 20th, 2013, 10:02 AM
  2. Adding an array to an arraylist
    By raja211991 in forum Collections and Generics
    Replies: 1
    Last Post: January 7th, 2013, 03:46 PM
  3. Somehow it's not adding to an ArrayList of ArrayLists.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 05:09 PM
  4. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  5. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM