Skip to content

Commit

Permalink
Update knuth-bendix-hpp for v1/3
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-mitchell committed Dec 11, 2024
1 parent 8261f17 commit bb244e1
Show file tree
Hide file tree
Showing 32 changed files with 1,359 additions and 2,039 deletions.
2 changes: 1 addition & 1 deletion docs/source/main-algorithms/cong-intf/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ semigroups and monoids. These classes are:

* :any:`Congruence`
* :any:`Kambites`
* :any:`KnuthBendix`
* :any:`KnuthBendixRewriteTrie`
* :any:`ToddCoxeter`

.. toctree::
Expand Down
49 changes: 49 additions & 0 deletions docs/source/main-algorithms/knuth-bendix/class.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.. Copyright (c) 2021-2024 J. D. Mitchell
Distributed under the terms of the GPL license version 3.
The full license is in the file LICENSE, distributed with this software.
.. currentmodule:: _libsemigroups_pybind11

The KnuthBendix class
=====================

.. autoclass:: KnuthBendixRewriteTrie
:doc-only:
:class-doc-from: class

Contents
--------

.. autosummary::
:nosignatures:

~KnuthBendixRewriteTrie
KnuthBendixRewriteTrie.active_rules
KnuthBendixRewriteTrie.check_confluence_interval
KnuthBendixRewriteTrie.confluent
KnuthBendixRewriteTrie.confluent_known
KnuthBendixRewriteTrie.copy
KnuthBendixRewriteTrie.contains
KnuthBendixRewriteTrie.currently_contains
KnuthBendixRewriteTrie.generating_pairs
KnuthBendixRewriteTrie.gilman_graph
KnuthBendixRewriteTrie.gilman_graph_node_labels
KnuthBendixRewriteTrie.max_overlap
KnuthBendixRewriteTrie.max_pending_rules
KnuthBendixRewriteTrie.max_rules
KnuthBendixRewriteTrie.number_of_active_rules
KnuthBendixRewriteTrie.number_of_classes
KnuthBendixRewriteTrie.number_of_inactive_rules
KnuthBendixRewriteTrie.overlap_policy
KnuthBendixRewriteTrie.presentation
KnuthBendixRewriteTrie.reduce_no_run
KnuthBendixRewriteTrie.total_rules

Full API
--------

.. autoclass:: KnuthBendixRewriteTrie
:class-doc-from: init
:members:
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Knuth-Bendix helpers
====================

This page contains the documentation for various helper functions for
manipulating ``KnuthBendix`` objects. All such functions are contained in the
submodule ``libsemigroups_pybind11.knuth_bendix``.
manipulating :any:`KnuthBendixRewriteTrie` objects. All such functions are
contained in the submodule ``libsemigroups_pybind11.knuth_bendix``.

.. seealso::
:py:class:`overlap`
Expand All @@ -20,9 +20,9 @@ submodule ``libsemigroups_pybind11.knuth_bendix``.
Contents
--------

.. .. currentmodule:: libsemigroups_pybind11.knuth_bendix
.. currentmodule:: libsemigroups_pybind11.knuth_bendix

.. .. autosummary::
.. autosummary::
:nosignatures:

by_overlap_length
Expand All @@ -34,6 +34,7 @@ Contents
Full API
--------

.. .. automodule:: libsemigroups_pybind11.knuth_bendix
.. automodule:: libsemigroups_pybind11.knuth_bendix
:members:
:imported-members:
:exclude-members: KnuthBendix
10 changes: 5 additions & 5 deletions docs/source/main-algorithms/knuth-bendix/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
Knuth-Bendix
=============

This page describes the functionality for the Knuth-Bendix procedure
in ``libsemigroups_pybind11``.
This page describes the functionality for the Knuth-Bendix procedure
in ``libsemigroups_pybind11``.


.. toctree::
:maxdepth: 1

knuth-bendix
kb-helpers
overlap
class
helpers
overlap
79 changes: 0 additions & 79 deletions docs/source/main-algorithms/knuth-bendix/knuth-bendix.rst

