forked from libsemigroups/libsemigroups_pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmat8.cpp
More file actions
575 lines (466 loc) · 15.3 KB
/
Copy pathbmat8.cpp
File metadata and controls
575 lines (466 loc) · 15.3 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
//
// libsemigroups_pybind11
// Copyright (C) 2024 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/>.
//
// TODO
// * iwyu
// libsemigroups headers
#include <libsemigroups/bmat8.hpp>
// pybind11....
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
// libsemigroups_pybind11....
#include "main.hpp" // for init_bmat8
namespace libsemigroups {
namespace py = pybind11;
void init_bmat8(py::module& m) {
py::class_<BMat8> thing2(m,
"BMat8",
R"pbdoc(
Fast boolean matrices of dimension up to 8 x 8.
Instance of this class represent 8 x 8 matrices over the boolean semiring. The
functions for these small matrices over the boolean semiring are more optimised
than the generic functions for boolean matrices. Note that all :any:`BMat8` are
represented internally as an 8 x 8 matrix; any entries not defined by the user
are taken to be ``0``. This does not affect the results of any calculations.
There are numerous functions for computing things about :any:`BMat8` objects in
the submodule ``bmat8``.
.. toctree::
:maxdepth: 1
bmat8-helpers
.. doctest::
>>> from libsemigroups_pybind11 import BMat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> x[1, 1] = 1
>>> x
BMat8([[0, 1],
[1, 1]])
>>> x[0, 1]
True
>>> x[1, 1]
True
>>> x * x
BMat8([[1, 1],
[1, 1]])
>>> x < x * x
True
>>> x *= x
>>> x
BMat8([[1, 1],
[1, 1]])
>>> x.to_int()
13889101250810609664
>>> bin(x.to_int())
'0b1100000011000000000000000000000000000000000000000000000000000000'
>>> x == BMat8([[1, 1, 0], [1, 1, 0], [0, 0, 0]]) # All BMat8's are really 8x8!
True
>>> y = BMat8([[1, 0, 1], [0, 1, 0], [0, 0, 0]])
>>> y[0] # The first row
[True, False, True, False, False, False, False, False]
>>> x + y
BMat8([[1, 1, 1],
[1, 1, 0],
[0, 0, 0]])
>>> x += y
>>> x
BMat8([[1, 1, 1],
[1, 1, 0],
[0, 0, 0]])
>>> 1 * x == x
True
>>> x * 0
BMat8(0)
:any:`BMat8` objects can be used with the following algorithms in
``libsemigroups_pybind11``
* :any:`FroidurePin`
* :any:`Konieczny`
* :any:`Action`
)pbdoc");
// The next function __len__ is not really required, but without this the
// error messages from, e.g. FroidurePin.current_position gives misleading
// error messages in:
// >>> from libsemigroups_pybind11 import FroidurePin, Perm, BMat8
// >>> S = FroidurePin(Perm([1, 0, 2, 3, 4, 5, 6]),
// ... Perm([1, 2, 3, 4, 5, 0, 6]))
// >>> S.current_position(BMat8(0))
// TypeError: object of type '_libsemigroups_pybind11.BMat8' has no len()
//
// when it should be:
//
// TypeError: current_position(): incompatible function arguments. The
// following argument types are supported:
// 1. (self: _libsemigroups_pybind11.FroidurePinPerm16, x:
// _libsemigroups_pybind11.StaticPerm16) -> int
// 2. (self: _libsemigroups_pybind11.FroidurePinBase, w: List[int]) -> int
// 3. (self: _libsemigroups_pybind11.FroidurePinBase, i: int) -> int
thing2.def("__len__", [](BMat8 const& x) { return 8; });
thing2.def("__repr__",
[](BMat8 const& x) { return to_human_readable_repr(x, "[]"); });
thing2.def(
"__setitem__",
[](BMat8& x, std::pair<size_t, size_t> tup, bool val) {
x.at(tup.first, tup.second) = val;
},
py::is_operator());
thing2.def(
"__getitem__",
[](BMat8 const& x, std::pair<size_t, size_t> tup) {
return x.at(tup.first, tup.second);
},
py::is_operator());
thing2.def(
"__getitem__",
[](BMat8 const& x, size_t r) { return bmat8::to_vector(x.at(r)); },
py::is_operator());
thing2.def(
"__hash__",
[](BMat8 const& x) { return Hash<BMat8>()(x); },
py::is_operator());
thing2.def(py::self == py::self);
thing2.def(py::self != py::self);
thing2.def(py::self <= py::self);
thing2.def(py::self >= py::self);
thing2.def(py::self += py::self);
thing2.def(py::self + py::self);
thing2.def(py::self * bool());
thing2.def(bool() * py::self);
thing2.def(py::self < py::self);
thing2.def(py::self > py::self);
thing2.def(py::self * py::self);
thing2.def(py::self *= py::self);
thing2.def(py::init<>(), R"pbdoc(
Default constructor.
There is no guarantee about the contents of the matrix constructed.
:complexity:
Constant.
)pbdoc");
thing2.def(py::init<uint64_t>(),
py::arg("val"),
R"pbdoc(
Construct from ``int``.
This constructor initializes a :any:`BMat8` to have rows equal to the 8 chunks,
of 8 bits each, of the binary representation of ``mat``.
:param val: the integer representation of the matrix being constructed.
:type val: int
:complexity: Constant.)pbdoc");
thing2.def(py::init<std::vector<std::vector<bool>> const&>(),
py::arg("rows"),
R"pbdoc(
Construct from list of rows.
This constructor initializes a matrix where the rows of the matrix are the
lists in ``rows``.
:param rows: the list of rows of the matrix being constructed.
:type rows: list[list[bool]]
:raises LibsemigroupsError: if *rows* has 0 rows.
:raises LibsemigroupsError: if *rows* has more than 8 rows.
:raises LibsemigroupsError: if the rows of *rows* are not all of the same length.
:complexity: Constant.)pbdoc");
thing2.def("degree", [](BMat8 const& self) { return 8; });
thing2.def(
"copy",
[](BMat8 const& self) { return BMat8(self); },
R"pbdoc(
Copy a BMat8.
:returns: A copy of the argument.
:rtype: BMat8
)pbdoc");
thing2.def("__copy__", [](BMat8 const& self) { return BMat8(self); });
thing2.def("to_int",
&BMat8::to_int,
R"pbdoc(
Returns the integer representation of a :any:`BMat8`.
Returns a non-negative integer obtained by interpreting an 8 x 8 :any:`BMat8`
as a sequence of 64 bits (reading rows left to right, from top to bottom) and
then realising this sequence as an unsigned int.
:complexity:
Constant.
:returns:
The integer value of the matrix.
:rtype:
int
.. doctest::
>>> from libsemigroups_pybind11 import BMat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> x.to_int()
4647714815446351872
>>> bin(x.to_int())
'0b100000010000000000000000000000000000000000000000000000000000000'
)pbdoc");
thing2.def("swap",
&BMat8::swap,
py::arg("that"),
R"pbdoc(
Swaps ``self`` with ``that``.
This function swaps the values of ``self`` and ``that``.
:param that: the :any:`BMat8` to swap this with.
:type that: BMat8
:complexity: Constant.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> y = BMat8([[1, 1], [0, 0]])
>>> BMat8.swap(x,y)
>>> x
BMat8([[1, 1],
[0, 0]])
>>> y
BMat8([[0, 1],
[1, 0]])
)pbdoc");
////////////////////////////////////////////////////////////////////////
// Helper functions from libsemigroups::bmat8
////////////////////////////////////////////////////////////////////////
m.def("bmat8_one",
&bmat8::one<BMat8>,
py::arg("dim") = 8,
R"pbdoc(
Returns the identity :any:`BMat8` of a given dimension.
This function returns the :any:`BMat8` with the first *dim* entries in the
main diagonal equal to ``1`` and every other value equal to ``0``.
:param dim: the dimension of the identity (default: 8)
:type dim: int
:returns: A :any:`BMat8`.
:rtype: BMat8
:complexity: Constant.
.. doctest::
>>> from libsemigroups_pybind11 import bmat8
>>> bmat8.one(4)
BMat8([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
)pbdoc");
m.def(
"random",
[](size_t dim) { return bmat8::random(dim); },
py::arg("dim") = 8,
R"pbdoc(
Construct a random :any:`BMat8` of dimension at most *dim*.
This function returns a :any:`BMat8` chosen at random, where only the top-left
*dim* by *dim* entries can be non-zero.
:param dim: the dimension.
:type dim: int
:returns: A :any:`BMat8`.
:rtype: BMat8
)pbdoc");
m.def("transpose",
&bmat8::transpose,
py::arg("x"),
R"pbdoc(
Returns the transpose of a :any:`BMat8`.
This function returns the transpose of its argument ``x`` , which is computed
using the technique found in :cite:`Knuth2009aa`.
:param x: the matrix to transpose.
:type x: BMat8
:returns: A :any:`BMat8`.
:rtype: BMat8
:complexity: Constant.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[1, 0, 1], [0, 1, 0], [0, 0, 0]])
>>> bmat8.transpose(x)
BMat8([[1, 0, 0],
[0, 1, 0],
[1, 0, 0]])
)pbdoc");
m.def("row_space_basis",
&bmat8::row_space_basis,
py::arg("x"),
R"pbdoc(
Find a basis for the row space of a :any:`BMat8`.
This function returns a :any:`BMat8` whose non-zero rows form a basis for the
row space of ``x``.
:param x: the matrix.
:type x: BMat8
:returns: A :any:`BMat8`.
:rtype: BMat8
:complexity: Constant.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[1, 0, 1], [0, 1, 0], [0, 0, 0]])
>>> bmat8.row_space_basis(x)
BMat8([[1, 0, 1],
[0, 1, 0],
[0, 0, 0]])
)pbdoc");
m.def("col_space_basis",
&bmat8::col_space_basis,
py::arg("x"),
R"pbdoc(
Find a basis for the column space of a :any:`BMat8`.
This function returns a :any:`BMat8` whose non-zero columns form a basis for
the column space of ``x``.
:param x: the matrix.
:type x: BMat8
:returns: A :any:`BMat8`.
:rtype: BMat8
:complexity: Constant.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[1, 0, 1], [0, 1, 0], [0, 0, 0]])
>>> bmat8.col_space_basis(x)
BMat8([[1, 0],
[0, 1]])
)pbdoc");
m.def("number_of_rows",
&bmat8::number_of_rows,
py::arg("x"),
R"pbdoc(
Returns the number of non-zero rows in a :any:`BMat8`.
:any:`BMat8` objects do not know their "dimension" - in effect they are all of dimension 8.
However, this function can be used to obtain the number of non-zero rows of a
:any:`BMat8`.
:param x: the matrix.
:type x: BMat8
:returns: The number of non-zero rows.
:rtype: int
:complexity: Constant.
.. seealso:: :any:`number_of_cols` and :any:`minimum_dim`.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[1, 0, 1], [0, 1, 0], [0, 0, 0]])
>>> bmat8.number_of_rows(x)
2
)pbdoc");
m.def("number_of_cols",
&bmat8::number_of_cols,
py::arg("x"),
R"pbdoc(
Returns the number of non-zero columns in a :any:`BMat8`.
:any:`BMat8` objects do not know their "dimension" - in effect they are all of dimension 8.
However, this function can be used to obtain the number of non-zero rows of a
:any:`BMat8`.
:param x: the matrix.
:type x: BMat8
:returns: The number of non-zero columns.
:rtype: int
:complexity: Constant.
.. seealso:: :any:`number_of_rows` and :any:`minimum_dim`.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[1, 0, 1], [0, 1, 0], [0, 0, 0]])
>>> bmat8.number_of_cols(x)
3
)pbdoc");
m.def("row_space_size",
&bmat8::row_space_size,
py::arg("x"),
R"pbdoc(
Returns the size of the row space of a :any:`BMat8`.
:returns: The size of the row space of ``x``.
:rtype: int
:param x: the matrix.
:type x: BMat8
:complexity: :math:`O(n)` where :math:`n` is the return value of this function.
.. seealso:: :any:`col_space_size`.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[1, 0, 0], [0, 1, 1], [0, 1, 0]])
>>> bmat8.row_space_size(x)
6
)pbdoc");
m.def("col_space_size",
&bmat8::col_space_size,
py::arg("x"),
R"pbdoc(
Returns the size of the column space of a :any:`BMat8`.
:param x: the matrix.
:type x: BMat8
:returns: The size of the column space of ``x``.
:rtype: int
:complexity: :math:`O(n)` where :math:`n` is the return value of this function.
.. seealso:: :any:`row_space_size`.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> bmat8.col_space_size(x)
4
)pbdoc");
m.def("minimum_dim",
&bmat8::minimum_dim,
py::arg("x"),
R"pbdoc(
Returns the minimum dimension of a :any:`BMat8`.
This function returns the maximal ``n`` such that row ``n`` or column ``n``
contains a ``1``. Equivalent to the maximum of :any:`number_of_rows` and
:any:`number_of_cols`.
:param x: the matrix.
:type x: BMat8
:returns: The minimum dimension of **x**
:rtype: int
:complexity: Constant.
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> bmat8.minimum_dim(x)
2
)pbdoc");
m.def(
"rows",
[](BMat8 const& x) {
std::vector<std::vector<bool>> result;
for (auto row : bmat8::rows(x)) {
result.push_back(bmat8::to_vector(row));
}
return result;
},
py::arg("x"),
R"pbdoc(
Returns a list of the rows of a :any:`BMat8`.
This function returns the rows of ``x``. The returned list always has length 8,
even if ``x`` was constructed with fewer rows.
:param x: the matrix.
:type x: BMat8
:complexity: Constant.
:returns: The list of rows represented as integers.
:rtype: list[list[bool]]
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> bmat8.rows(x) # doctest: +NORMALIZE_WHITESPACE
[[False, True, False, False, False, False, False, False],
[True, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
)pbdoc");
m.def("is_regular_element",
&bmat8::is_regular_element,
py::arg("x"),
R"pbdoc(
Check whether ``x`` is a regular element of the full boolean matrix monoid of
appropriate dimension.
:param x: the matrix.
:type x: BMat8
:complexity: Constant.
:returns:
A ``True`` if there exists a boolean matrix ``y`` such that ``x * y * x = x``
where ``x`` , and ``False`` otherwise.
:rtype: bool
.. doctest::
>>> from libsemigroups_pybind11 import BMat8, bmat8
>>> x = BMat8([[0, 1], [1, 0]])
>>> bmat8.is_regular_element(x)
True
>>> sum(1 for x in range(100000) if bmat8.is_regular_element(BMat8(x)))
97996
)pbdoc");
} // init_bmat8
} // namespace libsemigroups