Java数学表达式解析器,可以将复数作为变量? [英] Java math expression parser that can take complex numbers as a variable?

查看:121
本文介绍了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)

其中z是复数。现在,我已经看过JEP和一些示例,但是我无法解决它允许你实际输入z作为变量(并且在任何情况下它都不是免费的)。是否有一个Java表达式解析器(在处理中工作,使用旧版本的java并且没有泛型)我可以用来做这个吗?

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?

推荐答案

如PhiLo所述,你可以使用仿制药。试试这个处理草图:

As mentioned by PhiLo, you can use generics. Try this Processing sketch:

import java.util.*;
java.util.List<String> list = Arrays.asList("a", "b", "c");
textFont(loadFont("UMingCN-30.vlw"));
for(int i = 0; i < list.size(); i++) {
  text(list.get(i), 5, int(i*30)+30);
}

还有一个非商业版的JEP(GPL)。下载此处并将其添加到Processing类路径(导入它)。
成功完成后,您可以像这样使用JEP:

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:

void setup() {
  org.nfunk.jep.JEP parser = new org.nfunk.jep.JEP();
  parser.addComplex();
  try {
    parser.parseExpression("(1+2*i) + (3+8*i)");
    println(parser.getComplexValue());
  } catch(Exception e) {
    e.printStackTrace();
  }
}

产生(预期)输出:(4.0, 10.0)

which produces the (expected) output: (4.0, 10.0)

这篇关于Java数学表达式解析器,可以将复数作为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