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

AffineGroup elements should be hashable #38381

Closed
1 task done
wphooper opened this issue Jul 17, 2024 · 1 comment · Fixed by #38508
Closed
1 task done

AffineGroup elements should be hashable #38381

wphooper opened this issue Jul 17, 2024 · 1 comment · Fixed by #38508

Comments

@wphooper
Copy link

Problem Description

Currently AffineGroup elements do not implement hash and so can not be used as dictionary keys.

Currently the internal variables of AffineGroupElement are mutable, but the matrix method is cached suggesting that the values are expected to be immutable. This makes it possible to produce (far fetched) bugs where matrix returns something incompatible with the current state of the transformation:

sage: from sage.groups.affine_gps.affine_group import AffineGroup
sage: G = AffineGroup(2, QQ)
sage: G
Affine Group of degree 2 over Rational Field
sage: g = G([[1, 2], [3, 4]], [5,6])
sage: g.matrix()
[1 2|5]
[3 4|6]
[---+-]
[0 0|1]
sage: g.A()[0,0] = 9
sage: g.A()[0,0] = 9
sage: g
      [9 2]     [5]
x |-> [3 4] x + [6]
sage: g.matrix()
[1 2|5]
[3 4|6]
[---+-]
[0 0|1]
sage: hash(g)
TypeError

Proposed Solution

  1. Make affine group elements immutable by calling set_immutable() on the parameters _A and _b in the constructor.
  2. Implement a hash.

Alternatives Considered

If mutability is desired (for some reason I'm not aware of), then the matrix method should not be cached to avoid the bug above. In this case, I would imagine we'd also want to implement something explaining how to change the values.

Additional Information

No response

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
@tscrim
Copy link
Collaborator

tscrim commented Aug 7, 2024

+1 on this. The element is meant to be immutable, so the A and b should be immutable. Consequently it should be hashable.

vbraun pushed a commit to vbraun/sage that referenced this issue Dec 6, 2024
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Fixes sagemath#38381 and makes sure the defining parts of the group element are
immutable.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#38508
Reported by: Travis Scrimshaw
Reviewer(s): Frédéric Chapoton, Matthias Köppe, Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this issue Dec 8, 2024
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Fixes sagemath#38381 and makes sure the defining parts of the group element are
immutable.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#38508
Reported by: Travis Scrimshaw
Reviewer(s): Frédéric Chapoton, Matthias Köppe, Travis Scrimshaw
@vbraun vbraun closed this as completed in 04eaef4 Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants