How much is "many times"? Million?
What features can be introduced? Can we assume that they are continuous?
Have you tried to measure how well your code works?
(Sorry, it started with questions!)
You can try one of two approaches (or both) described below (perhaps many more):
1) Separate the trees.
You can create a parse tree. Then do what most compilers do to optimize expressions, constantly bend, eliminate common subexpression (which you could achieve by linking common clipping expressions and caching the result), etc.
Then you can use lazy valuation methods to avoid whole subtrees. For example, if you have a tree
* / \ AB
where A is rated at 0, you can completely avoid rating B, as you know, the result is 0. With RPN, you lose on a lazy rating.
2) Interpolation
Assuming your function is continuous, you can approach your function with a high degree of accuracy using Polynomial interpolation . Thus, you can perform a complex calculation of the function several times (depending on the degree of polynomial you have chosen), and then perform fast polynomial calculations for the remaining time.
To create an initial dataset, you can simply use approach 1 or just stick to your RPN, since you will only generate a few values.
So, if you use Interpolation, you can save your RPN ...
Hope this helps!
Aryabhatta
source share