# Writing a programming code directly from the mathematical formula?

For using any programming language, a mathematical formula should be written in the corresponding code. I wonder if there is any service (for any programming language, Matlab, Mathematica, Python, etc) to directly translating a mathematical formula (e.g., written in TeX) into the programming code.

Input: a mathematical formula in TeX Output: the corresponding code to be executed in Matlab, Mathematica, Python, etc

• For PDEs, FEniCS is the closest I know to what you want. They have a domain specific language in python that resembles the weak form of a PDE. Oct 20 '17 at 20:50
• I think that this question is off-topic. That said, you can directly type TeX in WolframAlpha and it will evaluate it. SymPy also has a parser. And I have seen a couple more pass by. Oct 21 '17 at 0:15
• @nicoguaro do you have any idea how to install the parser? askubuntu.com/questions/967155/how-to-install-latex2sympy Oct 21 '17 at 11:48
• Try to see Cfl if it is what you are looking for. Note I know the person who made it. Oct 21 '17 at 13:35
• For Mathematica see: mathematica.stackexchange.com/questions/132861/… Oct 24 '17 at 23:11

Julia's macros and string macro are made for doing this kind of thing. You can define a domain-specific language (DSL) within a macro scope and use that as a way to define problems. One example of this is the DifferentialEquations.jl @ode_def macro. You may have to use a string macro (due to parsability into ASTs) but this sort of tool is what allows you to auto-translate syntax. Some interesting examples of string macros are the MATLAB.jl mat"" strings which allows you to write MATLAB code and have it execute directly in MATLAB:

mat"""
$$u =$$x + $$y$$v = $$x -$$y
"""


Similarly the Rcall.jl r"" string which runs R code

R"rnorm(10)"


Those call to other languages, whereas a string macro like LispSyntax.jl just translates Clojure syntax to Julia:

lisp"(fib 30)"


So writing one that converts from LaTeX to standard Julia is definitely possible, but it looks no one has hit that exact niche yet.

One problem with converting Tex syntax to equations is that it can be ambiguous - the main goal with TeX is typesetting the look, not the actual meaning. If you have many formulas already in TeX, then looking to parse them into a symbolic form is okay. But if starting from scratch, a symbolic mathematics package would be a better choice.

Another issue is that converting symbolic equations to code is very domain-specific. In order for mathematical notation to be concise, there are many assumptions and implicit context surrounding the written formulas. Any system for interpreting formulas needs to know about these.

Most symbolic mathematics packages have support for code generation. For some examples in Sympy, see the Sympy code generation tutorial