On Sat, Mar 19, 2011 at 8:10 AM, Nicolas M. Thiery
<nicolas.thi...@u-psud.fr> wrote:
> On Thu, Mar 17, 2011 at 03:29:08PM +0100, Jeroen Demeyer wrote:
>> On 2011-03-17 15:04, Jason Grout wrote:
>> > def hash(self):
>> >     h=0
>> >     for i,j,entry in m.nonzero_entries(): # nonzero entries for sparse
>> > matrices
>> >        h^=hash(entry)^i^j
>> >     return h
>>
>> Since you're only xorring, this will give a lot of collisions.  I think
>> something like
>>
>> def hash(self):
>>     n = m.ncols()
>>     h = (m.nrows() + m.ncols())**2 + n
>>     for i,j,entry in m.nonzero_entries():
>>         h ^= hash(entry)*(i*n + j + 1)
>>     return h
>>
>> has less chance of collisions.  Of course everything should be computed
>> as a C long.
>
> Btw: any reason not to use Python's builtin hash function for tuples?
> I assume it as been designed with precisely this kind of issues in
> mind?  Or do we want that a matrix gets the same hash in sparse and a
> dense representation?

Equal sparse and dense matrices should have the same hash. Also,
converting to a tuple and then hashing would be *way* slower for many
matrices as one would have to construct a Python object for each
entry.

Not to say we can't do much better.

- Robert

-- 
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