195

7
+3  Q:

## How would someone implement mathematical formulae in java?

What I mean is like have to user input a string with multiple variables and get the value of those variable. 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?

+5  A:

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.

A:

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

Octave is Open-Source an 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 CAS are very expensive.

But those are numerical, so how would you solve for a variable?
+1  A:

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.

+1  A:

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`

A:

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

+1  A:

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]
``````
A:

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.