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

北城余情 提交于 2020-02-20 06:05:40

问题


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?


回答1:


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.




回答2:


I know the following libraries:

  • Symja Parser (GPL License) supports Mathematica like syntax

  • Jep - Java Math Expression Parser (commercial - older versions under GNU Public License?)

  • JFormula (commercial)

  • MathEval - Math Expression Evaluator (public domain and very small footprint)




回答3:


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).




回答4:


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.




回答5:


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



回答6:


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))

  • Demo applet
  • Download



回答7:


Some may be interested in a new tool called formula4j.

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




回答8:


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.



来源:https://stackoverflow.com/questions/402833/java-is-there-a-tool-available-that-can-allow-me-input-store-and-evaluate-mat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!