Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1d9f894

Browse files
Merge pull request#341 from Maegereg/dasm/hash
Add hash function and re-enable hash tests
2 parents83979e2 +621b0ad commit1d9f894

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

‎src/flint/test/test_arb.py‎

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,35 @@ def test_contains():
110110
]:
111111
assert (xiny)==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+
deftest_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+
defarb_pi(prec):
122+
"""Helper to calculate arb to a given precision."""
123+
withctx.workprec(prec):
124+
returnarb.pi()
125+
forx,y,expectedin [
126+
(arb(10),arb(10),True),
127+
(arb(10),arb(11),False),
128+
(arb(10.0),arb(10),True),
129+
]:
130+
assert (hash(x)==hash(y))==expected
131+
132+
forxin [
133+
arb(mid=10,rad=2),
134+
arb_pi(100),
135+
]:
136+
try:
137+
hash(x)
138+
exceptValueError:
139+
pass
140+
else:
141+
assertFalse,f"Expected{x} to raise an error if hashed, but succeeded."
144142

145143

146144

@@ -338,8 +336,7 @@ def test_no_tests_missing():
338336
test_lower,
339337
test_upper,
340338
test_contains,
341-
# TODO: Re-enable this if we ever add the ability to hash arbs.
342-
# test_hash,
339+
test_hash,
343340
test_arb_sub,
344341
test_arb_add,
345342
test_arb_mul,

‎src/flint/types/arb.pyx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,12 @@ cdef class arb(flint_scalar):
527527
arb_clear(tval)
528528
return res
529529

530+
def__hash__(self):
531+
"""Hash."""
532+
ifself.is_exact():
533+
returnhash((self.mid().man_exp(),self.rad().man_exp()))
534+
raiseValueError(f"Cannot hash non-exact arb: {self}. See pull/341 for details.")
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)

[8]ページ先頭

©2009-2025 Movatter.jp