We are working on a big mathematical project with a lot of long equations and derivatives, which are produced by Wolfram Mathematica. We have more than 1000 very long equations.
Master program is written in Java and Mathematica is only used for generating equations. Our goal is to transform "Mathematica" form to "Java" form of equation. Then we can copy/paste generated code directly to "Java" code.
So for example we have short equation in Mathematica form:
Sqrt[((Cos[R]*X1 - X2)^2 + (Sin[R]*Y1 - Y2)^2)/S^2]/S
And we want to have it in Java form, so this is expected result:
Math.sqrt((Math.pow(Math.cos(R) * X1 - X2, 2) + Math.pow(Math.sin(R) * Y1 - Y2, 2)) / Math.pow(S, 2)) / S
Here is short python script, which manages some functions:
E = "Sqrt[((Cos[R]*X1 - X2)^2 + (Sin[R]*Y1 - Y2)^2)/S^2]/S"
E = E.replace("[", "(") # Replaces Square brackets with normal brackets
E = E.replace("]", ")") # Replaces Square brackets with normal brackets
E = E.replace("*", " * ") # Add some spaces for easier reading
E = E.replace("/", " / ") # Add some spaces for easier reading
E = E.replace("Cos", "Math.cos") # Change "Mathematica" cos to "Java" cos
E = E.replace("Sin", "Math.sin") # Change "Mathematica" sin to "Java" sin
E = E.replace("Sqrt", "Math.sqrt") # Change "Mathematica" SQRT to "Java" SQRT
# Converting Power function is missing here... This is a must :)
print(E)
Above code produces:
Math.sqrt(((Math.cos(R) * X1 - X2)^2 + (Math.sin(R) * Y1 - Y2)^2) / S^2) / S
The problem is that we didn't find any solution for power function. We wanted to use python regex, but we cannot find any proper solution. The problem is that power function has to take everything within brackets, so for example:
(Math.cos(R) * X1 - X2)^2 >>>> Math.pow(Math.cos(R) * X1 - X2, 2)
I hope that somebody has a quick and fancy solution. Otherwise I will need to take some time and write a long and "dirty" script, which will take care of this problem.
Thanks for your help :)