在java中解析数学表达式的好库是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2226863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:57:32  来源:igfitidea点击:

What's a good library for parsing mathematical expressions in java?

javaandroidmathparsing

提问by CodeFusionMobile

I'm an Android Developer and as part of my next app I will need to evaluate a large variety of user created mathematical expressions and equations. I am looking for a good java library that is lightweight and can evaluate mathematical expressions using user defined variables and constants, trig and exponential functions, etc.

我是一名 Android 开发人员,作为我的下一个应用程序的一部分,我需要评估大量用户创建的数学表达式和方程。我正在寻找一个很好的 Java 库,它是轻量级的,可以使用用户定义的变量和常量、三角函数和指数函数等来评估数学表达式。

I've looked around and Jepseems to be popular, but I would like to hear more suggestions, especially from people who have used these libraries before.

我环顾四周,Jep似乎很受欢迎,但我想听听更多建议,尤其是以前使用过这些库的人的建议。

回答by gpampara

JEvalis a good alternative. I abandoned Jep due to it becoming commercial. The only concern is that JEval seems to be a little dormant at the moment (last release in 2008).

JEval是一个不错的选择。我放弃了 Jep,因为它变得商业化了。唯一的担忧是 JEval 目前似乎有点休眠(最后一次发布是在 2008 年)。

回答by Lawrence Dol

I wrote a simple but capable Math Expression Evaluatora while back, which is free and open-source. It's main advantage is being fast and tiny - both are a good thing with hand-held devices. If it meets your need you are welcome to use it.

不久前,我编写了一个简单但功能强大的Math Expression Evaluator,它是免费且开源的。它的主要优点是快速和小巧 - 两者对于手持设备来说都是一件好事。如果它满足您的需要,欢迎您使用。

Primary Features:

主要特点:

  • Basic math operators, with inferred precedence (^ * × · / ÷ % + -).
  • Explicit precedence with parenthesis.
  • Implicit multiplication of bracketed subexpressions.
  • Correct right-associativity of exponentials (power operator).
  • Direct support for hexadecimal numbers prefixed by 0x.
  • Constants and variables.
  • Extensible operators.
  • Extensible functions.
  • 20 KiB footprint.
  • 基本数学运算符,具有推断优先级 (^ * × · / ÷ % + -)。
  • 带括号的显式优先级。
  • 括号内的子表达式的隐式乘法。
  • 修正指数的右结合性(幂运算符)。
  • 直接支持以 0x 为前缀的十六进制数字。
  • 常量和变量。
  • 可扩展的运算符。
  • 可扩展的功能。
  • 20 KiB 占用空间。

Example

例子

MathEval math=new MathEval();

math.setVariable("Top",    5);
math.setVariable("Left",  20);
math.setVariable("Bottom",15);
math.setVariable("Right", 60);

System.out.println("Middle: "+math.evaluate("floor((Right+1-Left)/2)"));

回答by CodeFusionMobile

This doesn't exactly fit my initial conditions, but I found a wonderful parser written in C++. I'm trying to figure out Android's Native code support to see if I can use it. It's exactly what I need.

这并不完全符合我的初始条件,但我发现了一个用 C++ 编写的很棒的解析器。我正在尝试弄清楚 Android 的本机代码支持,看看我是否可以使用它。这正是我所需要的。

Here's the documentation for the project.
http://www.codeproject.com/KB/recipes/MathieuMathParser.aspx

这是该项目的文档。
http://www.codeproject.com/KB/recipes/MathieuMathParser.aspx

回答by John O

There is a new commercial tool called formula4j, which may be of interest to some.

有一种新的商业工具,称为formula4j,某些人可能会对它感兴趣。

回答by Mohit Gupta

Try https://code.google.com/p/expressionoasis/. It is an extensible Expression Evaluation framework and will meet such requirements.

试试https://code.google.com/p/expressionoasis/。它是一个可扩展的表达式评估框架,将满足此类要求。