HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.0
Loading...
Searching...
No Matches
bmat8.hpp
Go to the documentation of this file.
1//****************************************************************************//
2// Copyright (C) 2018-2024 Finn Smith <fls3@st-andrews.ac.uk> //
3// Copyright (C) 2018-2024 James Mitchell <jdm3@st-andrews.ac.uk> //
4// Copyright (C) 2018-2024 Florent Hivert <Florent.Hivert@lisn.fr>, //
5// //
6// This file is part of HP-Combi <https://github.com/libsemigroups/HPCombi> //
7// //
8// HP-Combi is free software: you can redistribute it and/or modify it //
9// under the terms of the GNU General Public License as published by the //
10// Free Software Foundation, either version 3 of the License, or //
11// (at your option) any later version. //
12// //
13// HP-Combi is distributed in the hope that it will be useful, but WITHOUT //
14// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //
15// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License //
16// for more details. //
17// //
18// You should have received a copy of the GNU General Public License along //
19// with HP-Combi. If not, see <https://www.gnu.org/licenses/>. //
20//****************************************************************************//
21
22// This file contains a declaration of fast boolean matrices up to dimension 8.
23
24#ifndef HPCOMBI_BMAT8_HPP_
25#define HPCOMBI_BMAT8_HPP_
26
27#include <array> // for array
28#include <bitset> // for bitset
29#include <cstddef> // for size_t
30#include <cstdint> // for uint64_t, uint8_t
31#include <functional> // for hash, __scalar_hash
32#include <iostream> // for ostream
33#include <memory> // for hash
34#include <utility> // for pair, swap
35#include <vector> // for vector
36
37#include "debug.hpp" // for HPCOMBI_ASSERT
38#include "epu8.hpp" // for epu8
39#include "perm16.hpp" // for Perm16
40
41namespace HPCombi {
42
52class BMat8 {
53 public:
57 BMat8() noexcept = default;
58
63 explicit BMat8(uint64_t mat) noexcept : _data(mat) {}
64
69 // Not sure if this is noexcept or not
70 explicit BMat8(std::vector<std::vector<bool>> const &mat);
71
75 BMat8(BMat8 const &) noexcept = default;
76
80 BMat8(BMat8 &&) noexcept = default;
81
85 BMat8 &operator=(BMat8 const &) noexcept = default;
86
90 BMat8 &operator=(BMat8 &&) noexcept = default;
91
93 ~BMat8() = default;
94
98 bool operator==(BMat8 const &that) const noexcept {
99 return _data == that._data;
100 }
101
105 bool operator!=(BMat8 const &that) const noexcept {
106 return _data != that._data;
107 }
108
113 bool operator<(BMat8 const &that) const noexcept {
114 return _data < that._data;
115 }
116
121 bool operator>(BMat8 const &that) const noexcept {
122 return _data > that._data;
123 }
124
130 bool operator()(size_t i, size_t j) const noexcept;
131
137 void set(size_t i, size_t j, bool val) noexcept;
138
144 uint64_t to_int() const noexcept { return _data; }
145
150 BMat8 transpose() const noexcept;
151
156 BMat8 transpose_mask() const noexcept;
157
162 BMat8 transpose_maskd() const noexcept;
163
168 static void transpose2(BMat8 &, BMat8 &) noexcept;
169
176 BMat8 mult_transpose(BMat8 const &that) const noexcept;
177
183 BMat8 operator*(BMat8 const &that) const noexcept {
184 return mult_transpose(that.transpose());
185 }
186
193 BMat8 row_space_basis() const noexcept;
194
199 BMat8 col_space_basis() const noexcept {
201 }
202
204 size_t nr_rows() const noexcept;
205
207 // Not noexcept because it constructs a vector
208 std::vector<uint8_t> rows() const;
209
213 // Not noexcept because row_space_bitset_ref isn't
214 uint64_t row_space_size_ref() const;
215
219 // Not noexcept because it creates a vector
220 std::bitset<256> row_space_bitset_ref() const;
221
225 void row_space_bitset(epu8 &res1, epu8 &res2) const noexcept;
226
231 uint64_t row_space_size_bitset() const noexcept;
232
238 uint64_t row_space_size_incl() const noexcept;
239
244 uint64_t row_space_size_incl1() const noexcept;
245
249 uint64_t row_space_size() const noexcept { return row_space_size_incl(); }
250
254 bool row_space_included_ref(BMat8 other) const noexcept;
255
259 bool row_space_included_bitset(BMat8 other) const noexcept;
260
265 epu8 row_space_mask(epu8 vects) const noexcept;
266
270 bool row_space_included(BMat8 other) const noexcept;
271
275 // Not noexcept because std::make_pair is not
276 static std::pair<bool, bool> row_space_included2(BMat8 a1, BMat8 b1,
277 BMat8 a2, BMat8 b2);
278
283 BMat8 row_permuted(Perm16 p) const noexcept;
284
289 BMat8 col_permuted(Perm16 p) const noexcept;
290
295 static BMat8 row_permutation_matrix(Perm16 p) noexcept;
296
301 static BMat8 col_permutation_matrix(Perm16 p) noexcept;
302
309
315 // Not noexcept because vectors are allocated
317
321 static BMat8 one(size_t dim = 8) noexcept {
322 HPCOMBI_ASSERT(dim <= 8);
323 static std::array<uint64_t, 9> const ones = {
324 0x0000000000000000, 0x8000000000000000, 0x8040000000000000,
325 0x8040200000000000, 0x8040201000000000, 0x8040201008000000,
326 0x8040201008040000, 0x8040201008040200, 0x8040201008040201};
327 return BMat8(ones[dim]);
328 }
329
333 // Not noexcept because random things aren't
334 static BMat8 random();
335
340 // Not noexcept because BMat8::random above is not
341 static BMat8 random(size_t dim);
342
343 void swap(BMat8 &that) noexcept { std::swap(this->_data, that._data); }
344
346 // Not noexcept
347 std::ostream &write(std::ostream &os) const;
348
349#ifdef HPCOMBI_HAVE_DENSEHASHMAP
350 // FIXME do this another way
351 BMat8 empty_key() const noexcept { return BMat8(0xFF7FBFDFEFF7FBFE); }
352#endif
353
354 private:
355 uint64_t _data;
356
357 epu8 row_space_basis_internal() const noexcept;
358};
359
360} // namespace HPCombi
361
362#include "bmat8_impl.hpp"
363
364namespace std {
365template <> struct hash<HPCombi::BMat8> {
366 inline size_t operator()(HPCombi::BMat8 const &bm) const {
367 return hash<uint64_t>()(bm.to_int());
368 }
369};
370} // namespace std
371#endif // HPCOMBI_BMAT8_HPP_
Class for fast boolean matrices of dimension up to 8 x 8.
Definition bmat8.hpp:52
uint64_t row_space_size_incl() const noexcept
Returns the cardinality of the row space of this.
Definition bmat8_impl.hpp:316
BMat8 row_space_basis() const noexcept
Returns a canonical basis of the row space of this.
Definition bmat8_impl.hpp:238
static std::pair< bool, bool > row_space_included2(BMat8 a1, BMat8 b1, BMat8 a2, BMat8 b2)
Returns inclusion of row spaces.
Definition bmat8_impl.hpp:361
static void transpose2(BMat8 &, BMat8 &) noexcept
Transpose two matrices at once.
Definition bmat8_impl.hpp:189
uint64_t row_space_size_bitset() const noexcept
Returns the cardinality of the row space of this.
Definition bmat8_impl.hpp:291
static BMat8 row_permutation_matrix(Perm16 p) noexcept
Returns the matrix associated to the permutation p by rows.
Definition bmat8_impl.hpp:436
BMat8 mult_transpose(BMat8 const &that) const noexcept
Returns the matrix product of this and the transpose of that.
Definition bmat8_impl.hpp:209
BMat8(BMat8 const &) noexcept=default
A constructor.
BMat8 transpose_maskd() const noexcept
Returns the transpose of this.
Definition bmat8_impl.hpp:175
BMat8(BMat8 &&) noexcept=default
A constructor.
bool row_space_included_bitset(BMat8 other) const noexcept
Returns whether the row space of this is included in other's.
Definition bmat8_impl.hpp:332
bool operator>(BMat8 const &that) const noexcept
Returns true if this is greater than that.
Definition bmat8.hpp:121
BMat8 transpose() const noexcept
Returns the transpose of this.
Definition bmat8_impl.hpp:152
static BMat8 col_permutation_matrix(Perm16 p) noexcept
Returns the matrix associated to the permutation p by columns.
Definition bmat8_impl.hpp:440
epu8 row_space_mask(epu8 vects) const noexcept
Returns a mask for which vectors of a 16 rows epu8 are in the row space of this.
Definition bmat8_impl.hpp:351
BMat8 row_permuted(Perm16 p) const noexcept
Returns the matrix whose rows have been permuted according to p.
Definition bmat8_impl.hpp:424
BMat8() noexcept=default
A default constructor.
BMat8 col_space_basis() const noexcept
Returns a canonical basis of the col space of this.
Definition bmat8.hpp:199
bool row_space_included(BMat8 other) const noexcept
Returns whether the row space of this is included in other's.
Definition bmat8_impl.hpp:340
std::ostream & write(std::ostream &os) const
Write this on os.
Definition bmat8_impl.hpp:485
void row_space_bitset(epu8 &res1, epu8 &res2) const noexcept
Returns the the row space of this as 256 bits.
Definition bmat8_impl.hpp:276
Perm16 right_perm_action_on_basis(BMat8) const noexcept
Give the permutation whose right multiplication change *this to other.
Definition bmat8_impl.hpp:475
std::bitset< 256 > row_space_bitset_ref() const
Returns the the row space of this.
Definition bmat8_impl.hpp:375
bool operator!=(BMat8 const &that) const noexcept
Returns true if this does not equal that.
Definition bmat8.hpp:105
BMat8 col_permuted(Perm16 p) const noexcept
Returns the matrix whose columns have been permuted according to p.
Definition bmat8_impl.hpp:432
uint64_t to_int() const noexcept
Returns the integer representation of this.
Definition bmat8.hpp:144
uint64_t row_space_size_ref() const
Returns the cardinality of the row space of this.
Definition bmat8_impl.hpp:403
uint64_t row_space_size() const noexcept
Returns the cardinality of the row space of this.
Definition bmat8.hpp:249
BMat8 transpose_mask() const noexcept
Returns the transpose of this.
Definition bmat8_impl.hpp:163
void swap(BMat8 &that) noexcept
Definition bmat8.hpp:343
static BMat8 one(size_t dim=8) noexcept
Returns the identity BMat8.
Definition bmat8.hpp:321
std::vector< uint8_t > rows() const
Returns a std::vector for rows of this.
Definition bmat8_impl.hpp:407
size_t nr_rows() const noexcept
Returns the number of non-zero rows of this.
Definition bmat8_impl.hpp:416
void set(size_t i, size_t j, bool val) noexcept
Sets the (i, j)th position to val.
Definition bmat8_impl.hpp:110
static BMat8 random()
Returns a random BMat8.
Definition bmat8_impl.hpp:134
bool operator()(size_t i, size_t j) const noexcept
Returns the entry in the (i, j)th position.
Definition bmat8_impl.hpp:104
bool operator<(BMat8 const &that) const noexcept
Returns true if this is less than that.
Definition bmat8.hpp:113
uint64_t row_space_size_incl1() const noexcept
Returns the cardinality of the row space of this.
Definition bmat8_impl.hpp:300
bool row_space_included_ref(BMat8 other) const noexcept
Returns whether the row space of this is included in other's.
Definition bmat8_impl.hpp:397
Perm16 right_perm_action_on_basis_ref(BMat8) const
Give the permutation whose right multiplication change *this to other.
Definition bmat8_impl.hpp:444
#define HPCOMBI_ASSERT(x)
Definition debug.hpp:28
const Transf16 a1
Definition image.cpp:52
const Transf16 a2
Definition image.cpp:53
Definition bmat8.hpp:41
uint8_t __attribute__((vector_size(16))) epu8
SIMD vector of 16 unsigned bytes.
Definition epu8.hpp:45
Definition bmat8.hpp:364
Permutations of .
Definition perm16.hpp:208
size_t operator()(HPCombi::BMat8 const &bm) const
Definition bmat8.hpp:366