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?

5
  • 1
    FYI, ^ in Java is not exponentiation. See stackoverflow.com/questions/1991380/… Commented Jun 16, 2010 at 6:37
  • Also, see en.wikipedia.org/wiki/Equation_solving Commented Jun 16, 2010 at 6: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. Commented Jun 16, 2010 at 6: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. Commented Jun 16, 2010 at 7: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. Commented Jun 16, 2010 at 16:28

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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] 

Comments

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

Comments

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.

1 Comment

Do you links, pics, or code snippets as examples of this?
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.

Comments

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.

1 Comment

But those are numerical, so how would you solve for a variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.