• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Polynomial class that uses recursive math functions to combine 2 polynomials

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm building a Polynomial class that uses recursive math functions to combine 2 polynomials. In order to impliment the merge algorithm, I need to have a public method that returns a new polynomial, but without the first term(node) and a private recursive function that copy's the old terms to the new polynomial. I know that I must be doing it wrong, but I can't figure out how.

 
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

new Term() sets 'next' to null by default
therefore this is the same as


 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Tanya!

Maybe you could provide us with an example of what you want to do?

It looks to me like your polynomial is stored internally as a linked list of terms, in descending order of exponent. Like for example if your polynomial was 3x^7 + 11x^2 - 9, then you would have three terms in the list. The first term would represent 3x^7, the second term would represent 11x^2, and the third term would represent -9x^0. Am I right so far?

Now you're asking how to create a new list with the first node dropped. So you've lost the thread already. It would be better to ask your question in terms of the problem you're dealing with. And remember, your problem is "Doing arithmetic with polynomials" and not "Manipulating linked lists". So is your problem how to create a new polynomial with the term with the largest exponent removed?

I'm asking this because it isn't clear to me what your specific question is, in terms of polynomials. Neither "combine" nor "merge" is a word which means anything to me in terms of polynomials.
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The goal of the program is to do math operations on 2 polynomials. I'm having some problems with each function I've written, where they are 'mostly' working, but fail under certain conditions, and I'm hoping someone can help me spot them. I'm attaching my code and the output I'm getting.




Example Output:

Testing addition

[0] + [0] = 0

[0] + [1] = 1

[0] + [x] = x

[3 x] + [-3 x] = 0 x

[-3 x] + [0] = -3 x

[-3 x] + [1] = -3 x + 1

[-3 x] + [x] = -2 x

[-3 x] + [3] = -3 x + 3

[-3 x] + [3 x] = 0 x


[2/3 x^2 - 2] + [-3/4 x^10 + 1/5 x^5] = -3/4 x^10 + 1/5 x^5 + 2/3 x^2 - 2

[2/3 x^2 - 2] + [2 x^5 + 2] = 2 x^5 + 2/3 x^2 -

[2/3 x^2 - 2] + [3 x^10 + 2] = 3 x^10 + 2/3 x^2 -

[-3/4 x^10 + 1/5 x^5] + [0] = -3/4 x^10 + 1/5 x^5

[-3/4 x^10 + 1/5 x^5] + [1] = -3/4 x^10 + 1/5 x^5 + 1

[3 x^10 + 2] + [2/3 x^2 - 2] = 3 x^10 + 2/3 x^2 -

[3 x^10 + 2] + [-3/4 x^10 + 1/5 x^5] = 9/4 x^10 + 1/5 x^5 + 2

[3 x^10 + 2] + [2 x^5 + 2] = 3 x^10 + 2 x^5 + 4

[3 x^10 + 2] + [3 x^10 + 2] = 6 x^10 + 4

Testing subtraction

[0] - [0] = 0

[0] - [1] = 1

[0] - [x] = x

[0] - [3] = 3

[0] - [3 x] = 3 x

[0] - [-3] = -3

[0] - [-3 x] = -3 x

[0] - [2 x^2 - 2 x] = 2 x^2 - 2 x

[0] - [3 x^10 + 2] = 3 x^10 + 2

[1] - [0] = 1

[1] - [1] = 0

[1] - [x] = x + 1

[2 x^5 + 2] - [-3/4 x^10 + 1/5 x^5] = 3/4 x^10 + 9/5 x^5 + 2

[2 x^5 + 2] - [2 x^5 + 2] = 0

[2 x^5 + 2] - [3 x^10 + 2] = -3 x^10 + 2 x^5

[3 x^10 + 2] - [0] = 3 x^10 + 2

[3 x^10 + 2] - [1] = 3 x^10 + 1

[3 x^10 + 2] - [x] = 3 x^10 - x + 2

[3 x^10 + 2] - [3] = 3 x^10 - 1

