Skip to content

Define hash for PointSpace and DiracSpace - #698

Open
jishnub wants to merge 1 commit into
JuliaApproximation:masterfrom
jishnub:pointspace-hash
Open

Define hash for PointSpace and DiracSpace#698
jishnub wants to merge 1 commit into
JuliaApproximation:masterfrom
jishnub:pointspace-hash

Conversation

@jishnub

@jishnub jishnub commented Aug 31, 2026

Copy link
Copy Markdown
Member

Space defines == generically in src/Space.jl:165:

==(A::Space,B::Space) = spacescompatible(A,B) && domain(A) == domain(B)

but there is no matching hash, so spaces fall back to the identity-based default. Two equal-valued PointSpaces hash differently, which silently breaks Set and Dict:

julia> PointSpace(1:2) == PointSpace(1:2)
true

julia> hash(PointSpace(1:2)) == hash(PointSpace(1:2))
false

julia> Set([PointSpace(1:2), PointSpace(3:4)]) == Set([PointSpace(3:4), PointSpace(1:2)])
false

For PointSpace and DiracSpace, spacescompatible reduces to comparing the sorted points, and domain is derived from that same field, so hashing the points reproduces == exactly. The type is mixed into the hash so PointSpace(1:2) and DiracSpace(1:2) don't collide — they are not ==, since spacescompatible is only defined within each type.

Note == compares points numerically, so PointSpace([1,2]) == PointSpace([1.0,2.0]); the hash agrees, since Julia hashes numerically-equal numbers identically. Covered in the tests.

Scope

This fixes only the two types where the failure is demonstrated. Every other Space subtype still hashes by identity== is generic but hash is not. A generic hash(::Space, ::UInt) can't be derived safely, because spacescompatible is an arbitrary user-overridable predicate and nothing constrains which fields it consults. Fixing the rest means a hash alongside each spacescompatible, which seems worth a separate issue rather than bundling here.

Full test suite passes locally (219 passed, 1 pre-existing broken, 0 failures).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DZeBMyvW7qYiDfC7Tkb1vT

Space defines == (Space.jl:165) as spacescompatible && equal domains, but
no matching hash, so these spaces fell back to the identity-based default.
Two equal-valued spaces therefore hashed differently and could not be used
as Set elements or Dict keys:

    julia> PointSpace(1:2) == PointSpace(1:2)
    true
    julia> hash(PointSpace(1:2)) == hash(PointSpace(1:2))
    false

For both types spacescompatible reduces to comparing the sorted points, and
domain is derived from the same field, so hashing the points reproduces ==
exactly. The type is mixed in so PointSpace and DiracSpace with equal points
do not collide, matching == which is only defined within each type.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DZeBMyvW7qYiDfC7Tkb1vT
@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.10%. Comparing base (023b1a4) to head (42a1b10).
⚠️ Report is 47 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #698      +/-   ##
==========================================
- Coverage   75.20%   74.10%   -1.11%     
==========================================
  Files          79       81       +2     
  Lines        8445     8603     +158     
==========================================
+ Hits         6351     6375      +24     
- Misses       2094     2228     +134     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant