Methods for entering equations while programming in C/C++ , Python or Fortran Methods for entering equations while programming in C/C++ , Python or Fortran numpy numpy

Methods for entering equations while programming in C/C++ , Python or Fortran


Have you looked at Sympy? It has a module for generating LaTeX from python code, but it's actually quite a bit more.

Sympy, as you can probably guess from the name, is a python library for symbolic computation.

The Sympy library also includes it's own built-in interpreter (cd to the sympy directory in site-packages, and type ipython at a shell prompt).

With the sympy interpeter you can do things like this:

In [1]: (1/cos(x)).series(x, 0, 10)Out[1]:      2      4       6        8               x    5⋅x    61⋅x    277⋅x            1 + ── + ──── + ───── + ────── + O(x**10)    2     24     720     8064            In [2]: ((x+y)**2).expand()Out[2]:  2            2x  + 2⋅x⋅y + y In [3]: (1/cos(x)).series(x, 0, 10)Out[3]:      2      4       6        8               x    5⋅x    61⋅x    277⋅x            1 + ── + ──── + ───── + ────── + O(x**10)    2     24     720     8064            # not quite LaTeX--but Sympy can easily generate LaTeX from python code: >>> from sympy import Integral, latex>>> from sympy.abc import x>>> latex(x**2)    'x^{2}'>>> latex(x**2, mode='inline')    '$x^{2}$'>>> latex(x**2, mode='equation')    '\\begin{equation}x^{2}\\end{equation}'

I also wanted to generally recommend the Sympy Library--under active development for about four years now and it's improved substantially each year; it's an excellent, mature library for symbolic computation with excellent docs, and an active and helpful community. (Aside from submitting a couple of patches, I am not a Sympy dev/committer, just a user.)


Edit: It seems that for certain equations it is definitely possible to automate the process, see below. Original answer left intact!


Based on many painful hours fighting LaTeX equation settings and my own failures to notice missing elements in huge equation blocks: while is almost certainly possible to convert LaTeX to python or vice versa, it will probably be more painful than just doing it by hand, and you'll likely need to spend time tidying the results anyway.

That said, similar questions have been asked and answered,

Maybe you can get started there.


editI took a look through previous questions, and tested a combination of comments (1 2 3). All credit to the authors of those comments!

import sympydef python_to_latex(expression, simplify=False):    sym_expr = sympy.sympify(expression)     if simplify: sym_expr = sympy.simplify(sym_expr)    return sympy.latex(sym_expr)if __name__ == '__main__':    print python_to_latex(raw_input("Enter a python math expression: "), simplify=True)


The best tool I know of for this is the sage project. It supports symbolic computation, and can pretty-print any equation to the terminal as ASCII or the terminal as LaTeX code or straight to PDF using LaTeX. It supersedes some of the other suggestions as it also offers interfaces to MATLAB, Mathmatica, Maple, etc.

enter image description here