overloading * for Set intersection

35 views
Skip to first unread message

G. M.-S.

unread,
Mar 29, 2022, 5:34:14 AM3/29/22
to sage-...@googlegroups.com

I have noticed the overloading of + for Set union, which I find very convenient.

Correspondingly, I would like to propose the overloading of * for Set intersection.

It seems to me that it suffices to modify
local/lib/python3.9/site-packages/sage/sets/set.py
by adding in
class Set_add_sub_operators:
the lines
    def _mul_(self, X):
        """
        Return the intersection of ``self`` and ``X``.
        """
        return self.intersection(X)

I have tested this and it works as expected.

Should I open a ticket?  I shall have to learn how to deal with commits.

I am aware of the overloading of * in real_set.py, but I think they are compatible.

Thanks,

Guillermo

Sébastien Labbé

unread,
Mar 29, 2022, 6:19:46 AM3/29/22
to sage-devel
Why not use "&" and "|" ?

sage: A = Set([1,2,3,4,5])
sage: B = Set([3,4,5,6,7])
sage: A & B
{3, 4, 5}
sage: A | B
{1, 2, 3, 4, 5, 6, 7}

Matthias Koeppe

unread,
Mar 29, 2022, 2:25:03 PM3/29/22
to sage-devel
-1 on this; as Sébastien says, we already have overloaded the bitwise operators &, | for this -- which is consistent with Python's operators for set and frozenset - https://docs.python.org/3/library/stdtypes.html#set
(Note Python's set and frozenset do not support +.)

G. M.-S.

unread,
Mar 29, 2022, 2:34:52 PM3/29/22
to sage-...@googlegroups.com

Sorry, I did not find anything about this in the docs for Sets (but I should have tried the operators for Python sets).

Guillermo
Reply all
Reply to author
Forward
0 new messages