2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
  • 1
    FYI, `^` in Java is not exponentiation. See http://stackoverflow.com/questions/1991380/what-does-the-operator-do-in-java/2672217#2672217 – polygenelubricants Jun 16 '10 at 06:37
  • Also, see http://en.wikipedia.org/wiki/Equation_solving – polygenelubricants Jun 16 '10 at 06:41
  • I am sorry. I dont understand this quite well. If a question can be converted to a programming language, so well the answer can be. Is it not? - for this QE - find what is a, b , c and implement the formula (-b + Math.sqrt(Math.pow(b,2) - 4*a*c))/2a and so on. – gekrish Jun 16 '10 at 06:55
  • @gurupriyan.e -- it's not so simple; a quadratic equations might be easy, but the form of all possible equations is going to be a pretty large list; extending, for example, to cubic, quartic, and nth-degree polynomials is already hard enough. You could conceivably create special cases for trigonometric functions, exponentials, variables in the denominator, etc... It's not impossible, but just not as trivial as you imply. – Justin L. Jun 16 '10 at 07:43
  • If you can state what you require I'm sure I can help. But your description makes no sense. Are you trying to evaluate strings like `x^2 + 5x + 10` for some value of `x`? Or are you trying to solve an equation? If you're solving an equation, which one? You haven't given any equations. Are you trying to solve `x^2 + 5x + 10 = 0`? If so, there are some nice methods you can implement, that'll often work, that I can help with, that don't need anything like an algebra package. – sigfpe Jun 16 '10 at 16:28

8 Answers8

5

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

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.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
1

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

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1

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]
axelclk
  • 950
  • 9
  • 28
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nuriaion
  • 2,621
  • 2
  • 23
  • 18
0

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

Deniz Acay
  • 1,609
  • 1
  • 13
  • 24
0

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.

0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SIVAKUMAR.J
  • 4,258
  • 9
  • 45
  • 80