bool_mat.h
#include <flint/bool_mat.h>A bool_mat_t is a dense matrix over the Boolean semiring : sums are logical or and products are logical and. Entries are stored as int values, which must be 0 or 1; the arithmetic functions use bitwise operations and give wrong results for other values. A square Boolean matrix is the adjacency matrix of a directed graph, with when there is an edge from vertex to vertex ; then records which vertices are joined by a walk of length exactly . The graph functions documented at the end answer questions about walks, cycles and strongly connected components.
arb_mat_exp and acb_mat_exp use bool_mat_nilpotency_degree and bool_mat_all_pairs_longest_walk on the pattern of nonzero entries of the input: when the pattern is nilpotent, the Taylor series of the exponential is a finite sum, and its error bound can be set to zero entry by entry.
The dimensions of a matrix are fixed at initialisation and may be zero. Unless stated otherwise, the caller must ensure that the dimensions of the arguments are compatible; some functions throw an exception if they are not. Output and input matrices may be the same object.
Types and entries#
A matrix: entries points to the entries stored row by row, rows to an array of pointers to the start of each row, and r and c are the numbers of rows and columns. A bool_mat_t is an array of one bool_mat_struct, so it is passed by reference.
int bool_mat_get_entry(const bool_mat_t mat, slong i, slong j)void bool_mat_set_entry(bool_mat_t mat, slong i, slong j, int x)Get and set the entry in row and column . The value must be 0 or 1. Indices are not checked.
Memory management#
void bool_mat_init(bool_mat_t mat, slong r, slong c)Initialises mat as the zero matrix. No memory is allocated if or is zero.
void bool_mat_clear(bool_mat_t mat)Frees the memory used by mat.
void bool_mat_swap(bool_mat_t mat1, bool_mat_t mat2)Swaps the two matrices, including their dimensions, in constant time.
Assignment and special matrices#
void bool_mat_set(bool_mat_t dest, const bool_mat_t src)Copies src to dest, which must have the same dimensions.
void bool_mat_zero(bool_mat_t mat)void bool_mat_one(bool_mat_t mat)Set mat to the zero matrix, or to the matrix with ones on the main diagonal and zeros elsewhere (also for non-square matrices).
void bool_mat_directed_path(bool_mat_t A)void bool_mat_directed_cycle(bool_mat_t A)Set the square matrix of order to the adjacency matrix of the directed path , with exactly when , or of the directed cycle, with exactly when . Throw an exception if is not square.
Random generation#
void bool_mat_randtest(bool_mat_t mat, flint_rand_t state)void bool_mat_randtest_diagonal(bool_mat_t mat, flint_rand_t state)Set mat to a random matrix, or to a random matrix that is zero off the main diagonal. A density between 0 and 1 is chosen at random, and each entry (each diagonal entry) is 1 with that probability.
void bool_mat_randtest_nilpotent(bool_mat_t mat, flint_rand_t state)Sets the square, nonempty matrix mat to a random nilpotent matrix: a random strictly lower triangular matrix with rows and columns permuted by a common random permutation, that is, the adjacency matrix of a random directed acyclic graph. Throws an exception if mat is not square or is empty.
Input and output#
void bool_mat_print(const bool_mat_t mat)void bool_mat_fprint(FILE * file, const bool_mat_t mat)Print the matrix to standard output or to file, one row per line in the form [1, 0, 1].
Comparison and properties#
int bool_mat_equal(const bool_mat_t mat1, const bool_mat_t mat2)Returns 1 if the matrices have the same dimensions and the same entries, and 0 otherwise.
int bool_mat_any(const bool_mat_t mat)int bool_mat_all(const bool_mat_t mat)Return 1 if some entry, respectively every entry, of mat is 1, and 0 otherwise. For an empty matrix bool_mat_any returns 0 and bool_mat_all returns 1.
int bool_mat_is_empty(const bool_mat_t mat)int bool_mat_is_square(const bool_mat_t mat)Return 1 if mat has zero rows or zero columns, respectively the same number of rows and columns, and 0 otherwise.
int bool_mat_is_diagonal(const bool_mat_t A)int bool_mat_is_lower_triangular(const bool_mat_t A)Return 1 if for all , respectively for all , and 0 otherwise. The matrix need not be square.
int bool_mat_is_transitive(const bool_mat_t mat)Returns 1 if for all , that is, if entrywise, and 0 otherwise. Requires a square matrix; uses operations.
int bool_mat_is_nilpotent(const bool_mat_t A)Returns 1 if some power with is zero, that is, if the graph of has no cycles (including loops ), and 0 otherwise. Uses a depth-first search for cycles, in operations. Throws an exception if is not square. For an empty matrix it returns 0.
Arithmetic#
void bool_mat_transpose(bool_mat_t dest, const bool_mat_t src)Sets dest to the transpose of src. Throws an exception if the dimensions of dest are not those of the transpose.
void bool_mat_complement(bool_mat_t B, const bool_mat_t A)Sets to the entrywise negation of : .
void bool_mat_add(bool_mat_t res, const bool_mat_t mat1, const bool_mat_t mat2)Sets res to the sum of the two matrices, the entrywise or.
void bool_mat_mul_entrywise(bool_mat_t res, const bool_mat_t mat1, const bool_mat_t mat2)Sets res to the entrywise product of the two matrices, the entrywise and. Throws an exception if the input dimensions differ.
void bool_mat_mul(bool_mat_t res, const bool_mat_t mat1, const bool_mat_t mat2)void bool_mat_sqr(bool_mat_t B, const bool_mat_t A)Set res to the matrix product, , and to . The product is computed by the classical algorithm with early exit, in operations. Throws an exception if the dimensions are incompatible.
void bool_mat_pow_ui(bool_mat_t B, const bool_mat_t A, ulong exp)Sets to by binary exponentiation, with the identity matrix. Throws an exception if is not square.
int bool_mat_trace(const bool_mat_t mat)Returns the trace of the square matrix in the Boolean semiring: 1 if some diagonal entry is 1, and 0 otherwise (also for an empty matrix). Throws an exception if the matrix is not square.
Graph functions#
These functions regard a square matrix of order as the adjacency matrix of a directed graph on the vertices . They throw an exception if is not square. See Walks, cycles and components for an example.
void bool_mat_transitive_closure(bool_mat_t B, const bool_mat_t A)Sets to the transitive closure of : if and only if there is a walk of positive length from to . Uses Warshall's algorithm [War1962], in operations.
Full documentationslong bool_mat_get_strongly_connected_components(slong * p, const bool_mat_t A)Computes the strongly connected components of the graph of and returns their number . Sets , for each vertex , to the index in of the component containing . The components form a directed acyclic graph, and they are numbered in postorder with respect to it: if there is an edge from a vertex of component to a vertex of a different component , then . Uses Tarjan's algorithm [Tar1972], in operations.
Full documentationslong bool_mat_nilpotency_degree(const bool_mat_t A)Returns the nilpotency degree of , the smallest with , or if is not nilpotent. For an empty matrix it returns 0. For a nilpotent matrix the degree is one more than the length of the longest path in the graph, which is computed by a topological sort.
Full documentationslong bool_mat_all_pairs_longest_walk(fmpz_mat_t B, const bool_mat_t A)Sets to the length of the longest walk from vertex to vertex in the graph of , where the empty walk from a vertex to itself has length 0. if there is no walk from to , and if there are arbitrarily long walks (a walk from to through a cycle). The matrix must be .
Returns if some entry of is , and otherwise the largest entry of , which is one less than bool_mat_nilpotency_degree; for an empty matrix it returns .
References#
- [War1962] S. Warshall. A theorem on Boolean matrices. Journal of the ACM 9(1), 11–12, 1962.
- [Tar1972] R. E. Tarjan. Depth-first search and linear graph algorithms. SIAM Journal on Computing 1(2), 146–160, 1972.
The FLINT team, “bool_mat.h (module overview),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/bool_mat/index.html (generated 2026-11-02).
@misc{flint-doc-bool-mat-index,
author = {{The FLINT team}},
title = {{bool\_mat.h (module overview)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/bool_mat/index.html},
note = {Generated 2026-11-02}
}