Skip to content

Commit 4281670

Browse files
authored
[FEATURE] Develop structure to generate random permutations (#109)
1 parent 7b30599 commit 4281670

File tree

14 files changed

+304
-78
lines changed

14 files changed

+304
-78
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ FEATURE:
2626
- `symmetria.Permutation`: add `degree` method
2727
- `symmetria.CyclePermutation`: add `degree` method
2828
- `symmetria.Cycle`: add `degree` method
29+
- `symmetria.random`: add random module to create random permutations
2930

3031
ENHANCEMENT:
3132
- `symmetria.Permutation`: change how the sign is computed

docs/source/index.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,38 @@ with the symmetric group and its elements.
8484

8585
API reference
8686

87+
.. grid-item-card:: :octicon:`iterations` Generators
88+
:text-align: center
89+
:class-title: sd-fs-5
90+
:class-card: sd-p-3
91+
92+
Generating permutations
93+
94+
.. button-ref:: pages/API_reference/generators/index
95+
:ref-type: doc
96+
:color: primary
97+
:outline:
98+
:click-parent:
99+
:expand:
100+
101+
API Reference
102+
103+
.. grid-item-card:: :octicon:`file-code` CLI
104+
:text-align: center
105+
:class-title: sd-fs-5
106+
:class-card: sd-p-3
107+
108+
Command Line Interface
109+
110+
.. button-ref:: pages/cli/command_line_interface
111+
:ref-type: doc
112+
:color: primary
113+
:outline:
114+
:click-parent:
115+
:expand:
116+
117+
API reference
118+
87119

88120

89121
.. warning:: The documentations is still a working in progress.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Deterministic Generation
2+
========================
3+
4+
`Symmetria` provides a way to generate all the permutations of a given degree. The generation follows different
5+
algorithms which can be specified.
6+
7+
You can use the generator of permutations in the following way
8+
9+
.. code-block:: python
10+
11+
import symmetria
12+
13+
permutations = symmetria.generate(algorithm="lexicographic", degree=3)
14+
15+
A list of implemented algorithms to generate permutations:
16+
17+
.. list-table:: overview
18+
:widths: 35 50 15
19+
:header-rows: 1
20+
21+
* - Algorithm
22+
- Description
23+
- Reference
24+
* - ``lexicographic``
25+
- The permutations are generate following the **lexicographic order**.
26+
- `[1]`_
27+
* - ``heap``
28+
- The permutations are generate following the **Heap's algorithm**.
29+
- `[2]`_
30+
* - ``steinhaus-johnson-trotter``
31+
- The permutations are generate following the **Steinhaus-Johnson-Trotter algorithm**.
32+
- `[3]`_
33+
34+
.. _[1]: https://en.wikipedia.org/wiki/Lexicographic_order
35+
.. _[2]: https://academic.oup.com/comjnl/article/6/3/293/360213
36+
.. _[3]: https://en.wikipedia.org/wiki/Steinhaus–Johnson–Trotter_algorithm
37+
38+
====
39+
40+
The API of the method is given as following:
41+
42+
.. automodule:: symmetria.generators.algorithm.api
43+
:members: generate
44+
:exclude-members: random, random_generator

docs/source/pages/API_reference/generators/generate.rst

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
Generators
22
==========
33

4-
`Symmetria` provides a way to generate all the permutations of a given degree. The generation follows different
5-
algorithms which can be specified.
4+
`Symmetria` provides a way to generate permutations of a given degree in two way:
65

7-
.. note:: The permutation are generated following a well-defined pattern, i.e., they are not random.
6+
- algorithmically, i.e., the permutations are generated following a given algorithm;
7+
- randomly, i.e., the permutations are generate randomly.
88

9-
A list of implemented algorithms to generate permutations:
10-
11-
.. list-table:: overview
12-
:widths: 35 50 15
13-
:header-rows: 1
14-
15-
* - Algorithm
16-
- Description
17-
- Reference
18-
* - ``lexicographic``
19-
- The permutations are generate following the **lexicographic order**.
20-
- `[1]`_
21-
* - ``heap``
22-
- The permutations are generate following the **Heap's algorithm**.
23-
- `[2]`_
24-
* - ``steinhaus-johnson-trotter``
25-
- The permutations are generate following the **Steinhaus-Johnson-Trotter algorithm**.
26-
- `[3]`_
27-
28-
.. _[1]: https://en.wikipedia.org/wiki/Lexicographic_order
29-
.. _[2]: https://academic.oup.com/comjnl/article/6/3/293/360213
30-
.. _[3]: https://en.wikipedia.org/wiki/Steinhaus–Johnson–Trotter_algorithm
319

3210
.. toctree::
3311
:maxdepth: 1
3412
:hidden:
3513

36-
generate
14+
deterministic
15+
random
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Random Generation
2+
=================
3+
4+
`Symmetria` provides a way to generate random permutation.
5+
6+
You can use the random generation of permutations in the following way
7+
8+
.. code-block:: python
9+
10+
import symmetria
11+
12+
permutations = symmetria.random_generator(algorithm="lexicographic", degree=3)
13+
14+
If you don't want to have a generator and you want to just have a singular random permutation
15+
you can use write
16+
17+
.. code-block:: python
18+
19+
import symmetria
20+
21+
permutation = symmetria.random(algorithm="lexicographic", degree=3)
22+
23+
24+
A list of implemented algorithms to generate permutations:
25+
26+
.. list-table:: overview
27+
:widths: 35 50 15
28+
:header-rows: 1
29+
30+
* - Algorithm
31+
- Description
32+
- Reference
33+
* - ``random``
34+
- A permutation is generated by choosing the integer uniformly.
35+
-
36+
* - ``fisher-yates``
37+
- The permutations are generate following the **Steinhaus-Johnson-Trotter algorithm**.
38+
- `[1]`_
39+
40+
.. _[1]: https://en.wikipedia.org/wiki/Lexicographic_order
41+
42+
====
43+
44+
The API of the method is given as following:
45+
46+
.. automodule:: symmetria.generators.random.api
47+
:members: random_generator
48+
:exclude-members: random

symmetria/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
from symmetria.elements.cycle import Cycle
2-
from symmetria.generators.api import generate
32
from symmetria.elements.permutation import Permutation
3+
from symmetria.generators.random.api import random, random_generator
4+
from symmetria.generators.algorithm.api import generate
45
from symmetria.elements.cycle_decomposition import CycleDecomposition
56

67
__version__ = "0.2.0"
7-
__all__ = ["__version__", "generate", "Permutation", "Cycle", "CycleDecomposition"]
8+
__all__ = [
9+
"__version__",
10+
"generate",
11+
"random",
12+
"random_generator",
13+
"Permutation",
14+
"Cycle",
15+
"CycleDecomposition",
16+
]

symmetria/generators/_validators.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
def _check_algorithm_parameter(value: str, supported: List[str]) -> None:
5+
"""Private method to check the value provided for the parameter `algorithm`.
6+
7+
Recall that the parameter `algorithm` must be a string present in the list `supported`.
8+
"""
9+
if isinstance(value, str) is False:
10+
raise TypeError(f"The parameter `algorithm` must be of type string, but {type(value)} was provided.")
11+
if value not in supported:
12+
raise ValueError(
13+
f"The given algorithm ({value}) is not supported. \n "
14+
f"Here, a list of supported algorithm for generations of permutations {supported}."
15+
)
16+
17+
18+
def _check_degree_parameter(value: int) -> None:
19+
"""Private method to check the value provided for the parameter `degree`.
20+
21+
Recall that the parameter `degree` must be a non-negative integer different from zero.
22+
"""
23+
if isinstance(value, int) is False:
24+
raise TypeError(f"The parameter `degree` must be of type int, but {type(value)} was provided.")
25+
if value < 1:
26+
raise ValueError(f"The parameter `degree` must be a non-zero positive integer, but {value} was provided.")

symmetria/generators/algorithm/__init__.py

Whitespace-only changes.

symmetria/generators/_algorithms.py renamed to symmetria/generators/algorithm/_algorithms.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import List, Generator
22

33
import symmetria.elements.permutation
4+
from symmetria import Permutation
45

56

6-
def _lexicographic(degree: int, start: List[int]) -> Generator["Permutation", None, None]:
7+
def _lexicographic(degree: int, start: List[int]) -> Generator[Permutation, None, None]:
78
"""Private method to generate all the permutations of degree `degree` in lexicographic order.
89
910
The algorithm is described as follows:
@@ -38,7 +39,7 @@ def _lexicographic(degree: int, start: List[int]) -> Generator["Permutation", No
3839
permutation[k + 1 :] = reversed(permutation[k + 1 :])
3940

4041

41-
def _heap(degree: int, start: List[int]) -> Generator["Permutation", None, None]:
42+
def _heap(degree: int, start: List[int]) -> Generator[Permutation, None, None]:
4243
"""Private method to generate all the permutations of degree `degree` using the Heap's algorithm.
4344
4445
A description of the algorithm can be founded in the article:
@@ -63,7 +64,7 @@ def _heap(degree: int, start: List[int]) -> Generator["Permutation", None, None]
6364
yield from _heap(k - 1, permutation)
6465

6566

66-
def _steinhaus_johnson_trotter(degree: int, start: List[int]) -> Generator["Permutation", None, None]:
67+
def _steinhaus_johnson_trotter(degree: int, start: List[int]) -> Generator[Permutation, None, None]:
6768
"""Private method to generate all the permutations of degree `degree` using the Steinhaus-Johnson-Trotter algorithm.
6869
6970
A description of the algorithm is given at:

0 commit comments

Comments
 (0)