Joel's posting led me to read the old thread (March '07, before I joined the sage lists) which was very interesting.
python wants hashes to be defined so that x==y implies hash(x)==hash(y), while Sage wants to be clever mathematically so that "x==y" is possible for a lot of complicated reasons. Including: (1) x and y are the same member of the same parent (the easy case); (2) The parents of x and y (say, X and Y) are such that there is a natural coercion from X to Y -- or vice versa -- and coercion(x)==y; (3) more generally again, there exists a structure Z into which both parents X and Y can be coerced and coerce(x)==coerce(y) (in Z); all these seem ok to me, provided that we restrict to injective coercions (e.g. ZZ in QQ), but not when the coercions are not injective (e.g. ZZ to ZZ(100)). That would be a complete nonsense since (for example, as someone already noted) the coercion ZZ to ZZ(1) which is the trivial rings maps all integers to 0. Personally I would be happy for x==y to be false when x=ZZ(0) and y=Integers(100)(0), since that leads to the relation == not being transitive! sage: x=ZZ(0) sage: y=Integers(100)(0) sage: z=ZZ(100) sage: x==y True sage: y==z True sage: x==z False !!! On 07/11/2007, Joel B. Mohler <[EMAIL PROTECTED]> wrote: > > Hi, > > I'd like some confirmation for the patch at > http://www.sagetrac.org/sage_trac/ticket/1075 > The purpose of the patch is to fix the lack of substitution in the following > code snippet (it also has other ramifications in similar contexts): > sage: R.<x,y>=ZZ[] > sage: (x/y).subs({x:1}) > x/y > > The patch is to add a __hash__ method to the FractionFieldElement class to > make fractions with denominator 1 hash the same as the numerator. Thus, this > patch makes == and __hash__ agree for the case of embedding a ring into it's > fraction field. > > For some reason, which is a mystery to me, that code snippet works as expected > if you replace ZZ with QQ. I realize that behind the scenes that gives you > an entirely different implementation (for the poly ring -- not the > fractionfield), but I don't understand why the hash values somehow work out > for the QQ case, but not the ZZ case. > > Note that '==' and __hash__ have been hashed (pardon the pun) at some length > in the thread with this message: > http://groups.google.com/group/sage-devel/msg/b3812493e9d2d2c8 > > thanks > Joel > > > > -- John Cremona --~--~---------~--~----~------------~-------~--~----~ 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/ -~----------~----~----~----~------~----~------~--~---