One way to do this is to compile a Java class for each different expression, with a single static method that implements the function. See this question.
So, for example, if all your functions just use one variable x
, then you could generate a .java file looking like this...
class Expression {
static double apply(double x) {
return /* expression here */;
}
}
Then compile and load that class as in the cited question, get the apply()
method with reflection, and call it whenever you want to compute that expression. (Alternately, have your compiled classes implement an interface, create instances of them, and avoid the reflection)
Admittedly, this is not particularly friendly to your memory usage if you end up with thousands of such classes...