Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sympy.init_printing change set notation?

When I call sympy.init_printing(), set notation changes from {a, b, c} to set([a, b, c]). Why does this happen?

In [1]: import sympy

In [2]: (x, y, z) = sympy.symbols("x y z")

In [3]: x+y**z
Out[3]: x + y**z

In [4]: (x+y**z).free_symbols
Out[4]: {z, y, x}

In [5]: sympy.init_printing()

In [6]: x+y**z
Out[6]: 
     z
x + y 

In [7]: (x+y**z).free_symbols
Out[7]: set([x, y, z])

In [8]: {1, 2, 3}
Out[8]: set([1, 2, 3])

(It also changes the order of the items as shown)

like image 214
gerrit Avatar asked Mar 30 '26 00:03

gerrit


1 Answers

This is a Python 2 vs. 3 issue. In Python 2, sets are printed like set([...]), because {...} set literals were not added until Python 2.7. The SymPy printer was made before Python 3.

After SymPy version 1.0, SymPy no longer supports Python 2.6, so this has been fixed in SymPy master to always print using {...} (even in Python 2) at https://github.com/sympy/sympy/pull/11116 for the string printer, but apparently I missed that the pretty printer does this as well. I've put a fix in at https://github.com/sympy/sympy/pull/12087.

So, in short, after these changes (i.e., in the development version of SymPy and in all future versions), SymPy printing will print sets using {...} notation.

like image 130
asmeurer Avatar answered Apr 02 '26 03:04

asmeurer