A. Barrozo
A. Barrozo

Reputation: 95

SymPy does not evaluate Latex2SymPy conversion for sin(-x) or sin(pi)

I have been using Latex2SymPy for a while successfully to handle all sorts of LaTeX inputs, but I have been running into troubles with a few functions. For instance:

from latex2sympy.process_latex import process_sympy
from sympy import *

inputLatex = '\\sin{-x}\\sin{-x}'
trigsimp(process_sympy(inputLatex))

sin(x)**2

That works great: trigsimp handled the simplification well. Now, if I try:

inputLatex = '\\sin{-x}'
trigsimp(process_sympy(inputLatex))

sin(-x)

Even though this is obviously correct, I expected trigsimp() to give me -sin(x) as an answer. In fact, if I run trigsimp straight from a sympy expression:

trigsimp(sin(-x))

-sin(x)

This is what I expect. Even running sin(-x) without the trigsimp() command returns me -sin(x). I checked the object type of my process_sympy('\\sin{-x}') call, and it is 'sin'.

I thought this might be something related with the way x is transformed into a Symbol type, but I then tried putting pi in the sin function.

inputLatex = '\\sin{\\pi}'
trigsimp(process_sympy(inputLatex))

sin(pi)

If I run straight sin(pi), I get 0 as an answer, with or without trigsimp().

Can any of you shed a light on this?

Upvotes: 1

Views: 1333

Answers (2)

sclizard
sclizard

Reputation: 56

Although it is probably too late, you can solve that issue by calling the doit() method in the sympy expression obtained from process_sympy(inputLatex).

When calling to process_sympy, it generates an unevaluated sympy expression, not a string. This expression cannot be evaluated just by calling sympify, since it has an attribute telling not to evaluate it.

You solved the problem by converting that expression to a string and then sympifying it, but a more direct way of doing it is just by using the doit() method in the expression:

inputLatex = '\\sin{-x}'
expr = process_sympy(inputLatex)
trigsimp(expr.doit())

would do the job.

Upvotes: 2

A. Barrozo
A. Barrozo

Reputation: 95

I haven't solved the whole mystery yet, but I found a dirty workaround, which is to use sympify(). So if anyone else faces the same issue, here is what I did to get it to work:

inputLatex = '\\sin{-x}'
sympify(str(process_sympy(inputLatex)))

The answer now is -sin(x), what I wanted.

As for the sin(pi) thing, the issue is that process_sympy() cannot distinguish the pi as a symbol from pi as a number. I did the test here, and type(process_sympy('\pi')) returns Symbol type, and not a number. The same solution applies here.

Upvotes: 0

Related Questions