[3 x^10 + 2] - [3 x] = 3 x^10 - 3 x + 2

[3 x^10 + 2] - [-3] = 3 x^10 + 5

[3 x^10 + 2] - [3 x^10 + 2] = 0

Testing multiplication

[0] * [0] = 0

[0] * [1] = 0

[0] * [x] = 0

[0] * [3] = 0

[0] * [3 x] = 0

[0] * [-3] = 0

[0] * [-3 x] = 0

[1] * [0] = 1

[1] * [3] = 1

[1] * [3 x] = 1

[1] * [-3] = 1

[1] * [-3 x] = 1

[1] * [2 x^2 - 2 x] = 1

[x] * [2 x^5 + 2] = x

[x] * [3 x^10 + 2] = x

[3] * [0] = 3

[3] * [3] = 3


[3] * [-3 x] = 3

[3 x] * [3 x^10 + 2] = 3 x

[-3] * [0] = -3

[-3 x] * [-3 x] = -3 x

[-3 x] * [3 x^10 + 2] = -3 x

[2 x^2 - 2 x] * [0] = 4 x^2 - 4 x

[2 x^2 - 2 x] * [2 x^5 + 2] = 4 x^2 - 4 x

[-3/4 x^10 + 1/5 x^5] * [3 x] = -3/2 x^10 + 2/5 x^5

[2 x^5 + 2] * [0] = 4 x^5 + 4

[3 x^10 + 2] * [x] = 6 x^10 + 4

 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What problems are you seeing? Are all of them wrong? Only some? Which ones? What output did you expect?
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm finding your project interesting though my polynomial experience is minimal. I would like to be able to run your code to see if I can help but I'd need to see your other classes as well, Rational, and testing classes.
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This only adds to first.next. It does not add to the end of the linked list.
You'll need something like this.

 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What problems are you seeing? Are all of them wrong? Only some? Which ones? What output did you expect?


Example Output:

Testing addition

[0] + [0] = 0

[0] + [1] = 1

[0] + [x] = x

[3 x] + [-3 x] = 0 x

Just noticed you were using color to indicate bad output. I'm slightly color blind and couldn't see it. It is preferable to use bold, italic, or underline for this reason.
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My division method is failing when I go into the while loop to perform the division algorithm. Can anyone see my error here? I'm including the files in case someone wants to try and compile them.

 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you describe how it is failing? What inputs are you giving? What outputs do you get? What were you expecting? Did you get any error messages?

You need to help us help you.
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't provided the Rational class, nor a class containing main() so we can't compile and run it.
 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I thought I had attached main and Rational. But it won't let me use .txt or .java so I'll paste the code

The error I'm getting is just an infinite loop.


Rational class

 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the main test class

 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Rational class won't compile because on line 34 you attempt to switch on a long ('denom').
 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler ignores the error in the Rational class and it works anyway.
If I fix the error that causes the while loop to go into an infinite loop, then my next issue is that the remainder is always 0 and the quotient is only ever one term.
I think it's because I'm not moving the quotient pointer to the next term.
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use remainder.first before you check to see if it's null. This must never be null for the data you are giving it or else you would have gotten a null pointer exception.
 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taking that segment of code out of the while loop stops the infinite looping, but now it's failing for a different reason.
I think it's this line that's the issue
binomial.first = new Term(Rational.div(remainder.first.coef, p2.first.coef),
                 remainder.first.exp - p2.first.exp);

but I'm not sure why.
 
Tanya Wilcox
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I set remainder.first to p1.first after I've checked for p1 being null, I'm not sure why that would cause the infinite loop. Because remainder is being updated inside the while loop, I thought I needed to check for remainder.next not being null as an exit condition for the loop. Is that wrong? Removing the condition remainder.first !=null stops the infinite recursion, but then I still have issues with assigning the new term to binomial.first

Any idea why?
 
Carey Brown
Saloon Keeper
Posts: 9719
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pulling out remainder.first != null should not have had an effect as near as I can tell, so I'm at a loss here. I might suggest adding a few prinln()'s in the loop so you get an idea of what it is doing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic