bit::matrix
— Construction
Constructors for a bit-matrix.
constexpr
1::matrix(std::size_t r, std::size_t c);
bit
constexpr
2::matrix(std::size_t n = 0);
bit
constexpr
::matrix(const vector_type &v,
bit3std::size_t r = 1, bool by_rows = true);
constexpr
::matrix(const vector_type &u,
bit4const vector_type &v, bool product = true);
explicit constexpr
::matrix(std::size_t r, std::size_t c,
bit5std::invocable<std::size_t, std::size_t> auto f);
explicit constexpr
6::matrix(std::size_t n, <std::size_t, std::size_t> auto f);
bit
explicit
7::matrix(std::string &src, bool bit_order = false); bit
- 1
-
Construct an
r x c
bit-matrix initialized to 0.
If either parameter is zero, the bit-matrix will be 0 x 0. - 2
-
Construct an
n x n
square bit-matrix with all elements initialized to 0.
Default construction creates an empty 0 x 0 bit-matrix. - 3
-
Reshape a bit-vector into a bit-matrix with
r
rows. The constructor uses all the elements of the bit-vector, sor
must dividev.size()
evenly!
Ifr = 1
the constructed bit-matrix has a single row; ifr = 0
, it will have a single column instead.
By default,v
stores the elements of the bit-matrix by rows. Ifby_rows == false
, thenv
stores the elements by columns. - 4
-
Construct a bit-matrix from the outer product or outer sum of two bit-vectors.
Ifu.size() == m
andv.size() == n
, the resulting bit-matrix will bem x n
.
Ifproduct == true
thenmat(i, j) = u(i) & v(j)
.
Ifproduct == false
thenmat(i, j) = u(i) ^ v(j)
. - 5
-
Construct an
r x c
bit-matrix filled using a function call for each index pair(i, j)
. - 6
-
Construct an
n x n
square bit-matrix filled using a function call for each index pair(i, j)
. - 7
-
Construct a bit-matrix from a string that contains the elements row by row. + Newlines, white spaces, commas, or semi-colons must separate the rows. Each row should be encoded in a string as documented in the
vector::constructors
page.
If parse errors exist, these methods throw a std::invalid_argument exception.
|
Method Arguments
Argument | Description |
---|---|
r |
The number of rows required in the bit-matrix. |
c |
The number of columns required in the bit-matrix. |
n |
The number of rows & columns required in a square bit-matrix. |
f |
This function will be called as f(i, j) for \(i \in 0,\ldots,m-1, \; j \in 0,\ldots,n-1\). A non-zero return sets the corresponding element in the bit-matrix to 1. |
bit_order |
Defaults to false , but if present and set to true , then binary strings for the rows will have the lowest bits on the right. The parameter is ignored for hex-strings. |
Example — Construction from non-string data
#include <bit/bit.h>
int main()
{
1::matrix m0;
bit2::matrix m1(3, 5);
bit3::matrix m2(4);
bit
std::cout << "matrix: \n" << m0 << "\n";
std::cout << "matrix(3, 5): \n" << m1 << "\n\n";
std::cout << "matrix(4): \n" << m2 << "\n\n";
4::vector u(16, [](std::size_t i) { return (i + 1) % 2; });
bitstd::cout << "Constructing a bit-matrix by reshaping bit-vector u: " << u << "\n";
5::matrix m3(u, 2);
bit6::matrix m4(u, 4);
bit7::matrix m5(u, 4, false);
bitstd::cout << "matrix(u, 2) \n" << m3 << "\n\n";
std::cout << "matrix(u, 2, true) \n" << m4 << "\n\n";
std::cout << "matrix(u, 4, false) \n" << m5 << "\n\n";
.resize(6);
uauto v = bit::vector::ones(4);
std::cout << "Constructing a bit-matrix from the outer product and sum of bit-vector u: "
<< u << " and v: " << v << "\n";
8::matrix m6(u, v);
bit9::matrix m7(u, v, false);
bitstd::cout << "matrix(u, v, true) \n" << m6 << "\n\n";
std::cout << "matrix(u, v, false) \n" << m7 << "\n\n";
::matrix m8(8, [](size_t i, size_t) { return (i + 1) % 2; });
bitstd::cout << "matrix(lambda) \n" << m8 << "\n";
}
- 1
- Default constructor makes an empty bit-matrix.
- 2
- 3 x 5 bit-matrix initialized to all zeros.
- 3
- 4 x 4 square bit-matrix initialized to all zeros.
- 4
- Bit-matrix from a bit-vector reshaped into two rows.
- 5
- Bit-matrix from a bit-vector reshaped into four rows.
- 6
- Bit-matrix from a bit-vector reshaped into four rows where the bit-vector stores the elements column by column.
- 7
- Bit-matrix from the outer product of two bit-vectors.
- 8
- Bit-matrix from the outer sum of two bit-vectors.
- 9
- Bit-matrix from a lambda that sets the even rows to all ones and odd rows to all zeros.
Output
matrix:
[]
matrix(3, 5):
│0 0 0 0 0│
│0 0 0 0 0│
│0 0 0 0 0│
matrix(4):
│0 0 0 0│
│0 0 0 0│
│0 0 0 0│
│0 0 0 0│
Constructing a bit-matrix by reshaping bit-vector u: [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]
matrix(u, 2)
│1 0 1 0 1 0 1 0│
│1 0 1 0 1 0 1 0│
matrix(u, 2, true)
│1 0 1 0│
│1 0 1 0│
│1 0 1 0│
│1 0 1 0│
matrix(u, 4, false)
│1 1 1 1│
│0 0 0 0│
│1 1 1 1│
│0 0 0 0│
Constructing a bit-matrix from the outer product and sum of bit-vector u: [1 0 1 0 1 0] and v: [1 1 1 1]
matrix(u, v, true)
│1 1 1 1│
│0 0 0 0│
│1 1 1 1│
│0 0 0 0│
│1 1 1 1│
│0 0 0 0│
matrix(u, v, false)
│0 0 0 0│
│1 1 1 1│
│0 0 0 0│
│1 1 1 1│
│0 0 0 0│
│1 1 1 1│
matrix(lambda)
│1 1 1 1 1 1 1 1│
│0 0 0 0 0 0 0 0│
│1 1 1 1 1 1 1 1│
│0 0 0 0 0 0 0 0│
│1 1 1 1 1 1 1 1│
│0 0 0 0 0 0 0 0│
│1 1 1 1 1 1 1 1│
│0 0 0 0 0 0 0 0│
Example — Construction from strings
#include <bit/bit.h>
int main()
{
1::matrix m1("111 000 111");
bit2::matrix m2("0b111 0b000 0b111");
bit3::matrix m3("0x111;0x000;0x111");
bit4::matrix m4("0x1, 0x1, 0x1");
bit5::matrix m5("0x1_8;0x1_8;0x1_8");
bit6::matrix m6("0x1_4;0x1_4;0x1_4");
bit7::matrix m7("0x1_2;0x1_2;0x1_2");
bit
std::cout << "m1: \n" << m1 << "\n\n";
std::cout << "m2: \n" << m2 << "\n\n";
std::cout << "m3: \n" << m3 << "\n\n";
std::cout << "m4: \n" << m4 << "\n\n";
std::cout << "m5: \n" << m5 << "\n\n";
std::cout << "m6: \n" << m6 << "\n\n";
std::cout << "m7: \n" << m7 << "\n\n";
}
- 1
- Construction from strings separated by white space. All characters are 0’s and 1’s, so we interpret each element as a binary number.
- 2
-
Construction from the same binary strings, each with a binary prefix
0b
. - 3
-
Construction from the same digits, but each is now interpreted as a hex character thanks to the
0x
prefix. Here, semi-colons separate rows. - 4
- Construction where the final characters have no suffix, so by default, are parsed as a hex/base-16 number. Here, commas separate rows.
- 5
-
Construction where the final characters have a suffix
_8
so are parsed as base-8 numbers. - 6
-
Construction where the final characters have a suffix
_4
so are parsed as base-4 numbers. - 7
-
Construction where the final characters have a suffix
_2
so are parsed as base-2 numbers.
Output
m1:
│1 1 1│
│0 0 0│
│1 1 1│
m2:
│1 1 1│
│0 0 0│
│1 1 1│
m3:
│1 0 0 0 1 0 0 0 1 0 0 0│
│0 0 0 0 0 0 0 0 0 0 0 0│
│1 0 0 0 1 0 0 0 1 0 0 0│
m4:
│1 0 0 0│
│1 0 0 0│
│1 0 0 0│
m5:
│1 0 0│
│1 0 0│
│1 0 0│
m6:
│1 0│
│1 0│
│1 0│
m7:
│1│
│1│
│1│