Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac 20829: make quaternion hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Jun 14, 2016
1 parent 0c1f89f commit 2221ab8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sage/algebras/quatalg/quaternion_algebra_element.pxd
Expand Up @@ -14,7 +14,7 @@ cdef class QuaternionAlgebraElement_abstract(AlgebraElement):
cpdef reduced_trace(self)

cdef class QuaternionAlgebraElement_generic(QuaternionAlgebraElement_abstract):
cdef object x, y, z, w, d, a, b
cdef object x, y, z, w
# we will assume that our element has the representation
# x + yi + zj + wk, where i^2 = a, j^2 = b

Expand Down
35 changes: 35 additions & 0 deletions src/sage/algebras/quatalg/quaternion_algebra_element.pyx
Expand Up @@ -5,6 +5,14 @@ Sage allows for computation with elements of quaternion algebras over
a nearly arbitrary base field of characteristic not 2. Sage also has
very highly optimized implementation of arithmetic in rational
quaternion algebras and quaternion algebras over number fields.
TESTS:
Check that :trac:`20829` is fixed::
sage: D.<i,j,k>=QuaternionAlgebra(QQ,-1,-3)
sage: hash(i)
184301497
"""

#*****************************************************************************
Expand Down Expand Up @@ -185,6 +193,33 @@ cdef inline print_coeff(y, i, bint atomic):
return '%s*%s'%(y, i)

cdef class QuaternionAlgebraElement_abstract(AlgebraElement):
def __hash__(self):
r"""
TESTS::
sage: from itertools import product
sage: for K in [QQ, QuadraticField(2), AA, Frac(QQ['x'])]:
....: Q.<i,j,k> = QuaternionAlgebra(K,-5,-2)
....: assert hash(Q.one()) == hash(K.one())
....: assert hash(Q(2)) == hash(K(2))
....: elts = []
....: for (x,y,z,w) in product([K(0), K(1), K(2), K(-1)], repeat=4):
....: elts.append(x + y*i + z*j + w*k)
....: assert len(set(map(hash, elts))) == len(elts)
"""
cdef long h
h = hash(self[0])
x = self[1]
if x:
h = ((h+14152L)*13023L) ^ hash(x)
x = self[2]
if x:
h = ((h+33325L)*31321L) ^ hash(x)
x = self[3]
if x:
h = ((h+34125L)*51125L) ^ hash(x)
return h

cpdef bint is_constant(self):
"""
Return True if this quaternion is constant, i.e., has no i, j, or k term.
Expand Down

0 comments on commit 2221ab8

Please sign in to comment.