Skip to content

Commit

Permalink
make gap objects selectively hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Oct 27, 2024
1 parent 7726cd9 commit bd5053d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
15 changes: 7 additions & 8 deletions src/sage/combinat/designs/gen_quadrangles_with_spread.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,19 @@ def generalised_quadrangle_hermitian_with_ovoid(const int q):

e1 = [one, zero, zero, zero] # isotropic point

points = list(libgap.Orbit(GU, e1, libgap.OnLines)) # all isotropic points
pointInt = { x: int(i + 1) for i, x in enumerate(points) }
# above we sum 1 because GAP starts at 1
points = libgap.Orbit(GU, e1, libgap.OnLines) # all isotropic points
pointInt = {tuple(x): i for i, x in enumerate(points)}

GUp = libgap.Action(GU, points, libgap.OnLines)

e2 = [zero, one, zero, zero] # another isotropic point
line = V.Subspace([e1, e2]) # totally isotropic line
lineAsPoints = [libgap.Elements(libgap.Basis(b))[0]
lineAsPoints = [tuple(libgap.Elements(libgap.Basis(b))[0])
for b in libgap.Elements(line.Subspaces(1))]
line = libgap.Set([pointInt[p] for p in lineAsPoints])
line = libgap.Set([pointInt[p] + 1 for p in lineAsPoints])

lines = libgap.Orbit(GUp, line, libgap.OnSets) # all isotropic lines
lines = [list(map(lambda x: int(x - 1), b)) for b in lines] # convert to int
lines = [[int(x)-1 for x in b] for b in lines] # convert to int
# lines defines the GQ H(3, q^2)

# to find an ovoid, we embed H(3,q^2) in H(4,q^2)
Expand All @@ -330,9 +329,9 @@ def generalised_quadrangle_hermitian_with_ovoid(const int q):
# note that p'Jp^q = bx^q + c where p' = (a,b,0,c,d) and p = (0,1,y,x,0)
ovoid = []
xq = p[3]**q
for p2 in points:
for p2, i in pointInt.items():
if p2[1]*xq + p2[2] == zero: # p collinear to p2
ovoid.append(pointInt[p2] - 1)
ovoid.append(i)

D = IncidenceStructure(lines)
return (D, ovoid)
19 changes: 11 additions & 8 deletions src/sage/libs/gap/element.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,31 @@ cdef class GapElement(RingElement):

cpdef GapElement deepcopy(self, bint mut)

cdef class GapElement_Integer(GapElement):
cdef class GapElement_with_hash(GapElement):
pass

cdef class GapElement_Rational(GapElement):
cdef class GapElement_Integer(GapElement_with_hash):
pass

cdef class GapElement_IntegerMod(GapElement):
cdef class GapElement_Rational(GapElement_with_hash):
pass

cdef class GapElement_IntegerMod(GapElement_with_hash):
cpdef GapElement_Integer lift(self)

cdef class GapElement_FiniteField(GapElement):
cdef class GapElement_FiniteField(GapElement_with_hash):
cpdef GapElement_Integer lift(self)

cdef class GapElement_Cyclotomic(GapElement):
cdef class GapElement_Cyclotomic(GapElement_with_hash):
pass

cdef class GapElement_Ring(GapElement):
pass

cdef class GapElement_String(GapElement):
cdef class GapElement_String(GapElement_with_hash):
pass

cdef class GapElement_Boolean(GapElement):
cdef class GapElement_Boolean(GapElement_with_hash):
pass

cdef class GapElement_Function(GapElement):
Expand All @@ -96,5 +99,5 @@ cdef class GapElement_RecordIterator():
cdef class GapElement_List(GapElement):
pass

cdef class GapElement_Permutation(GapElement):
cdef class GapElement_Permutation(GapElement_with_hash):
pass
44 changes: 25 additions & 19 deletions src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -832,17 +832,6 @@ cdef class GapElement(RingElement):
if not self._compare_by_id:
raise ValueError('this requires a GAP object whose comparison is by "id"')

def __hash__(self):
"""
Make hashable.
EXAMPLES::
sage: hash(libgap(123)) # random output
163512108404620371
"""
return hash(str(self))

cpdef _richcmp_(self, other, int op):
"""
Compare ``self`` with ``other``.
Expand Down Expand Up @@ -1400,6 +1389,23 @@ cdef class GapElement(RingElement):
raise NotImplementedError('cannot construct equivalent Sage object')


cdef class GapElement_with_hash(GapElement):
"""
A GapElement class that provides a hash via the str
representation of the object.
"""
def __hash__(self):
"""
Make hashable.
EXAMPLES::
sage: hash(libgap(123)) # random output
163512108404620371
"""
return hash(str(self))


############################################################################
### GapElement_Integer #####################################################
############################################################################
Expand All @@ -1420,7 +1426,7 @@ cdef GapElement_Integer make_GapElement_Integer(parent, Obj obj):
return r


cdef class GapElement_Integer(GapElement):
cdef class GapElement_Integer(GapElement_with_hash):
r"""
Derived class of GapElement for GAP integers.
Expand Down Expand Up @@ -1656,7 +1662,7 @@ cdef GapElement_IntegerMod make_GapElement_IntegerMod(parent, Obj obj):
r._initialize(parent, obj)
return r

cdef class GapElement_IntegerMod(GapElement):
cdef class GapElement_IntegerMod(GapElement_with_hash):
r"""
Derived class of GapElement for GAP integers modulo an integer.
Expand Down Expand Up @@ -1747,7 +1753,7 @@ cdef GapElement_FiniteField make_GapElement_FiniteField(parent, Obj obj):
return r


cdef class GapElement_FiniteField(GapElement):
cdef class GapElement_FiniteField(GapElement_with_hash):
r"""
Derived class of GapElement for GAP finite field elements.
Expand Down Expand Up @@ -1916,7 +1922,7 @@ cdef GapElement_Cyclotomic make_GapElement_Cyclotomic(parent, Obj obj):
return r


cdef class GapElement_Cyclotomic(GapElement):
cdef class GapElement_Cyclotomic(GapElement_with_hash):
r"""
Derived class of GapElement for GAP universal cyclotomics.
Expand Down Expand Up @@ -2001,7 +2007,7 @@ cdef GapElement_Rational make_GapElement_Rational(parent, Obj obj):
return r


cdef class GapElement_Rational(GapElement):
cdef class GapElement_Rational(GapElement_with_hash):
r"""
Derived class of GapElement for GAP rational numbers.
Expand Down Expand Up @@ -2231,7 +2237,7 @@ cdef GapElement_Boolean make_GapElement_Boolean(parent, Obj obj):
return r


cdef class GapElement_Boolean(GapElement):
cdef class GapElement_Boolean(GapElement_with_hash):
r"""
Derived class of GapElement for GAP boolean values.
Expand Down Expand Up @@ -2321,7 +2327,7 @@ cdef GapElement_String make_GapElement_String(parent, Obj obj):
return r


cdef class GapElement_String(GapElement):
cdef class GapElement_String(GapElement_with_hash):
r"""
Derived class of GapElement for GAP strings.
Expand Down Expand Up @@ -2987,7 +2993,7 @@ cdef GapElement_Permutation make_GapElement_Permutation(parent, Obj obj):
return r


cdef class GapElement_Permutation(GapElement):
cdef class GapElement_Permutation(GapElement_with_hash):
r"""
Derived class of GapElement for GAP permutations.
Expand Down

0 comments on commit bd5053d

Please sign in to comment.