forked from libsemigroups/libsemigroups_pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschreier-sims.cpp
More file actions
530 lines (400 loc) · 14.6 KB
/
schreier-sims.cpp
File metadata and controls
530 lines (400 loc) · 14.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
//
// libsemigroups_pybind11
// Copyright (C) 2024 Joseph Edwards
//
// 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/>.
//
// TODO(0) Check types
// C++ stl headers....
#include <memory> // for allocator, make_unique, unique_ptr
// libsemigroups headers
#include <libsemigroups/hpcombi.hpp>
#include <libsemigroups/schreier-sims.hpp>
#include <libsemigroups/transf.hpp>
// pybind11....
#include <pybind11/pybind11.h>
// libsemigroups_pybind11....
#include "main.hpp" // for init_schreier_sims
namespace libsemigroups {
namespace py = pybind11;
namespace {
template <size_t N, typename Point, typename Element>
void bind_schreier_sims(py::module& m, std::string const& name) {
using SchreierSims_ = SchreierSims<N, Point, Element>;
std::string pyclass_name = std::string("SchreierSims") + name;
py::class_<SchreierSims_> thing(m,
pyclass_name.c_str(),
R"pbdoc(
This class implements a deterministic version of the Schreier-Sims algorithm
acting on a relatively small number of points (< 1000).
:example:
.. doctest:: python
>>> from libsemigroups_pybind11 import SchreierSims, Perm
>>> p1 = Perm([1, 0, 2, 3, 4] + list(range(5, 255)))
>>> p2 = Perm([1, 2, 3, 4, 0] + list(range(5, 255)))
>>> S = SchreierSims([p1, p2])
>>> S.size()
120
)pbdoc");
thing.def("__repr__", [](SchreierSims_ const& S) {
return to_human_readable_repr(S);
});
thing.def(py::init<>(), R"pbdoc(
:sig=(self: SchreierSims, gens: list[Element]) -> None:
Construct from a list of generators.
This function constructs a :any:`SchreierSims` instance with generators in
the list *gens*.
:param gens: the list of generators.
:type gens: list[Element]
:raises LibsemigroupsError: if the generators do not have degree equal to
:math:`255` or :math:`511`, or the number of generators exceeds the
maximum capacity.
)pbdoc");
thing.def("__copy__",
[](SchreierSims_ const& self) { return SchreierSims_(self); });
thing.def(
"copy",
[](SchreierSims_ const& self) { return SchreierSims_(self); },
R"pbdoc(
:sig=(self: SchreierSims) -> SchreierSims:
Copy a :any:`SchreierSims`.
:returns: A copy.
:rtype: SchreierSims
)pbdoc");
thing.def("add_base_point",
&SchreierSims_::add_base_point,
py::arg("pt"),
R"pbdoc(
:sig=(self: SchreierSims, pt: int) -> SchreierSims:
Add a base point to the stabiliser chain.
:param pt: the base point to add.
:type pt: int
:returns: *self*.
:rtype: SchreierSims
:raises LibsemigroupsError: if *pt* is out of range.
:raises LibsemigroupsError: if *pt* is already a base point.
:raises LibsemigroupsError: if :any:`finished()` returns ``True``.
:complexity: Linear in the current number of base points.)pbdoc");
thing.def("add_generator",
&SchreierSims_::add_generator,
py::arg("x"),
R"pbdoc(
:sig=(self: SchreierSims, x: Element) -> bool:
Add a generator.
This functions adds the argument *x* as a new generator if and only if *x* is
not already an element of the group represented by the Schreier-Sims object.
:param x: the generator to add.
:type x: Element
:returns: ``True`` if *x* is added as a generator and ``False`` if it is not.
:rtype: bool
:raises LibsemigroupsError: if the degree of *x* is not equal to :math:`255`
or :math:`511`, or if *self* already contains the maximum number of
elements.
:complexity: Constant
)pbdoc");
thing.def("base",
&SchreierSims_::base,
py::arg("index"),
R"pbdoc(
:sig=(self: SchreierSims, index: int) -> int:
Get a base point.
This function gets the base point with a given index.
:param index: the index of the base point.
:type index: int
:returns: The base point with index *index*.
:rtype: int
:raises LibsemigroupsError: if *index* is out of range.
:complexity: Constant.
)pbdoc");
thing.def("base_size",
&SchreierSims_::base_size,
R"pbdoc(
:sig=(self: SchreierSims) -> int:
Get the size of the current base.
:returns: The base size.
:rtype: int
:complexity: Constant.
)pbdoc");
thing.def("contains",
&SchreierSims_::contains,
py::arg("x"),
R"pbdoc(
:sig=(self: SchreierSims, x: Element) -> bool:
Test membership of an element.
:param x: the possible element.
:type x: Element
:returns: ``True`` if *element* is a contained in the :any:`SchreierSims`
instance, and ``False`` otherwise.
:rtype: bool
)pbdoc");
thing.def("currently_contains",
&SchreierSims_::currently_contains,
py::arg("x"),
R"pbdoc(
:sig=(self: SchreierSims, x: Element) -> bool:
Test membership of an element without running.
This function tests the membership of an element without running the algorithm.
:param x: the possible element.
:type x: Element
:returns: ``True`` if *x* is a contained in the :any:`SchreierSims`
instance, and ``False`` otherwise.
:rtype: bool
)pbdoc");
thing.def("empty",
&SchreierSims_::empty,
R"pbdoc(
:sig=(self: SchreierSims) -> bool:
Check if any generators have been added so far.
:returns: ``True`` if ``number_of_generators() == 0`` and ``False`` otherwise.
:rtype: bool
:complexity: Constant.
)pbdoc");
thing.def("finished",
&SchreierSims_::finished,
R"pbdoc(
:sig=(self: SchreierSims) -> bool:
Check if the stabiliser chain is fully enumerated.
:returns: ``True`` if the stabiliser chain is fully enumerated and ``False`` otherwise.
:rtype: bool
:complexity: Constant.
)pbdoc");
thing.def("generator",
&SchreierSims_::generator,
py::return_value_policy::reference_internal,
py::arg("index"),
R"pbdoc(
:sig=(self: SchreierSims, index: int) -> Element:
Get a generator.
This function returns the generator with a given index.
:param index: the index of the generator to return.
:type index: int
:returns: The generator with index *index*.
:rtype: Element
:raises LibsemigroupsError: if the *index* is out of bounds.
:complexity: Constant.
)pbdoc");
thing.def("init",
&SchreierSims_::init,
R"pbdoc(
:sig=(self: SchreierSims) -> SchreierSims:
Reset to the trivial group.
This function removes all generators, and orbits, and resets *self* so that it
represents the trivial group, as if *self* had been newly constructed.
:returns: *self*.
:rtype: SchreierSims
:complexity: Constant.
)pbdoc");
thing.def("inverse_transversal_element",
&SchreierSims_::inverse_transversal_element,
py::return_value_policy::reference_internal,
py::arg("depth"),
py::arg("pt"),
R"pbdoc(
:sig=(self: SchreierSims, depth:int, pt: int) -> Element:
Get an inverse of a transversal element.
This function returns the transversal element at depth *depth* which sends *pt*
to the basepoint.
:param depth: the depth.
:type depth: int
:param pt: the point to map to the base point under the inverse transversal
element.
:type pt: int
:returns: the inverse transversal element.
:rtype: Element
:raises LibsemigroupsError: if the *depth* is out of bounds.
:raises LibsemigroupsError: if *pt* is not in the orbit of the basepoint.
:complexity: Constant.
)pbdoc");
thing.def("number_of_generators",
&SchreierSims_::number_of_generators,
R"pbdoc(
:sig=(self: SchreierSims) -> int:
The number of generators.
This function returns the number of generators.
:returns: The number of generators.
:rtype: int
:complexity: Constant.
)pbdoc");
thing.def("number_of_strong_generators",
&SchreierSims_::number_of_strong_generators,
py::arg("depth"),
R"pbdoc(
:sig=(self: SchreierSims, depth: int) -> int:
The number of strong generators at a given depth.
This function returns the number of strong generators of the stabiliser chain at
a given depth.
:param depth: the depth.
:type depth: int
:returns: The number of strong generators.
:rtype: int
:raises LibsemigroupsError: if the *depth* is out of bounds.
:complexity: Constant.
)pbdoc");
thing.def("one",
&SchreierSims_::one,
py::return_value_policy::reference_internal,
R"pbdoc(
:sig=(self: SchreierSims) -> Element:
Returns the identity permutation.
This function returns the identity permutation of the same degree as the
permutations belonging to a :any:`SchreierSims` object.
:returns: The identity element.
:rtype: Element
)pbdoc");
thing.def("orbit_lookup",
&SchreierSims_::orbit_lookup,
py::arg("depth"),
py::arg("pt"),
R"pbdoc(
:sig=(self: SchreierSims, depth: int, pt: int) -> bool:
Check if a point is in the orbit of a basepoint.
:param depth: the depth.
:type depth: int
:param pt: the point.
:type pt: int
:returns:
``True`` if the point *pt* is in the orbit of the base point of
*self* at depth *depth*, and ``False`` otherwise.
:rtype: bool
:raises LibsemigroupsError:
if the *depth*` is out of bounds or if *pt* is out of bounds.
:complexity: Constant.
)pbdoc");
thing.def("run",
&SchreierSims_::run,
R"pbdoc(
:sig=(self: SchreierSims) -> None:
Run the Schreier-Sims algorithm.
:complexity:
:math:`O(N^2\log^3|G|+|T|N^2\log|G|)` time and
:math:`O(N^2\log|G|+|T|N)` space, where ``N`` is the degree of the
generators, :math:`|G|` is the size of the group and :math:`|T|` is the
number of generators of the group.
)pbdoc");
thing.def("sift",
&SchreierSims_::sift,
py::arg("x"),
R"pbdoc(
:sig=(self: SchreierSims, x: Element) -> Element:
Sift an element through the stabiliser chain.
:param x: A group element.
:type x: Element
:returns: A sifted element.
:rtype: Element
:raises LibsemigroupsError: if the degree of *x* is not equal to the degree of
the generators.
)pbdoc");
thing.def("sift_inplace",
&SchreierSims_::sift_inplace,
py::arg("x"),
R"pbdoc(
:sig=(self: SchreierSims, x: Element) -> None:
Sift an element through the stabiliser chain in-place.
:param x: a group element.
:type x: Element
:raises LibsemigroupsError: if the degree of *x* is not equal to the degree of
the generators.
)pbdoc");
thing.def("size",
&SchreierSims_::size,
R"pbdoc(
:sig=(self: SchreierSims) -> int:
Returns the size of the group represented by *self*.
:returns: the size of the group.
:rtype: int
)pbdoc");
thing.def("current_size",
&SchreierSims_::current_size,
R"pbdoc(
:sig=(self: SchreierSims) -> int:
Returns the size of the group represented by this, without running the algorithm.
:returns: the size of the group.
:rtype: int
)pbdoc");
thing.def("strong_generator",
&SchreierSims_::strong_generator,
py::return_value_policy::reference_internal,
py::arg("depth"),
py::arg("index"),
R"pbdoc(
:sig=(self: SchreierSims, depth: int, index: int) -> Element:
Get a strong generator.
This function returns the generator with a given depth and index.
:param depth: the depth.
:type depth: int
:param index: the index of the generator to return.
:type index: int
:returns: The strong generator of at depth *depth* and with index *index*.
:rtype: Element
:raises LibsemigroupsError: if the *depth* is out of bounds.
:raises LibsemigroupsError: if the *index* is out of bounds.
:complexity: Constant.
)pbdoc");
thing.def("transversal_element",
&SchreierSims_::transversal_element,
py::return_value_policy::reference_internal,
py::arg("depth"),
py::arg("pt"),
R"pbdoc(
:sig=(self: SchreierSims, depth: int, pt: int) -> Element:
Get an transversal element.
This function returns the transversal element at depth *depth* which sends the
corresponding basepoint to the point *pt*.
:param depth: the depth.
:type depth: int
:param pt: the image of the base point under the traversal.
:type pt: int
:returns: The transversal element.
:rtype: Element
:raises LibsemigroupsError: if *depth* is out of bounds.
:raises LibsemigroupsError: if *pt* is not in the orbit of the basepoint.
:complexity: Constant.
)pbdoc");
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
m.def(
"schreier_sims_intersection",
[](SchreierSims_& T, SchreierSims_& S1, SchreierSims_& S2) {
return schreier_sims::intersection(T, S1, S2);
},
py::arg("result"),
py::arg("x"),
py::arg("y"),
R"pbdoc(
:sig=(result: SchreierSims, x: SchreierSims, y: SchreierSims) -> None:
:only-document-once:
Find the intersection of two permutation groups.
This function finds the intersection of two permutation groups.
It modifies the first parameter *result* to be the :any:`SchreierSims` object
corresponding to the intersection of *x* and *y*.
:param result: an empty :any:`SchreierSims` object that will hold the result.
:type result: SchreierSims
:param x: the first group of the intersection.
:type x: SchreierSims
:param y: the second group of the intersection.
:type y: SchreierSims
:raises LibsemigroupsError: if *result* is not empty.
)pbdoc");
} // bind_schreier_sims
} // namespace
void init_schreier_sims(py::module& m) {
// One call to bind is required per list of types
bind_schreier_sims<255, uint8_t, Perm<0, uint8_t>>(m, "Perm1");
bind_schreier_sims<511, uint16_t, Perm<0, uint16_t>>(m, "Perm2");
#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
bind_schreier_sims<16, uint8_t, HPCombi::Perm16>(m, "HPCombiPerm16");
#endif
}
} // namespace libsemigroups