The precedence in the printers is just about where it needs to add parentheses. I suppose negation is the same as Add because it doesn't require parentheses for anything with a higher precedence. For instance -x*y is fine rather than -(x*y), but -(x + y) requires the parentheses.
Aaron Meurer On Thu, Mar 25, 2021 at 1:18 AM Paul Royik <[email protected]> wrote: > > from sympy.printing.precedence, PRECEDENCE > precedence(-x) == PRECEDENCE['Add'] > It gives True. > > On Wednesday, March 24, 2021 at 10:36:52 PM UTC+2 [email protected] wrote: >> >> Can you clarify what you mean by this? >> >> The precedence of operators in Python is determined by the language. >> See https://docs.python.org/3/reference/expressions.html#operator-precedence. >> Unary - has a higher precedence than binary + (or binary -). >> >> In SymPy, -x is represented as (-1)*x, but this is done after Python >> parses the expression and converts them into SymPy objects. >> >> In Python, x - y is different from x + -y. In the former, - is a >> binary operator and the latter it is a unary operator. They both use >> the same symbol because it's unambiguous which is which. SymPy >> represents these both as x + (-1)*y because this makes things simpler, >> but this is only because it converts them both to that canonical form. >> >> Aaron Meurer >> >> On Wed, Mar 24, 2021 at 2:13 PM Paul Royik <[email protected]> wrote: >> > >> > Is there any reason why precedence of -x equals precedence of Add? >> > -x is (-1)*x >> > >> > -- >> > 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 [email protected]. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/sympy/604cbda3-7b2f-4bb7-af4c-2f3b532edaa5n%40googlegroups.com. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/dc0c166a-3c9f-42f8-bc9a-b4acc504d4d3n%40googlegroups.com. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BVFUZ9vtpdStpyNOkPYNqCU%3D3cqtxCfr7psPkLpD4zhw%40mail.gmail.com.
