ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 25 Dec 2023 07:22:46 +0100Test for Set equalityhttps://ask.sagemath.org/question/75091/test-for-set-equality/[ 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](https://ask.sagemath.org/question/67158/how-to-get-selective-outputs-printed/), I stumbled in an unexpected difficulty when trying to compare two `Set`s :
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](https://doc.sagemath.org/html/en/reference/sets/sage/sets/set.html) doesn't mention testing forv `Set`s equality.
Is that a bug, an oversight or an expected behaviour ?Sun, 24 Dec 2023 14:26:26 +0100https://ask.sagemath.org/question/75091/test-for-set-equality/Answer by Max Alekseyev for <p>[ 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... ]</p>
<p>Inspired by <a href="https://ask.sagemath.org/question/67158/how-to-get-selective-outputs-printed/">this question</a>, I stumbled in an unexpected difficulty when trying to compare two <code>Set</code>s :</p>
<pre><code>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
</code></pre>
<p>Quite unexpected... However :</p>
<pre><code>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
</code></pre>
<p>as expected.</p>
<p>The <code>Sets</code> <a href="https://doc.sagemath.org/html/en/reference/sets/sage/sets/set.html">reference</a> doesn't mention testing forv <code>Set</code>s equality.</p>
<p>Is that a bug, an oversight or an expected behaviour ?</p>
https://ask.sagemath.org/question/75091/test-for-set-equality/?answer=75097#post-id-75097`Set` is [known to be buggy](https://github.com/sagemath/sage/issues/23324) 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`.Sun, 24 Dec 2023 19:59:59 +0100https://ask.sagemath.org/question/75091/test-for-set-equality/?answer=75097#post-id-75097Comment by Emmanuel Charpentier for <p><code>Set</code> is <a href="https://github.com/sagemath/sage/issues/23324">known to be buggy</a> 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 <code>Set(map(lambda m: Matrix(m,immutable=True),L[0][1:]))</code> instead of <code>Set(L[0][1:])</code>. Alternatively, you can upfront declare all matrices immutable, or call <code>.set_immutable()</code> method on the elements of <code>L</code>.</p>
https://ask.sagemath.org/question/75091/test-for-set-equality/?comment=75114#post-id-75114Thank you very much !Mon, 25 Dec 2023 07:22:46 +0100https://ask.sagemath.org/question/75091/test-for-set-equality/?comment=75114#post-id-75114