• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculus in Java  RSS feed

 
Nick Fio
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just finished two quarters of Java programming in college. I thought I'd kill two birds with one stone : I could continue improving my Java skills by writing programs which pertain to the upcoming quarter of Calculus (the multi-variable kind). I've got some spare time, so I started writing a program to do single-variable differentiation and integration.

Currently, I have a really nice GUI in which I enter a function in a JTextArea on the left, I select a JRadioCheckButton for "dy/dx" or "integrate" in the middle, and a JTextArea on the right side gets the solution after the Calculus class gets done. The Calculus class takes the expression in String form, iterates through it, and puts coefficients, variables, exponents, trig functions, and ln each into indexes in an ArrayList<String>. If "dy/dx" was selected, my Calculus.java can successfully apply the Power Rule (if the exponent is positive). The Chain Rule...Implicit Differentiation...all of Integral Calculus...I think I'm getting a headache...

In a nutshell, my program looks like a hot mess of nested ifs and a 50-line Switch statement just to get the expression into the ArrayList<String>. It's Rube Goldberg: Java Programmer.

My goals with this project, as I mentioned, are to use Java practice as a way of reinforcing what I learn during the course. Between now and when the quarter begins, I want a program which can do all or most of what I've learned thus far, and then I can mix in the stuff I learn as I learn it.

So...what should my strategy be? How do I use these libraries that are out there such as Apache Commons? Where I can find discussion of similar endeavors, TreeNodes seem to be the preferred tool; why is that?

I would definitely appreciate any help I can get on this. And, if anyone's in the Seattle area, I'll buy ya a beer sometime!
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me, at the heart of performing calculus in a program lies parsing. You need to create classes that represent different mathematical entities that you need, and then parse mathematical expressions to these data types.

Example, you could have a class Polynomial that contains different terms with their powers and coefficients, and give it a static Polynomial parse(String s) method that creates a new instance of it from a String. You could then define methods on it such as derive(), integrate(), or whatever that produce a new function.

This is just a small example, you may not even need such a data type, but it should give you an impression on how you can divide up your problem into bite-size chunks.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But a proper parser for expressions is way above a two-quarter undergraduate level. Perhaps its suitable for Senior project in a serious Computer Science program, but even for a "Applied Information Technology" its going to be a huge leap.

There are lots of textbooks on language and expression parsing, its part of any compiler. There is even one that uses Java instead of Lexx and Yacc.
 
Winston Gutkowski
Bartender
Posts: 10574
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Fio wrote:So...what should my strategy be?

Well, I don't know enough about calculus to offer much advice on that front, but I will say this:
Keep your GUI code and your "calculus mill" completely separate. Specifically, if you have anything that starts with a 'J' (JFrame, JTextField, etc) mixed in with your calculus operations get rid of it.

GUI code should only be concerned with displaying to and interacting with the user. Anything else would suggest that its more tightly coupled than it should be.
Ideally, your "calculus mill" should be a set of functions and classes that can interact as easily with a GUI, a console, a log, or a piece of speech-recognition software; and should probably only ever be dealing with numeric data types.

My 2 cents. HIH.

Winston
 
Nick Fio
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't thought about treating polynomials as objects. What I had thus far was procedural. It seems like I would need test for and then instantiate an object for a polynomial of type ax^n + bx^n-1 ... + c; another for composite functions which use the Chain Rule; and somewhere along the way, have something that replaces trig functions and inverse trig functions with their respective derivations.

In searching around online, I am shocked that I haven't found previously-written source code for similar projects. What I have found has either been very basic (I found a polynomial.java that uses the Power Rule and only works for positive powers) or various libraries that seem geared towards Java professionals whose home address includes the word "cubicle" and math PhDs in labcoats. There ain't much in between.

I do have the GUI separate. It's the only part of my code that doesn't look like a steaming pile of fail.

Thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the reason these libraries are not widespread is because there are entire languages that deal just with maths, such as MatLab or Maple.

Nick, I would forget about the GUI for now and focus on writing mathematical classes and test them on the command line.

As Pat said, parsing is a difficult subject, but I don't think that parsing mathematical expressions is insurmountable for an enthusiastic programmer. It requires a good understanding and familiarity with recursion, and lexical scanning though.

If parsing proves too difficult, you can construct objects directly using a constructor in which you provide the necessary information, and tailor the GUI for different types of expressions, such as different text boxes for different coefficients and powers in a polynomial.
 
Paul Clapham
Sheriff
Posts: 22708
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:But a proper parser for expressions is way above a two-quarter undergraduate level. Perhaps its suitable for Senior project in a serious Computer Science program...


Yes, I remember writing a "d/dx" program in my fourth-year CS course (way back in the Old Stone Age, the university only had three CS courses and this was the most advanced). But I don't remember the parsing as being the hard part, I found that calculating the derivative of the expression was the hard part. However this was most likely because I wasn't using an object-oriented language (those not having been invented yet) and most of the hard work went into keeping track of what bits of expression went where and making sure the pointers between them didn't get broken.

Given a correct data structure for a mathematical expression, it shouldn't be hard to find its derivative by using a bit of recursive programming based on the standard d/dx rules.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12559
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And derivatives will be much easier than integration. There are many functions that have no known integral...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!