-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add
lexicographic
algorithm (#91)
- Loading branch information
1 parent
a07a74f
commit 4f93f70
Showing
8 changed files
with
129 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
...ence/generators/permutation_generator.rst → ...ges/API_reference/generators/generate.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
Permutation Generator | ||
Generate | ||
===================== | ||
|
||
To use the generator of permutation, import it as | ||
|
||
.. code-block:: python | ||
from symmetria import permutation_generator | ||
import symmetria | ||
... | ||
permutations = symmetria.generate(algorithm="lexicographic", degree=3) | ||
The API of the method is given as following: | ||
|
||
.. automodule:: symmetria | ||
:members: permutation_generator | ||
:members: generate | ||
:exclude-members: Cycle, CycleDecomposition, Permutation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ A list of implemented algorithms to generate permutations: | |
:maxdepth: 1 | ||
:hidden: | ||
|
||
permutation_generator | ||
generate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Generator | ||
|
||
import symmetria.elements.permutation | ||
|
||
|
||
def _lexicographic_generator(degree: int) -> Generator["Permutation", None, None]: | ||
"""Private method to generate all the permutations of degree `degree` in lexicographic order. | ||
The algorithm is described as follows: | ||
- Step 1: Consider the identity permutation in the given degree. | ||
- Step 2: Find the largest index k such that permutation[k] < permutation[k + 1]. | ||
- Step 3: If no k exists, then it is permutation is the last permutation. END. | ||
- Step 4: Find the largest index j greater than k such that permutation[k] < permutation[j]. | ||
- Step 5: Swap the value of permutation[k] with that of permutation[j]. | ||
- Step 6: Reverse the sequence from permutation[k + 1] up to and including the final element permutation[degree]. | ||
- Step 7: Go to Step 2. | ||
""" | ||
# step 1 | ||
permutation = list(range(1, degree + 1)) | ||
|
||
while True: | ||
yield symmetria.Permutation(*permutation) | ||
|
||
# step 2 | ||
k = next((i for i in range(degree - 2, -1, -1) if permutation[i] < permutation[i + 1]), -1) | ||
|
||
# step 3 | ||
if k == -1: | ||
return None | ||
|
||
# step 4 | ||
j = next(i for i in range(degree - 1, k, -1) if permutation[k] < permutation[i]) | ||
|
||
# step 5 | ||
permutation[k], permutation[j] = permutation[j], permutation[k] | ||
|
||
# step 6 | ||
permutation[k + 1 :] = reversed(permutation[k + 1 :]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,51 @@ | ||
from symmetria import Permutation | ||
|
||
TEST_PERMUTATION_GENERATOR_EXCPETIONS = [ | ||
(13, 3, TypeError, "The parameter `algorithm` must be of type string"), | ||
("test", 3, ValueError, "The given algorithm"), | ||
("lexicographic", "test", TypeError, "The parameter `degree`"), | ||
("lexicographic", 0, ValueError, "The parameter `degree` must be a non-zero positive integer"), | ||
] | ||
TEST_LEXICOGRAPHIC_GENERATOR = [ | ||
(1, [Permutation(1)]), | ||
( | ||
3, | ||
[ | ||
Permutation(1, 2, 3), | ||
Permutation(1, 3, 2), | ||
Permutation(2, 1, 3), | ||
Permutation(2, 3, 1), | ||
Permutation(3, 1, 2), | ||
Permutation(3, 2, 1), | ||
], | ||
), | ||
( | ||
4, | ||
[ | ||
Permutation(1, 2, 3, 4), | ||
Permutation(1, 2, 4, 3), | ||
Permutation(1, 3, 2, 4), | ||
Permutation(1, 3, 4, 2), | ||
Permutation(1, 4, 2, 3), | ||
Permutation(1, 4, 3, 2), | ||
Permutation(2, 1, 3, 4), | ||
Permutation(2, 1, 4, 3), | ||
Permutation(2, 3, 1, 4), | ||
Permutation(2, 3, 4, 1), | ||
Permutation(2, 4, 1, 3), | ||
Permutation(2, 4, 3, 1), | ||
Permutation(3, 1, 2, 4), | ||
Permutation(3, 1, 4, 2), | ||
Permutation(3, 2, 1, 4), | ||
Permutation(3, 2, 4, 1), | ||
Permutation(3, 4, 1, 2), | ||
Permutation(3, 4, 2, 1), | ||
Permutation(4, 1, 2, 3), | ||
Permutation(4, 1, 3, 2), | ||
Permutation(4, 2, 1, 3), | ||
Permutation(4, 2, 3, 1), | ||
Permutation(4, 3, 1, 2), | ||
Permutation(4, 3, 2, 1), | ||
], | ||
), | ||
] |