Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve converting Latex to SymPy #4

Open
bhpayne opened this issue Mar 4, 2022 · 3 comments
Open

improve converting Latex to SymPy #4

bhpayne opened this issue Mar 4, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@bhpayne
Copy link
Member

bhpayne commented Mar 4, 2022

SymPy is capable of converting Latex to SymPy. It works for simple Latex, but complicated expressions do not result in the desired SymPy representation.

@bhpayne bhpayne added the enhancement New feature or request label Mar 4, 2022
@bhpayne
Copy link
Member Author

bhpayne commented Mar 4, 2022

SymPy converts a simple Latex expression to SymPy representation:

>>> import sympy
>>> sympy.__version__
'1.8'
>>> from sympy import *
>>> from sympy.parsing.latex import parse_latex
>>> sympy.srepr(parse_latex('T = 1/f'))
ANTLR runtime and generated code versions disagree: 4.9.2!=4.7.2
"Equality(Symbol('T'), Mul(Integer(1), Pow(Symbol('f'), Integer(-1))))"

Integration:

>>> sympy.srepr(parse_latex('f = \int_a^b x dx'))
ANTLR runtime and generated code versions disagree: 4.9.2!=4.7.2
"Equality(Symbol('f'), Integral(Symbol('x'), Tuple(Symbol('x'), Symbol('a'), Symbol('b'))))"

@bhpayne
Copy link
Member Author

bhpayne commented Mar 4, 2022

SymPy fails on this Latex

>>> import sympy
>>> sympy.__version__
'1.8'
>>> from sympy import *
>>> from sympy.parsing.latex import parse_latex
>>> sympy.srepr(parse_latex('\vec{\psi}(r, t)'))
antlr4.error.Errors.LexerNoViableAltException: LexerNoViableAltException(
sympy.parsing.latex.errors.LaTeXParsingError: I don't understand this

The desired SymPy representation would be something like

Function('psi')(Symbol('r'), Symbol('t')

One reason this example is difficult because the dimensionality of \vec{\psi} is undefined. If we impose the assumption of 3 dimensions, SymPy does have

>>> from sympy.vector import CoordSys3D, Del, curl, divergence, gradient

so it is feasible to define

>>> N = CoordSys3D('N')
>>> psi_vector = Symbol('a')*N.i+Symbol('b')*N.j + Symbol('c')*N.k

This seems to necessitate a multi-line SymPy operation to support a single (ill-defined) Latex expression.

@msgoff
Copy link
Contributor

msgoff commented May 21, 2023

The grammar has more than a dozen logical errors.

It would be ideal to reuse the non-recursive terminals, and to rewrite the recursive rules, with the primary goal of improving correctness while not breaking rules that people depend on.

ideas on how to proceed would be welcome.

I think the following would be helpful.
Having one or more people to communicate with that would care about the output.
Generating useful test cases that are required for updates to the grammar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants