On Wed, Apr 20, 2011 at 1:45 PM, Florent Hivert
<florent.hiv...@univ-rouen.fr> wrote:
>
>>  - Much worst: the Python assumption that the hash value of an object
>>    does not change can easily be broken inadvertently, even by a total
>>    beginner, by using the rename feature:
>>
>>       sage: K = QQ['x']
>>       sage: hash(K)
>>       -764788796815899192
>>       sage: K.rename("K")
>>       sage: hash(K)
>>       9600028874
>
> This one is fixed in #8119 thanks to Robert Bradshaw. I'm ok with the patch
> but I added a review patch so I can't put positive review myself. Also, #9181
> is a very simple patch (only doc) about hash and awaiting for review for long.

The hash is still incorrect (regardless of the fix in #8119), e.g.

sage: p1 = PolynomialRing(ZZ,'x')
sage: p2 = PolynomialRing(ZZ,'x',sparse=True)
sage: p1 == p2
True
sage: hash(p1), hash(p2)
(-1525918542791298668, -684417098584176435)


Given the definition of cmp for polynomial rings is:

        c = cmp(type(left),type(right))
        if c: return c
        return cmp((left.base_ring(), left.variable_name()),
(right.base_ring(), right.variable_name()))

then the definition of hash could just be:

        return hash((type(self), self.base_ring(),self.variable_name()))

This is resilient to renames and it makes it very clear that

        cmp(left, right) == 0   ===>   hash(left) == hash(right)


Best,
Gonzalo

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to