Methods for entering equations while programming in C/C++ , Python or Fortran
I am writing a code which had long mathematical equations with many trigonometric and othe开发者_运维技巧r 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
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.)
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,
- How to convert Math Formula to Python code?
- Converting a python numeric expression to LaTeX
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)
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.
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.
精彩评论