-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpresent.cpp
More file actions
2099 lines (1683 loc) · 69.6 KB
/
Copy pathpresent.cpp
File metadata and controls
2099 lines (1683 loc) · 69.6 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 <cstddef> // for size_t
// C++ stl headers....
#include <string> // for string, basic_string, oper...
// 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/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 libsemigroups {
namespace py = pybind11;
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(
Presentations for semigroups and monoids.
This class can be used to construct 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 list of words as the *rules* of the presentation. The :any:`Presentation`
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(
:sig=(self: Presentation) -> None:
Default constructor.
Constructs an empty presentation with no rules and no alphabet.)pbdoc");
thing.def(
"copy",
[](Presentation_ const& self) { return Presentation_(self); },
R"pbdoc(
:sig=(self: Presentation) -> Presentation:
Copy a :any:`Presentation` object.
:returns: A copy.
:rtype: Presentation
)pbdoc");
thing.def("__copy__",
[](Presentation_ const& that) { return Presentation_(that); });
thing.def(
"alphabet",
[](Presentation_ const& self) { return self.alphabet(); },
R"pbdoc(
:sig=(self: Presentation) -> 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) -> Presentation_& {
return self.alphabet(n);
},
py::arg("n"),
R"pbdoc(
:sig=(self: Presentation, n: int) -> Presentation:
Set the alphabet by size.
Sets the alphabet to 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: Presentation
: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) -> Presentation_& {
return self.alphabet(lphbt);
},
py::arg("lphbt"),
R"pbdoc(
:sig=(self: Presentation, lphbt: Word) -> Presentation:
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: Presentation
: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(
:sig=(self: Presentation) -> Presentation:
Set the alphabet to be the letters in the rules.
:returns: *self*.
:rtype: Presentation
: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(
:sig=(self: Presentation) -> bool:
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) -> Presentation_& {
return self.contains_empty_word(val);
},
py::arg("val"),
R"pbdoc(
:sig=(self: Presentation, val: bool) -> Presentation:
Set 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: Presentation
:complexity: Constant.
)pbdoc");
thing.def("in_alphabet",
&Presentation_::in_alphabet,
py::arg("val"),
R"pbdoc(
:sig=(self: Presentation, 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(
:sig=(self: Presentation, val: Letter) -> int:
Return the index of a letter in the alphabet.
After checking that *val* is in 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(
:sig=(self: Presentation) -> Presentation:
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: Presentation
)pbdoc");
thing.def("letter",
&Presentation_::letter,
py::arg("i"),
R"pbdoc(
:sig=(self: Presentation, 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(
:sig=(self: Presentation) -> None:
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 the longest
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(
:sig=(self: Presentation) -> None:
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: Presentation, 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: Presentation, w: Word) -> None:
Check if every letter in a word belongs to the alphabet or not.
:param w: the word to check.
:type w: :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(
:sig=(self: Presentation) -> None:
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: Presentation) -> 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)
-> Presentation_& { return self.add_generator(x); },
py::arg("x"),
R"pbdoc(
:sig=(self: Presentation, x: Letter) -> Presentation:
Add the letter *x* as a generator.
:param x: the letter to add as a generator.
:type x: :ref:`Letter<pseudo_letter_type_class>`
:returns: *self*.
:rtype: Presentation
:raises LibsemigroupsError: if *x* is in ``alphabet()``.)pbdoc");
thing.def(
"remove_generator",
[](Presentation_& self, typename Presentation_::letter_type x)
-> Presentation_& { return self.remove_generator(x); },
py::arg("x"),
R"pbdoc(
:sig=(self: Presentation, x: Letter) -> Presentation:
Remove the letter *x* as a generator.
:param x: the letter to remove as a generator.
:type x: :ref:`Letter<pseudo_letter_type_class>`
:returns: *self*.
:rtype: Presentation
: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");
////////////////////////////////////////////////////////////////////////
// Helper functions
////////////////////////////////////////////////////////////////////////
m.def("presentation_add_identity_rules",
&presentation::add_identity_rules<Word>,
py::arg("p"),
py::arg("e"),
R"pbdoc(
:sig=(p: Presentation, e: Letter) -> None:
:only-document-once:
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: Presentation
: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(
"presentation_add_inverse_rules",
[](Presentation_& p,
Word const& vals,
typename Presentation_::letter_type e) {
presentation::add_inverse_rules(p, vals, e);
},
py::arg("p"),
py::arg("vals"),
py::arg("e")
= static_cast<typename Presentation_::letter_type>(UNDEFINED),
R"pbdoc(
:sig=(p: Presentation, 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: Presentation
: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 the length of *vals* is not equal to ``alphabet().size()``.
:raises LibsemigroupsError:
if the letters in *vals* are not exactly those in ``alphabet()`` (perhaps in a different order).
:raises LibsemigroupsError:
if :math:`(a_i ^ {-1}) ^ {-1} = a_i` does not hold for some :math:`i`.
:raises LibsemigroupsError:
if :math:`e ^ {-1} = e` does not hold.
:complexity: :math:`O(n)` where :math:`n` is ``p.alphabet().size()``.)pbdoc");
m.def("presentation_add_involution_rules",
&presentation::add_involution_rules<Word>,
py::arg("p"),
py::arg("letters"),
R"pbdoc(
:sig=(p: Presentation, letters: Word) -> None:
:only-document-once:
Add rules that define involutions.
Adds rules to *p* of the form :math:`a^2 = \varepsilon` for every letter
:math:`a` in *letters*.
:param p: the presentation to add rules to.
:type p: Presentation
:param letters: the letters to add involution rules for.
:type letters: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError:
if any letter in *letters* is not in ``p.alphabet()``, or if
``p.contains_empty_word()`` returns ``False``.
)pbdoc");
m.def(
"presentation_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: Presentation, lhop: Word, rhop: Word) -> None:
:only-document-once:
Check and add a rule to the presentation.
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: Presentation
: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(
"presentation_add_rules",
[](Presentation_& p, Presentation_ const& q) {
presentation::add_rules(p, q);
},
R"pbdoc(
:sig=(p: Presentation, q: Presentation) -> 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 q: Presentation
:raises LibsemigroupsError:
if any rule contains any letters not belonging to
``p.alphabet()``.)pbdoc");
m.def("presentation_add_zero_rules",
&presentation::add_zero_rules<Word>,
py::arg("p"),
py::arg("z"),
R"pbdoc(
:sig=(p: Presentation, 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: Presentation
: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(
"presentation_commutator",
[](Presentation<Word> const& p, Word const& x, Word const& y) {
return presentation::commutator(p, x, y);
},
py::arg("p"),
py::arg("x"),
py::arg("y"),
R"pbdoc(
:sig=(p: Presentation, x: Word, y: Word) -> Word:
:only-document-once:
Return the commutator of two words.
Returns the word :math:`x^{-1}y^{-1}xy`, after attempting to detect inverses
from the rules in *p*, using :any:`try_detect_inverses`.
:param p: the presentation.
:type p: Presentation
:param x: the first word in the commutator.
:type x: :ref:`Word<pseudo_word_type_helper>`
:param y: the second word in the commutator.
:type y: :ref:`Word<pseudo_word_type_helper>`
:returns: The commutator :math:`x^{-1}y^{-1}xy`.
:rtype: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError:
if *x* or *y* contains a letter not belonging to ``p.alphabet()``, if
:any:`try_detect_inverses` throws, or if *x* or *y* contains a letter for
which no inverse was detected.
)pbdoc");
m.def(
"presentation_commutator",
[](Presentation<Word> const& p,
Word const& x,
Word const& y,
Word const& inverses) {
return presentation::commutator(p, x, y, inverses);
},
py::arg("p"),
py::arg("x"),
py::arg("y"),
py::arg("inverses"),
R"pbdoc(
:sig=(p: Presentation, x: Word, y: Word, inverses: Word) -> Word:
:only-document-once:
Return the commutator of two words.
Returns the word :math:`x^{-1}y^{-1}xy`. The letter ``a`` with index ``i`` in
*inverses* is the inverse of the letter in ``p.alphabet()`` with index ``i``.
:param p: the presentation.
:type p: Presentation
:param x: the first word in the commutator.
:type x: :ref:`Word<pseudo_word_type_helper>`
:param y: the second word in the commutator.
:type y: :ref:`Word<pseudo_word_type_helper>`
:param inverses: the inverses of the letters in p.alphabet().
:type inverses: :ref:`Word<pseudo_word_type_helper>`
:returns: The commutator :math:`x^{-1}y^{-1}xy`.
:rtype: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError:
if *inverses* are not valid inverses for ``p.alphabet()``, or if *x* or
*y* contains a letter not belonging to ``p.alphabet()``.
)pbdoc");
m.def(
"presentation_commutator",
[](Word const& x,
Word const& y,
Word const& alphabet,
Word const& inverses) {
return presentation::commutator(x, y, alphabet, inverses);
},
py::arg("x"),
py::arg("y"),
py::arg("alphabet"),
py::arg("inverses"),
R"pbdoc(
:sig=(x: Word, y: Word, alphabet: Word, inverses: Word) -> Word:
:only-document-once:
Return the commutator of two words.
Returns the word :math:`x^{-1}y^{-1}xy`. The letter ``a`` with index ``i`` in
*inverses* is the inverse of the letter in *alphabet* with index ``i``.
:param x: the first word in the commutator.
:type x: :ref:`Word<pseudo_word_type_helper>`
:param y: the second word in the commutator.
:type y: :ref:`Word<pseudo_word_type_helper>`
:param alphabet:
the alphabet, which should be a superset of the letters in x and y.
:type alphabet: :ref:`Word<pseudo_word_type_helper>`
:param inverses: the inverses of the letters in alphabet.
:type inverses: :ref:`Word<pseudo_word_type_helper>`
:returns: The commutator :math:`x^{-1}y^{-1}xy`.
:rtype: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError:
if *alphabet* contains duplicates, if *inverses* are not valid inverses
for *alphabet*, or if *x* or *y* contains a letter not belonging to
*alphabet*.
)pbdoc");
m.def(
"presentation_add_commutator_rule",
[](Presentation_& p,
Word const& x,
Word const& y,
Word const& alphabet,
Word const& inverses,
typename Presentation_::letter_type id) {
presentation::add_commutator_rule(p, x, y, alphabet, inverses, id);
},
py::arg("p"),
py::arg("x"),
py::arg("y"),
py::arg("alphabet"),
py::arg("inverses"),
py::kw_only(), // This is so id must be specified by a key-word
py::arg("id")
= static_cast<typename Presentation_::letter_type>(UNDEFINED),
R"pbdoc(
:sig=(p: Presentation, x: Word, y: Word, alphabet: Word, inverses: Word, *, id: Letter = UNDEFINED) -> None:
:only-document-once:
Add a commutator rule.
Adds the rule :math:`x^{-1}y^{-1}xy = id` to *p*. The letter ``a`` with index
``i`` in *inverses* is the inverse of the letter in *alphabet* with index
``i``. If *id* is :any:`UNDEFINED`, then the right-hand side is the empty
word.
:param p: the presentation.
:type p: Presentation
:param x: the first word in the commutator.
:type x: :ref:`Word<pseudo_word_type_helper>`
:param y: the second word in the commutator.
:type y: :ref:`Word<pseudo_word_type_helper>`
:param alphabet:
the alphabet, which should be a superset of the letters in *x* and *y*.
:type alphabet: :ref:`Word<pseudo_word_type_helper>`
:param inverses: the inverses of the letters in alphabet.
:type inverses: :ref:`Word<pseudo_word_type_helper>`
:param id:
the identity letter, or :any:`UNDEFINED` for the empty word. This is a
keyword-only argument.
:type id: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError:
if *alphabet*, *inverses*, *x*, *y*, or *id* contains a letter not
belonging to ``p.alphabet()``; if *alphabet* contains duplicates; if
*inverses* are not valid inverses for *alphabet*; or if *x* or *y*
contains a letter not belonging to *alphabet*.
)pbdoc");
m.def(
"presentation_add_commutator_rule",
[](Presentation_& p,
Word const& x,
Word const& y,
Word const& inverses,
typename Presentation_::letter_type id) {
presentation::add_commutator_rule(p, x, y, inverses, id);
},
py::arg("p"),
py::arg("x"),
py::arg("y"),
py::arg("inverses"),
py::kw_only(), // This is so id must be specified by a key-word
py::arg("id")
= static_cast<typename Presentation_::letter_type>(UNDEFINED),
R"pbdoc(
:sig=(p: Presentation, x: Word, y: Word, inverses: Word, *, id: Letter = UNDEFINED) -> None:
:only-document-once:
Add a commutator rule.
Adds the rule :math:`x^{-1}y^{-1}xy = id` to *p*. The letter ``a`` with index
``i`` in *inverses* is the inverse of the letter in ``p.alphabet()`` with
index ``i``. If *id* is :any:`UNDEFINED`, then the right-hand side is the
empty word.
:param p: the presentation.
:type p: Presentation
:param x: the first word in the commutator.
:type x: :ref:`Word<pseudo_word_type_helper>`
:param y: the second word in the commutator.
:type y: :ref:`Word<pseudo_word_type_helper>`
:param inverses: the inverses of the letters in p.alphabet().
:type inverses: :ref:`Word<pseudo_word_type_helper>`
:param id:
the identity letter, or :any:`UNDEFINED` for the empty word. This is a
keyword-only argument.
:type id: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError:
if *inverses*, *x*, *y*, or *id* contains a letter not belonging to
``p.alphabet()``, or if *inverses* are not valid inverses for
``p.alphabet()``.
)pbdoc");
m.def(
"presentation_add_commutator_rule",
[](Presentation_& p,
Word const& x,
Word const& y,
typename Presentation_::letter_type id) {
presentation::add_commutator_rule(p, x, y, id);
},
py::arg("p"),
py::arg("x"),
py::arg("y"),
py::kw_only(), // This is so id must be specified by a key-word
py::arg("id")
= static_cast<typename Presentation_::letter_type>(UNDEFINED),
R"pbdoc(
:sig=(p: Presentation, x: Word, y: Word, *, id: Letter = UNDEFINED) -> None:
:only-document-once:
Add a commutator rule.
Adds the rule :math:`x^{-1}y^{-1}xy = id` to *p*, after attempting to detect
inverses from the rules in *p*, using :any:`try_detect_inverses`. If
*id* is :any:`UNDEFINED`, then the right-hand side is the empty word.
:param p: the presentation.
:type p: Presentation
:param x: the first word in the commutator.
:type x: :ref:`Word<pseudo_word_type_helper>`
:param y: the second word in the commutator.
:type y: :ref:`Word<pseudo_word_type_helper>`
:param id:
the identity letter, or :any:`UNDEFINED` for the empty word. This is a
keyword-only argument.
:type id: :ref:`Letter<pseudo_letter_type_helper>`
:raises LibsemigroupsError:
if *x*, *y*, or *id* contains a letter not belonging to
``p.alphabet()``, if :any:`try_detect_inverses` throws, or if *x* or
*y* contains a letter for which no inverse was detected.
)pbdoc");
m.def(
"presentation_add_commutes_rules",
[](Presentation_& p, Word const& letters) {
presentation::add_commutes_rules(p, letters);
},
py::arg("p"),
py::arg("letters"),
R"pbdoc(
:sig=(p: Presentation, letters: Word) -> None:
:only-document-once:
Add rules so specific letters commute.
Adds rules to *p* of the form :math:`uv = vu` for every pair of letters
:math:`u, v` in *letters*.
:param p: the presentation to add rules to.
:type p: Presentation
:param letters: the collection of letters to add rules for.
:type letters: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError:
if any letter in *letters* is not in ``p.alphabet()``.
)pbdoc");
m.def(
"presentation_add_commutes_rules",
[](Presentation_& p, Word const& letters1, Word const& letters2) {
presentation::add_commutes_rules(p, letters1, letters2);
},
py::arg("p"),
py::arg("letters1"),
py::arg("letters2"),
R"pbdoc(
:sig=(p: Presentation, letters1: Word, letters2: Word) -> None:
:only-document-once:
Add rules so specific letters commute.
Adds rules to *p* of the form :math:`uv = vu` for every letter :math:`u` in
*letters1* and :math:`v` in *letters2*.
:param p: the presentation to add rules to.
:type p: Presentation
:param letters1: the first collection of letters to add rules for.
:type letters1: :ref:`Word<pseudo_word_type_helper>`
:param letters2: the second collection of letters to add rules for.
:type letters2: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError:
if any letter in *letters1* or *letters2* is not in ``p.alphabet()``.
)pbdoc");
m.def(
"presentation_add_commutes_rules",
[](Presentation_& p, Word const& letters, std::vector<Word> words) {
presentation::add_commutes_rules(p, letters, words);
},
py::arg("p"),
py::arg("letters"),
py::arg("words"),
R"pbdoc(
:sig=(p: Presentation, letters: Word, words: list[Word]) -> None:
:only-document-once:
Add rules so specific letters commute.
Adds rules to *p* of the form :math:`uv = vu` for every letter :math:`u` in
*letters* and word :math:`v` in *words*.
:param p: the presentation to add rules to.
:type p: Presentation
:param letters: the collection of letters to add rules for.
:type letters: :ref:`Word<pseudo_word_type_helper>`
:param words: the collection of words to add rules for.
:type words: list[:ref:`Word<pseudo_word_type_helper>`]
:raises LibsemigroupsError:
if any letter in *letters*, or any letter in any word in *words* is not in
``p.alphabet()``.
)pbdoc");
m.def(
"presentation_are_rules_sorted",
[](Presentation_ const& p) {
return presentation::are_rules_sorted(p);
},
py::arg("p"),
R"pbdoc(
:sig=(p: Presentation) -> 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: Presentation
:returns: Whether the rules are sorted.
:rtype: bool
:raises LibsemigroupsError: if ``p.rules.size()`` is odd.
.. seealso::
* :any:`sort_rules`.
)pbdoc");
m.def(
"presentation_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: Presentation, 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: Presentation
:param new_alphabet: the replacement alphabet.
:type new_alphabet: :ref:`Word<pseudo_word_type_helper>`
:raises LibsemigroupsError: if the size of ``p.alphabet()`` and
``new_alphabet`` do not agree.)pbdoc");
m.def("presentation_contains_rule",
&presentation::contains_rule<Word>,
py::arg("p"),
py::arg("lhs"),
py::arg("rhs"),
R"pbdoc(
:sig=(p: Presentation, 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: Presentation