bit::matrix
— Create Special Bit-Matrices
We supply factory methods to construct some special well-known bit-matrices.
static constexpr bit::matrix
1(std::size_t r, std::size_t c);
onesstatic constexpr bit::matrix
2(std::size_t n);
ones
static constexpr bit::matrix
3(std::size_t r, std::size_t c);
zerosstatic constexpr bit::matrix
4(std::size_t n);
zeros
static constexpr bit::matrix
5(std::size_t r, std::size_t c, int first = 1);
checker_boardstatic constexpr bit::matrix
6(std::size_t n, int first = 1);
checker_board
static constexpr bit::matrix
7(std::size_t n);
identity
static constexpr bit::matrix
8(std::size_t n, int p = -1);
shift
static constexpr bit::matrix
9(std::size_t n, int p = -1); rotate
- 1
-
Returns an
r x c
bit-matrix where all the elements are set to 1. - 2
-
Returns an
n x n
square bit-matrix where all the elements are set to 1. - 3
-
Returns an
r x c
bit-matrix where all the elements are set to 0. - 4
-
Returns an
n x n
square bit-matrix where all the elements are set to 0. - 5
-
Returns an
r x c
bit-matrix where the elements form a checker-board pattern. - 6
-
Returns an
n x n
square bit-matrix where the elements form a checker-board pattern. - 7
-
Returns the
n x n
identity bit-matrix (ones on the diagonal, other elements all zero). - 8
-
Returns the
n x n
bit-matrix that shifts a bit-vector byp
slots to the right ifp > 0
and the left ifp < 0
. - 9
-
Returns the
n x n
bit-matrix that rotates a bit-vector byp
slots to the right ifp > 0
and the left ifp < 0
.
Example
#include <bit/bit.h>
int main()
{
auto ones = bit::matrix<>::ones(4);
std::cout << "The all-set matrix:\n" << ones << "\n\n";
auto ident = bit::matrix<>::identity(8);
std::cout << "The identity matrix:\n" << ident << "\n\n";
auto shiftr = bit::matrix<>::shift(8, 1);
std::cout << "The shift right one place matrix:\n" << shiftr << "\n\n";
auto shiftl = bit::matrix<>::shift(8, -1);
std::cout << "The shift left one place matrix:\n" << shiftl << "\n\n";
auto rotr= bit::matrix<>::rotate(8, 1);
std::cout << "The rotate right one place matrix:\n" << rotr << "\n\n";
auto rotl = bit::matrix<>::rotate(8, -1);
std::cout << "The rotate left one place matrix:\n" << rotl << "\n\n";
auto u = bit::vector<>::ones(8);
std::cout << "Product identity matrix with " << u << " yields " << dot(ident, u) << '\n';
std::cout << "Product shiftr matrix with " << u << " yields " << dot(shiftr, u) << '\n';
std::cout << "Product shiftl matrix with " << u << " yields " << dot(shiftl, u) << '\n';
[0] = 0;
ustd::cout << "Product rotr matrix with " << u << " yields " << dot(rotr, u) << '\n';
std::cout << "Product rotl matrix with " << u << " yields " << dot(rotl, u) << "\n\n";
auto C1 = bit::matrix<>::checker_board(4,1);
auto C0 = bit::matrix<>::checker_board(4,0);
std::cout << "Two checker-board matrices:\n";
::print(C0, C1);
bit}
Output
The all-set matrix:
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
The identity matrix:
│1 0 0 0 0 0 0 0│
│0 1 0 0 0 0 0 0│
│0 0 1 0 0 0 0 0│
│0 0 0 1 0 0 0 0│
│0 0 0 0 1 0 0 0│
│0 0 0 0 0 1 0 0│
│0 0 0 0 0 0 1 0│
│0 0 0 0 0 0 0 1│
The shift right one place matrix:
│0 1 0 0 0 0 0 0│
│0 0 1 0 0 0 0 0│
│0 0 0 1 0 0 0 0│
│0 0 0 0 1 0 0 0│
│0 0 0 0 0 1 0 0│
│0 0 0 0 0 0 1 0│
│0 0 0 0 0 0 0 1│
│0 0 0 0 0 0 0 0│
The shift left one place matrix:
│0 0 0 0 0 0 0 0│
│1 0 0 0 0 0 0 0│
│0 1 0 0 0 0 0 0│
│0 0 1 0 0 0 0 0│
│0 0 0 1 0 0 0 0│
│0 0 0 0 1 0 0 0│
│0 0 0 0 0 1 0 0│
│0 0 0 0 0 0 1 0│
The rotate right one place matrix:
│0 1 0 0 0 0 0 0│
│0 0 1 0 0 0 0 0│
│0 0 0 1 0 0 0 0│
│0 0 0 0 1 0 0 0│
│0 0 0 0 0 1 0 0│
│0 0 0 0 0 0 1 0│
│0 0 0 0 0 0 0 1│
│1 0 0 0 0 0 0 0│
The rotate left one place matrix:
│0 0 0 0 0 0 0 1│
│1 0 0 0 0 0 0 0│
│0 1 0 0 0 0 0 0│
│0 0 1 0 0 0 0 0│
│0 0 0 1 0 0 0 0│
│0 0 0 0 1 0 0 0│
│0 0 0 0 0 1 0 0│
│0 0 0 0 0 0 1 0│
Product identity matrix with [1 1 1 1 1 1 1 1] yields [1 1 1 1 1 1 1 1]
Product shiftr matrix with [1 1 1 1 1 1 1 1] yields [1 1 1 1 1 1 1 0]
Product shiftl matrix with [1 1 1 1 1 1 1 1] yields [0 1 1 1 1 1 1 1]
Product rotr matrix with [0 1 1 1 1 1 1 1] yields [1 1 1 1 1 1 1 0]
Product rotl matrix with [0 1 1 1 1 1 1 1] yields [1 0 1 1 1 1 1 1]
Two checker-board matrices:
0101 1010
1010 0101
0101 1010
1010 0101