Prevent expression evaluation in repr()/latex()

17 views
Skip to first unread message

ABOU SAMRA Jean

unread,
Jun 2, 2019, 12:35:48 PM6/2/19
to sympy
Hello everyone,

I am trying to use SymPy for a mathematical projet, where I need expressions to be evaluated step-by-step.
Until now, I have always used, for example, sympify("2+2", evaluate=False) and all was fine.

But now I am trying to reconstruct an expression from another object that I have created and I sometimes get in trouble with the way the `minus` is handled. Let me give an example:

Python 3.8.0a2 (v3.8.0a2:23f4589b4b, Feb 25 2019, 10:59:08)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> from sympy.abc import *
>>>
>>> e1 = Mul(Integer(2), Integer(5), evaluate=False)
>>> e1 # all fine here
2*5
>>> print(srepr(e1)) # what I expected
Mul(Integer(2), Integer(5))
>>>
>>> e2 = Mul(Integer(-2), Integer(5), evaluate=False) # now with -2 instead of 2
>>> e2 # Hoops!
-10
>>> 
>>> # yet e2 has not been evaluated to Integer(-10) but its repr() above is wrong
>>> print(srepr(e2))
Mul(Integer(-1), Integer(2), Integer(5))
>>> # I'm trying to use expressions in LaTeX documents and there our e2 is still wrong.
... latex(e2)
'- 10'

I'm quite surprised that Mul(Integer(-1), Integer(2), Integer(5))  becomes "-10" when passed through repr(). This is even more surprising:

>>> init_printing()
>>> e2
-2⋅5

How can I prevent the expression to be represented by repr()/latex() in a single integer but keep the -2*5 form?

Many thanks for your help.
Regards,
Jean Abou Samra.

Oscar Benjamin

unread,
Jun 2, 2019, 3:41:05 PM6/2/19
to sympy
Hi Jean,

What version of SymPy are you using?

This seems to work fine on SymPy master and with the most recent version (1.4):

In [1]: repr(Mul(-1, 2, 5, evaluate=False))
Out[1]: '-2*5'

--
Oscar

Jean ABOU SAMRA

unread,
Jun 3, 2019, 11:54:04 AM6/3/19
to sy...@googlegroups.com, oscar.j....@gmail.com
Hi,
Le 2 juin 2019 à 21:40, Oscar Benjamin <oscar.j....@gmail.com> a écrit :

Hi Jean,

Thanks for your quick answer.

What version of SymPy are you using?
This seems to work fine on SymPy master and with the most recent version (1.4):

I'm also using 1.4.


In [1]: repr(Mul(-1, 2, 5, evaluate=False))
Out[1]: '-2*5'

True, but…

>>> repr(Mul(-2, 5, evaluate=False))
'-10'

In fact, all the problem lies here:

>>> # same...
... srepr(Mul(-1, 2, 5, evaluate=False))
'Mul(Integer(-1), Integer(2), Integer(5))'
>>> srepr(Mul(-2, 5, evaluate=False))
'Mul(Integer(-1), Integer(2), Integer(5))'
>>> 
>>> # but different
... repr(Mul(-1, 2, 5, evaluate=False))
'-2*5'
>>> repr(Mul(-2, 5, evaluate=False)) # Hoops!
'-10'
>>> 
>>> # even weirder
... init_printing()
>>> Mul(-1, 2, 5, evaluate=False)
-2⋅5
>>> Mul(-2, 5, evaluate=False) # here all is fine!
-2⋅5


Kind regards,
Jean Abou Samra

Aaron Meurer

unread,
Jun 3, 2019, 1:40:07 PM6/3/19
to sympy, Oscar Benjamin
There have been bugs in the past where printers evaluate
evaluate=False expressions. It looks like there is another such bug
here. Can you open an issue for this?

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/45D94875-5286-4A9C-B9B2-7812DF70A0B7%40abou-samra.fr.
> For more options, visit https://groups.google.com/d/optout.

Jean ABOU SAMRA

unread,
Jun 3, 2019, 2:19:56 PM6/3/19
to asme...@gmail.com, Oscar Benjamin, sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages