Wednesday, June 24, 2020

parsing latex using Sympy - what works and what does not


# python
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
>>> from sympy.parsing.latex import parse_latex
>>> parse_latex('a = b')
Eq(a, b)
>>> parse_latex('\\frac{\\partial}{\\partial t} \\psi( r,t)')
Derivative(\psi(r, t), t)
>>> parse_latex('\\int_{a}^{b} f(x) dx')
Integral(f(x), (x, a, b))

To can check that the Sympy expression gets converted to Latex correctly:
>>> from sympy import *
>>> sympy.latex(parse_latex('\\int_{a}^{b} f(x) dx'))


vectors do not get parsed correctly
>>> parse_latex('\\vec{a} = \\vec{g} t')
see https://docs.sympy.org/latest/modules/vector/index.html
Similarly,
>>> parse_latex('\\hat{x} + \\hat{y}')
The "hat" is typically used for unit vectors. These are referenced in
https://docs.sympy.org/latest/modules/physics/vector/vectors.html
but I don't see a Sympy-specific notation for unit vectors.

bra-ket notation does not raise an exception but is not interpreted correctly
>>> parse_latex('\\langle \\alpha | = 1')
see https://docs.sympy.org/latest/modules/physics/quantum/state.html

The following triggers an exception
>>> parse_latex('\\mathcal{H} = 5')
For a discussion of that symbol in Latex, see https://tex.stackexchange.com/questions/429749/two-different-mathcalh
https://docs.sympy.org/latest/modules/physics/quantum/hilbert.html

Complex conjugation notation is not interpreted correctly:
>>> parse_latex('Z Z^* = 5')

No comments:

Post a Comment