diff --git a/src/sage/rings/polynomial/laurent_polynomial.pyx b/src/sage/rings/polynomial/laurent_polynomial.pyx index 00b7acb4fde..4f2958202c8 100644 --- a/src/sage/rings/polynomial/laurent_polynomial.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial.pyx @@ -560,9 +560,6 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial): sage: hash(R.zero()) == hash(t - t) True """ - if self.__n == 0: - return hash(self.__u) - # we reimplement below the hash of polynomials to handle negative # degrees cdef long result = 0 @@ -573,10 +570,11 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial): result_mon = hash(self.__u[i]) if result_mon: j = i + self.__n - result_mon = (1000003 * result_mon) ^ var_hash_name if j > 0: + result_mon = (1000003 * result_mon) ^ var_hash_name result_mon = (1000003 * result_mon) ^ j elif j < 0: + result_mon = (1000003 * result_mon) ^ var_hash_name result_mon = (700005 * result_mon) ^ j result += result_mon return result @@ -1905,7 +1903,7 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): r""" TESTS: - Test that the hash is non-constant:: + Test that the hash is non-constant (see also :trac:`27914`):: sage: L. = LaurentPolynomialRing(QQ) sage: len({hash(w^i*z^j) for i in [-2..2] for j in [-2..2]}) @@ -1941,6 +1939,18 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): True sage: hash(1 - 7*x0 + x1*x2) == hash(L(1 - 7*x0 + x1*x2)) True + + Check that :trac:`27914` is fixed:: + + sage: L. = LaurentPolynomialRing(QQ) + sage: Lw = LaurentPolynomialRing(QQ, 'w') + sage: Lz = LaurentPolynomialRing(QQ, 'z') + sage: all(hash(w^k) == hash(Lw(w^k)) + ....: and hash(z^k) == hash(Lz(z^k)) for k in (-5..5)) + True + sage: p = w^-1 + 2 + w + sage: hash(p) == hash(Lw(p)) + True """ # we reimplement the hash from multipolynomial to handle negative exponents # (see multi_polynomial.pyx) @@ -1955,10 +1965,12 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): if c_hash != 0: for p in range(n): exponent = m[p] + self._mon[p] - if not exponent: - continue - c_hash = (1000003 * c_hash) ^ var_name_hash[p] - c_hash = (1000003 * c_hash) ^ exponent + if exponent > 0: + c_hash = (1000003 * c_hash) ^ var_name_hash[p] + c_hash = (1000003 * c_hash) ^ exponent + elif exponent < 0: + c_hash = (1000003 * c_hash) ^ var_name_hash[p] + c_hash = (700005 * c_hash) ^ exponent result += c_hash return result