Java: Is there a tool available that can allow me input, store, and evaluate math formulas?

StackOverflow https://stackoverflow.com/questions/402833

  •  03-07-2019
  •  | 
  •  

Question

Even something very rudimentary is fine. Like entering and evaluating something like x+1, where x is a variable I get to specify.

I was thinking of writing my own using Strings for each formula, regex for parsing them, etc., but let's assume my goal is to be as lazy as I can possibly get away with, and that if there's another option (especially a formal one), I'd rather try to use that instead, first.

Is there anything out there that does something like this?

Was it helpful?

Solution

You can think about using scripting from Java 6. The reference JDK comes with a JavaScript implementation, and you can plug in other languages too.

OTHER TIPS

I know the following libraries:

I've used JEP with success a couple of years ago and it still seems to be actively developed. I believe they had a more permissive license back in those days, but it's probably cheaper than coding it yourself (depending on your exact needs and skills).

I've built a java library to do exactly this as a learning example for Lex/YACC, for myself. It was incorporated into a larger project, where it seems to work well. Were I to start the larger project again, I'd follow the advice above and use the Java scripting engine.

You could try LibFormula, a formula eavluation library that's based on OpenDocument's OpenFormula standard.

OpenFormula was written for spreadsheet applications and uses a syntax close to Excel-formulas. More info can be found in the Pentaho-Wiki

Sample:

=[x]+1

you can check out exp4j which i released under the Apache License 2.0. it implements dijkstra's shunting yard algorithm, and works well with multivariable functions like f(x,foo)=log(foo^(x^-1))

Some may be interested in a new tool called formula4j.

It's commercial, so it's not for everyone.

Maybe MathML?

MathML is a low-level specification for describing mathematics as a basis for machine to machine communication which provides a much needed foundation for the inclusion of mathematical expressions in Web pages. It is also important in publishing workflows for science and technology and wherever mathematics has to be handled by software. The new version brings, for instance, improvements for accessibility of mathematics, and for formulas in languages written from right to left. The basic markup remains the same, as the community have seven years of experience with it.

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