On Wed, Aug 27, 2008 at 12:21 PM, Simon King
<[EMAIL PROTECTED]> wrote:
>
> On Aug 27, 9:05 pm, "Craig Citro" <[EMAIL PROTECTED]> wrote:
>> I also vote for fast -- but couldn't there be a flag for the slow
>> option? Maybe "consistent=True" or something, in case someone really
>> wants it?
>
> AFAIK, the key requirement for a hash function is that equivalent (in
> the sense of "==") objects must have the same hash.
Just for the record, we break that all over the place. However, we do
try to keep it if possible over a given base ring to some extent, maybe.
sage: 3 == Mod(1,2)
True
sage: hash(3)
3
sage: hash(Mod(1,2))
1
Here is a similar example with matrices:
sage: a = matrix(GF(2),2,[1..4]); b = matrix(ZZ,2,[1..4])
sage: a.set_immutable (); b.set_immutable()
sage: a == b
True
sage: hash(a), hash(b)
(2, 8)
sage:
> A natural situation in which one wants consistent cache is like this:
>
> sage: M=MatrixSpace(GF(3),10,10).random_element()
> sage: N=M.change_ring(ZZ)
> sage: M.set_immutable()
> sage: N.set_immutable()
> sage: D={M:1}
> sage: D[N]
>
> This should work, because
> sage: M==N
> True
>
> But it doesn't (KeyError), if hash(M) is different from hash(N).
True.
>
> And how should one pass the argument "consistent=True" in that
> situation?
What was malb's original definition of inconsistent? Did he *really*
have pairs of GF(2) matrices that are equal, but have different hashes?
> Hence the "key" question is: Should it be possible to get a dictionary
> item by using an equivalent (yet different) key?
> Certainly the answer is "yes".
Certainly the answer is that this is completely impossible, even with
the current "consistent" code. To continue my example from above?
sage: v = {a:1}
sage: v[b]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/wstein/iras/build/sage-3.0.6.final/<ipython console> in <module>()
KeyError: [1 2]
[3 4]
Moral -- dictionaries are fast and useful, but you *have* know that they
are not just linearly going through all keys and doing "==" to find
a given item.
>
> Hence, either in the above situation one should have M!=N (which
> wouldn't be nice) or they should have the same hash.
I disagree with this conclusion -- neither option is reasonable.
William
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---