On Sun, 11 Feb 2007 06:07:17 -0800, David Harvey <[EMAIL PROTECTED]> wrote: > Hmmmm.... apart from what's "mathematically correct", there is > another problem I just noticed. > > According to > > http://docs.python.org/ref/customization.html > > it says "The only required property is that objects which compare > equal have the same hash value". We've already broken this rule > numerous times in SAGE. For example: > > sage: R.<x> = PowerSeriesRing(ZZ) > > sage: f = x + O(x^2) > > sage: g = x + 2*x^2 + O(x^3) > > sage: f == g > True > > sage: hash(f), hash(g) > (813759615, 1049482705) > > Ditto for the current implementation of RealInterval, pAdicField, etc. > > I'm not sure exactly why the python people impose this rule. I guess > if you are looking up a key in a hashtable, you hash the key, look in > the appropriate bucket, loop over all items in the bucket. Things are > going to be pretty weird if the keys are something like a > RealInterval, but I still don't quite see why that rule is necessary.
I've actually done a lot of work in various cases to make sure that rule is satisfied in various cases, though certainly not always. They make it for the reason you suggest, and perhaps the following example will clarify things: sage: R.<x> = PowerSeriesRing(ZZ) sage: f = x + O(x^2) sage: g = x + 2*x^2 + O(x^3) sage: f == g True sage: w = {f:1} sage: w[g] <type 'exceptions.KeyError'>: x + 2*x^2 + O(x^3) But you might expect w[g] to give 1, since "f == g" is true. That said, in practice I haven't had a problem with this hashing issue, at least for power series. Also, the Python builtin set type behaviors strangely in some cases because of the relation between == and hash. This occurs mostly in cases where no other system I know of would even allow the creation of efficient sets (i.e., where you mix very different types of objects in the set). -- William --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com 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://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---