|
| 1 | +.. Copyright (c) 2025 Joseph Edwards |
| 2 | +
|
| 3 | + Distributed under the terms of the GPL license version 3. |
| 4 | +
|
| 5 | + The full license is in the file LICENSE, distributed with this software. |
| 6 | +
|
| 7 | +.. currentmodule:: libsemigroups_pybind11 |
| 8 | + |
| 9 | +Converting to an InversePresentationStrings |
| 10 | +=========================================== |
| 11 | + |
| 12 | +This page contains documentation relating to converting |
| 13 | +``libsemigroups_pybind11`` objects into :any:`InversePresentationStrings` |
| 14 | +instances using the :any:`to` function. |
| 15 | + |
| 16 | +.. seealso:: |
| 17 | + |
| 18 | + :doc:`/data-structures/to-function` for an overview of possible conversions |
| 19 | + between ``libsemigroups_pybind11`` types. |
| 20 | + |
| 21 | +Various uses |
| 22 | +------------ |
| 23 | + |
| 24 | +Recall that the signature for the :any:`to` function is ``to(*args, Return)``. |
| 25 | +In what follows, we explain how different values of *args* and *Return* may |
| 26 | +be used to construct :any:`InversePresentationStrings` objects. The following |
| 27 | +options are possible: |
| 28 | + |
| 29 | + * :ref:`presentation-to-inverse-presentation`; |
| 30 | + * :ref:`inverse-presentation-to-inverse-presentation`; and |
| 31 | + * :ref:`inverse-presentation-and-function-to-inverse-presentation`. |
| 32 | + |
| 33 | +.. _presentation-to-inverse-presentation: |
| 34 | + |
| 35 | +Converting a :any:`PresentationStrings` to an :any:`InversePresentationStrings` |
| 36 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 37 | + |
| 38 | +To construct an :any:`InversePresentationStrings` from a |
| 39 | +:any:`PresentationStrings`, specify the following values for *args*: |
| 40 | + |
| 41 | + * **p** (:any:`PresentationStrings`) -- the :any:`PresentationStrings` to |
| 42 | + convert. |
| 43 | + |
| 44 | +Additionally, specify the following for *Return*: |
| 45 | + |
| 46 | + * ``InversePresentation`` for constructing an |
| 47 | + :any:`InversePresentationStrings` over words of the same type as those in |
| 48 | + *p*. |
| 49 | + |
| 50 | +This function returns an :any:`InversePresentationStrings` with rules equivalent |
| 51 | +to those of the input :any:`PresentationStrings` *p*, but over a normalised |
| 52 | +alphabet. If the alphabet of *p* is :math:`\{a_0, a_1, \dots, a_{n-1}\}`, then |
| 53 | +the alphabet of the returned :any:`InversePresentationStrings` will be |
| 54 | +:math:`\{0, 1, \dots, n-1, n, \dots, 2n-1\}`, where the inverse of letter |
| 55 | +:math:`i` is the letter :math:`i + n\, (\text{mod }2n)`. |
| 56 | + |
| 57 | +This function throws a :any:`LibsemigroupsError` if ``p.validate()`` throws. |
| 58 | + |
| 59 | +.. seealso:: |
| 60 | + |
| 61 | + * :any:`PresentationStrings.validate`. |
| 62 | + |
| 63 | +.. doctest:: Python |
| 64 | + |
| 65 | + >>> from libsemigroups_pybind11 import ( |
| 66 | + ... InversePresentation, |
| 67 | + ... presentation, |
| 68 | + ... Presentation, |
| 69 | + ... to, |
| 70 | + ... ) |
| 71 | + |
| 72 | + >>> p = Presentation('abc') |
| 73 | + >>> presentation.add_rule(p, 'aaa', 'b') |
| 74 | + >>> presentation.add_rule(p, 'bac', 'cab') |
| 75 | + |
| 76 | + >>> ip = to(p, Return=InversePresentation) |
| 77 | + >>> ip.alphabet() |
| 78 | + 'abcdef' |
| 79 | + >>> ip.inverses() |
| 80 | + 'defabc' |
| 81 | + >>> ip.rules == p.rules |
| 82 | + True |
| 83 | + |
| 84 | +.. _inverse-presentation-to-inverse-presentation: |
| 85 | + |
| 86 | +Converting an :any:`InversePresentationStrings` to an :any:`InversePresentationStrings` |
| 87 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 88 | + |
| 89 | +To construct an :any:`InversePresentationStrings` from an |
| 90 | +:any:`InversePresentationStrings`, specify the following values for *args*: |
| 91 | + |
| 92 | + * **ip** (:any:`InversePresentationStrings`) -- the |
| 93 | + :any:`InversePresentationStrings` to convert. |
| 94 | + |
| 95 | +Additionally, specify one of the following for *Return*: |
| 96 | + |
| 97 | + * ``(InversePresentation, str)`` for constructing an |
| 98 | + * :any:`InversePresentationStrings` over words of type ``str``. |
| 99 | + * ``(InversePresentation, List[int])`` for constructing an |
| 100 | + :any:`InversePresentationStrings` over words of type ``List[int]``. |
| 101 | + |
| 102 | +This function behaves in one of two ways, depending on type of words in *p*, and |
| 103 | +the type of words specified in *Return*: |
| 104 | + |
| 105 | + #. When the type of words in *ip* and type of words specified in *Return* |
| 106 | + are not the same, this function returns an |
| 107 | + :any:`InversePresentationStrings` equivalent to the input |
| 108 | + :any:`InversePresentationStrings` *ip* but with words a different type |
| 109 | + (for example, can be used to convert from ``str`` to ``List[int]``). |
| 110 | + #. When the type of words in *ip* and type of words specified in *Return* |
| 111 | + are the same, this function just returns its argument *ip*, and is |
| 112 | + included solely for the purpose of simplifying certain client code, where |
| 113 | + objects of type :any:`InversePresentationStrings` must be converted from |
| 114 | + one type to another sometimes, but not other times. |
| 115 | + |
| 116 | +If the alphabet of of *ip* is :math:`\{a_0, a_1, \dots a_{n-1}\}`, where each |
| 117 | +letter is of type ``str``, then the conversion from one type to another is |
| 118 | +:math:`a_i \mapsto` ``human_readable_index(a_i)``. Conversely, if each letter is |
| 119 | +of type ``List[int]``, then the conversion from one type to another is |
| 120 | +:math:`a_i \mapsto` ``human_readable_letter(a_i)``. |
| 121 | + |
| 122 | +This function throws a :any:`LibsemigroupsError` if the type of words in *ip* is |
| 123 | +not the same as that specified in *Return* and ``p.validate()`` throws. |
| 124 | + |
| 125 | +.. seealso:: |
| 126 | + |
| 127 | + * :any:`words.human_readable_index`; |
| 128 | + * :any:`words.human_readable_letter`; and |
| 129 | + * :any:`InversePresentationStrings.validate`. |
| 130 | + |
| 131 | +.. doctest:: Python |
| 132 | + |
| 133 | + >>> from typing import List |
| 134 | + >>> from libsemigroups_pybind11 import presentation, Presentation, to |
| 135 | + |
| 136 | + >>> ip = InversePresentation('abc') |
| 137 | + >>> ip.inverses('cba') |
| 138 | + <inverse semigroup presentation with 3 letters, 0 rules, and length 0> |
| 139 | + >>> presentation.add_rule(ip, 'aaa', 'b') |
| 140 | + >>> presentation.add_rule(ip, 'bac', 'cab') |
| 141 | + |
| 142 | + >>> ip == to(ip, Return=(InversePresentation, str)) |
| 143 | + True |
| 144 | + |
| 145 | + >>> iq = to(ip, Return=(InversePresentation, List[int])) |
| 146 | + >>> iq.alphabet() |
| 147 | + [0, 1, 2] |
| 148 | + >>> iq.inverses() |
| 149 | + [2, 1, 0] |
| 150 | + >>> iq.rules |
| 151 | + [[0, 0, 0], [1], [1, 0, 2], [2, 0, 1]] |
| 152 | + |
| 153 | +.. _inverse-presentation-and-function-to-inverse-presentation: |
| 154 | + |
| 155 | +Converting an :any:`InversePresentationStrings` to n :any:`InversePresentationStrings` with a function |
| 156 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 157 | + |
| 158 | +To construct a :any:`InversePresentationStrings` from a |
| 159 | +:any:`InversePresentationStrings` using a custom letter conversion function, |
| 160 | +specify the following values for *args*: |
| 161 | + |
| 162 | + * **ip** (:any:`InversePresentationStrings`) -- the |
| 163 | + :any:`InversePresentationStrings` to convert. |
| 164 | + * **f** (``Callable[[str | int], int | str]``) -- the function used to |
| 165 | + convert between the different types of letters. |
| 166 | + |
| 167 | +Additionally, specify one of the following for *Return*: |
| 168 | + |
| 169 | + * ``(InversePresentation, str)`` for constructing an |
| 170 | + :any:`InversePresentationStrings` over words of type ``str``. |
| 171 | + * ``(InversePresentation, List[int])`` for constructing a |
| 172 | + :any:`InversePresentationStrings` over words of type ``List[int]``. |
| 173 | + |
| 174 | +This function returns an :any:`InversePresentationStrings` equivalent to the |
| 175 | +input :any:`InversePresentationStrings` *ip* but over words with letters of a |
| 176 | +different type (for example, can be used to convert from :any:`str` to |
| 177 | +:any:`int`). The second parameter *f* specifies how to map the letters of one |
| 178 | +:any:`InversePresentationStrings` to the other. |
| 179 | + |
| 180 | +This function throws a :any:`LibsemigroupsError` if ``ip.validate()`` throws, or |
| 181 | +if the function specified by *f* does not map letters of the type used in *ip* |
| 182 | +to letters of the type of word specified in *Return*. |
| 183 | + |
| 184 | +.. seealso:: |
| 185 | + |
| 186 | + * :any:`InversePresentationStrings.validate`. |
| 187 | + |
| 188 | +.. doctest:: Python |
| 189 | + |
| 190 | + >>> from libsemigroups_pybind11 import ( |
| 191 | + ... InversePresentation, |
| 192 | + ... presentation, |
| 193 | + ... Presentation, |
| 194 | + ... to, |
| 195 | + ... ) |
| 196 | + |
| 197 | + >>> ip = InversePresentation('abc') |
| 198 | + >>> ip.inverses('cba') |
| 199 | + <inverse semigroup presentation with 3 letters, 0 rules, and length 0> |
| 200 | + >>> presentation.add_rule(ip, 'aaa', 'b') |
| 201 | + >>> presentation.add_rule(ip, 'bac', 'cab') |
| 202 | + |
| 203 | + >>> iq = to( |
| 204 | + ... ip, # ip |
| 205 | + ... lambda x: chr(ord(x) + 11), # f |
| 206 | + ... Return=(InversePresentation, str) |
| 207 | + ... ) |
| 208 | + >>> iq.alphabet() |
| 209 | + 'lmn' |
| 210 | + >>> iq.inverses() |
| 211 | + 'nml' |
| 212 | + >>> iq.rules |
| 213 | + ['lll', 'm', 'mln', 'nlm'] |
0 commit comments