From dd43e3fd121ac0d0cfc2164f7bbed0a125770767 Mon Sep 17 00:00:00 2001 From: "James D. Mitchell" Date: Thu, 5 Dec 2024 14:09:27 +0000 Subject: [PATCH] Add method to FroidurePin that maybe returns UNDEFINED --- libsemigroups_pybind11/froidure_pin.py | 12 ++++---- tests/test_froidure_pin.py | 39 +++++++++++++++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/libsemigroups_pybind11/froidure_pin.py b/libsemigroups_pybind11/froidure_pin.py index 45dc955c..f7adb87c 100644 --- a/libsemigroups_pybind11/froidure_pin.py +++ b/libsemigroups_pybind11/froidure_pin.py @@ -157,14 +157,10 @@ def __iter__(self: Self) -> Iterator: ######################################################################## def current_elements(self: Self) -> Iterator: - return map( - lambda x: to_py(self.Element, x), self._cxx_obj.current_elements() - ) + return map(lambda x: to_py(self.Element, x), self._cxx_obj.current_elements()) def idempotents(self: Self) -> Iterator: - return map( - lambda x: to_py(self.Element, x), self._cxx_obj.idempotents() - ) + return map(lambda x: to_py(self.Element, x), self._cxx_obj.idempotents()) def sorted_elements(self: Self) -> Iterator: return map( @@ -184,6 +180,10 @@ def generator(self: Self, i: int) -> Element: def sorted_at(self: Self, i: int) -> Element: return self._cxx_obj.sorted_at(i) + @may_return_undefined + def current_position(self: Self, x: Element) -> Element: + return self._cxx_obj.current_position(to_cxx(x)) + ######################################################################## # Helpers -- from froidure-pin.cpp diff --git a/tests/test_froidure_pin.py b/tests/test_froidure_pin.py index 70785bd6..f0fa4b58 100644 --- a/tests/test_froidure_pin.py +++ b/tests/test_froidure_pin.py @@ -29,6 +29,7 @@ BMat8, froidure_pin, LibsemigroupsError, + UNDEFINED, ) @@ -101,7 +102,8 @@ def check_mem_compare(S): # [S.position(froidure_pin.factorisation(S, x)) for x in S], list(range(S.size())) # ) assert [ - froidure_pin.current_position(S, froidure_pin.factorisation(S, x)) for x in S + froidure_pin.current_position(S, froidure_pin.factorisation(S, x)) + for x in S ] == list(range(S.size())) assert [S.current_position(x) for x in S] == list(range(S.size())) @@ -189,17 +191,21 @@ def check_factor_prod_rels(S): # (minimal_)factorisation + to_element for i, x in enumerate(S): assert froidure_pin.to_element(S, froidure_pin.factorisation(S, x)) == x - assert froidure_pin.to_element(S, froidure_pin.minimal_factorisation(S, i)) == x + assert ( + froidure_pin.to_element(S, froidure_pin.minimal_factorisation(S, i)) + == x + ) # rules, number_of_rules assert len(list(froidure_pin.rules(S))) == S.number_of_rules() for lhs, rhs in froidure_pin.rules(S): - assert froidure_pin.current_position(S, lhs) == froidure_pin.current_position( - S, rhs - ) + assert froidure_pin.current_position( + S, lhs + ) == froidure_pin.current_position(S, rhs) assert ( - froidure_pin.factorisation(S, froidure_pin.current_position(S, rhs)) == rhs + froidure_pin.factorisation(S, froidure_pin.current_position(S, rhs)) + == rhs ) # product_by_reduction + fast_product @@ -471,7 +477,9 @@ def test_froidure_pin_min_plus(checks_for_froidure_pin, checks_for_generators): check(FroidurePin(gens)) -def test_froidure_pin_proj_max_plus(checks_for_froidure_pin, checks_for_generators): +def test_froidure_pin_proj_max_plus( + checks_for_froidure_pin, checks_for_generators +): ReportGuard(False) x = Matrix(MatrixKind.ProjMaxPlus, 2, 2) gens = [Matrix(MatrixKind.ProjMaxPlus, [[1, 0], [0, x.scalar_zero()]])] @@ -484,7 +492,9 @@ def test_froidure_pin_proj_max_plus(checks_for_froidure_pin, checks_for_generato check(FroidurePin(gens)) -def test_froidure_pin_max_plus_trunc(checks_for_froidure_pin, checks_for_generators): +def test_froidure_pin_max_plus_trunc( + checks_for_froidure_pin, checks_for_generators +): ReportGuard(False) gens = [Matrix(MatrixKind.MaxPlusTrunc, 11, [[1, 0], [0, 1]])] assert FroidurePin(gens).size() == 12 @@ -496,7 +506,9 @@ def test_froidure_pin_max_plus_trunc(checks_for_froidure_pin, checks_for_generat check(FroidurePin(gens)) -def test_froidure_pin_min_plus_trunc(checks_for_froidure_pin, checks_for_generators): +def test_froidure_pin_min_plus_trunc( + checks_for_froidure_pin, checks_for_generators +): ReportGuard(False) gens = [Matrix(MatrixKind.MinPlusTrunc, 11, [[1, 0], [0, 1]])] assert FroidurePin(gens).size() == 2 @@ -545,7 +557,9 @@ def test_froidure_pin_method_wrap(): S.init() with pytest.raises(LibsemigroupsError): - S.add_generators([Perm([0, 1, 2, 3, 4, 5]), Perm([0, 1, 2, 3, 4, 5, 6])]) + S.add_generators( + [Perm([0, 1, 2, 3, 4, 5]), Perm([0, 1, 2, 3, 4, 5, 6])] + ) S = FroidurePin(Perm([1, 0, 2, 3, 4, 5, 6]), Perm([1, 2, 3, 4, 5, 6, 0])) @@ -569,6 +583,11 @@ def test_froidure_pin_method_wrap(): # TODO more +def test_froidure_pin_return_undefined_1(): + S = FroidurePin(Perm([1, 0, 2, 3, 4, 5, 6])) + assert S.current_position(Perm([1, 0, 2])) is UNDEFINED + + # def test_froidure_pin_tce(checks_for_froidure_pin): # ReportGuard(False) # tc = ToddCoxeter(congruence_kind.twosided)