Is it possible to resolve mathematical function dynamically

743 views Asked by At

Is it possible, to resolve mathematical functions dynamically, e.g by the use of a given API?

Given there is a function

a = b + c

is there a way to write something comparable to:

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

Ideally, resolve(Object... args) should accept further classes of type Function.

EDIT 1: The API should be includable into a Java EE environment such as jBossAS.

EDIT 2: Actually I want to solve equations with one unknown variable, but I want to avoid hard coded functions. Thus, for the function

a+b=c

I don't want to write the functions hard coded

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

Instead, as @Berylium says, I want to evaluate the expression dynamically.


EDIT 3: I'm trying symja right now and I think I'm getting closer, but I have troubles with the syntax.

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

Can you help me with the input syntax?


EDIT 4: Got it :-) For input

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

the output is

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

There are 2 answers

0
aboger On BEST ANSWER

After adding symja JAR to the build path, the following code prints the output below:

Code:

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();
    }

}

Output:

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

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.