Skip to content

Commit 297d3ee

Browse files
presentation: add index_rule
1 parent 33f2de4 commit 297d3ee

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

docs/source/data-structures/presentations/present-helpers.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Contents
4747
first_unused_letter
4848
greedy_reduce_length
4949
greedy_reduce_length_and_number_of_gens
50+
index_rule
5051
is_strongly_compressible
5152
length
5253
longest_rule

src/libsemigroups_pybind11/presentation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
presentation_to_gap_string as _to_gap_string,
5555
presentation_balance as _balance,
5656
presentation_add_cyclic_conjugates as _add_cyclic_conjugates,
57+
presentation_index_rule as _index_rule,
5758
)
5859

5960
from libsemigroups_pybind11.detail.cxx_wrapper import (
@@ -267,3 +268,4 @@ def __init__(self: _Self, *args, **kwargs) -> None:
267268
to_gap_string = _wrap_cxx_free_fn(_to_gap_string)
268269
balance = _wrap_cxx_free_fn(_balance)
269270
add_cyclic_conjugates = _wrap_cxx_free_fn(_add_cyclic_conjugates)
271+
index_rule = _wrap_cxx_free_fn(_index_rule)

src/present.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,40 @@ empty word to the presentation *p*, for every cyclic permutation ``w`` of
13971397
if *relator* contains any letters not belonging to ``p.alphabet()``.
13981398
13991399
:raises LibsemigroupsError: if *p* does not contain the empty word.)pbdoc");
1400+
1401+
// We do not bind presentation::find_rule because it returns an iterator
1402+
//
1403+
m.def(
1404+
"presentation_index_rule",
1405+
[](Presentation<Word>& p, Word const& lhs, Word const& rhs) {
1406+
return from_int(presentation::index_rule(p, lhs, rhs));
1407+
},
1408+
py::arg("p"),
1409+
py::arg("lhs"),
1410+
py::arg("rhs"),
1411+
R"pbdoc(
1412+
:sig=(p: Presentation, lhs: Word, rhs: Word) -> int | Undefined:
1413+
:only-document-once:
1414+
1415+
Returns the index of a rule or :any:`UNDEFINED`.
1416+
1417+
This function returns the minimum index ``i`` of *lhs* such that ``p.rules[i +
1418+
1]`` equals *rhs* ; or :any:`UNDEFINED` if there is not such rule
1419+
1420+
:param p: the presentation.
1421+
:type p: Presentation
1422+
1423+
:param lhs: the left-hand side of the rule.
1424+
:type lhs: :ref:`Word<pseudo_word_type_helper>`
1425+
1426+
:param rhs: the right-hand side of the rule.
1427+
:type rhs: :ref:`Word<pseudo_word_type_helper>`
1428+
1429+
:returns: The index of the rule or :any:`UNDEFINED`.
1430+
:rtype: int | Undefined
1431+
1432+
:raises LibsemigroupsError: if ``p.throw_if_bad_alphabet_or_rules()`` throws.
1433+
)pbdoc");
14001434
} // bind_present
14011435

14021436
template <typename Word>

tests/test_present.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,3 +2192,12 @@ def test_presentation_add_cyclic_conjugates():
21922192
presentation.add_cyclic_conjugates(p, "abcaabbcc")
21932193
with pytest.raises(LibsemigroupsError):
21942194
presentation.add_cyclic_conjugates(p, "dadadad")
2195+
2196+
2197+
def test_presentation_index_rule():
2198+
p = Presentation("abc")
2199+
p.contains_empty_word(True)
2200+
p.rules = ["aaaa", ""]
2201+
2202+
assert presentation.index_rule(p, "aaaa", "") == 0
2203+
assert presentation.index_rule(p, "a", "") == UNDEFINED

0 commit comments

Comments
 (0)