Converting to an InversePresentation

This page contains documentation relating to converting libsemigroups_pybind11 objects into InversePresentation instances using the to function.

See also

The to function for an overview of possible conversions between libsemigroups_pybind11 types.

Various uses

Recall that the signature for the to function is to(*args, Return). In what follows, we explain how different values of args and Return may be used to construct InversePresentation objects. The following options are possible:

Converting a Presentation to an InversePresentation

To construct an InversePresentation from a Presentation, specify the following values for args:

Additionally, specify the following for Return:

  • (InversePresentation,) for constructing an InversePresentation over words of the same type as those in p.

This function returns an InversePresentation with rules equivalent to those of the input Presentation p, but over a normalised alphabet. If the alphabet of p is \(\{a_0, a_1, \dots, a_{n-1}\}\), then the alphabet of the returned InversePresentation will be \(\{0, 1, \dots, n-1, n, \dots, 2n-1\}\), where the inverse of letter \(i\) is the letter \(i + n\, (\text{mod }2n)\).

This function throws a LibsemigroupsError if p.throw_if_bad_alphabet_or_rules() throws.

>>> from libsemigroups_pybind11 import (
...     InversePresentation,
...     presentation,
...     Presentation,
...     to,
... )

>>> p = Presentation('abc')
>>> presentation.add_rule(p, 'aaa', 'b')
>>> presentation.add_rule(p, 'bac', 'cab')

>>> ip = to(p, rtype=(InversePresentation,))
>>> ip.alphabet()
'abcdef'
>>> ip.inverses()
'defabc'
>>> ip.rules == p.rules
True

Converting an InversePresentation to an InversePresentation

To construct an InversePresentation from an InversePresentation, specify the following values for args:

Additionally, specify one of the following for Return:

  • (InversePresentation, str) for constructing an

  • InversePresentation over words of type str.

  • (InversePresentation, list[int]) for constructing an InversePresentation over words of type list[int].

This function behaves in one of two ways, depending on type of words in p, and the type of words specified in Return:

  1. When the type of words in ip and type of words specified in Return are not the same, this function returns an InversePresentation equivalent to the input InversePresentation ip but with words a different type (for example, can be used to convert from str to list[int]).

  2. When the type of words in ip and type of words specified in Return are the same, this function just returns its argument ip, and is included solely for the purpose of simplifying certain client code, where objects of type InversePresentation must be converted from one type to another sometimes, but not other times.

If the alphabet of of ip is \(\{a_0, a_1, \dots a_{n-1}\}\), where each letter is of type str, then the conversion from one type to another is \(a_i \mapsto\) human_readable_index(a_i). Conversely, if each letter is of type list[int], then the conversion from one type to another is \(a_i \mapsto\) human_readable_letter(a_i).

This function throws a LibsemigroupsError if the type of words in ip is not the same as that specified in Return and p.throw_if_bad_alphabet_rules_or_inverses() throws.

>>> from libsemigroups_pybind11 import presentation, Presentation, to

>>> ip = InversePresentation('abc')
>>> ip.inverses('cba')
<inverse semigroup presentation with 3 letters, 0 rules, and length 0>
>>> presentation.add_rule(ip, 'aaa', 'b')
>>> presentation.add_rule(ip, 'bac', 'cab')

>>> ip == to(ip, rtype=(InversePresentation, str))
True

>>> iq = to(ip, rtype=(InversePresentation, list[int]))
>>> iq.alphabet()
[0, 1, 2]
>>> iq.inverses()
[2, 1, 0]
>>> iq.rules
[[0, 0, 0], [1], [1, 0, 2], [2, 0, 1]]

Converting an InversePresentation to n InversePresentation with a function

To construct a InversePresentation from a InversePresentation using a custom letter conversion function, specify the following values for args:

Additionally, specify one of the following for Return:

  • (InversePresentation, str) for constructing an InversePresentation over words of type str.

  • (InversePresentation, list[int]) for constructing a InversePresentation over words of type list[int].

This function returns an InversePresentation equivalent to the input InversePresentation ip but over words with letters of a different type (for example, can be used to convert from str to int). The second parameter f specifies how to map the letters of one InversePresentation to the other.

This function throws a LibsemigroupsError if ip.throw_if_bad_alphabet_rules_or_inverses() throws, or if the function specified by f does not map letters of the type used in ip to letters of the type of word specified in Return.

>>> from libsemigroups_pybind11 import (
...     InversePresentation,
...     presentation,
...     Presentation,
...     to,
... )

>>> ip = InversePresentation('abc')
>>> ip.inverses('cba')
<inverse semigroup presentation with 3 letters, 0 rules, and length 0>
>>> presentation.add_rule(ip, 'aaa', 'b')
>>> presentation.add_rule(ip, 'bac', 'cab')

>>> iq = to(
...     ip,                                 # ip
...     lambda x: chr(ord(x) + 11),         # f
...     rtype=(InversePresentation, str)
... )
>>> iq.alphabet()
'lmn'
>>> iq.inverses()
'nml'
>>> iq.rules
['lll', 'm', 'mln', 'nlm']