Skip to content

Commit c125ecf

Browse files
committed
Add hash function and re-enable hash tests.
1 parent 4d1dbab commit c125ecf

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

src/flint/test/test_arb.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,36 @@ def test_contains():
110110
]:
111111
assert (x in y) == expected
112112

113-
# TODO: Re-enable this if we ever add the ability to hash arbs.
114-
# def test_hash():
115-
# """`x` and `y` hash to the same value if they have the same midpoint and radius.
116-
117-
# Args:
118-
# x: An arb.
119-
# y: An arb.
120-
# expected: Whether `x` and `y` should hash to the same value.
121-
# """
122-
# def arb_pi(prec):
123-
# """Helper to calculate arb to a given precision."""
124-
# with ctx.workprec(prec):
125-
# return arb.pi()
126-
# for x, y, expected in [
127-
# (arb(10), arb(10), True),
128-
# (arb(10), arb(11), False),
129-
# (arb(10.0), arb(10), True),
130-
# (
131-
# arb(mid=10, rad=2),
132-
# arb(mid=10, rad=2),
133-
# True,
134-
# ),
135-
# (
136-
# arb(mid=10, rad=2),
137-
# arb(mid=10, rad=3),
138-
# False,
139-
# ),
140-
# (arb_pi(100), arb_pi(100), True),
141-
# (arb_pi(100), arb_pi(1000), False),
142-
# ]:
143-
# assert (hash(x) == hash(y)) == expected
113+
def test_hash():
114+
"""`x` and `y` hash to the same value if they have the same midpoint and radius.
115+
116+
Args:
117+
x: An arb.
118+
y: An arb.
119+
expected: Whether `x` and `y` should hash to the same value.
120+
"""
121+
def arb_pi(prec):
122+
"""Helper to calculate arb to a given precision."""
123+
with ctx.workprec(prec):
124+
return arb.pi()
125+
for x, y, expected in [
126+
(arb(10), arb(10), True),
127+
(arb(10), arb(11), False),
128+
(arb(10.0), arb(10), True),
129+
(
130+
arb(mid=10, rad=2),
131+
arb(mid=10, rad=2),
132+
True,
133+
),
134+
(
135+
arb(mid=10, rad=2),
136+
arb(mid=10, rad=3),
137+
False,
138+
),
139+
(arb_pi(100), arb_pi(100), True),
140+
(arb_pi(100), arb_pi(1000), False),
141+
]:
142+
assert (hash(x) == hash(y)) == expected
144143

145144

146145

@@ -338,8 +337,7 @@ def test_no_tests_missing():
338337
test_lower,
339338
test_upper,
340339
test_contains,
341-
# TODO: Re-enable this if we ever add the ability to hash arbs.
342-
# test_hash,
340+
test_hash,
343341
test_arb_sub,
344342
test_arb_add,
345343
test_arb_mul,

src/flint/types/arb.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from cpython.float cimport PyFloat_AsDouble
22
from cpython.long cimport PyLong_Check
33

4+
import random
5+
46
from flint.flint_base.flint_context cimport getprec
57
from flint.flint_base.flint_context cimport thectx
68
from flint.flint_base.flint_base cimport flint_scalar
@@ -527,6 +529,10 @@ cdef class arb(flint_scalar):
527529
arb_clear(tval)
528530
return res
529531

532+
def __hash__(self):
533+
"""Hash."""
534+
return hash((self.mid().man_exp(), self.rad().man_exp()))
535+
530536
def __contains__(self, other):
531537
other = any_as_arb(other)
532538
return arb_contains(self.val, (<arb>other).val)

0 commit comments

Comments
 (0)