> > But there is also a generically implemented method .reduce() for > fraction field elements (so, not necessarily needed to implement it in > subclasses). Of course, it is expensive and it may fail (if > the gcd is not defined). But *if* it works, then fraction f1 is equal to > fraction f2 if and only if numerator and denominator of f1.reduce() are > equal to those of f2.reduce(). Hence, this is the right thing to do for > the hash: Just insert self.reduce() at the beginning of self.__hash__(). >
Actually, this is not quite true. reduce() is, by default, called automatically for elements of exact rings at creation time. It will correctly get rid of common factors, but it does not normalize the leading coefficients: sage: H5.<a,b,c> = FractionField(PolynomialRing(ZZ, ['a', 'b', 'c'])) sage: p1 = 1/(1-c) sage: p2 = (-1)/(c-1) sage: p3 = (c+1)/(c^2 - 1); p3 1/(c - 1) sage: p1.reduce(); p1 1/(-c + 1) sage: p2.reduce(); p2 (-1)/(c - 1) > I'd rather have a slow hash than a totally broken hash. > Meh. I still want to do this billions of times. This is OK, because (x+1)^2 and x^2+2*x+1 do not compare equal, in terms > of cmp: > > sage: cmp((x+1)^2, x^2 + 2*x + 1) > 1 > Actually, this does not hold in my example (with the polynomial ring defined above): sage: cmp((c+1)^2, c^2 + 2*c+1) 0 Again, my main issue is that the elements are *from the same ring*. Cheers, Stefan. -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/groups/opt_out.