0

是否有可能动态地解析数学函数,例如通过使用给定的 API?

鉴于有一个功能

a = b + c

有没有办法写出类似的东西:

Function func = new Function("a=b+c");
Result result = func.resolve("a=1", "b=?", "c=a"); 
System.out.println(result.getValue());

理想情况下,resolve(Object... args)应该接受更多的 type 类Function

编辑 1: API 应该包含在 Java EE 环境中,例如 jBossAS。

编辑 2:实际上我想用一个未知变量求解方程,但我想避免硬编码函数。因此,对于函数

a+b=c

我不想编写硬编码的函数

getA(int b, int c){...}
getB(int a, int c){...}
getC(int a, int b){...}

相反,正如@Berylium 所说,我想动态评估表达式。


编辑 3:我现在正在尝试 symja,我想我越来越近了,但我在语法上遇到了麻烦。

try {
    F.initSymbols(null);
    EvalUtilities util = new EvalUtilities();
    StringBufferWriter buf = new StringBufferWriter();
    String input = "....";  
    IExpr result = util.evaluate(input);
    OutputFormFactory.get().convert(buf, result);
    String output = buf.toString();
    System.out.println("Evaluation for " + input + " is " + output);
} catch (final Exception e) {

你能帮我输入语法吗?


编辑 4:知道了 :-) 用于输入

String input = "Solve[{a==10,c==20,a+b==c},{a,b,c}]";  

输出是

Evaluation for Solve[{a==10,c==20,a+b==c},{a,b,c}] is {{a->10,c->20,b->10}}
4

2 回答 2

1

Any embeddable Java scripting engine will do:

For example, using BeanShell:

    final Interpreter interp = new Interpreter();
    try {
        interp.eval("System.out.println(\"Hello, world\");");

        final String s = "Hello, world (2)";
        interp.set("test", s);
        interp.eval("System.out.println(test);");

        System.out.println("3+4=" + interp.eval("3+4"));

        interp.set("a", 4);
        interp.set("b", 5);
        System.out.println("a + b = " + interp.eval("a + b"));

        final String script1 =
          "public int f(int a) { return a * a; }; System.out.println(f(4));";
        interp.eval(script1);

        final String script2 = 
          "public int f(int a) { return a * a; }; f(4)";
        System.out.println(interp.eval(script2));

        final String script3 = 
          "import java.util.Date; Date date = new Date(); date";
        System.out.println(interp.eval(script3));

        final String script4 = 
          "class X { public int a; } X x = new X(); x.a = 5; x.a";
        System.out.println(interp.eval(script4));

    } catch (EvalError e) {
        e.printStackTrace();
    }

One advantage is that BeanShell uses Java syntax which is quite close to Java. So there is no need to learn/use another language/syntax.

于 2013-08-29T11:11:18.967 回答
0

symja JAR添加到构建路径后,以下代码将打印以下输出:

代码:

public static void main(String[] args) {

    try {
        F.initSymbols(null);
        EvalUtilities util = new EvalUtilities();

        StringBufferWriter buf = new StringBufferWriter();
        String input = "Solve[{a==10,c==20,a+b==c},{a,b,c}]";  
        IExpr result = util.evaluate(input);
        OutputFormFactory.get().convert(buf, result);
        String output = buf.toString();
        System.out.println("Evaluation for " + input + " is " + output);

    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        // Call terminate() only one time at the end of the program  
        ComputerThreads.terminate();
    }

}

输出:

Evaluation for Solve[{a==10,c==20,a+b==c},{a,b,c}] is {{a->10,c->20,b->10}}
于 2013-08-29T13:42:47.500 回答