Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing affine group elements #38508

Merged
merged 4 commits into from
Dec 8, 2024
Merged
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
62 changes: 51 additions & 11 deletions src/sage/groups/affine_gps/group_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@
- Volker Braun
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2013 Volker Braun <vbraun.name@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from copy import copy

from sage.structure.element import Matrix
from sage.misc.cachefunc import cached_method
Expand Down Expand Up @@ -119,15 +121,17 @@
A = A.matrix()
except AttributeError:
pass
if isinstance(A, Matrix) and A.nrows() == A.ncols() == parent.degree()+1:
if isinstance(A, Matrix) and A.nrows() == A.ncols() == parent.degree() + 1:
g = A
d = parent.degree()
A = g.submatrix(0, 0, d, d)
b = [ g[i,d] for i in range(d) ]
b = [g[i,d] for i in range(d)]
convert = True
if convert:
A = parent.matrix_space()(A)
b = parent.vector_space()(b)
A.set_immutable()
b.set_immutable()
if check:
# Note: the coercion framework expects that we raise TypeError for invalid input
if not isinstance(A, Matrix):
Expand All @@ -138,6 +142,12 @@
raise TypeError('b must be an element of ' + str(parent.vector_space()))
parent._element_constructor_check(A, b)
super().__init__(parent)
if not A.is_immutable():
A = copy(A)
A.set_immutable()
if not b.is_immutable():
b = copy(b)
b.set_immutable()
self._A = A
self._b = b

Expand All @@ -151,10 +161,12 @@

sage: G = AffineGroup(3, QQ)
sage: g = G([1,2,3,4,5,6,7,8,0], [10,11,12])
sage: g.A()
sage: A = g.A(); A
[1 2 3]
[4 5 6]
[7 8 0]
sage: A.is_immutable()
True
"""
return self._A

Expand All @@ -168,8 +180,10 @@

sage: G = AffineGroup(3, QQ)
sage: g = G([1,2,3,4,5,6,7,8,0], [10,11,12])
sage: g.b()
sage: b = g.b(); b
(10, 11, 12)
sage: b.is_immutable()
True
"""
return self._b

Expand Down Expand Up @@ -345,7 +359,9 @@
parent = self.parent()
A = self._A * other._A
b = self._b + self._A * other._b
return parent.element_class(parent, A, b, check=False)
A.set_immutable()
b.set_immutable()
return parent.element_class(parent, A, b, convert=False, check=False)

def __call__(self, v):
"""
Expand Down Expand Up @@ -439,13 +455,14 @@
x |-> [0 1] x + [0]
sage: v = vector(GF(3), [1,-1]); v
(1, 2)
sage: g*v
sage: g * v
(1, 2)
sage: g*v == g.A() * v + g.b()
sage: g * v == g.A() * v + g.b()
True
"""
if self_on_left:
return self(x)
return None

Check warning on line 465 in src/sage/groups/affine_gps/group_element.py

View check run for this annotation

Codecov / codecov/patch

src/sage/groups/affine_gps/group_element.py#L465

Added line #L465 was not covered by tests

def __invert__(self):
"""
Expand All @@ -472,7 +489,9 @@
parent = self.parent()
A = parent.matrix_space()(~self._A)
b = -A * self.b()
return parent.element_class(parent, A, b, check=False)
A.set_immutable()
b.set_immutable()
return parent.element_class(parent, A, b, convert=False, check=False)

def _richcmp_(self, other, op):
"""
Expand All @@ -497,6 +516,27 @@

return richcmp(self._b, other._b, op)

def __hash__(self):
"""
Return the hash of ``self``.

OUTPUT: int

EXAMPLES::

sage: F = AffineGroup(3, QQ)
sage: g = F([1,2,3,4,5,6,7,8,0], [10,11,12])
sage: h = F([1,2,3,4,5,6,7,8,0], [10,11,0])
sage: hash(g) == hash(h)
False
sage: hash(g) == hash(copy(g))
True
sage: f = g * h
sage: hash(f) == hash(~f)
False
"""
return hash((self._A, self._b))

def list(self):
"""
Return list representation of ``self``.
Expand Down
Loading