Iak
Iak

Reputation: 13

Java - Efficient evaluation of user-input math functions (preparation possible, existing variables)

In a Java program which has a variable t counting up the time (relative to the program start, not system time), how can I turn a user-input String into a math formula that can be evaluated efficiently when needed. (Basically, the preparation of the formula can be slow as it happens Pre run-time, but each stored function may be called several times during run-time and then has to be evaluated efficiently)

As I could not find a Math parser that would keep a formula loaded for later reference instead of finding a general graph solving the equation of y=f(x), I was considering to instead have my Java program generate a script (JS, Python, etc) out of the input String and then call said script with the current t as input parameter. -However I have been told that Scripts are rather slow and thus impractical for real-time applications.

Is there a more efficient way of doing this? (I would even consider making my Java application generate and compile C-code for every user input if this would be viable)

Edit: A tree construct does work to store expressions, but is still fairly slow to evaluate as from what I understand I would need to turn it into a chain of expressions again when evaluating (as in, traverse the tree object) which should need more calls than direct solving of an equation. Instead I will attempt the generation of additional java classes.

Upvotes: 1

Views: 1163

Answers (2)

Leroy Kegan
Leroy Kegan

Reputation: 1216

Can you provide some information on assumed function type and requested performance? Maybe it will be enough just to use math parser library, which pre-compiles string containing math formula with variables just once, and then use this pre-compiled form of formula to deliver result even if variables values are changing? This kind of solutions are pretty fast as it typically do not require repeating string parsing, syntax checking and so on.

An example of such open-source math parser I recently used for my project is mXparser:

mXparser on GitHub

http://mathparser.org/

Usage example containing function definition

Function f = new Function("f(x,y) = sin(x) + cos(y)");
double v1 = f.calculate(1,2);
double v2 = f.calculate(3,4);
double v3 = f.calculate(5,6);

In the above code real string parsing will be done just once, before calculating v1. Further calculation v1, v2 (an possible vn) will be done in fast mode.

Additionally you can use function definition in string expression

Expression e = new Expression("f(1,2)+f(3,4)", f);
double v = e.calculate();

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533500

What I do is generate Java code at a runtime and compile it. There are a number of libraries to help you do this, one I wrote is https://github.com/OpenHFT/Java-Runtime-Compiler This way it can be as efficient as if you had hand written the Java code yourself and if called enough times will be compiled to native code.

Upvotes: 1

Related Questions