Question

How would someone implement mathematical formulae in Java?

What I mean, the user inputs a string with multiple variables. Like a simple quadratic formula: x^2 + 5x + 10. Or in Java: (Math.pow(x,2)) + (x * 5) + 10. The user would then enter that and then the program would solve for x. I will be using the BeanShell Interpreter class to interpret the string as an equation. But how would I solve for x?

Was it helpful?

Solution

This is a hard problem, unless you restrict yourself to simple equation types.

Here are some links for you to chase:

Trawling through the second link, I spotted 3 open-source Java systems.

OTHER TIPS

I don't think this is a homework (way too hard!), and I don't think it's a research problem either (what's new?), so depending on the context of the problem, the easiest solution may just be to leverage Wolfram Alpha.

WolframAlpha: solve x^2 + 5x + 10

x = -1/2 i (sqrt(15)-5 i) ~~ -2.5-1.93649 i
x = 1/2 i (sqrt(15)+5 i) ~~ -2.5+1.93649 i

Links

As @nuriaion suggests you could use a Computer Algebra System, though I think that Mathematica or Maple or Sage or Macsyma would have been better suggestions. There are others too. I'm not sure that many would regard either Matlab or Octave as CAS, they are more like numerical computing environments. Though the Matlab Symbolic Toolbox might provide enough CAS-ability for your needs.

It is relatively easy to integrate Mathematica into a Java-programmed system. Possibly not cheap mind you.

I'm using the Java Algebra System (JAS) library in my Symja project to solve univariate polynomials.

Example input for the symbolic mode:

Roots[x^2 + 5x + 10]

You can map coefficients of x to power of x's. For example; assume you have a formula like this: 3x^2 - 5x + C = 0 But this simple approach is just for small degree equations. For example equation i gave is in degree of 2 (biggest x power); so it has 2 solution values for x, and can be computed with Vieta equations.

PS: I'm studying Math Engineering in college and i'm pretty satisfied with Gnu Octave.

http://mathworld.wolfram.com/PolynomialRoots.html

http://en.wikipedia.org/wiki/Root-finding_algorithm

One possibility is to use Maxima compiled with ABCL to solve the equations (and do any other algebra you need). ABCL is an implementation of Common Lisp in Java. Your front end program would take the input and pass it to Maxima to solve it, then display the result. Essentially you could use Maxima as a big math library.

It is better if you study the data structure algorithms for converting expressions from infix to postfix & to & fro. Then in the data structure there is an algorithm for evaluating expressions. This evaluation is done by using a stack.

You can study the data structure books. Some of the books are "Fundamentals of data structures" by Ellis Horowitz, Schaum's Outline of Data Structures with Java, etc.

Maybe you can use a CAS like MATLAB, Octave, etc.

Octave is open Source and I believe it has a Java binding (joPas).

Edit: MATLAB with a symbolic toolbox can solve a lot. Octave also have some symbolic toolbox, but I don't know how good it is.

I propose Octave because it is open source. Most CASes are very expensive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top