missing '^' logical operator : bug (oversight) or design ?

47 views
Skip to first unread message

Emmanuel Charpentier

unread,
May 16, 2021, 2:09:35 AM5/16/21
to sage-devel

Dear list,

consider python :

>>> a = set(['a', 'b', 'c'])
>>> b = set(['b', 'c', 'd'])
>>> a & b
{'b', 'c'}
>>> a | b
{'b', 'a', 'd', 'c'}
>>> a - b
{'a'}
>>> a ^ b
{'a', 'd'}

vs Sage :

sage:  a = set(['a', 'b', 'c'])
sage:  b = set(['b', 'c', 'd'])
sage:  a & b
{'b', 'c'}
sage:  a | b
{'a', 'b', 'c', 'd'}
sage:  a - b
{'a'}
sage:  a ^ b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-7c22c32c86c9> in <module>
----> 1 a ** b

TypeError: unsupported operand type(s) for ** or pow(): 'set' and 'set'
sage:  a = Set(['a', 'b', 'c'])
sage:  b = Set(['b', 'c', 'd'])
sage:  a & b
{'b', 'c'}
sage:  a | b
{'d', 'b', 'c', 'a'}
sage:  a - b
{'a'}
sage:  a ^ b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-7c22c32c86c9> in <module>
----> 1 a ** b

TypeError: unsupported operand type(s) for ** or pow(): 'Set_object_enumerated_with_category' and 'Set_object_enumerated_with_category'

Is the non-overloading of the ^ operator intentional (and, if so, for which reason ?) or an oversight (a. k. a. bug) ?

Sincerely,

Nils Bruin

unread,
May 16, 2021, 3:51:39 AM5/16/21
to sage-devel
If the preparser is active in sage (and it is by default if you run sage in its normal, interactive way or if you use *.sage files) then the substitution "^" -> "**" is made. Hence, the error message that you see: python is complaining about "**" as an unsupported operand type, not "^". The decision to let "^" mean exponentiation rather than "bitwise exclusive or" is very intentional in sage: the notation x^2 for "x squared" is so ingrained that not recognizing it would render sage unacceptable for its intended mathematical target group.

I don't think it's desirable to let {1,2,3} ** {1,4,5} stand for "symmetric difference" of sets.

If you want to access the python "^" operator with the preparser active, you have to spell it as "^^". This is supported for both python sets and for the sage Set :

sage: Set({1,2}) ^^ Set({1,4})
{2, 4}

Emmanuel Charpentier

unread,
May 16, 2021, 8:44:12 AM5/16/21
to sage-devel
Thank you very much ! I didn't know that.
I note that this information is a bit difficult to reach in the documentation... and tha, in my Sage installation (from git tree, on Debian testing), "search_doc" is broken.

TB

unread,
May 18, 2021, 12:58:30 PM5/18/21
to sage-...@googlegroups.com
A bit late: Few years ago I wanted to see how to easily call integer
bitwise xor in Sage. This led to https://trac.sagemath.org/ticket/14621
which added the ^^ to the documentation.

Do you think it is time for a new ticket about documentation improvement
of https://doc.sagemath.org/html/en/reference/sets/sage/sets/set.html
detailing for the different methods (union, intersection, etc.) how to
use the shorter syntax with overloaded operators?

Regards,
TB
> --
> You received this message because you are subscribed to the Google
> Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sage-devel+...@googlegroups.com
> <mailto:sage-devel+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-devel/78d6ba4d-0e3e-454d-9486-fbdd62f45694n%40googlegroups.com
> <https://groups.google.com/d/msgid/sage-devel/78d6ba4d-0e3e-454d-9486-fbdd62f45694n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Reply all
Reply to author
Forward
0 new messages