Skip to content

Fix equality, hash and is_reference of PhysicalToReference - #88

Closed
jorgensd wants to merge 1 commit into
mainfrom
dokken/equality-of-mapping-operators
Closed

jorgensd wants to merge 1 commit into
mainfrom
dokken/equality-of-mapping-operators

Conversation

@jorgensd

@jorgensd jorgensd commented Oct 8, 2026

Copy link
Copy Markdown
Member

PhysicalToReference.__eq__ checks isinstance(other, ReferenceToPhysical), and __hash__ uses the key "uflx.ReferenceToPhysical": both were copied from ReferenceToPhysical. As a result:

  • a PhysicalToReference is not equal to itself, or to a copy rebuilt from the same arguments, which is how replace and the pull-back rebuild nodes, so a rebuilt copy is not found in a dict keyed by the original;
  • it is equal to the ReferenceToPhysical with the same arguments, and has the same hash, so a set or graph merges the two opposite maps into one node.

It also does not override is_reference, so a point mapped to the reference cell reports False.

"""PhysicalToReference compares, hashes and reports itself as a ReferenceToPhysical."""

from basix_uflx import element
from uflx import coordinate_element
from uflx.expressions import RealScalar
from uflx.geometry import PhysicalToReference, ReferenceToPhysical
from uflx.points import Point

domain = coordinate_element(element("Lagrange", "triangle", 1, shape=(2,)))
x = Point([RealScalar(0.1), RealScalar(0.2)], is_reference=True)

G = PhysicalToReference(x, domain)
F = ReferenceToPhysical(x, domain)
print("G == G:", G == G)
print("G == PhysicalToReference(x, domain):", G == PhysicalToReference(x, domain))
print("G == ReferenceToPhysical(x, domain):", G == F)
print("len({G, ReferenceToPhysical(x, domain)}):", len({G, F}))
print("G.is_reference:", G.is_reference)

returns on main

G == G: False
G == PhysicalToReference(x, domain): False
G == ReferenceToPhysical(x, domain): True
len({G, ReferenceToPhysical(x, domain)}): 1
G.is_reference: False

and with this PR

G == G: True
G == PhysicalToReference(x, domain): True
G == ReferenceToPhysical(x, domain): False
len({G, ReferenceToPhysical(x, domain)}): 2
G.is_reference: True

Changes

  • PhysicalToReference.__eq__ and __hash__ refer to PhysicalToReference.
  • PhysicalToReference.is_reference is True.
  • New test/test_point_map_equality.py. Both tests fail on main.

@jorgensd jorgensd added the bug Something isn't working label Oct 8, 2026
@jhale

jhale commented Oct 11, 2026

Copy link
Copy Markdown
Member

Good catch. This is fixed in #100 which drops is_reference.

@jhale jhale closed this Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants