Today I will present how flexible mXparser really is. As an example we will approximate and using indirect recursion steps, which means two functions depending on each other. Let us start with a bit of theory starting with basic trigonometric identities:
Above formals can be equivalently written as
Please notice that knowing the solution for smaller value it is possible to get solution for the original one . This simply means that mentioned trigonometric identities are in fact example of recursion - here indirect recursion as function is using , and is using . The complete recursion definition requires base case definition (stop condition).
For near to function can be well approximated exactly by , while in case of constant is pretty good approximation. This gives good stop condition.
For small let us define two recursive functions:
Once again please notice that (and separately) is using both and .
We expect that smaller parameter is giving better approximations - below you will find functions graphs separately for and .
import org.mariuszgromada.math.mxparser.*; ... /* Recursive functions definition */ Constant a = new Constant("a", 0.1); Function s = new Function("s(x) = if( abs(x) < a, x, 2*s(x/2)*c(x/2) )", a); Function c = new Function("c(x) = if( abs(x) < a, 1, c(x/2)^2-s(x/2)^2 )", a); /* Pointing that 's' is using 'c', and 'c' is using 's' */ s.addDefinitions(c); c.addDefinitions(s);