Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions src/flint/test/test_arb.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,36 @@ def test_contains():
]:
assert (x in y) == expected

# TODO: Re-enable this if we ever add the ability to hash arbs.
# def test_hash():
# """`x` and `y` hash to the same value if they have the same midpoint and radius.

# Args:
# x: An arb.
# y: An arb.
# expected: Whether `x` and `y` should hash to the same value.
# """
# def arb_pi(prec):
# """Helper to calculate arb to a given precision."""
# with ctx.workprec(prec):
# return arb.pi()
# for x, y, expected in [
# (arb(10), arb(10), True),
# (arb(10), arb(11), False),
# (arb(10.0), arb(10), True),
# (
# arb(mid=10, rad=2),
# arb(mid=10, rad=2),
# True,
# ),
# (
# arb(mid=10, rad=2),
# arb(mid=10, rad=3),
# False,
# ),
# (arb_pi(100), arb_pi(100), True),
# (arb_pi(100), arb_pi(1000), False),
# ]:
# assert (hash(x) == hash(y)) == expected
def test_hash():
"""`x` and `y` hash to the same value if they have the same midpoint and radius.

Args:
x: An arb.
y: An arb.
expected: Whether `x` and `y` should hash to the same value.
"""
def arb_pi(prec):
"""Helper to calculate arb to a given precision."""
with ctx.workprec(prec):
return arb.pi()
for x, y, expected in [
(arb(10), arb(10), True),
(arb(10), arb(11), False),
(arb(10.0), arb(10), True),
(
arb(mid=10, rad=2),
arb(mid=10, rad=2),
True,
),
(
arb(mid=10, rad=2),
arb(mid=10, rad=3),
False,
),
(arb_pi(100), arb_pi(100), True),
(arb_pi(100), arb_pi(1000), False),
]:
assert (hash(x) == hash(y)) == expected



Expand Down Expand Up @@ -338,8 +337,7 @@ def test_no_tests_missing():
test_lower,
test_upper,
test_contains,
# TODO: Re-enable this if we ever add the ability to hash arbs.
# test_hash,
test_hash,
test_arb_sub,
test_arb_add,
test_arb_mul,
Expand Down
4 changes: 4 additions & 0 deletions src/flint/types/arb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ cdef class arb(flint_scalar):
arb_clear(tval)
return res

def __hash__(self):
"""Hash."""
return hash((self.mid().man_exp(), self.rad().man_exp()))

def __contains__(self, other):
other = any_as_arb(other)
return arb_contains(self.val, (<arb>other).val)
Expand Down
Loading