Ask Your Question
0

Test for Set equality

asked 2023-12-24 14:26:26 +0100

Emmanuel Charpentier gravatar image

[ Note : as of 2023-12-24, my browser retrieves this question that seems t have never been asked, (an still of interest). So I pot it very lately... ]

Inspired by this question, I stumbled in an unexpected difficulty when trying to compare two Sets :

sage: L[:2]
[[
[ 2 -2]  [0 1]  [   0    1]
[-2  1], [0 1], [   0 -1/2]
],
 [
[ 2 -2]  [   0    1]  [0 1]
[-2  1], [   0 -1/2], [0 1]
]]
sage: Set(L[0][1:])==Set(L[1][1:])
False

Quite unexpected... However :

sage: all([u in Set(L[1][1:]) for u in Set(L[0][1:])])
True
sage: all([u in Set(L[0][1:]) for u in Set(L[1][1:])])
True

as expected.

The Sets reference doesn't mention testing forv Sets equality.

Is that a bug, an oversight or an expected behaviour ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-12-24 19:59:59 +0100

Max Alekseyev gravatar image

updated 2023-12-24 21:45:58 +0100

Set is known to be buggy for mutable (non-hashable) objects. If you plan to form a set of matrices, you need to declare those matrices as immutable. A simple workaround would be using Set(map(lambda m: Matrix(m,immutable=True),L[0][1:])) instead of Set(L[0][1:]). Alternatively, you can upfront declare all matrices immutable, or call .set_immutable() method on the elements of L.

edit flag offensive delete link more

Comments

Thank you very much !

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-12-25 07:22:46 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-12-24 14:26:26 +0100

Seen: 211 times

Last updated: Dec 24 '23