Java数学表达式解析器可以将复数作为变量?
我正在 Processing 中编写一个程序来转换复数。但是,我想要一种获取输入字符串并使用复杂变量计算转换的方法。例如:
1/(z+1)
(z^2)/(z/2)
其中 z 是复数。现在,我已经查看了 JEP 和一些 示例,但我无法确定是否它允许您实际输入 z 作为变量(无论如何它都不是免费的)。是否有一个 Java 表达式解析器(在处理中工作,它使用旧版本的 java 并且没有泛型)可以用来执行此操作?
如果没有,有人可以告诉我如何创建一个的基础知识吗?
I am writing a program in Processing that transforms complex numbers. However, I want to have a method of taking an input string and calculating the transformation using a complex variable. For example:
1/(z+1)
(z^2)/(z/2)
where z is a complex number. Now, I've looked at JEP and some examples, but I cannot work out if it would allow you to actually enter z as a variable (and in any case it is not free). Is there an expression parser for Java (that works in processing, which uses an old version of java and does not have generics) that I could use to do this?
If there is not, could someone point me to the basics of how to create one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如 PhiLo 所提到的,您可以使用泛型。尝试这个处理草图:
并且有可用的 JEP (GPL) 的非商业版本。下载它 此处 并将其添加到您的处理类路径(导入它)。
成功执行此操作后,您可以像这样使用 JEP:
它会产生(预期的)输出:(4.0, 10.0)
As mentioned by PhiLo, you can use generics. Try this Processing sketch:
And there's a non commercial version of JEP available (GPL). Download it here and add it to your Processing classpath (import it).
After successfully doing so, you can use JEP like this:
which produces the (expected) output: (4.0, 10.0)
看看这个: http://bracer.sourceforge.net 这是我对调车场算法的实现,这个解析器支持复数。
Have a look at this: http://bracer.sourceforge.net It's my implementation of shunting-yard algorithm and this parser supports complex numbers.
使用Apache Common Math。它非常容易使用。
您可以初始化实部+虚部。您还可以从字符串初始化它们。它支持可以对虚数执行的多种运算。
下面是执行一些常见操作的代码示例:
如果您需要其他选择,另一个替代方案是 FrAid。
Use Apache Common Math. It is very easy to use.
You can initialize both real+imaginary parts. You can also initialize them from a string. It supports a wide array of operations that you can do with imaginary numbers.
Here is a example of code for doing some common operations:
Another alternative is FrAid, if you want another option.
如果由于某种原因您需要比迄今为止建议的“罐装”复杂数学表达式解析器更大的灵活性(=完全控制运算符、优先级、树结构),您可能需要考虑我的可配置解析器:
https://github.com/stefanhaustein/expressionparser
针对您的案例的直接评估代码示例:
调用示例:
解析器本身在单个 < 中实现没有依赖项的 href="https://github.com/stefanhaustein/expressionparser/blob/master/src/main/java/org/kobjects/expressionparser/ExpressionParser.java" rel="nofollow">java 文件 ,因此出于评估目的,可以简单地将其复制到您自己的项目中
If for some reason you need more flexibility than the "canned" complex math expression parsers suggested so far (= full control over operators, precedence, tree construction), you may want to consider my configurable parser:
https://github.com/stefanhaustein/expressionparser
Example direct evaluation code for your case:
Example invocation:
The parser itself is implemented in a single java file without dependencies, so for evaluation purposes it's simple to copy to your own project
我会(实际上)手动创建一个解析表并使用简单的 LR 或 LALR 解析器来处理它。在减少时,您可以执行计算。这样做的一个优点是很容易修改“语言”或可接受的输入。
I would (and have, actually) manually make a parse table and use a simple LR or LALR parser to process it. At a reduction, you can perform the calculations. One advantage to this is that it is easy to modify the "language", or acceptable input.
这是疯狂的解决方案:java有内置的JavaScript引擎(我想你可以从Processing访问它)。现在,您编写一个处理复数的 javascript 类(从 复制它在这里)。然后,按照此处。之后,您可以从 java 中评估该字符串。这太疯狂了,我不确定它是否会起作用(我不知道 javascript)。也许无需解析表达式就能找到一些更简单的解决方案。
Here's crazy solution: java has built-in JavaScript engine (I suppose you can access it from Processing). Now, you write a javascript class that works with complex numbers(copy it from here). Then, overload math operators as specified here. AFter that you can just eval this string from java. It's crazy and I'm not sure that it will work (i don't know javascript). Maybe it will make to find some simplier solution without parsing expressions.
以下是直接数学表达式解析器的链接(64 行): http://javadots.blogspot.com/2008/11/arithemetic-expressions-solver-in-64.html
调整它来支持您的需求应该不会太困难
Here is a link to a straight-forward math expression parser (64 lines): http://javadots.blogspot.com/2008/11/arithemetic-expressions-solver-in-64.html
Tweaking it to support your needs should not be too difficult