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
Change minlex to use the linear time least_rotation. #20433
Conversation
✅ Hi, I am the SymPy bot (v161). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.8. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
Codecov Report
@@ Coverage Diff @@
## master #20433 +/- ##
=============================================
- Coverage 75.764% 75.739% -0.026%
=============================================
Files 673 673
Lines 174119 174437 +318
Branches 41109 41200 +91
=============================================
+ Hits 131921 132117 +196
- Misses 36480 36602 +122
Partials 5718 5718 |
This appears to no longer retain the string/tuple invariant for output. |
Thank you for letting me know about this. I have added a commit to ensure that the returned type is a tuple, unless the input is a string and have updated the docstring of the function to note that this is the desired behaviour. |
It would be good to add a test for a string, too. |
Using "default_sort_key" might be more restrictive than this was before. Things in iterables should mostly be sympy-agnostic. So previously the items were simply compared: |
What is the reason for using |
So without the default sort key the Polyhedron test (
|
Sorry, I didn't see that @smichr does this look good to you? (I don't know this particular routine so well but the change seems reasonable to me from a high level) |
So having given this some thought, I think one refinement that would be reasonable is to adjust both |
@smichr There is already a doctest for this. Are you suggesting a separate unittest too? |
Doctests are just for testing the documentation. We should have unit tests to test the correctness of the code. |
Ok, I've just checked and there is also a unittest already. |
A future pull request could replace all uses of: from sympy.utilities.iterables import default_sort_key and: from sympy.utilities import default_sort_key with: from sympy.core.compatibility import default_sort_key
I thought it was ready to merge. What were the last two commits for? |
So following the discussion around whether I've added a unittest & doctest to demonstrate the use of this. |
Thanks @MarkCBell and @smichr! This looks good |
Currently the
minlex
utility function computes the earliest rotation of an iterable by producing all rotations of the iterable and looking for the smallest one. This is a quadratic time operation since there are linearly many rotations and it takes linear time to determine whether a rotation is smaller than the best rotation found so far. The utility functionleast_rotation
uses Booth's algorithm to compute the index of the smallest rotation in linear time henceminlex
can be fundamentally done using:return left_rotate(seq, least_rotation(seq))
however a few extra cases are needed since
minlex
can also check for the smallest rotation of the iterable and its reverse.Since this is now a linear time algorithm there is no benefit in passing in some of the hints that speed up the old function such as
is_set
andsmall
.key=
arguments similar tosorted
.