This file was deleted.

5 changes: 3 additions & 2 deletions docs/source/main-algorithms/knuth-bendix/overlap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Overlap
=======

TODO: An explanation of overlaps
.. TODO(0) this should be included in the KnuthBendix doc somehow
TODO(0): An explanation of overlaps

.. autoclass:: overlap
.. autoclass:: overlap
2 changes: 1 addition & 1 deletion docs/source/main-algorithms/todd-coxeter/class/common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Common methods

This page contains documentation of the methods of :any:`ToddCoxeter`
that are implemented in all of the classes :any:`Congruence`, :any:`Kambites`,
:any:`KnuthBendix`, and :any:`ToddCoxeter`.
:any:`KnuthBendixRewriteTrie`, and :any:`ToddCoxeter`.

.. automethod:: ToddCoxeter.add_generating_pair
.. automethod:: ToddCoxeter.contains
Expand Down
2 changes: 1 addition & 1 deletion libsemigroups_pybind11/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
from .adapters import ImageRightAction, ImageLeftAction


# from .knuth_bendix import KnuthBendix
from .knuth_bendix import KnuthBendix
from .matrix import _Matrix as Matrix, _MatrixKind as MatrixKind
from .presentation import Presentation, InversePresentation
from .transf import (
Expand Down
125 changes: 125 additions & 0 deletions libsemigroups_pybind11/knuth_bendix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2024, J. D. Mitchell, Joseph Edwards
#
# Distributed under the terms of the GPL license version 3.
#
# The full license is in the file LICENSE, distributed with this software.

# pylint: disable=no-name-in-module, unused-import, protected-access,
# pylint: disable=missing-function-docstring

"""
This package provides the user-facing python part of libsemigroups_pybind11 for
the KnuthBendix class from libsemigroups.
"""

from functools import wraps as _wraps
from typing import List

from _libsemigroups_pybind11 import (
KnuthBendixRewriteFromLeft as _KnuthBendixRewriteFromLeft,
KnuthBendixRewriteTrie as _KnuthBendixRewriteTrie,
PresentationStrings as _PresentationStrings,
PresentationWords as _PresentationWords,
congruence_kind as _congruence_kind,
knuth_bendix_by_overlap_length as by_overlap_length,
knuth_bendix_str_normal_forms as _knuth_bendix_str_normal_forms,
knuth_bendix_word_normal_forms as _knuth_bendix_word_normal_forms,
knuth_bendix_str_non_trivial_classes as _knuth_bendix_str_non_trivial_classes,
knuth_bendix_word_non_trivial_classes as _knuth_bendix_word_non_trivial_classes,
knuth_bendix_redundant_rule as redundant_rule,
knuth_bendix_is_reduced as is_reduced,
)

from .detail.decorators import (
may_return_positive_infinity as _may_return_positive_infinity,
)

_Presentation = (_PresentationStrings, _PresentationWords)
_KnuthBendix = (_KnuthBendixRewriteFromLeft, _KnuthBendixRewriteTrie)

for KB in _KnuthBendix:
KB.number_of_classes = _may_return_positive_infinity(KB._number_of_classes)


def KnuthBendix(*args, rewriter="RewriteTrie"): # pylint: disable=invalid-name
"""
Construct a KnuthBendix instance of the type specified by its arguments
"""
if len(args) not in (0, 2):
raise TypeError(f"expected 0 or 2 positional arguments, found {len(args)}")

if len(args) == 2 and not isinstance(args[0], _congruence_kind):
raise TypeError(
(
"the 1st positional argument must be congruence_kind, "
f"found ({type(args[0])})"
)
)

if len(args) == 2 and not isinstance(args[1], _Presentation):
raise TypeError(
(
"the 2nd positional argument must be presentation, "
f"found ({type(args[1])})"
)
)

if rewriter == "RewriteFromLeft":
result = _KnuthBendixRewriteFromLeft(*args)
elif rewriter == "RewriteTrie":
result = _KnuthBendixRewriteTrie(*args)
else:
raise TypeError(
(
f'expected the rewriter kwarg to be either "RewriteTrie" or '
f'"RewriteFromLeft", but found {rewriter}'
)
)

return result


@_wraps(_knuth_bendix_str_non_trivial_classes)
def non_trivial_classes(kb1: KnuthBendix, kb2: KnuthBendix, **kwargs):
if len(kwargs) != 1:
raise TypeError(f"expected 1 keyword argument, but found {len(kwargs)}")
if "Word" not in kwargs:
raise TypeError(
f'expected keyword argument "Word", but found "{next(iter(kwargs))}"'
)
if kwargs["Word"] is List[int]:
return _knuth_bendix_word_non_trivial_classes(kb1, kb2)
if kwargs["Word"] is str:
return _knuth_bendix_str_non_trivial_classes(kb1, kb2)

val = kwargs["Word"]
val = f'"{val}"' if isinstance(val, str) else val

raise TypeError(
'expected the value of the keyword argument "Word" to be '
f"List[int] or str, but found {val}"
)


@_wraps(_knuth_bendix_str_normal_forms)
def normal_forms(kb: KnuthBendix, **kwargs):
if len(kwargs) != 1:
raise TypeError(f"expected 1 keyword argument, but found {len(kwargs)}")
if "Word" not in kwargs:
raise TypeError(
f'expected keyword argument "Word", but found "{next(iter(kwargs))}"'
)
if kwargs["Word"] is List[int]:
return _knuth_bendix_word_normal_forms(kb)
if kwargs["Word"] is str:
return _knuth_bendix_str_normal_forms(kb)

val = kwargs["Word"]
val = f'"{val}"' if isinstance(val, str) else val

raise TypeError(
'expected the value of the keyword argument "Word" to be '
f"List[int] or str, but found {val}"
)
2 changes: 1 addition & 1 deletion src/aho-corasick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ with the Aho-Corasick dictionary searching algorithm. An introduction to this
algorithm can be found `here <https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm>`_.
The implementation of :any:`AhoCorasick` uses two different types of node;
*active* and *inactive* . An active node is a node that is currently a node
*active* and *inactive*. An active node is a node that is currently a node
in the trie. An inactive node is a node that used to be part of the trie, but
has since been removed. It may later become active again after being
reinitialised, and exists as a way of minimising how frequently memory needs
Expand Down
2 changes: 1 addition & 1 deletion src/bipart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ returned vector is ``True`` if the block with index ``i`` is transverse and
Return the identity bipartition with the same degree as the given bipartition.
The *identity bipartition* of degree :math:`n` has blocks :math:`\{i, -i\}` for
all :math:`i\in \{0, \ldots, n - 1\}` . This function returns a new identity
all :math:`i\in \{0, \ldots, n - 1\}`. This function returns a new identity
bipartition of degree equal to the degree of ``self``.
:param f: a bipartition
Expand Down
4 changes: 3 additions & 1 deletion src/bmat8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,15 @@ Returns the size of the column space of a :any:`BMat8`.
Returns the minimum dimension of a :any:`BMat8`.
This function returns the maximal ``n`` such that row ``n`` or column ``n``
contains a ``1`` . Equivalent to the maximum of :any:`number_of_rows` and
contains a ``1``. Equivalent to the maximum of :any:`number_of_rows` and
:any:`number_of_cols`.
:param x: the matrix.
:type x: BMat8
:returns: The minimum dimension of **x**
:rtype: int
:complexity: Constant.
.. doctest::
Expand Down
2 changes: 1 addition & 1 deletion src/cong-intf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data that are common to all its derived classes. These classes are:
* :any:`Congruence`
* :any:`Kambites`
* :any:`KnuthBendix`
* :py:class:`KnuthBendixRewriteTrie`
* :any:`ToddCoxeter`
)pbdoc");
thing.def(
Expand Down
Loading

0 comments on commit bb244e1

Please sign in to comment.