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

4

12

I am writing a code which had long mathematical equations with many trigonometric and other identities. Is there a way of visualising the same expression in latex and making a C or python expression from it or the other way around.

How do you enter and check mathematical expressions to see if the brackets etc are in the right position and use them in latex documents?

Thanks in advance

Mcnully answered 5/9, 2011 at 11:12 Comment(3)
probably not relevant but the (experimental, no longer funded) language fortress, which was intended to replace fortran, supports this - en.wikipedia.org/wiki/Fortress_(programming_language)Goer
What are you asking for really? A way to automatically turn Latex expressions into C/Fortran/Python expressions, or ...?Schmo
You can split expressions in extra local variables to make it readable; the compiler will optimize those assignments away. For python, there will be theoretically some overhead, but likely not noticeable.Prudy
B
16

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            2
x  + 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.)

Bassinet answered 5/9, 2011 at 13:4 Comment(1)
sympy also has C and Fortran code and LaTeX export functions, which just about makes it perfect for what the asker seems to be looking for.Midstream
B
3

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.


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

import sympy

def 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)
Basilio answered 5/9, 2011 at 11:49 Comment(0)
B
2

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

Bloodred answered 10/9, 2011 at 19:19 Comment(0)
E
0

If you have Mathematica, it can import LaTeX code to a Mathematica expression, which can then be exported to C or MATLAB code, possibly FORTRAN as well.

It is fairly simple to convert MATLAB to Python syntax, I've done it in the past by search-and-replace, but a simple script could probably do it even quicker.

Evannia answered 5/9, 2011 at 12:35 Comment(1)
Can you explain how you import Latex code to a Mathematica expression? I have never seen this.Becoming

© 2022 - 2024 — McMap. All rights reserved.