int bool_mat_is_transitive(const bool_mat_t mat) { slong n, i, j, k; if (!bool_mat_is_square(mat)) { flint_printf("bool_mat_is_transitive: a square matrix is required!\n"); abort(); } if (bool_mat_is_empty(mat)) return 1; n = bool_mat_nrows(mat); for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) if (bool_mat_get_entry(mat, i, j) && bool_mat_get_entry(mat, j, k) && !bool_mat_get_entry(mat, i, k)) { return 0; } return 1; }
void bool_mat_transpose(bool_mat_t B, const bool_mat_t A) { slong i, j; if (bool_mat_nrows(B) != bool_mat_ncols(A) || bool_mat_ncols(B) != bool_mat_nrows(A)) { flint_printf("bool_mat_transpose: Incompatible dimensions.\n"); abort(); } if (bool_mat_is_empty(A)) return; if (A == B) /* In-place, guaranteed to be square */ { int tmp; for (i = 0; i < bool_mat_nrows(B) - 1; i++) { for (j = i + 1; j < bool_mat_ncols(B); j++) { tmp = bool_mat_get_entry(B, i, j); bool_mat_set_entry(B, i, j, bool_mat_get_entry(B, j, i)); bool_mat_set_entry(B, j, i, tmp); } } } else /* Not aliased; general case */ { for (i = 0; i < bool_mat_nrows(B); i++) for (j = 0; j < bool_mat_ncols(B); j++) bool_mat_set_entry(B, i, j, bool_mat_get_entry(A, j, i)); } }
void bool_mat_complement(bool_mat_t dest, const bool_mat_t src) { slong i, j; if (bool_mat_is_empty(src)) return; for (i = 0; i < bool_mat_nrows(src); i++) for (j = 0; j < bool_mat_ncols(src); j++) bool_mat_set_entry(dest, i, j, !bool_mat_get_entry(src, i, j)); }
void bool_mat_add(bool_mat_t res, const bool_mat_t mat1, const bool_mat_t mat2) { slong i, j; if (bool_mat_is_empty(mat1)) return; for (i = 0; i < bool_mat_nrows(mat1); i++) for (j = 0; j < bool_mat_ncols(mat1); j++) bool_mat_set_entry(res, i, j, (bool_mat_get_entry(mat1, i, j) | bool_mat_get_entry(mat2, i, j))); }
int bool_mat_all(const bool_mat_t mat) { slong i, j; if (bool_mat_is_empty(mat)) return 1; for (i = 0; i < bool_mat_nrows(mat); i++) for (j = 0; j < bool_mat_ncols(mat); j++) if (!bool_mat_get_entry(mat, i, j)) return 0; return 1; }
/* following Tarjan */ slong bool_mat_get_strongly_connected_components(slong *partition, const bool_mat_t A) { slong i, n, result; _tarjan_t t; if (!bool_mat_is_square(A)) { flint_printf("bool_mat_get_strongly_connected_components: " "a square matrix is required!\n"); flint_abort(); } if (bool_mat_is_empty(A)) return 0; n = bool_mat_nrows(A); if (n == 1) { partition[0] = 0; return 1; } _tarjan_init(t, n); for (i = 0; i < n; i++) { partition[i] = _tarjan_UNDEFINED; } for (i = 0; i < n; i++) { if (*_tarjan_index(t, i) == _tarjan_UNDEFINED) { _tarjan_strongconnect(partition, t, A, i); } } result = t->nsccs; _tarjan_clear(t); return result; }
slong bool_mat_nilpotency_degree(const bool_mat_t A) { slong n; if (!bool_mat_is_square(A)) { flint_printf("bool_mat_nilpotency_degree: a square matrix is required!\n"); abort(); } if (bool_mat_is_empty(A)) return 0; n = bool_mat_nrows(A); if (n == 1) { return bool_mat_get_entry(A, 0, 0) ? -1 : 1; } else { _toposort_s s; slong i; int has_cycle; int result; _toposort_init(&s, n); for (has_cycle = 0, i = 0; !has_cycle && i < n; i++) if (!s.v[i]) has_cycle = _toposort_visit(&s, A, i); if (has_cycle) { result = -1; } else { /* Find the length of the longest path within the DAG */ /* http://stackoverflow.com/a/10737524/4072759 */ slong x, y, z; slong max_overall; fmpz_mat_t E; fmpz_mat_init(E, n, n); fmpz_mat_zero(E); max_overall = 0; for (i = n - 1; i >= 0; i--) { slong max_in = 0; y = s.post[i]; for (x = 0; x < n; x++) { max_in = FLINT_MAX(max_in, fmpz_get_si(fmpz_mat_entry(E, x, y))); } for (z = 0; z < n; z++) { if (bool_mat_get_entry(A, y, z)) { fmpz_set_si(fmpz_mat_entry(E, y, z), max_in + 1); max_overall = FLINT_MAX(max_overall, max_in + 1); } } } fmpz_mat_clear(E); result = max_overall + 1; } _toposort_clear(&s); return result; } }