Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ The version is represented by three digits: a.b.c.

## Unreleased

FEATURE:
- `symmetria.Permutation`: add `lexicographic_rank` method
- `symmetria.CycleDecomposition`: add `lexicographic_rank` method

## \[0.3.0\] - 2024-07-15

FEATURE:
Expand Down
5 changes: 5 additions & 0 deletions docs/source/pages/API_reference/elements/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ Here, **P** denotes the class ``Permutation``, **C** the class ``Cycle``, and **
- ✅
- ❌
- ✅
* - ``lexicographic_rank``
- Return the lexicographic rank of the permutation
- ✅
- ❌
- ✅
* - ``map``
- Return the map defining the permutation
- ✅
Expand Down
21 changes: 21 additions & 0 deletions symmetria/elements/cycle_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,27 @@ def is_regular(self) -> bool:
"""
return all(len(cycle) == len(self[0]) for cycle in self)

def lexicographic_rank(self) -> int:
"""Return the lexicographic rank of the cycle decomposition.

Recall that the lexicographic rank of a permutation refers to its position in the list of all
permutations of the same degree sorted in lexicographic order.

:return: the lexocographic rank of the cycle decomposition.
:rtype: int

:example:
>>> from symmetria import Cycle, CycleDecomposition
...
>>> CycleDecomposition(Cycle(1)).lexicographic_rank()
1
>>> CycleDecomposition(Cycle(1, 3, 2)).lexicographic_rank()
5
>>> CycleDecomposition(Cycle(1, 3, 2), Cycle(4, 5)).lexicographic_rank()
50
"""
return symmetria.elements.permutation.Permutation.from_cycle_decomposition(self).lexicographic_rank()

@property
def map(self) -> Dict[int, int]:
"""Return a dictionary representing the mapping of the cycle decomposition,
Expand Down
34 changes: 34 additions & 0 deletions symmetria/elements/permutation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import factorial
from typing import Any, Set, Dict, List, Tuple, Union, Iterable
from collections import OrderedDict

Expand Down Expand Up @@ -876,6 +877,39 @@ def is_regular(self) -> bool:
cycle_decomposition = self.cycle_decomposition()
return all(len(cycle) == len(cycle_decomposition[0]) for cycle in cycle_decomposition)

def lexicographic_rank(self) -> int:
"""Return the lexicographic rank of the permutation.

Recall that the lexicographic rank of a permutation refers to its position in the list of all
permutations of the same degree sorted in lexicographic order.

:return: the lexocographic rank of the permutation.
:rtype: int

:example:
>>> from symmetria import Permutation
...
>>> Permutation(1).lexicographic_rank()
1
>>> Permutation(1, 2, 3).lexicographic_rank()
1
>>> Permutation(1, 3, 2).lexicographic_rank()
2
>>> Permutation(3, 2, 1, 4).lexicographic_rank()
15
"""
n = self.__len__()
rank = 1

for i in range(n):
right_smaller = 0
for j in range(i + 1, n):
if self[i + 1] > self[j + 1]:
right_smaller += 1
rank += right_smaller * factorial(n - i - 1)

return rank

@property
def map(self) -> Dict[int, int]:
"""Return a dictionary representing the mapping of the permutation.
Expand Down
10 changes: 10 additions & 0 deletions tests/tests_elements/tests_permutation/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@
(Permutation(2, 1), True),
(Permutation(2, 1, 3), False),
]
TEST_LEXICOGRAPHIC_RANK = [
(Permutation(1), 1),
(Permutation(1, 2), 1),
(Permutation(2, 1), 2),
(Permutation(1, 2, 3), 1),
(Permutation(1, 3, 2), 2),
(Permutation(3, 2, 1), 6),
(Permutation(3, 2, 1, 4), 15),
(Permutation(1, 2, 5, 4, 3), 6),
]
TEST_MAP = [
(Permutation(1), {1: 1}),
(Permutation(2, 1), {1: 2, 2: 1}),
Expand Down
15 changes: 15 additions & 0 deletions tests/tests_elements/tests_permutation/test_generic_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TEST_IS_DERANGEMENT,
TEST_ONE_LINE_NOTATION,
TEST_IS_CONJUGATE_ERROR,
TEST_LEXICOGRAPHIC_RANK,
TEST_CYCLE_DECOMPOSITION,
)

Expand Down Expand Up @@ -256,6 +257,20 @@ def test_is_regular(permutation, expected_value) -> None:
)


@pytest.mark.parametrize(
argnames="permutation, expected_value",
argvalues=TEST_LEXICOGRAPHIC_RANK,
ids=[f"{p}.lexicographic_rank()={m}" for p, m in TEST_LEXICOGRAPHIC_RANK],
)
def test_lexicographic_rank(permutation, expected_value) -> None:
"""Tests for the method `lexicographic_rank()`."""
_check_values(
expression=f"{permutation.rep()}.lexicographic_rank()",
evaluation=permutation.lexicographic_rank(),
expected=expected_value,
)


@pytest.mark.parametrize(
argnames="permutation, expected_value",
argvalues=TEST_MAP,
Expand Down