Skip to content

Fix Derangements(0) to return 1 and yield the empty permutation #39766

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

Merged
merged 1 commit into from
Apr 2, 2025

Conversation

TinaJin0228
Copy link
Contributor

Fixes #39734

What this does

This PR fixes the behavior of Derangements(0) in two ways:

  • Derangements(0).cardinality() now correctly returns 1 instead of 0, aligning with standard combinatorics conventions and OEIS A000166.
  • Derangements(0).list() now returns [[]], representing the single (empty) derangement of the empty set, instead of an empty list.

These changes ensure consistency with mathematical definitions and fix an inconsistency between Derangements(0) and Permutations(0).

Why this matters

Returning 1 for Derangements(0).cardinality() is consistent with the recursive definition of derangements and is mathematically correct. Without this fix, Sage currently contradicts standard references and user expectations.

📝 Checklist

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

⌛ Dependencies

None.

@tscrim
Copy link
Collaborator

tscrim commented Mar 25, 2025

@darijgr Do you have any thoughts on this?

Copy link
Contributor

@darijgr darijgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job. D_0 should be 1; there is no controversy here.

@tscrim
Copy link
Collaborator

tscrim commented Mar 25, 2025

Thanks you. I also thought so too, but I haven't really worked on derangements to know for certain. Once tests pass, I will set a positive review.

Copy link

Documentation preview for this PR (built with commit 9f22ac9; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@TinaJin0228
Copy link
Contributor Author

Thanks you. I also thought so too, but I haven't really worked on derangements to know for certain. Once tests pass, I will set a positive review.

Thank you for your kind review. I noticed that one of the long tests timed out:

sage -t --long --warn-long 30.0 --random-seed=286735480429121101562228604801325644303 src/sage/data_structures/bounded_integer_sequences.pyx # Timed out

I'm not sure how to address this timeout failure. Could you please share any suggestions? Thanks!

@user202729
Copy link
Contributor

Thanks you. I also thought so too, but I haven't really worked on derangements to know for certain. Once tests pass, I will set a positive review.

Thank you for your kind review. I noticed that one of the long tests timed out:

sage -t --long --warn-long 30.0 --random-seed=286735480429121101562228604801325644303 src/sage/data_structures/bounded_integer_sequences.pyx # Timed out

I'm not sure how to address this timeout failure. Could you please share any suggestions? Thanks!

The failure is probably unrelated to this pull request. (We have been having sporadic issue with these)

vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 1, 2025
sagemathgh-39766: Fix Derangements(0) to return 1 and yield the empty permutation
    
Fixes sagemath#39734

### What this does

This PR fixes the behavior of `Derangements(0)` in two ways:

- `Derangements(0).cardinality()` now correctly returns `1` instead of
`0`, aligning with standard combinatorics conventions and OEIS
[A000166](https://oeis.org/A000166).
- `Derangements(0).list()` now returns `[[]]`, representing the single
(empty) derangement of the empty set, instead of an empty list.

These changes ensure consistency with mathematical definitions and fix
an inconsistency between `Derangements(0)` and `Permutations(0)`.

### Why this matters

Returning `1` for `Derangements(0).cardinality()` is consistent with the
recursive definition of derangements and is mathematically correct.
Without this fix, Sage currently contradicts standard references and
user expectations.

### 📝 Checklist

- [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

None.
    
URL: sagemath#39766
Reported by: TinaJin0228
Reviewer(s): Darij Grinberg
@vbraun vbraun merged commit 329be07 into sagemath:develop Apr 2, 2025
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cardinality of derangements of the empty set
5 participants