Excellent, very good. I just have one change, noted below. The output of "a == RDF(1)" is needed, because I don't know what is supposed to happen.
Nick On Mar 21, 9:00 am, "William Stein" <[EMAIL PROTECTED]> wrote: > FYI, I've added the following discussion of hashing to the programming > guide for the next version of SAGE: > > > \section{The {\tt \_\_hash\_\_} Special Method} > > Here's the definition of \code{__hash__} > > from the Python reference manual. > > > \begin{quote} > > Called for the key object for dictionary operations, and by the > > built-in function \code{hash()}. Should return a 32-bit integer > > usable as a hash value for dictionary operations. The only required > > property is that objects which compare equal have the same hash > > value; it is advised to somehow mix together (e.g., using exclusive > > or) the hash values for the components of the object that also play > > a part in comparison of objects. If a class does not define a > > \code{__cmp__()} method it should not define a \code{__hash__()} > > operation either; if it defines \code{__cmp__()} or \code{__eq__()} > > but not \code{__hash__()}, its instances will not be usable as > > dictionary keys. If a class defines mutable objects and implements a > > \code{__cmp__()} or \code{__eq__()} method, it should not implement > > \code{__hash__()}, since the dictionary implementation requires that > > a key's hash value is immutable (if the object's hash value changes, > > it will be in the wrong hash bucket). > > \end{quote} > > > Notice that ``The only required property is that objects which compare > > equal have the same hash value.'' This is an assumption made by the > > Python language, {\em which in \sage we simply cannot make} (!), and > > violating it has consequences. Fortunately, the consequences are pretty > > clearly defined and reasonably easy to understand, so if you know > > about them they don't cause you trouble. I think the following > > example illustrates them pretty well: > > \begin{verbatim} > > sage: v = [Mod(2,7)] > > sage: 9 in v > > True > > sage: v = set([Mod(2,7)]) > > sage: 9 in v > > False > > sage: 2 in v > > True > > sage: w = {Mod(2,7):'a'} > > sage: w[2] > > 'a' > > sage: w[9] > > Traceback (most recent call last): > > ... > > KeyError: 9 > > \end{verbatim} > > > Here's another example, > > \begin{verbatim} > > sage: R = RealField(10000) > > sage: a = R(1) + R(10)^-100 > > sage: a == RDF(1) True? Output needed here! > > \end{verbatim} > > but \code{hash(a)} should not equal \code{hash(1)}. > > > Unfortunately, in \SAGE we simply can't require: > > \begin{verbatim} > > (*) "a == b ==> hash(a) == hash(b)" > > \end{verbatim} > > because serious mathematics is simply too complicated for this > > rule. For example, the equalities > > \code{z == Mod(z, 2)} and \code{z == Mod(z, 3)} > > would force \code{hash()} to be constant on the > > integers. > > > The only way we could ``fix'' this problem for good would be to > > abandon using the "==" operator for "\SAGE equality", and implement > > \SAGE equality as a new method attached to each object. Then we could > > follow Python rules for "==" and our rules for everything else, and > > all \SAGE code would become completely unreadable (and for that matter > > unwriteable). So we just have to live with it. > > > So what is done in \sage is to {\em attempt} to satisfy (*) when it > > is reasonably easy to do so, but use judgment and not go overboard. > > For example, > > \begin{verbatim} > > sage: hash(Mod(2,7)) > > 2 > > \end{verbatim} > > I.e., we get 2. That's better than some weird random hash that also > > involves the moduli, but it's of course not right from the Python point > > of view, since \code{9 == Mod(2,7)}. > > > The goal is to make a hash function that is fast but within reason > > respects any obvious, natural inclusions and coercions. --~--~---------~--~----~------------~-------~--~----~ 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/ -~----------~----~----~----~------~----~------~--~---