-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpresent.cpp
More file actions
1359 lines (1101 loc) · 46 KB
/
present.cpp
File metadata and controls
1359 lines (1101 loc) · 46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// libsemigroups_pybind11
// Copyright (C) 2024 Joseph Edwards, James D. Mitchell
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// C std headers....
#include <ctype.h> // for isprint
#include <stddef.h> // for size_t
#include <stdint.h> // for int32_t, uint32_t
// C++ stl headers....
#include <algorithm> // for for_each, none_of, search
#include <cmath> // for pow
#include <iterator> // for distance
#include <string> // for string, basic_string, oper...
#include <unordered_set> // for operator!=, operator==
#include <utility> // for move, swap
#include <vector> // for vector
// libsemigroups....
#include <libsemigroups/constants.hpp> // for operator==, UNDEFINED
#include <libsemigroups/exception.hpp> // for LibsemigroupsException
#include <libsemigroups/order.hpp> // for shortlex_compare
#include <libsemigroups/presentation.hpp> // for Presentation
#include <libsemigroups/ranges.hpp> // for is_sorted
#include <libsemigroups/types.hpp> // for word_type
// pybind11....
#include <pybind11/cast.h> // for arg
#include <pybind11/detail/common.h> // for const_, overload_cast, ove...
#include <pybind11/detail/descr.h> // for operator+
#include <pybind11/functional.h> // for std::function conversion
#include <pybind11/pybind11.h> // for class_, init, module
#include <pybind11/pytypes.h> // for sequence, str_attr_accessor
#include <pybind11/stl.h> // for std::vector conversion
// libsemigroups_pybind11....
#include "main.hpp" // for init_present
namespace py = pybind11;
namespace libsemigroups {
namespace {
template <typename Word>
void bind_present(py::module& m, std::string const& name) {
using Presentation_ = Presentation<Word>;
using size_type = typename Presentation_::size_type;
py::class_<Presentation_> thing(m,
name.c_str(),
R"pbdoc(
For an implementation of presentations for semigroups or monoids.
This class can be used to construction presentations for semigroups or monoids
and is intended to be used as the input to other algorithms in
``libsemigroups_pybind11``. The idea is to provide a shallow wrapper around a
collection of words of type :ref:`Word<pseudo_word_type_class>`. We refer to this vector of words as the rules
of the presentation. The :any:`PresentationStrings` class also provides some checks
that the rules really define a presentation, (i.e. it's consistent with its
alphabet), and some related functionality is available in the module
:any:`libsemigroups_pybind11.presentation`.)pbdoc");
thing.def("__repr__", [](Presentation_ const& p) -> std::string {
return to_human_readable_repr(p);
});
thing.def("__eq__",
[](Presentation_ const& lhop, Presentation_ rhop) -> bool {
return lhop == rhop;
});
thing.def_readwrite("rules",
&Presentation_::rules,
R"pbdoc(
Data member holding the rules of the presentation.
The rules can be altered using the member functions of ``list``, and the
presentation can be checked for validity using :any:`throw_if_bad_alphabet_or_rules`.)pbdoc");
thing.def(py::init<>(), R"pbdoc(
Default constructor.
Constructs an empty presentation with no rules and no alphabet.)pbdoc");
thing.def(
"__copy__",
[](const Presentation_& that) { return Presentation_(that); },
R"pbdoc(
Default copy constructor.
Default copy constructor)pbdoc");
thing.def(
"alphabet",
[](Presentation_ const& self) { return self.alphabet(); },
R"pbdoc(
:sig=(self: PresentationStrings) -> Word:
Return the alphabet of the presentation.
:returns: The alphabet of the presentation.
:rtype: :ref:`Word<pseudo_word_type_class>`
:complexity: Constant.
)pbdoc");
thing.def(
"alphabet",
[](Presentation_& self, size_type n) { return self.alphabet(n); },
py::arg("n"),
R"pbdoc(
:sig=(self: PresentationStrings, int: n) -> PresentationStrings:
Set the alphabet by size.
Sets the alphabet to the the first :math:`n` values with type
:ref:`Letter<pseudo_letter_type_class>`. For :any:`str`-types, we assume the
order of letters to be a-zA-Z0-9.
:param n: the size of the alphabet.
:type n: int
:returns: ``self``
:rtype: PresentationStrings
:raises LibsemigroupsError: if the value of ``n`` is greater than the
maximum number of letters supported by :ref:`Letter<pseudo_letter_type_class>`.
.. seealso::
* :any:`throw_if_alphabet_has_duplicates`
* :any:`throw_if_bad_rules`
* :any:`throw_if_bad_alphabet_or_rules`
)pbdoc");
thing.def(
"alphabet",
[](Presentation_& self,
typename Presentation_::word_type const& lphbt) {
return self.alphabet(lphbt);
},
py::arg("lphbt"),
R"pbdoc(
:sig=(self: PresentationStrings, lphbt: Word) -> PresentationStrings:
Set the alphabet.
Sets the alphabet to be the letters in *lphbt*.
:param lphbt: the alphabet.
:type lphbt: :ref:`Word<pseudo_word_type_class>`
:returns: ``self``
:rtype: PresentationStrings
:raises LibsemigroupsError: if there are duplicate letters in *lphbt*.
.. seealso::
* :any:`throw_if_bad_rules`
* :any:throw_if_bad_alphabet_or_rules
)pbdoc");
thing.def("alphabet_from_rules",
&Presentation_::alphabet_from_rules,
R"pbdoc(
Set the alphabet to be the letters in the rules.
:returns: ``self``
:rtype: PresentationStrings
:complexity: At most :math:`O(mn)` where :math:`m` is the number of rules,
and :math:`n` is the length of the longest rule.
.. seealso::
* :any:`throw_if_bad_rules`
* :any:throw_if_bad_alphabet_or_rules
)pbdoc");
thing.def(
"contains_empty_word",
[](Presentation_ const& self) { return self.contains_empty_word(); },
R"pbdoc(
Return whether the empty word is a valid relation word.
Returns ``True`` if the empty word is a valid relation word, and ``False`` if
the empty word is not a valid relation word.
If the presentation is not allowed to contain the empty word (according
to this function), the presentation may still be isomorphic to a monoid,
but is not given as a quotient of a free monoid.
:returns: whether the presentation can contain the empty word.
:rtype: bool
:complexity: Constant.
)pbdoc");
thing.def(
"contains_empty_word",
[](Presentation_& self, bool val) {
return self.contains_empty_word(val);
},
py::arg("val"),
R"pbdoc(
Set whether whether the empty word is a valid relation word.
Specify whether the empty word should be a valid relation word (corresponding
to *val* being ``True``), or not (corresponding to *val* being ``False``).
If the presentation is not allowed to contain the empty word (according to
the value specified here), the presentation may still be isomorphic to a
monoid, but is not given as a quotient of a free monoid.
:param val: whether the presentation can contain the empty word.
:type val: bool
:returns: ``self``
:rtype: PresentationStrings
:complexity: Constant
)pbdoc");
thing.def("in_alphabet",
&Presentation_::in_alphabet,
py::arg("val"),
R"pbdoc(
:sig=(self: PresentationStrings, val: Letter) -> bool:
Check if a letter belongs to the alphabet or not.
:param val: the letter to check.
:type val: :ref:`Letter<pseudo_letter_type_class>`
:returns: whether the letter belongs to the alphabet
:rtype: bool
:complexity: Constant on average, worst case linear in the size of the
alphabet.
)pbdoc");
thing.def("index",
&Presentation_::index,
py::arg("val"),
R"pbdoc(
Return the index of a letter in the alphabet.
After checking that *val* is in the the alphabet, get the index of a letter in
the alphabet
:param val: the letter.
:type val: :ref:`Letter<pseudo_letter_type_class>`
:returns: the index.
:rtype: int
:raises LibsemigroupsError: if *val* does not belong to the alphabet.
:complexity: Constant.
)pbdoc");
thing.def("init",
&Presentation_::init,
R"pbdoc(
Remove the alphabet and all rules.
This function clears the alphabet and all rules from the presentation,
putting it back into the state it would be in if it was newly constructed.
:returns: ``self``.
:rtype: PresentationStrings
)pbdoc");
thing.def("letter",
&Presentation_::letter,
py::arg("i"),
R"pbdoc(
:sig=(self: PresentationStrings, i: int) -> Letter:
Get a letter in the alphabet by index.
After checking that *i* is in the range :math:`[0, n)`, where :math:`n` is
the length of the alphabet, this function returns the letter of the alphabet in
position *i*.
:param i: the index.
:type i: int
:returns: the letter
:rtype: :ref:`Letter<pseudo_letter_type_class>`
:raises LibsemigroupsError: if *i* is not in the range :math:`[0, n)`.)pbdoc");
thing.def("throw_if_bad_alphabet_or_rules",
&Presentation_::throw_if_bad_alphabet_or_rules,
R"pbdoc(
Check if the alphabet and rules are valid.
:raises LibsemigroupsError: if :any:`throw_if_alphabet_has_duplicates` or
:any:`throw_if_bad_rules` does.
:complexity: Worst case :math:`O(mnp)` where :math:`m` is the length of
length of the word, :math:`n` is the size of the alphabet and :math:`p` is
the number of rules.)pbdoc");
thing.def(
"throw_if_alphabet_has_duplicates",
[](Presentation_ const& self) {
return self.throw_if_alphabet_has_duplicates();
},
R"pbdoc(
Check if the alphabet is valid.
:raises LibsemigroupsError: if there are duplicate letters in the alphabet.
:complexity: Linear in the length of the alphabet.)pbdoc");
thing.def(
"throw_if_letter_not_in_alphabet",
[](Presentation_ const& self, typename Word::value_type c) {
return self.throw_if_letter_not_in_alphabet(c);
},
py::arg("c"),
R"pbdoc(
:sig=(self: PresentationStrings, c: Letter) -> None:
Check if a letter belongs to the alphabet or not.
:param c: the letter to check.
:type c: :ref:`Letter<pseudo_letter_type_class>`
:raises LibsemigroupsError: if ``c`` does not belong to the alphabet.
:complexity: Constant on average, worst case linear in the size of the
alphabet.)pbdoc");
thing.def(
"throw_if_letter_not_in_alphabet",
[](Presentation_ const& self, Word const& w) {
self.throw_if_letter_not_in_alphabet(w.begin(), w.end());
},
py::arg("w"),
R"pbdoc(
:sig=(self: PresentationStrings, w: Word) -> None:
Check if every letter in a word belongs to the alphabet or not.
:param w: the word to check.
:type c: :ref:`Word<pseudo_word_type_class>`
:raises LibsemigroupsError: if any letter in *w* does not belong to the alphabet.
)pbdoc");
thing.def("throw_if_bad_rules",
&Presentation_::throw_if_bad_rules,
R"pbdoc(
Check if every rule consists of letters belonging to the alphabet.
:raises LibsemigroupsError: if any word contains a letter not in the
alphabet.
:complexity: Worst case :math:`O(mnt)` where :math:`m` is the length of the
longest word, :math:`n` is the size of the alphabet and :math:`t` is the
number of rules.)pbdoc");
thing.def(
"add_generator",
[](Presentation_& self) { return self.add_generator(); },
R"pbdoc(
:sig=(self: PresentationStrings) -> Letter:
Add a generator.
Add the first letter not in the alphabet as a generator, and return this letter.
:returns: the letter added to the alphabet.
:rtype: :ref:`Letter<pseudo_letter_type_class>`
)pbdoc");
thing.def(
"add_generator",
[](Presentation_& self, typename Presentation_::letter_type x) {
self.add_generator(x);
},
py::arg("x"),
R"pbdoc(
:sig=(self: PresentationStrings, x: Letter) -> Letter:
Add the letter *x* as a generator.
:param x: the letter to add as a generator.
:type x: :ref:`Letter<pseudo_letter_type_class>`
:raises LibsemigroupsError: if *x* is in ``alphabet()``.)pbdoc");
thing.def(
"remove_generator",
[](Presentation_& self, typename Presentation_::letter_type x) {
self.remove_generator(x);
},
py::arg("x"),
R"pbdoc(
:sig=(self: PresentationStrings, x: Letter):
Remove the letter *x* as a generator.
:param x: the letter to remove as a generator.
:type x: :ref:`Letter<pseudo_letter_type_class>`
:raises LibsemigroupsError: if *x* is not in `p.alphabet()`.
:complexity: Average case: linear in the length of the alphabet, worst case:
quadratic in the length of the alphabet.
)pbdoc");
m.def("add_identity_rules",
&presentation::add_identity_rules<Word>,
py::arg("p"),
py::arg("e"),
R"pbdoc(
Add rules for an identity element.
Adds rules of the form :math:`ae = ea = a` for every letter :math:`a` in the
alphabet of *p*, and where :math:`e` is the second parameter.
:param p: the presentation to add rules to.
:type p: PresentationStrings
:param e: the identity element.
:type e: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError: if *e* is not a letter in ``p.alphabet()``.
:complexity: Linear in the number of rules.)pbdoc");
m.def(
"add_inverse_rules",
[](Presentation_& p,
Word const& vals,
typename Presentation_::letter_type e = UNDEFINED) {
presentation::add_inverse_rules(p, vals, e);
},
py::arg("p"),
py::arg("vals"),
py::arg("e"),
R"pbdoc(
:sig=(p: PresentationStrings, vals: Word, e: Letter = UNDEFINED)->None:
:only-document-once:
Add rules for inverses.
The letter *a* with index ``i`` in *vals* is the inverse of the letter in
``alphabet()`` with index ``i``. The rules added are :math:`a_ib_i = e` where
the alphabet is :math:`\{a_1, \ldots, a_n\}` ; the 2nd parameter *vals* is
:math:`\{b_1, \ldots, b_n\}` ; and :math:`e` is the 3rd parameter.
:param p: the presentation to add rules to.
:type p: PresentationStrings
:param vals: the inverses.
:type vals: :ref:`Word<pseudo_word_type_helper>`
:param e: the identity element (defaults to :any:`UNDEFINED`, meaning use the empty word).
:type e: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError: if any of the following apply:
* the length of *vals* is not equal to ``alphabet().size()`` ;
* the letters in *vals* are not exactly those in ``alphabet()`` (perhaps in a different order);
* :math:`(a_i ^ {-1}) ^ {-1} = a_i` does not hold for some :math:`i` ;
* :math:`e ^ {-1} = e` does not hold
:complexity: :math:`O(n)` where :math:`n` is ``p.alphabet().size()``.)pbdoc");
m.def(
"add_inverse_rules",
[](Presentation_& p, Word const& vals) {
presentation::add_inverse_rules(p, vals);
},
py::arg("p"),
py::arg("vals"));
m.def(
"add_rule",
[](Presentation_& p, Word const& lhop, Word const& rhop) {
presentation::add_rule(p, lhop, rhop);
},
py::arg("p"),
py::arg("lhop"),
py::arg("rhop"),
R"pbdoc(
:sig=(p: PresentationStrings, lhop: Word, rhop: Word)->None:
:only-document-once:
Add a rule to the presentation by reference and check.
Adds the rule with left-hand side *lhop* and right-hand side *rhop* to the
rules, after checking that *lhop* and *rhop* consist entirely of letters in the
alphabet of *p*.
:param p: the presentation.
:type p: PresentationStrings
:param lhop: the left-hand side of the rule.
:type lhop: :ref:`Word<pseudo_word_type_helper>`
:param rhop: the right-hand side of the rule.
:type rhop: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError: if *lhop* or *rhop* contains any letters not
belonging to ``p.alphabet()``.)pbdoc");
m.def(
"add_rules",
[](Presentation_& p, Presentation_ const& q) {
presentation::add_rules(p, q);
},
R"pbdoc(
:sig=(p: PresentationStrings, q: PresentationStrings)->None:
:only-document-once:
Add the rules of *q* to *p*.
Before it is added, each rule is validated to check it contains only letters of
the alphabet of *p*. If the :math:`n`th rule causes this function to throw, the
first :math:`n-1` rules will still be added to *p*.
:param p: the presentation to add rules to.
:type p: Presentation
:param q: the presentation to add words from.
:type first: Presentation
:raises LibsemigroupsError:
if any rule contains any letters not belonging to
``p.alphabet()``.)pbdoc");
m.def("add_zero_rules",
&presentation::add_zero_rules<Word>,
py::arg("p"),
py::arg("z"),
R"pbdoc(
:sig=(p: PresentationStrings, z: Letter)->None:
:only-document-once:
Add rules for a zero element.
Adds rules of the form :math:`az = za = z` for every letter :math:`a` in the
alphabet of *p*, and where :math:`z` is the second parameter.
:param p: the presentation to add rules to.
:type p: PresentationStrings
:param z: the zero element.
:type z: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError: if *z* is not a letter in ``p.alphabet()``.
:complexity: Linear in the number of rules.)pbdoc");
m.def(
"are_rules_sorted",
[](Presentation_ const& p) {
return presentation::are_rules_sorted(p);
},
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->bool:
:only-document-once:
Check the rules are sorted relative to shortlex.
Check if the rules :math:`u_1 = v_1, \ldots, u_n = v_n` satisfy
:math:`u_1v_1 < \cdots < u_nv_n` where :math:`<` is shortlex order.
:param p: the presentation to check.
:type p: PresentationStrings
:returns: whether the rules are sorted.
:rtype: bool
:raises LibsemigroupsError: if ``p.rules.size()`` is odd.
.. seealso::
* :any:`sort_rules`.
)pbdoc");
m.def(
"change_alphabet",
[](Presentation_& p, Word const& new_alphabet) {
presentation::change_alphabet(p, new_alphabet);
},
py::arg("p"),
py::arg("new_alphabet"),
R"pbdoc(
:sig=(p: PresentationStrings, new_alphabet: Word)->None:
:only-document-once:
Change or re-order the alphabet.
This function replaces ``p.alphabet()`` with ``new_alphabet``, where possible,
and re-writes the rules in the presentation using the new alphabet.
:param p: the presentation .
:type p: PresentationStrings
:param new_alphabet: the replacement alphabet.
:type new_alphabet: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError: if the size of ``p.alphabet()`` and
``new_alphabet`` do not agree.)pbdoc");
m.def("contains_rule",
&presentation::contains_rule<Word>,
py::arg("p"),
py::arg("lhs"),
py::arg("rhs"),
R"pbdoc(
:sig=(p: PresentationStrings, lhs: Word, rhs: Word)->bool:
:only-document-once:
Check if a presentation contains a rule.
Checks if the rule with left-hand side *lhs* and right-hand side *rhs* is
contained in *p*.
:param p: the presentation.
:type p: PresentationStrings
:param lhs: the left-hand side of the rule.
:type lhs: :ref:`Word<pseudo_word_type_helper>`
:param rhs: the right-hand side of the rule.
:type rhs: :ref:`Word<pseudo_word_type_helper>`
:returns: whether the presentation contains the rule
:rtype: bool
:complexity: Linear in the number of rules.
)pbdoc");
m.def("first_unused_letter",
&presentation::first_unused_letter<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->Letter:
:only-document-once:
Return the first letter not in the alphabet of a presentation.
This function returns ``letter(p, i)`` when ``i`` is the least possible value
such that ``!p.in_alphabet(letter(p, i))`` if such a letter exists.
:param p: the presentation.
:type p: PresentationStrings
:returns: the letter.
:rtype: :ref:`Letter<pseudo_word_type_helper>`
:raises LibsemigroupsError: if *p* already has an alphabet of the maximum
possible size.
)pbdoc");
m.def("greedy_reduce_length",
&presentation::greedy_reduce_length<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
Greedily reduce the length of the presentation using
:any:`longest_subword_reducing_length`.
This function repeatedly calls :any:`longest_subword_reducing_length` and
:any:`replace_subword` to introduce a new generator and reduce the length of the
presentation *p* until :any:`longest_subword_reducing_length` returns the empty
word.
:param p: the presentation.
:type p: PresentationStrings
:raises LibsemigroupsError: if :any:`longest_subword_reducing_length` or
:any:`replace_word` does.)pbdoc");
m.def("greedy_reduce_length_and_number_of_gens",
&presentation::greedy_reduce_length_and_number_of_gens<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
Greedily reduce the length and number of generators of the presentation.
This function repeatedly calls `longest_subword_reducing_length` and
`replace_subword` to introduce a new generator to try to reduce the
length of the presentation \p p and the number of generators. This is
done until either `longest_subword_reducing_length` returns the empty
word, or the new length and number of generators is greater than or
equal to that of the presentation in the previous iteration.
In the latter case, the presentation \p p gets restored to the state it
was in after the previous iteration.
:param p: the presentation.
:type p: PresentationStrings
:raises LibsemigroupsError: if :any:`longest_subword_reducing_length` or
:any`replace_word` does.)pbdoc");
m.def("is_strongly_compressible",
&presentation::is_strongly_compressible<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->bool:
:only-document-once:
Return true if the :math:`1`-relation presentation can be strongly compressed.
A :math:`1` -relation presentation is *strongly compressible* if both relation
words start with the same letter and end with the same letter. In other words,
if the alphabet of the presentation *p* is :math:`A` and the relation words are
of the form :math:`aub = avb` where :math:`a, b\in A` (possibly :math:` a = b` )
and :math:`u, v\in A ^ *`, then *p* is strongly compressible.
See`Section 3.2 <https://doi.org/10.1007/s00233-021-10216-8>`_ for details.
:param p: the presentation.
:type p: PresentationStrings
:returns: whether the presentation is strongly compressible
:rtype: bool
.. seealso::
* :any:`strongly_compress`
)pbdoc");
m.def(
"length",
[](Presentation_ const& p) { return presentation::length(p); },
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->int:
:only-document-once:
Return the sum of the lengths of the rules.
:param p: the presentation.
:type p: PresentationStrings
:returns: the length of the presentation
:rtype: int
)pbdoc");
m.def(
"longest_rule",
[](Presentation_ const& p) {
if (p.rules.empty()) {
LIBSEMIGROUPS_EXCEPTION("expected non-zero number of rules.");
}
return std::distance(p.rules.cbegin(),
presentation::longest_rule(p));
},
R"pbdoc(
:sig=(p: PresentationStrings)->int:
:only-document-once:
Return the index of the left-hand side of the longest rule.
Returns the index of the left-hand side of the first rule in the presentation
with maximal length. The *length* of a rule is defined to be the sum of the
lengths of its left-hand and right-hand sides.
:param p: the presentation.
:type p: PresentationStrings
:returns: the index of the rule
:rtype: int
:raises LibsemigroupsError: if the length of ``p.rules`` is odd.
)pbdoc");
m.def(
"longest_rule_length",
[](Presentation_ const& p) {
if (p.rules.empty()) {
LIBSEMIGROUPS_EXCEPTION("expected non-zero number of rules.");
}
return presentation::longest_rule_length(p);
},
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->int:
:only-document-once:
Return the maximum length of a rule in the presentation.
Returns the maximum length of a rule in the presentation. The *length* of a rule
is defined to be the sum of the lengths of its left-hand and right-hand sides.
:param p: the presentation.
:type p: PresentationStrings
:returns: the maximum length
:rtype: int
:raises LibsemigroupsError: if the length of ``p.rules`` is odd.
)pbdoc");
m.def("longest_subword_reducing_length",
&presentation::longest_subword_reducing_length<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->Word:
:only-document-once:
Return the longest common subword of the rules.
If it is possible to find a subword :math:`w` of the rules :math:`u_1 = v_1,
\ldots, u_n = v_n` such that the introduction of a new generator :math:`z` and
the relation :math:`z = w` reduces the :any`presentation.length` of the
presentation, then this function returns the longest such word :math:`w`.
If no such word can be found, then a word of length :math:`0` is returned.
:param p: the presentation.
:type p: PresentationStrings
:returns: the longest common subword, if it exists.
:rtype: :ref:`Word<pseudo_word_type_helper>`.
)pbdoc");
m.def("make_semigroup",
&presentation::make_semigroup<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->Word:
:only-document-once:
Convert a monoid presentation to a semigroup presentation.
This function modifies its argument in-place by replacing the empty word in all
relations by a new generator, and the identity rules for that new generator.
If ``p.contains_empty_word()`` is ``False``, then the presentation is not
modified and :any:`UNDEFINED` is returned. If a new generator is added as the
identity, then this generator is returned.
:param p: the presentation.
:type p: PresentationStrings
:returns: The new generator added, if any, and :any:`UNDEFINED` if not.
:rtype: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError: if :any:`replace_word` or
:any:`add_identity_rules` does.
)pbdoc");
m.def("normalize_alphabet",
&presentation::normalize_alphabet<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
Normalize the alphabet to :math:`\{0, \ldots, n - 1\}`.
Modify the presentation in-place so that the alphabet is :math:`\{0, \ldots,
n - 1\}` (or equivalent) and rewrites the rules to use this alphabet. If the
alphabet is already normalized, then no changes are made to the presentation.
:param p: the presentation.
:type p: PresentationStrings
:raises LibsemigroupsError:
if :any:`PresentationStrings.throw_if_bad_alphabet_or_rules` throws on the initial
presentation.)pbdoc");
m.def("reduce_complements",
&presentation::reduce_complements<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
If there are rules :math:`u = v` and :math:`v = w` where :math:`|w| < |v|`,
then replace :math:`u = v` by :math:`u = w`.
Attempts to reduce the length of the words by finding the equivalence relation
on the relation words generated by the pairs of identical relation words. If
:math:`\{u_1, u_2, \ldots, u_n\}` are the distinct words in an equivalence class
and :math:`u_1` is the short-lex minimum word in the class, then the relation
words are replaced by :math:`u_1 = u_2, u_1 = u_3, \cdots, u_1 = u_n`.
The rules may be reordered by this function even if there are no reductions
found.
:param p: the presentation to add rules to.
:type p: PresentationStrings
:raises LibsemigroupsError: if ``p.rules.size()`` is odd.)pbdoc");
m.def("reduce_to_2_generators",
&presentation::reduce_to_2_generators<Word>,
py::arg("p"),
py::arg("index") = 0,
R"pbdoc(
:sig=(p: PresentationStrings, index: int)->bool:
:only-document-once:
Reduce the number of generators in a :math:`1`-relation presentation to 2.
Reduce the number of generators in a :math:`1` -relation presentation to ``2``.
Returns ``True`` if the :math:`1` -relation presentation *p* has been modified
and ``False`` if not. A :math:`1` -relation presentation is *left cycle-free* if
the relation words start with distinct letters. In other words, if the alphabet
of the presentation *p* is :math:`A` and the relation words are of the form
:math:`au = bv` where :math:`a, b\in A` with :math:`a \neq b` and
:math:`u, v \in A ^ *`, then *p* is left cycle-free.
The word problem for a left cycle-free :math:`1` -relation monoid is solvable if
the word problem for the modified version obtained from this function is
solvable.
:param p: the presentation.
:type p: PresentationStrings
:param index: determines the choice of letter to use, 0 uses p.rules[0].front() and 1 uses p.rules[1].front() (defaults to: 0).
:type index: int
:returns: whether the presentation has been modified
:rtype: bool
:raises LibsemigroupsError: if *index* is not ``0`` or ``1``.
)pbdoc");
m.def("remove_duplicate_rules",
&presentation::remove_duplicate_rules<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
Remove duplicate rules.
Removes all but one instance of any duplicate rules (if any). Note that rules of
the form :math:`u = v` and :math:`v = u` (if any) are considered duplicates.
Also note that the rules may be reordered by this function even if there are no
duplicate rules.
:param p: the presentation.
:type p: PresentationStrings
:raises LibsemigroupsError: if ``p.rules.size()`` is odd.
:complexity: Linear in the number of rules.)pbdoc");
m.def("remove_redundant_generators",
&presentation::remove_redundant_generators<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
Remove any trivially redundant generators.
If one side of any of the rules in the presentation *p* is a letter ``a`` and
the other side of the rule does not contain ``a``, then this function replaces
every occurrence of ``a`` in every rule by the other side of the rule. This
substitution is performed for every such rule in the presentation; and the
trivial rules (with both sides being identical) are removed. If both sides of a
rule are letters, then the greater letter is replaced by the lesser one.
:param p: the presentation.
:type p: PresentationStrings
:raises LibsemigroupsError: if ``p.rules.size()`` is odd.)pbdoc");
m.def("remove_trivial_rules",
&presentation::remove_trivial_rules<Word>,
py::arg("p"),
R"pbdoc(
:sig=(p: PresentationStrings)->None:
:only-document-once:
Remove rules consisting of identical words.
Removes all instance of rules (if any) where the left-hand side and the
right-hand side are identical.
:param p: the presentation.
:type p: PresentationStrings
:raises LibsemigroupsError: if ``p.rules.size()`` is odd.
:complexity: Linear in the number of rules.)pbdoc");
m.def(
"replace_subword",
[](Presentation_& p, Word const& existing, Word const& replacement) {
return presentation::replace_subword(p, existing, replacement);
},
py::arg("p"),
py::arg("existing"),
py::arg("replacement"),
R"pbdoc(
:sig=(p: PresentationStrings, existing: Word, replacement: Word)->None:
:only-document-once:
Replace non-overlapping instances of a subword by another word.
If *existing* and *replacement* are words, then this function replaces every
non-overlapping instance of *existing* in every rule by *replacement*. The
presentation *p* is changed in-place.
:param p: the presentation .
:type p: PresentationStrings
:param existing: the word to be replaced.
:type existing: :ref:`Word<pseudo_word_type_helper>`
:param replacement: the replacement word.
:type replacement: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError: if *existing* is empty.)pbdoc");
m.def("replace_word",
&presentation::replace_word<Word>,
py::arg("p"),
py::arg("existing"),
py::arg("replacement"),
R"pbdoc(
:sig=(p: PresentationStrings, existing: Word, replacement: Word)->None:
:only-document-once:
Replace instances of a word on either side of a rule by another word.
If *existing* and *replacement* are words, then this function replaces every
instance of *existing* in every rule of the form *existing* :math:`= w` or :math:`w =`
*existing*, with the word *replacement*. The presentation *p* is changed in-place.
:param p: the presentation.
:type p: PresentationStrings
:param existing: the word to be replaced.
:type existing: :ref:`Word<pseudo_word_type_helper>`
:param replacement: the replacement word.
:type replacement: :ref:`Word<pseudo_word_type_helper>`
)pbdoc");
m.def(
"replace_word_with_new_generator",
[](Presentation_& p, Word const& w) {
return presentation::replace_word_with_new_generator(p, w);
},
py::arg("p"),
py::arg("w"),
R"pbdoc(
:sig=(p: PresentationStrings, w: Word)->Letter:
:only-document-once:
Replace non-overlapping instances of a word with a new generator via const reference.
This function replaces every non-overlapping instance (from left to right) of