TransWikia.com

Writing a programming code directly from the mathematical formula?

Computational Science Asked by user25735 on April 1, 2021

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

2 Answers

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.

Answered by Chris Rackauckas on April 1, 2021

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

Answered by Mark Dewing on April 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP