5

I'm trying to find something like Java Embedding Plugin (JEP) that can evaluate a mathematical formula (string) and give back the answer.

But it should also calculate a variable, for example: (25+36+x)*2 = 25 should give: x = -11

A little like http://www.wolframalpha.com/, but it shouldn't be that versatile, and it should work off-line.

Open source is preferred.

I need it for my little calculator project, http://sourceforge.net/projects/calex/.

2
  • This shouldn't be closed, since he wants to solve an equation and is not looking for something like javascripts eval().
    – stacker
    Nov 13, 2010 at 17:40
  • 2
    Unless you're limiting yourself to a very few forms of equation (say linear up to quartic), then this would be a major project. en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems has a few Java ones. Nov 13, 2010 at 18:47

2 Answers 2

3

This is called Arithmetic evaluation. One of the easiest way to implement this is using Edsger Dijkstra Shunting-yard_algorithm.

The shunting-yard algorithm is a method for parsing mathematical equations specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST). The algorithm was invented by Edsger Dijkstra and named the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard. Like the evaluation of RPN, the shunting yard algorithm is stack-based. Infix expressions are the form of mathematical notation most people are used to, for instance 3+4 or 3+4*(2−1). For the conversion there are two text variables (strings), the input and the output. There is also a stack that holds operators not yet added to the output queue. To convert, the program reads each symbol in order and does something based on that symbol.

But I have seen exact solution what you are looking for on some stackoverflow user blog, but I can't remember the address (it was like 'code monkeyism'). It was lightweight class, that you could use in applets (you could also define constants and reset values).

Edit: Found it: http://tech.dolhub.com/Code/MathEval

A Linear-Recursive Math Evaluator

This math expression evaluator was born out of a need to have a small-footprint and efficient solution which could evaluate arbitrary expressions reasonably efficiently without requiring pre-compilation. I needed something which would do basic math with variables, expressions like: "Top+2", "Bottom-2" and "(Right+1-Left)/2".

Research on the Internet turned up a number of fairly good solutions, all of which revolved around creating parse trees (which makes sense). The problem was - they were all rather bulky, and I couldn't afford to add 100K to my applet size just for math. So I started wondering about a linear recursive solution to the problem. The end result is an acceptably performing single class with no external dependencies, weighing in under 10 KiB.

4
  • 2
    Except my evaluator won't solve equations like the example in the question where the variable is the unknown. It could only take various values of x using trial and error and evaluate (25+36+x)*2 until the answer given is 25. Nov 13, 2010 at 20:39
  • 1
    You would have to manipulate the equation to get x by itself on one side of the equals: I think (25+36+x)*2=25 => x=25/2-(25+36), if I recall my math correctly, evaluates (correctly) to -48.5, not -11. Nov 13, 2010 at 20:45
  • Well, yes. Wolfram alpha uses some fancy AI to fix invalid input, and (25 + 36 + x)*2 = 25 becomes Solve[(25 + 36 + x)*2 == 25, x], so I assumed, that he needs to write a 'linear systems solver' or do it manually.
    – Margus
    Nov 13, 2010 at 21:05
  • wow, thx for the fast reply, i'll check it out and let you know what algebra_system i used :)
    – Berty
    Nov 15, 2010 at 7:02
0

I released an expression evaluator based on Dijkstra's Shunting Yard algorithm, under the terms of the Apache License 2.0:

http://projects.congrace.de/exp4j/index.html

1
  • looks nice :), i might add it to my project. although for a calculator the bootspeed of java is rather slow. maybe i need to revise it and start over in c++.
    – Berty
    Jul 24, 2011 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.