bit::matrix — Upper & Lower Triangles

We have methods to extract the upper or lower triangular sub-matrix as a stand-alone, distinct copy of the elements from this bit-matrix.

1constexpr bit::matrix lower() const;
2constexpr bit::matrix strictly_lower() const;
3constexpr bit::matrix unit_lower() const;

4constexpr bit::matrix upper() const;
5constexpr bit::matrix strictly_upper() const;
6constexpr bit::matrix unit_upper() const;
1
Returns a copy of the lower triangle with zeros above the diagonal.
2
Returns a copy of the lower triangle with zeros on or above the diagonal.
3
Returns a copy of the lower triangle with ones on the diagonal and zeros above.
4
Returns a copy of the upper triangle with zeros below the diagonal.
5
Returns a copy of the upper triangle with zeros on or below the diagonal.
6
Returns a copy of the upper triangle with ones on the diagonal and zeros below.
These methods work with arbitrary rectangular bit-matrices, always starting with the top left (0,0) element as the anchor for the diagonal.

Example

#include <bit/bit.h>
int main()
{
1    std::size_t M = 6;
2    std::size_t N = 16;
    bit::matrix A(M, N);
3    A.set();

    std::cout << "bit-matrix, lower triangular sub-matrix, and the strictly lower triangular sub-matrix:\n";
    print(A, A.lower(), A.strictly_lower());

    std::cout << "bit-matrix, upper triangular sub-matrix, and the strictly upper triangular sub-matrix:\n";
    print(A, A.upper(), A.strictly_upper());

    return 0;
}
1
Number of rows.
2
Number of columns.
3
A is an M x N bit-matrix of all ones.

Output

bit-matrix, lower triangular sub-matrix, and the strictly lower triangular sub-matrix:
1111111111111111        1000000000000000        0000000000000000
1111111111111111        1100000000000000        1000000000000000
1111111111111111        1110000000000000        1100000000000000
1111111111111111        1111000000000000        1110000000000000
1111111111111111        1111100000000000        1111000000000000
1111111111111111        1111110000000000        1111100000000000
bit-matrix, upper triangular sub-matrix, and the strictly upper triangular sub-matrix:
1111111111111111        1111111111111111        0111111111111111
1111111111111111        0111111111111111        0011111111111111
1111111111111111        0011111111111111        0001111111111111
1111111111111111        0001111111111111        0000111111111111
1111111111111111        0000111111111111        0000011111111111
1111111111111111        0000011111111111        0000001111111111

See Also

matrix::sub

Back to top