NuriKit v0.1.0b2
Loading...
Searching...
No Matches
bool_matrix.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2023 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5#ifndef NURI_CORE_CONTAINER_BOOL_MATRIX_H_
6#define NURI_CORE_CONTAINER_BOOL_MATRIX_H_
7
9#include <cstdint>
10#include <limits>
11#include <vector>
12
13#include <absl/base/optimization.h>
14#include <Eigen/Dense>
16
17#include "nuri/eigen_config.h"
18
19namespace nuri {
20namespace internal {
21 using Block = uint64_t;
22 constexpr Eigen::Index kBitsPerBlock = std::numeric_limits<Block>::digits;
23} // namespace internal
24
26private:
27 using Index = Eigen::Index;
28
29public:
30 BoolMatrixKey() = delete;
31
32 BoolMatrixKey(const Index idx)
33 : BoolMatrixKey(idx / internal::kBitsPerBlock,
34 idx % internal::kBitsPerBlock) {
35 ABSL_ASSUME(idx >= 0);
36 }
37
38 BoolMatrixKey(const Index blk, const Index bit)
39 : blk_(blk), bit_(static_cast<int>(bit)), mask_(1ULL << bit_) { }
40
41 Index to_index() const { return blk_ * internal::kBitsPerBlock + bit_; }
42
43 Index blk() const { return blk_; }
44 int bit() const { return bit_; }
45 internal::Block mask() const { return mask_; }
46
47private:
48 Index blk_;
49 int bit_;
50 internal::Block mask_;
51};
52
54public:
55 using Index = Eigen::Index;
56
57 BoolMatrix() = delete;
58
60 : data_((rows - 1) / internal::kBitsPerBlock + 1, cols), rows_(rows) { }
61
62 bool operator()(BoolMatrixKey row, Index col) const {
63 return (data_(row.blk(), col) & row.mask()) != 0;
64 }
65
66 Index rows() const { return rows_; }
67
68 Index cols() const { return data_.cols(); }
69
70 std::vector<int> gaussian_elimination() { return col_reduction_impl<true>(); }
71
72 std::vector<int> partial_reduction(const int reduce_begin) {
73 return col_reduction_impl<false>(reduce_begin);
74 }
75
76 void colwise_xor(Index c1, Index c2) {
77 for (Index blk = 0; blk < data_.rows(); ++blk)
78 data_(blk, c1) ^= data_(blk, c2);
79 }
80
81 void zero() { data_.setZero(); }
82
83 void resize(int cols) {
84 const Index oldcols = data_.cols();
85 data_.conservativeResize(Eigen::NoChange, cols);
86 if (cols > oldcols)
87 data_.rightCols(cols - oldcols).setZero();
88 }
89
90 // Move col c1 to c2; c2 is overwritten, c1 is zeroed
91 void move_col(Index c1, Index c2) {
92 data_.col(c2) = data_.col(c1);
93 data_.col(c1).setZero();
94 }
95
96 void set(BoolMatrixKey row, Index col) {
97 data_(row.blk(), col) |= row.mask();
98 }
99
100 void unset(BoolMatrixKey row, Index col) {
101 data_(row.blk(), col) &= ~row.mask();
102 }
103
104 void assign(BoolMatrixKey row, Index col, bool value) {
105 internal::Block &data = data_(row.blk(), col);
106 data = (data & ~row.mask())
107 | (static_cast<internal::Block>(value) << row.bit());
108 }
109
110private:
111 template <bool full>
112 std::vector<int> col_reduction_impl(const int reduce_begin = 0) {
113 std::vector<int> used(data_.cols(), 0);
114
115 for (int row = 0; row < rows_; ++row) {
116 const int p = static_cast<int>(find_pivot(row, used));
117 // Try basis in [0, begin)
118 if (p < 0)
119 continue;
120
121 used[p] = 1;
122
123 if constexpr (!full)
124 if (p >= reduce_begin)
125 continue;
126
127 for (Index i = reduce_begin; i < data_.cols(); ++i)
128 if (used[i] == 0 && (*this)(row, i))
129 colwise_xor(i, p);
130 }
131
132 return used;
133 }
134
135 Index find_pivot(BoolMatrixKey row, const std::vector<int> &used) const {
136 for (Index i = 0; i < data_.cols(); ++i)
137 if (used[i] == 0 && (*this)(row, i))
138 return i;
139
140 return -1;
141 }
142
143 // Col-wise compressed data
144 Matrix<internal::Block, Eigen::Dynamic, Eigen::Dynamic> data_;
145 Eigen::Index rows_;
146};
147} // namespace nuri
148
149#endif /* NURI_CORE_CONTAINER_BOOL_MATRIX_H_ */
Definition bool_matrix.h:25
internal::Block mask() const
Definition bool_matrix.h:45
Index blk() const
Definition bool_matrix.h:43
Index to_index() const
Definition bool_matrix.h:41
BoolMatrixKey(const Index blk, const Index bit)
Definition bool_matrix.h:38
BoolMatrixKey(const Index idx)
Definition bool_matrix.h:32
int bit() const
Definition bool_matrix.h:44
void set(BoolMatrixKey row, Index col)
Definition bool_matrix.h:96
Index cols() const
Definition bool_matrix.h:68
void resize(int cols)
Definition bool_matrix.h:83
BoolMatrix()=delete
Index rows() const
Definition bool_matrix.h:66
bool operator()(BoolMatrixKey row, Index col) const
Definition bool_matrix.h:62
std::vector< int > gaussian_elimination()
Definition bool_matrix.h:70
void assign(BoolMatrixKey row, Index col, bool value)
Definition bool_matrix.h:104
void unset(BoolMatrixKey row, Index col)
Definition bool_matrix.h:100
Eigen::Index Index
Definition bool_matrix.h:55
void zero()
Definition bool_matrix.h:81
std::vector< int > partial_reduction(const int reduce_begin)
Definition bool_matrix.h:72
void move_col(Index c1, Index c2)
Definition bool_matrix.h:91
BoolMatrix(Index rows, Index cols=0)
Definition bool_matrix.h:59
void colwise_xor(Index c1, Index c2)
Definition bool_matrix.h:76
Definition crdgen.h:16