1 == Set([0]) works, 0 == Set([1]) does not

38 views
Skip to first unread message

Nathann Cohen

unread,
Apr 25, 2013, 4:43:29 AM4/25/13
to Sage devel
Hellooooooooo everybody !!!

I met a bug the other day but I'm a too scared to explore it myself
and I do not know what to do with it... I usually do not sleep soundly
before I find somebody who can deal with it, and in this case I do not
know who to ask :-)

It's a very simple thing.

sage: 1 == Set([0])
False
sage: 0 == Set([1])
...
AttributeError: 'Set_object_enumerated_with_category' object has no
attribute '_richcmp_'

And it only works in the command line -- not in a script -- unless you
wrap 0 and 1 with Integer( ... ).

Does anybody know how to fix that ??

Have fuuuuuuuuuuuuuuuuuuuun !

Nathann

Volker Braun

unread,
Apr 25, 2013, 5:05:06 AM4/25/13
to sage-...@googlegroups.com
The reason for the different treatment is in the coercion model, which is used to figure out how to compare them:

sage: cm = get_coercion_model()
sage: _a, _b = cm.canonical_coercion(Set([1]), 0)
sage: _a, _b = cm.canonical_coercion(Set([1]), 1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-a41b3a79237c> in <module>()
----> 1 _a, _b = cm.canonical_coercion(Set([Integer(1)]), Integer(1))

/home/vbraun/opt/sage-5.9.beta5/local/lib/python2.7/site-packages/sage/structure/coerce.so in sage.structure.coerce.CoercionModel_cache_maps.canonical_coercion (sage/structure/coerce.c:9707)()

/home/vbraun/opt/sage-5.9.beta5/local/lib/python2.7/site-packages/sage/structure/coerce.so in sage.structure.coerce.CoercionModel_cache_maps.canonical_coercion (sage/structure/coerce.c:9603)()

TypeError: no common canonical parent for objects with parents: '<class 'sage.sets.set.Set_object_enumerated_with_category'>' and 'Integer Ring'

Volker Braun

unread,
Apr 25, 2013, 5:18:45 AM4/25/13
to sage-...@googlegroups.com
And that it because the Set_object constructor performes no argument checking whatsoever. Sure, we are not supposed to call it by hand, but the coercion model will call it to see if 0 can be converted into it:

sage: from sage.sets.set import Set_object
sage: Set_object(0)
Set of elements of 0

Actually, I am very confused about why a Set is a Parent and not the Element of a universal set parent. We want sets to be quickly constructable, non ? The mathematical elements of sets are not the Elements of this Parent either. 

Nathann Cohen

unread,
Apr 25, 2013, 5:28:45 AM4/25/13
to Sage devel
> Actually, I am very confused about why a Set is a Parent and not the Element
> of a universal set parent. We want sets to be quickly constructable, non ?

AHAHAHAHAH. I never noticed... :-P

sage: %timeit set([1])
1000000 loops, best of 3: 660 ns per loop
sage: %timeit Set([1])
10000 loops, best of 3: 78.8 us per loop

100x slower... Yeah there's something wrong... :-P

Nathann

Volker Braun

unread,
Apr 25, 2013, 5:38:05 AM4/25/13
to sage-...@googlegroups.com

Nils Bruin

unread,
Apr 25, 2013, 11:57:02 AM4/25/13
to sage-devel
On Apr 25, 2:18 am, Volker Braun <vbraun.n...@gmail.com> wrote:
> Actually, I am very confused about why a Set is a Parent and not the
> Element of a universal set parent. We want sets to be quickly
> constructable, non ?

Do we really need Set to be extremely fast? We already have python
sets for that.

I would expect the main purpose of Set is to be a target for forgetful
functors and maps:

sage: V=Set(ZZ)
sage: CC.explain(ZZ,V)
Action discovered.
Left Integer Multiplication by Integer Ring on Set of elements of
Integer Ring
Result lives in Set of elements of Integer Ring
Set of elements of Integer Ring
sage: 3 in V
True
sage: parent(V.an_element()) #this shows some limitations
Integer Ring

In this role they have to be members of categories, i.e., "parents".

Nathann Cohen

unread,
Apr 25, 2013, 12:02:36 PM4/25/13
to Sage devel
Do we really need Set to be extremely fast? We already have python
sets for that.

Yep, but a Set object is immutable.. So you can have it as a key in a dictionary, or as a vertex in a graph :-)

The matoids guys too by the way : 


And you obtain Set objects through many useful functions !

for s in Subsets(15,3):
    ...

In this role they have to be members of categories, i.e., "parents".

HMmm... In that role, perhaps yes. But I personnally use Set objects as I would use tuples or lists, and I often create a *LOT* of them ^^;

Had no idea that there was such a difference in speed between set() and Set() though O_o

Nathann 

Stefan

unread,
Apr 25, 2013, 12:05:56 PM4/25/13
to sage-...@googlegroups.com
Sage sets have a few advantages: they print nicer, there are some tools from sage-combinat that use them (Subsets is the first that comes to mind). But currently, the speed tradeoff is so large that the sage-matroid people are not using them, going for set() and frozenset() instead. I believe there's a GSoC candidate who wants to look into this.

--Stefan.
Reply all
Reply to author
Forward
0 new messages